Meta Quest passthrough toggle in Unity

Using MRTK 2.7.2 and the Oculus Integration

With the release of the Meta Quest 3, I thought I'd share how we toggle the passthrough on and off during runtime.

First, we need to retrieve the OVRPassthroughLayer component from the OVRCamerRig game object. I'm using MRTK 2.7.2 and I believe we need to wait for the MRTK/Oculus Integration to generate the needed game objects (I wrote this code over a year ago so I can't completely remember if this is the main reason). Essentially if the Update() method of the script I check if we already have it and if we don't then I try to retrieve it by going through the children of the OVRCameraRig game object:

[SerializeField] private Transform playSpace;// MRTK MixedRealityPlayspace
private OVRPassthroughLayer passthroughLayer;

void Update()
{
    RetrieveOVRCamera();
}

private void RetrieveOVRCamera()
{
    if (passthroughLayer != null)
        return;

    for (int i = 0; i < playSpace.childCount; i++)
    {
        Transform child = playSpace.GetChild(i);
        if (child.name.Contains("OVRCameraRig"))
        {
            passthroughLayer = ovrCameraRig.EnsureComponent<OVRPassthroughLayer>();
            passthroughLayer.overlayType = OVROverlay.OverlayType.Overlay;
            passthroughLayer.textureOpacity = 0.5f;
            passthroughLayer.enabled = false;
            return;
        }
    }    
}

Once you have the passthroughLayer component you can simply toggle its enabled-ness to turn passthrough on and off:

public void TogglePassthrough()
{
    if (passthroughLayer != null)
        passthroughLayer.enabled = !passthroughLayer.enabled;
}

You may also modify the opacity of the passthrough with passthroughLayer.textureOpacity like this:

public void SetPassthroughOpacity(float opacity)
{
    if (passthroughLayer != null)
        passthroughLayer.textureOpacity = opacity;
}

I hope this has helped! Let me know if this didn't work for you or if you have any other questions about the MRTK, HoloLens or Meta Quest development in Unity.