Switch between Single and Stereo Camera Vuforia 5.0

July 30, 2015 - 9:05am #1

Hi,

 

I have import both Vuforia 5.0 and latest Google Cardboard SDK and implemented like the tutorial below:

 

http://developer.vuforia.com/library/articles/Solution/Integrating-Cardboard-to-the-ARVR-Sample

 

But I want to swtich between two camera modes so that I can have a single camera to scan marker, then bring user into VR mode. If I unchecked the "VR Mode Enabled" on the Cardboard script, I got the backgroundPlane surrounded by black. How can I dismiss the black or enlarge the plane?

 

 

AttachmentSize
Image icon screenshot.png441.9 KB

Switch between Single and Stereo Camera Vuforia 5.0

July 31, 2015 - 7:47am #6

Great to hear.

Switch between Single and Stereo Camera Vuforia 5.0

July 31, 2015 - 7:34am #5

Thanks! It is working fine now! Although I get "View port sizes of left and right camera are not identical. This is currently not supported by Vuforia!" when I switch AR and VR mode back and forth a few times. It is working just fine.

Switch between Single and Stereo Camera Vuforia 5.0

July 31, 2015 - 6:21am #4

Here is the "complete" recipe for what you are trying to achieve (we also plan to add a small tutorial in our Library soon):

 

If you want to have your AR start in mono (non-stereo) full-screen view and then switch to a Cardboard-based stereoscopic visualization at runtime (for instance upon detection of a target or in response to a user interface event), you can follow these steps:

First, check the Cardboard integration guide here how to set up a stereo view in your scene with Cardboard, and execute all the steps (including the necessary script code changes) so to integrate the Cardboard Main prefab and to bind the ARCamera to the Cardboard left and right cameras:

https://developer.vuforia.com/library//articles/Solution/Integrating-Cardboard-SDK-050

 

Then, with respect to the instructions above, configure your Scene slightly differently, so that it additionally includes:

-          a “MonoAnchorPoint” empty game-object

o   a “MonoCamera” (a standard Unity camera object) parented under the“MonoAnchorPoint” game-object

Then disable the Cardboard Main game object in the scene, and enable the “Bind Alternate Camera” option of the ARCamera and make sure to bind it to the MonoCamera by:

-          dragging the “MonoAnchorPoint” game object onto the “Central Anchor Point” field

-          dragging the “MonoCamera” onto the “Left Camera” field in the ARCamera inspector.

Leave the “Right Camera” field set to “None (Camera)”.

When dragging the MonoCamera game object onto the “Left Camera” field, a button “Add Vuforia Components” will appear in the inspector; make sure to click it in order to add the necessary Vuforia components and background plane to the MonoCamera.

Make sure to uncheck (disable) the “Synchronize Pose updates” (as this will be enabled at runtime via script).

 

 

The setup above will make your App start with a full screen mono view; you can then enable Cardboard stereo viewing at runtime using these steps:

-          set the central anchor point to the Cardboard Head object (Transform):

VuforiaBehaviour.Instance.SetCentralAnchorPoint( cardboardHead );

 

-           set the Primary and Secondary Cameras to the Cardboard left and right cameras:

VuforiaBehaviour.Instance.PrimaryCamera = cardboardCameraLeft;

VuforiaBehaviour.Instance.PrimaryCamera = cardboardCameraRight;

 

-          enable the SynchronizePoseUpdates property, and set the HeadSetPresent property to "Cardboard":

VuforiaBehaviour.Instance.SynchronizePoseUpdates = true;

VuforiaBehaviour.Instance.SetHeadsetPresent("Cardboard");

 

-          disable the MonAnchorPoint game object in the scene:

monoAnchorPoint.SetActive( false );

It is also recommended to stop the Camera before calling the code above, and restarting it right after:

-          Stopping the camera:

CameraDevice.Instance.Stop();

CameraDevice.Instance.Deinit();

 

-          Re-starting the camera:

CameraDevice.Instance.Init(CameraDevice.CameraDirection.CAMERA_BACK);

CameraDevice.Instance.SelectVideoMode(CameraDevice.CameraDeviceMode.MODE_OPTIMIZE_SPEED);

CameraDevice.Instance.Start();

 

 

In order to switch back to Mono from an AR Stereo view, you can use this code:

 

// set the new tracking reference

VuforiaBehaviour.Instance.SetCentralAnchorPoint(monoAnchorPoint);

VuforiaBehaviour.Instance.PrimaryCamera = monoCamera;

VuforiaBehaviour.Instance.SecondaryCamera = null;

 

// disable explicit updating

VuforiaBehaviour.Instance.SynchronizePoseUpdates = false;

 

// turn off the cardboard UI:

Cardboard.SDK.EnableAlignmentMarker = false;

Cardboard.SDK.EnableSettingsButton = false;

 

// deactivate the cardboard prefab:

cardboardHead.SetActive(false);

 

// enable the mono anchor point:

monoAnchorPoint.gameObject.SetActive(true);

 

// Stop and Deinit the camera:

CameraDevice.Instance.Stop();

CameraDevice.Instance.Deinit();

 

// now tell Vuforia that it's being removed from the Cardboard headset:

vuforiaBehaviour.SetHeadsetNotPresent();

 

// initialize the camera again:

CameraDevice.Instance.Init(CameraDevice.CameraDirection.CAMERA_BACK);

