Last active
February 28, 2022 08:08
-
-
Save jasoncg/7f68f804f67869d0b53d05633fd7f0ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//based on https://github.com/Brackeys/Portal-In-Unity/blob/master/Portal/Assets/ScreenCutoutShader.shader | |
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' | |
Shader "Unlit/BasicScreenCutoutShader" | |
{ | |
Properties | |
{ | |
_MainTex("Texture", 2D) = "white" {} | |
_EyeDistance("Eye Distance", Float) = 0 | |
} | |
SubShader | |
{ | |
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" } | |
Lighting Off | |
Cull Back | |
ZWrite On | |
ZTest Less | |
Fog{ Mode Off } | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
# include "UnityCG.cginc" | |
uniform fixed _EyeDistance; | |
struct appdata { | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
UNITY_VERTEX_INPUT_INSTANCE_ID //Insert | |
}; | |
struct v2f { | |
float2 uv : TEXCOORD0; | |
float4 vertex : SV_POSITION; | |
float4 screenPos : TEXCOORD1; | |
UNITY_VERTEX_OUTPUT_STEREO //Insert | |
}; | |
v2f vert(appdata v) { | |
v2f o; | |
UNITY_SETUP_INSTANCE_ID(v); //Insert | |
UNITY_INITIALIZE_OUTPUT(v2f, o); //Insert | |
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); //Insert | |
o.vertex = UnityObjectToClipPos(v.vertex); | |
//o.screenPos = ComputeScreenPos(o.vertex); | |
o.screenPos = ComputeNonStereoScreenPos(o.vertex);// ComputeScreenPos(o.vertex); | |
o.uv = v.uv; | |
return o; | |
} | |
UNITY_DECLARE_SCREENSPACE_TEXTURE(_MainTex); | |
//sampler2D _MainTex; | |
half4 _MainTex_ST; | |
fixed4 frag(v2f i) : SV_Target | |
{ | |
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i); //Insert | |
fixed4 col; | |
#if UNITY_SINGLE_PASS_STEREO | |
float2 screenUV = i.screenPos.xy / i.screenPos.w; | |
if (unity_StereoEyeIndex == 0) { | |
//Use left side of texture, then move to left eye position | |
screenUV.x = (screenUV.x)*0.5f | |
- _EyeDistance*0.5f; | |
//col = tex2D(_MainTex, UnityStereoScreenSpaceUVAdjust(screenUV, _MainTex_ST)); | |
col = tex2D(_MainTex, screenUV); | |
} else { | |
//Use right side of texture, then move to the right eye position | |
screenUV.x = ((screenUV.x)*0.5f) + 0.5f | |
+ _EyeDistance*0.5f; | |
col = tex2D(_MainTex, screenUV); | |
//col = tex2D(_MainTex, UnityStereoScreenSpaceUVAdjust(screenUV, _MainTex_ST)); | |
} | |
#else | |
i.screenPos /= i.screenPos.w; | |
col = tex2D(_MainTex, UnityStereoScreenSpaceUVAdjust(i.screenPos, _MainTex_ST)); | |
#endif | |
return col; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment