Develop
Develop
Select your platform

Passthrough Window Tutorial

Updated: Jun 26, 2024
In this tutorial, you will modify the Passthrough Basic Tutorial to implement a simple passthrough window. Here are the steps:
Note: In this tutorial, we have both the OVRPassthroughLayer and the new script components attached to the OVRCameraRig. (The OVRPassthroughLayer is added and configured in the Passthrough Basic Tutorial).

Before You Begin

You should first do the Passthrough Basic Tutorial. Once you complete that, you will be ready to do the Passthrough Window tutorial.

Create a New Scene

  1. In the Project tab, under the Assets folder, create a new folder named Passthrough Tutorial, and then select it to make it the current folder for new objects.
  2. In the Project Hierarchy, right click SampleScene, and choose Save Scene As. Give the new scene a unique name, such as PWScene. This becomes the active scene in the Hierarchy.
  3. Remove any game objects from the scene, except for the Directional Light and OVRCameraRig.

Create a Solid Plane

  1. On the top menu, go to GameObject > 3D Object > Plane, and name it SolidPlane.
  2. Set Position to 10, 0, 10.
  3. Set Scale to 20, 1, 20. This will cause it to fill the view at runtime.
  4. Set Rotation to 0, 90, -90 so that the plane faces the OVRCameraRig view.
  5. Drag the BallMaterial you used in the basic tutorial to SolidPlane.
Create a Solid Plane

Create a Passthrough Plane

  1. On the top menu, go to GameObject > 3D Object > Plane. Name it PassthroughPlane.
  2. Set Position to 10, 0, 5. This will place PassthroughPlane in front of SolidPlane.
  3. Set Scale to 1, 1, 1. This makes it smaller than SolidPlane.
  4. Set Rotation to 0, 90, -90 so that the plane faces the OVRCameraRig view.
At this point PassthroughPlane is between the OvrCameraRig and SolidPlane.
Create a Passthrough Plane

Create a New Shader

  1. On the top menu, go to Assets > Create > Shader > Unlit Shader, and name it PassthroughPlaneShader.
  2. Double-click the PassthroughPlaneShader to open it in your code editor.
  3. Immediately above CGPROGRAM add
       BlendOp RevSub
       Blend One Zero, Zero Zero
    
  4. Adjust struct v2f to remove UNITY_FOG_COORDS(1):
    struct v2f
    {
       float2 uv : TEXCOORD0;
       // UNITY_FOG_COORDS(1) // delete or comment
       float4 vertex : SV_POSITION;
    };
    
  5. Adjust struct v2f_vert to remove UNITY_TRANSFER_FOG(o,o.vertex);:
    v2f vert (appdata v)
    {
       v2f o;
       o.vertex = UnityObjectToClipPos(v.vertex);
       o.uv = TRANSFORM_TEX(v.uv, _MainTex);
       // UNITY_TRANSFER_FOG(o,o.vertex); // delete or comment
       return o;
    }
    
  6. Adjust fixed4 frag (v2f i) so that it just returns the float4:
      fixed4 frag (v2f i) : SV_Target
       {
          return float4(0, 0, 0, 0);
       }
    
  7. Save the file.
Create a New Shader

Create and Assign a Shader Material

  1. On the top menu, go to Assets > Create > Material, and name it PassthroughShaderMaterial.
  2. Click PassthroughShaderMaterial to select it. Then, in the Inspector, set the Shader property to PassthroughPlaneShader.
  3. In the Project pane, drag the PassthroughShaderMaterial to the PassthroughPlane.
Create and Assign a Shader Material

Create the PassthroughPlane Script

  1. On the top menu, go to Assets > Create > C# Script, and name it PassthroughPlaneScript.
  2. Double-click the new script to open it.
  3. Adjust the Start script as follows:
       void Start()
       {
    #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_ANDROID
          OVRManager.eyeFovPremultipliedAlphaModeEnabled = false;
    #endif
        }
    
  4. Save the script.
Create the PassthroughPlane Script

Assign the Script to OvrCameraRig

  1. In the Hierarchy window, click OvrCameraRig.
  2. In the Inspector, scroll to the bottom and choose Add Component.
  3. Search for the PassthroughPlaneScript and add it.
Assign the Script to OvrCameraRig

Save, Build, and Run the Project

  1. On the top menu, go to File > Save, and then go to File > Save Project.
  2. On the top menu, go to File > Build Settings to open the settings window.
  3. Remove the previous scenes from the Scenes in Build pane. Then click Add Open Scenes.
  4. Make sure that the Run Device is set to Meta Quest headset that is connected via USB cable.
  5. Click Build and Run.
  6. Save the .APK file.
  7. Put on the headset to test the passthrough window. You should see a small window of passthrough surrounded by a larger opaque field.
    Passthrough Window tutorial output

Reference: The Full Shader

For your reference, here is the full shader file. For more information on Shaders, refer to Materials, Shaders, and Textures in the Unity documentation.
Shader "Unlit/PassthroughTutorialShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
           BlendOp RevSub
           Blend One Zero, Zero Zero

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                return float4(0, 0, 0, 0);
            }
            ENDCG
        }
    }
}

Did you find this page helpful?
Thumbs up icon
Thumbs down icon