CameraDevice.Instance.SelectVideoMode(CameraDevice.CameraDeviceMode.MODE_OPTIMIZE_SPEED);

CameraDevice.Instance.Start();

 

Additionally, if you want to switch from an AR to a VR visualization (the latter being without video background), you will also need to implement the following:

-          Disable the HideExcessAreaBehaviour components on the left and right cameras of the Cardboard Main prefab (otherwise virtual content will be clipped to size of the video background, which is not filling the full field of view of the Cardboard cameras)

o   And make sure to re-enable them when switching back to an AR view

-          Disable the VideoBackgroundBehaviour components on the left and right cameras of the Cardboard Main prefab (this will save unnecessary operations and will enhance rendering performance)

-          Disable the MeshRenderer component of the BackgroundPlane under the left camera of the Cardboard prefab (this will save a little more performance)

 

Switch between Single and Stereo Camera Vuforia 5.0

July 31, 2015 - 2:41am #3

Thanks, I tried to implement it into the code. And I do have some questions:

  • Does the Google Cardboard gameobject needs to be disabled first?
  • When I switch into Stereo Cameras mode, it turns out to have two separate video background, Google Cardboard render is missing. But if I disable the child camera object ARCamera, it does show up. Does it mean I have to disable it on code? (See attached screenshot)
  • it seems like I can't disable the video background. I used the same method to disable the video background in the sample.

This is my code below:

using UnityEngine;
using System.Collections;
using Vuforia;

public class NewSwitchCamera : MonoBehaviour {
	
	[SerializeField] GameObject googleCardboardGameobject;
	[SerializeField] Transform googleHead;
	[SerializeField] Camera leftCam;
	[SerializeField] Camera rightCam;
	
	
	void Awake(){
		googleCardboardGameobject.SetActive(false);
	}

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {		
		if(Input.GetKeyDown(KeyCode.Return)){
			CameraDevice.Instance.Stop();
			CameraDevice.Instance.Deinit();
			VuforiaBehaviour.Instance.SetCentralAnchorPoint(googleHead);
			VuforiaBehaviour.Instance.PrimaryCamera = leftCam;
			VuforiaBehaviour.Instance.SecondaryCamera = rightCam;
			VuforiaBehaviour.Instance.SynchronizePoseUpdates = true;
			VuforiaBehaviour.Instance.SetHeadsetPresent("Cardboard");
            CameraDevice.Instance.Init(CameraDevice.CameraDirection.CAMERA_BACK);
            CameraDevice.Instance.SelectVideoMode(CameraDevice.CameraDeviceMode.MODE_OPTIMIZE_SPEED);
            CameraDevice.Instance.Start();
			googleCardboardGameobject.SetActive(true);
SetVideoBackgroundVisible(false);
		}
	}

private void SetVideoBackgroundVisible(bool visible)
    {
        Transform cameraRigRoot = GetCameraRigRoot();

        // Toggle the camera VideoBackgroundBehaviour to enable/disable the video background texture updates
        foreach (var vbb in cameraRigRoot.GetComponentsInChildren<VideoBackgroundAbstractBehaviour>())
        {
            if (vbb.enabled != visible)
                vbb.enabled = visible;
        }

        // We also enable/disable the Mesh Renderer of the BackgroundPlane
        var bgPlaneBehaviour = cameraRigRoot.GetComponentInChildren<BackgroundPlaneAbstractBehaviour>();
        if (bgPlaneBehaviour)
        {
             MeshRenderer mr = bgPlaneBehaviour.GetComponent<MeshRenderer>();
             if (mr.enabled != visible)
                 mr.enabled = visible;
        }
    }
	
	private Transform GetCameraRigRoot()
    {
        VuforiaBehaviour vuforia = VuforiaBehaviour.Instance;
        return (vuforia.CentralAnchorPoint ? vuforia.CentralAnchorPoint.transform.root : vuforia.transform);
    }
}
 
AttachmentSize
Image icon SS.jpg354.5 KB

Switch between Single and Stereo Camera Vuforia 5.0

July 30, 2015 - 9:36am #2

Hi, in Vuforia 5.0 you can enable the Stereo at runtime  (e.g. Cardboard cameras) using the following steps:

- set the central anchor point to the Transform that represents your head (e.g. the Cardboard Head object)

  VuforiaBehaviour.Instance.SetCentralAnchorPoint( cardboardHead );

 

then you need to set the Primary and Secondary Cameras:
 
      VuforiaBehaviour.Instance.PrimaryCamera = stereoCameraLeft; // this is the left camera of Cardboard
      VuforiaBehaviour.Instance.SecondaryCamera = stereoCameraRight; // this is the right camera of Cardboard
        

Then you need to enable the SynchronizePoseUpdates property, and set the HeadSetPresent property to "Cardboard":

VuforiaBehaviour.Instance.SynchronizePoseUpdates = true;
VuforiaBehaviour.Instance.SetHeadsetPresent("Cardboard");

 

Also, it is recommended to stop the Camera before doing the above, and restarting it after:

Stopping the camera:

CameraDevice.Instance.Stop();
CameraDevice.Instance.Deinit();

 

Starting the camera:

CameraDevice.Instance.Init(CameraDevice.CameraDirection.CAMERA_BACK);
CameraDevice.Instance.SelectVideoMode(CameraDevice.CameraDeviceMode.MODE_OPTIMIZE_SPEED);
CameraDevice.Instance.Start();

 

 

Topic locked