Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
psykomike's avatar
psykomike
Honored Guest
12 years ago

OVRCameraController - change clipping planes at runtime

Hi,
We need to change the clipping planes in the OVRCamera - for a short period of time to see a close-up. Then back to normal to render the rest of the scene with finer Depth Buffer in the higher distances.

Normally this works with
Camera cameraVar;
camVar.nearClipPlane = X.XXf;
in C#

With the Oculus Rift Cameras from the OVRCameraController prefab, I didn't find a way to adjust that value directly in runtime.
Tried the following:
OVRCameraController ovrCameraControllerVar;
OVRCamera ovrCameraVar;
Camera cameraVar;

void FixedUpdate () {
oVRCameraControllerVar.NearClipPlane = X.XXf;
oVRCameraControllerVar.camera.nearClipPlane = X.XXf;
oVRCameraVar.camera.nearClipPlane = X.XXf;
cameraVar.nearClipPlane = X.XXf;
}

It did show the change in the Camera's public NearClipPlane variable, but it clipped away the close up object anyway.

The work-around to this problem is to set-up two OVRCameraController prefabs with the according clip planes and then set the GameObjects active/inactive.

However, I'd be grateful if setting the clipping planes would work the same way it does with the standard Camera.

9 Replies

Replies have been turned off for this discussion
  • There is a way to do this, unfortunately it's not very straight-forward. We hope to have accessor functions that will make this easy, but they are not there yet. For now here is a work-around.

    Make a function that will manually find the left and right cameras, then adjust the near/far plane on them. If you look at the Awake() function in OVRCameraController, that is a good place to copy the code from. Then just make a new function that will adjust the clipping planes.

    Let me know if this works for you.
  • Hi,
    thanks for the fast answer. Sadly, the workaround doesn't work.

    tried the following:
    GameObject myLeftCam = GameObject.Find("CameraLeft");
    GameObject myRightCam = GameObject.Find("CameraRight");
    myLeftCam.camera.nearClipPlane = 0.01f;
    myRightCam.camera.nearClipPlane = 0.01f;


    and then similar to Awake()
    Camera[] cameras = GameObject.Find("OVRCameraController").GetComponentsInChildren<Camera>();
    Camera CameraLeft = new Camera();
    Camera CameraRight = new Camera();
    for (int i = 0; i < cameras.Length; i++)
    {
    if(cameras[i].name == "CameraLeft")
    CameraLeft = cameras[i];

    if(cameras[i].name == "CameraRight")
    CameraRight = cameras[i];
    }
    CameraLeft.nearClipPlane = 0.01f;
    CameraRight.nearClipPlane = 0.01f;


    That is, the clipping plane DID change according to the Inspector, but there was no effect in the Game View. No changes in the Depth Buffer and still clipping through the shoulder.

    Perhaps my understanding of how to find something manually was wrong?
    Also my Project can't convert to anything higher than Unity 4.0.1f2 Does this functionality need 4.1?
  • There are currently no access functions to the Near/FarClipPlane variables. This will be included in the next release (as well as for BackgroundColor).

    For now, you can add the following functions into OVRCameraController and use them to access the clip planes:

    // Get/Set NearClipPlane
    public void GetNearClipLane(ref float nearClipPlane)
    {
    nearClipPlane = NearClipPlane;
    }
    public void SetNearClipLane(float nearClipPlane)
    {
    NearClipPlane= nearClipPlane;
    UpdateCamerasDirtyFlag = true;
    }


    Repeat the above for FarClipPlane (as well as for BackgroundColor).

    Note that the key here is to set UpdateCameraDirtyFlag to trigger the camera to apply changes in the next update.
  • Thanks for your suggestion, but UpdateCamerasDirtyFlag didn't change much.

    The problem was i didn't put VRMenu into the scene. Somehow updating the Cameras only works with VRMenu present.
  • When the OVRMainMenu is active, it causes the cameras to be reconfigured every frame. I'm not sure if this is desired behavior, but that's what happens currently. I'm trying to create a fix that doesn't require the menu.
  • To fix this issue, you will need to modify the ConfigureCamera() method of the OVRCameraController to be:


    bool ConfigureCamera(ref Camera camera, float distOffset, float perspOffset, float eyePositionOffset)
    {
    Vector3 PerspOffset = Vector3.zero;
    Vector3 EyePosition = EyeCenterPosition;

    // Vertical FOV
    camera.fieldOfView = VerticalFOV;

    // Aspect ratio
    camera.aspect = AspectRatio;

    // Centre of lens correction
    camera.GetComponent<OVRLensCorrection>()._Center.x = distOffset;
    ConfigureCameraLensCorrection(ref camera);

    // Clip Planes
    camera.nearClipPlane = NearClipPlane;
    camera.farClipPlane = FarClipPlane;

    // Perspective offset for image
    PerspOffset.x = perspOffset;
    camera.GetComponent<OVRCamera>().SetPerspectiveOffset(ref PerspOffset);

    // Set camera variables that pertain to the neck and eye position
    // NOTE: We will want to add a scale vlue here in the event that the player
    // grows or shrinks in the world. This keeps head modelling behaviour
    // accurate
    camera.GetComponent<OVRCamera>().NeckPosition = NeckPosition;
    EyePosition.x = eyePositionOffset;

    camera.GetComponent<OVRCamera>().EyePosition = EyePosition;

    // Background color
    camera.backgroundColor = BackgroundColor;

    return true;
    }


    The difference here is that the clip planes are set before the SetPerspectiveOffset() call. With this change, you shouldn't need the OVRMainMenu.