Passthrough Window Tutorial
Updated: Jun 26, 2024
You should first do the
Passthrough Basic Tutorial. Once you complete that, you will be ready to do the Passthrough Window tutorial.
- 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.
- 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.
- Remove any game objects from the scene, except for the Directional Light and OVRCameraRig.
- On the top menu, go to GameObject > 3D Object > Plane, and name it SolidPlane.
- Set Position to 10, 0, 10.
- Set Scale to 20, 1, 20. This will cause it to fill the view at runtime.
- Set Rotation to 0, 90, -90 so that the plane faces the OVRCameraRig view.
- Drag the BallMaterial you used in the basic tutorial to SolidPlane.
Create a Passthrough Plane
- On the top menu, go to GameObject > 3D Object > Plane. Name it PassthroughPlane.
- Set Position to 10, 0, 5. This will place PassthroughPlane in front of SolidPlane.
- Set Scale to 1, 1, 1. This makes it smaller than SolidPlane.
- Set Rotation to 0, 90, -90 so that the plane faces the OVRCameraRig view.
At this point PassthroughPlane is between the OvrCameraRig and SolidPlane.
- On the top menu, go to Assets > Create > Shader > Unlit Shader, and name it PassthroughPlaneShader.
- Double-click the PassthroughPlaneShader to open it in your code editor.
- Immediately above
CGPROGRAM add
BlendOp RevSub
Blend One Zero, Zero Zero
- 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;
};
- 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;
}
- Adjust
fixed4 frag (v2f i) so that it just returns the float4:
fixed4 frag (v2f i) : SV_Target
{
return float4(0, 0, 0, 0);
}
- Save the file.
Create and Assign a Shader Material
- On the top menu, go to Assets > Create > Material, and name it PassthroughShaderMaterial.
- Click PassthroughShaderMaterial to select it. Then, in the Inspector, set the Shader property to PassthroughPlaneShader.
- In the Project pane, drag the PassthroughShaderMaterial to the PassthroughPlane.
Create the PassthroughPlane Script
- On the top menu, go to Assets > Create > C# Script, and name it PassthroughPlaneScript.
- Double-click the new script to open it.
- Adjust the
Start script as follows:
void Start()
{
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_ANDROID
OVRManager.eyeFovPremultipliedAlphaModeEnabled = false;
#endif
}
- Save the script.
Assign the Script to OvrCameraRig
- In the Hierarchy window, click OvrCameraRig.
- In the Inspector, scroll to the bottom and choose Add Component.
- Search for the PassthroughPlaneScript and add it.
Save, Build, and Run the Project
- On the top menu, go to File > Save, and then go to File > Save Project.
- On the top menu, go to File > Build Settings to open the settings window.
- Remove the previous scenes from the Scenes in Build pane. Then click Add Open Scenes.
- Make sure that the Run Device is set to Meta Quest headset that is connected via USB cable.
- Click Build and Run.
- Save the .APK file.
Put on the headset to test the passthrough window. You should see a small window of passthrough surrounded by a larger opaque field.
Reference: The Full Shader
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
}
}
}