Forum Discussion
vannoo67
12 years agoHonored Guest
Use mono camera when Rift is not present?
Hi Folks,
Sorry if this is obvious, but I haven't been able to find an answer anywhere.
I'd like to be able to write my game to make use of the Rift if it is available, but fall back to a standard (mono) camera if the Rift is not present. (By default an un-distorted, but still stereo, view is provided).
Is there a best practice method for this?
Sorry if this is obvious, but I haven't been able to find an answer anywhere.
I'd like to be able to write my game to make use of the Rift if it is available, but fall back to a standard (mono) camera if the Rift is not present. (By default an un-distorted, but still stereo, view is provided).
Is there a best practice method for this?
6 Replies
Replies have been turned off for this discussion
- brendenfrankHonored GuestI'm not sure about a best practice but the way I've been going about it is by writing a camera wrapper so that whenever I enable or disable a camera in my scene, it checks for a corresponding VRstereo camera if VR is enabled.
If you have a static game manager class you can write a method for VREnabled and check that value with your camera wrapper. Here's an example of how I'm currently handling it, though I can't say its the best way, it works well for me:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class VRCamera
{
public string name;
public GameObject camera;
public Camera leftlense;
public Camera rightlense;
public VRCamera(string Name, GameObject Cam)
{
name = Name;
camera = Cam;
leftlense = camera.transform.FindChild("CameraLeft").camera;
rightlense = camera.transform.FindChild("CameraRight").camera;
}
}
public static class CameraManager
{
private static int currentScene;
private static bool initialized;
private static List<VRCamera> VRCameras;
public static bool isInitialized()
{
if(!initialized)
return false;
if(currentScene != Application.loadedLevel)
return false;
return true;
}
public static void Initialize(bool force=false)
{
if(!isInitialized() || force)
{
VRCameras = new List<VRCamera>();
foreach(Camera camera in GameObject.FindObjectsOfType(typeof(Camera)))
{
if((camera.name == "CameraLeft") || (camera.name == "CameraRight"))
{
if(VRCameraExistsInList(camera))
continue;
VRCameras.Add(new VRCamera(camera.transform.parent.name, camera.transform.parent.gameObject));
}
}
initialized = true;
currentScene = Application.loadedLevel;
}
}
public static bool VRCameraExistsInList(Camera camera)
{
foreach(VRCamera vrcam in VRCameras)
{
if(vrcam.name == camera.transform.parent.name)
{
return true;
}
}
return false;
}
public static GameObject FindVRCamera(string search)
{
foreach(VRCamera vrcam in VRCameras)
{
if(vrcam.name == "OVR"+search)
return vrcam.camera;
}
Debug.LogError("A Camera was referenced that does not have a VR pair: OVR"+search);
return null;
}
public static void ToggleCamera(Camera camera, bool toggle)
{
if(!GameManager.VREnabled())
{
camera.enabled = toggle;
return;
}
Initialize();
if((FindVRCamera(camera.name)) == null)
Initialize(true);
FindVRCamera(camera.name).SetActive(toggle);
}
public static GameObject GetCurrentCamera(GameObject camera)
{
if(!GameManager.VREnabled())
return camera;
Initialize();
if((FindVRCamera(camera.name)) == null)
Initialize(true);
return FindVRCamera(camera.name);
}
}
It works because If I have a camera called MainCamera, I have a VR cam called OVRMainCamera and it pairs them by name and toggles or grabs the appropriate camera based on whether VREnabled() is true. Your method can store the bool based on setting or detect automatically via
if (OVRDevice.SensorCount > 0)
return true;
So instead of writing
mycam.enabled = whatever;
You use the wrapper and writeCameraManager.ToggleCamera(mycam, whatever);
Hope this helps! - brendenfrankHonored GuestIn the simplest respect however, you can run a check at the beginning that simply toggles off all mono cams and toggles on all your VRPairs by running something like this in Awake/Start
// VRSUPPORT
foreach(Camera camera in GameObject.FindObjectsOfType(typeof(Camera)))
{
if((camera.name == "CameraLeft") || (camera.name == "CameraRight"))
{
camera.transform.parent.gameObject.SetActive(VREnabled());
continue;
}
camera.enabled = !VREnabled();
} - KrisperExplorerI am not that experienced at coding c# or Unity, but my character has both a standard camera and the OVR camera. And at Start I just run this:-
mainCamera = GameObject.FindGameObjectWithTag("MainCamera");
riftCamera = GameObject.FindGameObjectWithTag("OVRCamera");
if(OVRDevice.SensorCount > 0) {
mainCamera.SetActive(false);
riftCamera.SetActive(true);
} else {
mainCamera.SetActive(true);
riftCamera.SetActive(false);
}
Maybe there are reasons not to do it this way, but it works for me. - brendenfrankHonored GuestYour way works just fine I think. The reason my solution is a bit more extravagant is because I deal with a lot of cameras and need a more systematic solution for handling them all.
- sholeHonored GuestIn my project I have cameras as prefabs and instantiate them as needed.
I use the quality setting to select rift/regular fps camera, allowing me to use fps view with my rift plugged in if I want to, or the other way around. - vannoo67Honored GuestI ended up doing something similar to @Krisper, however @brendenfrank's seems more comprehensive. I'll have a closer look at the code and see if I need the additional functionality.
Thanks for your help.
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device
Other Meta Support
Related Content
- 9 months ago
- 2 years ago