Unity - How to select Camera and mirroring

May 18, 2015 - 12:09am #1

In Unity you can select the Camera direction (BACK vs. FRONT camera) in the Unity Editor, by selecting the ARCamera object in your scene and looking at the Inspector panel; there you will find a component called QCARBehaviour which offers a Camera Direction setting which can be set to one of these:

  • CAMERA_BACK
  • CAMERA_FRONT
  • CAMERA_DEFAULT

The CAMERA_BACK option will select the rear camera on your device, while the CAMERA_FRONT option will select the front camera on your device, if the device has one. The CAMERA_DEFAULT option will typically select the rear camera, unless the device only has a front camera.

The ARCamera inspector also shows a property called "Mirror Video Background" which can be set to one of these:

  • DEFAULT
  • ON
  • OFF

When the DEFAULT option is selected (recommended for most common use cases), the Mirroring will be automatically disabled (OFF) when the BACK camera is used, and will be automatically enabled (ON) when the FRONT camera is used.

 

How to change the Camera direction and mirroring at runtime?

You can change the Camera direction at runtime by using the CameraDevice and QCARRenderer API, as shown in this example:

public class DirSwap : MonoBehaviour 
{
    private QCARAbstractBehaviour mQCAR;

    void Start() {
        mQCAR = (QCARAbstractBehaviour)FindObjectOfType(typeof(QCARAbstractBehaviour));
        
    }

    void OnGUI()
    {
        if (GUI.Button(new Rect(50,50,200,50), "Swap Camera"))
        {
            CameraDevice.CameraDirection currentDir = CameraDevice.Instance.GetCameraDirection();
            if (currentDir == CameraDevice.CameraDirection.CAMERA_BACK || currentDir == CameraDevice.CameraDirection.CAMERA_DEFAULT)
                RestartCamera(CameraDevice.CameraDirection.CAMERA_FRONT, true);
            else
                RestartCamera(CameraDevice.CameraDirection.CAMERA_BACK, false);
        }

        if (GUI.Button(new Rect(50,100,200,50), "Mirror OFF"))
        {
            RestartCamera(CameraDevice.Instance.GetCameraDirection(), false);
        }

        if (GUI.Button(new Rect(50, 150, 200, 50), "Mirror ON"))
        {
            var config = QCARRenderer.Instance.GetVideoBackgroundConfig();
            config.reflection = QCARRenderer.VideoBackgroundReflection.ON;
            QCARRenderer.Instance.SetVideoBackgroundConfig(config);

            RestartCamera(CameraDevice.Instance.GetCameraDirection(), true);
        }
    }

    private void RestartCamera(CameraDevice.CameraDirection newDir, bool mirror)
    {
        CameraDevice.Instance.Stop();
        CameraDevice.Instance.Deinit();

        CameraDevice.Instance.Init(newDir);

        // Set mirroring 
        var config = QCARRenderer.Instance.GetVideoBackgroundConfig();
        config.reflection = mirror ? QCARRenderer.VideoBackgroundReflection.ON : QCARRenderer.VideoBackgroundReflection.OFF;
        QCARRenderer.Instance.SetVideoBackgroundConfig(config);

        CameraDevice.Instance.Start();
    }
}

 

Topic locked