VR portal rendering

This commit is contained in:
2022-10-09 16:53:57 +02:00
parent 29b604d769
commit 464a7e6767
7 changed files with 420 additions and 173 deletions

View File

@@ -2,12 +2,13 @@ Shader "Escape Room Engine/Portal"
{
Properties
{
_MainTex("Portal Texture", 2D) = "" {}
_LeftTex("Left Eye Texture", 2D) = "" {}
_RightTex("Right Eye Texture", 2D) = "" {}
}
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "Queue" = "Geometry" }
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
Pass
{
@@ -21,33 +22,45 @@ Shader "Escape Room Engine/Portal"
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
uniform sampler2D _MainTex;
struct Attributes
{
float4 vertex : POSITION;
float4 positionOS : POSITION;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Varyings
{
float4 vertex : SV_POSITION;
float4 screen : TEXCOORD0;
float4 positionHCS : SV_POSITION;
float4 positionScreen : TEXCOORD0;
UNITY_VERTEX_OUTPUT_STEREO
};
uniform sampler2D _LeftTex, _RightTex;
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.vertex = TransformObjectToHClip(IN.vertex.xyz);
OUT.screen = ComputeScreenPos(OUT.vertex);
UNITY_SETUP_INSTANCE_ID(IN);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
// calculate the screen position which is used to map the portal texture onto the portal
OUT.positionScreen = ComputeScreenPos(OUT.positionHCS);
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
return tex2D(_MainTex, IN.screen.xy / IN.screen.w);
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(IN);
const float2 uv = IN.positionScreen.xy / IN.positionScreen.w;
// sample from the correct texture depending on the eye rendered
return unity_StereoEyeIndex == 0 ? tex2D(_LeftTex, uv) : tex2D(_RightTex, uv);
}
ENDHLSL
}