Search Unity

From camera RenderTexture to Screen Space Shader for each eye.

Discussion in 'AR/VR (XR) Discussion' started by Lixxan, Oct 8, 2018.

  1. Lixxan

    Lixxan

    Joined:
    Sep 26, 2018
    Posts:
    1
    I'm trying to make a shader that overlaps a second VR camera view over the actual VR game screen.
    I have the main VR camera and in another part of the scene i have a second camera (that follows every movement the main camera makes) that renders into a renderTexture (one for each eye).

    Now i'm trying to overlap the second cameras rendertextures over the screen on each eye in the shader.
    The problem is that i can't get he rendertexture to fit the eye. No matter what i do it either has an offset or not the whole render texture is showing (repeating pixel-lines on the edges) or zoomed in/out.

    (I'm working in lightweight renderpipe line and single-pass stereo rendering

    Code (CSharp):
    1. Shader "Custom/DimensionShader"
    2. {
    3.     Properties
    4.     {
    5.         _EffectText  ("Effect Texture", 2D) = "white" {}
    6.  
    7.         _LeftEyeTexture("Left Eye Texture", 2D) = "white" {}
    8.         _RightEyeTexture("Right Eye Texture", 2D) = "white" {}
    9.  
    10.         _Delta("Delta", Float) = 0
    11.     }
    12.     HLSLINCLUDE
    13.  
    14.         #include "PostProcessing/Shaders/StdLib.hlsl"
    15.  
    16.         TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
    17.         TEXTURE2D_SAMPLER2D(_LeftEyeTexture, sampler_LeftEyeTexture);
    18.         TEXTURE2D_SAMPLER2D(_EffectText, sampler_EffectText);
    19.         half4 _MainTex_ST;
    20.         float _Delta;
    21.  
    22.         float4 Merge(VaryingsDefault i, float4 col)
    23.         {
    24.             int eyeIndex = 0;
    25.             #if UNITY_SINGLE_PASS_STEREO
    26.             eyeIndex = unity_StereoEyeIndex;
    27.  
    28.             float4 scaleOffset = unity_StereoScaleOffset[unity_StereoEyeIndex];
    29.        
    30.  
    31.             float2 screenUV = (i.texcoordStereo.xy- (scaleOffset.zw )) / (scaleOffset.xy);
    32.             float4 leftEyeCol = SAMPLE_TEXTURE2D(_LeftEyeTexture, sampler_LeftEyeTexture,              UnityStereoScreenSpaceUVAdjust(screenUV, _MainTex_ST));
    33.  
    34.             float4 texCol = SAMPLE_TEXTURE2D(_EffectText, sampler_EffectText, UnityStereoScreenSpaceUVAdjust(i.texcoord, _MainTex_ST));
    35.             #endif
    36.  
    37.             float4 newCol = leftEyeCol;
    38.             float ratio = texCol.r+_Delta;
    39.             if (ratio > 1)
    40.                 ratio = 1;
    41.             if (ratio < 0)
    42.                 ratio = 0;
    43.             newCol.rgb = lerp(col.rgb, secCol.rgb, ratio);
    44.  
    45.             if (eyeIndex==0)
    46.                 return newCol;
    47.             else
    48.                 return col;
    49.         }
    50.     ENDHLSL
    51.  
    52.     SubShader
    53.     {
    54.         Cull Off ZWrite Off ZTest Always
    55.  
    56.         Pass
    57.         {
    58.             HLSLPROGRAM
    59.  
    60.             #pragma vertex VertDefault
    61.             #pragma fragment frag
    62.             #pragma multi_compile __ STEREO_RENDER
    63.             #pragma target 3.0
    64.  
    65.             sampler2D _LeftEyeTexture;
    66.             sampler2D _RightEyeTexture;
    67.          
    68.             float4 frag (VaryingsDefault i) : SV_Target
    69.             {
    70.                float4 col;
    71.              
    72.  
    73.                 float2 screenUV = i.texcoordStereo.xy;
    74.                 #if UNITY_SINGLE_PASS_STEREO
    75.                 float4 scaleOffset = unity_StereoScaleOffset[unity_StereoEyeIndex];
    76.                     //screenUV = (screenUV - scaleOffset.zw) / scaleOffset.xy;
    77.                 #endif
    78.                 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex,UnityStereoScreenSpaceUVAdjust(screenUV, _MainTex_ST));
    79.                 col = Merge(i, col);
    80.              
    81.                 return col;
    82.             }
    83.             ENDHLSL
    84.         }
    85.     }
    86. }
    87.  


    P.S. I'm a complete shader noob so i won't understand all shader-terms/words/syntaxes/whatever. I don't