Unity-Technologies / arfoundation-samples

Example content for Unity projects based on AR Foundation
Other
3.02k stars 1.12k forks source link

Cant Switch To Front Facing Camera while Using ARFoundation #939

Closed crandersn closed 2 years ago

crandersn commented 2 years ago

Hello,

I am new to Unity and ARFoundation and I am having an issue with switching between the front facing and rear facing camera. I currently have a demo application working which allows me to use the rear facing camera to do ray casting and place an AR object in the real world. However, whenever I touch the screen to switch to my front facing camera, my debug statements indicate that the requested camera was changed, but the application does not actually change which camera I am using. I am wondering if this has something to do with attempting to use ray casting on the front facing camera. I am ok with not being able to use ray casting with the front facing camera, but I would like to know how to disable ray casting so that I can switch to the front camera to enable some other functionality. My eventual goal is to use the rear facing camera for 3D object placement and use the front facing camera for face tracking. If anyone could help me figure out how to accomplish this that would be awesome! I have attached my code and a couple relevant screenshots below. Thanks!


using System.Collections.Generic;
using UnityEngine.XR.ARSubsystems;

namespace UnityEngine.XR.ARFoundation.Samples {
    public class ToggleCameraFacingDirection : MonoBehaviour {

        public GameObject objectToPlace;
        public GameObject placementIndicator;
        private ARRaycastManager rayCastMgr;
        private bool placementPoseIsValid = false;
        private Pose placeMentPose;

        [SerializeField]
        ARCameraManager m_CameraManager;

        public ARCameraManager cameraManager
        {
            get => m_CameraManager;
            set => m_CameraManager = value;
        }

        [SerializeField]
        ARSession m_Session;

        public ARSession session
        {
            get => m_Session;
            set => m_Session = value;
        }

        void Start()
        {
            rayCastMgr = GetComponent<ARRaycastManager>();
        }

        void Update()
        {

            UpdatePlacementPose();
            UpdatePlacementIndicator();

            if (m_CameraManager == null || m_Session == null){
                Debug.Log("missing camera manager or session");
                return;
            }

            if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
            {

                Debug.Log("touch detected");

                if (m_CameraManager.requestedFacingDirection == CameraFacingDirection.User)
                {

                    Debug.Log("changing to rear facing camera");
                    m_CameraManager.requestedFacingDirection = CameraFacingDirection.World;
                    Debug.Log(m_CameraManager.currentFacingDirection);
                }
                else
                {
                    Debug.Log("changing to front facing camera...");
                    m_CameraManager.requestedFacingDirection = CameraFacingDirection.User;
                    Debug.Log(m_CameraManager.currentFacingDirection);
                }
            }
        }

         private void UpdatePlacementPose(){

            var screenCenter = Camera.current.ViewportToScreenPoint(new Vector3(0.5f, 0.5f));
            var hits = new List<ARRaycastHit>();
            rayCastMgr.Raycast(screenCenter, hits, TrackableType.Planes);

            placementPoseIsValid = hits.Count > 0;

            if (placementPoseIsValid){
                placeMentPose = hits[0].pose; 

                var cameraFoward = Camera.current.transform.forward;
                var cameraBearing = new Vector3(cameraFoward.x, 0, cameraFoward.z).normalized;
                placeMentPose.rotation = Quaternion.LookRotation(cameraBearing);

            }
        }

        private void UpdatePlacementIndicator(){

            if (placementPoseIsValid){
                placementIndicator.SetActive(true);
                placementIndicator.transform.SetPositionAndRotation(placeMentPose.position, placeMentPose.rotation);
            } else {
                placementIndicator.SetActive(false);
            }

        }

    }
}
Screen Shot 2022-02-09 at 10 56 36 AM Screen Shot 2022-02-09 at 10 56 47 AM
ankur-unity commented 2 years ago

There is no configuration that support both Raycast and User Facing Camera features at the same time. If both are requested at runtime then the ConfigurationChooser will choose the configuration that has the maximum number of requested features.

To disable the Raycast feature, try disabling the ARRaycastManager.

                if (m_CameraManager.requestedFacingDirection == CameraFacingDirection.User)
                {
                    if (rayCastMgr != null)
                        rayCastMgr.enabled = true;

                    Debug.Log("changing to rear facing camera");
                    m_CameraManager.requestedFacingDirection = CameraFacingDirection.World;
                    Debug.Log(m_CameraManager.currentFacingDirection);
                }
                else
                {
                    if (rayCastMgr != null)
                        rayCastMgr.enabled = false;

                    Debug.Log("changing to front facing camera...");
                    m_CameraManager.requestedFacingDirection = CameraFacingDirection.User;
                    Debug.Log(m_CameraManager.currentFacingDirection);
                }
crandersn commented 2 years ago

Thank you so much! This resolved my issue