Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Single Pass Stereo mode shader strange behaviour

Discussion in 'Image Effects' started by fra3point, Jul 2, 2018.

  1. fra3point

    fra3point

    Joined:
    Aug 20, 2012
    Posts:
    269
    Hi,

    I'm working on a post processing fade system that fades all the objects with a given fade layer.
    To do this I make the main camera don't render the fade layer render and I use a separate camera (with a render target) that only renders that layer.
    Then I merge that target texture with the main (on screen) texture in OnRenderImage().

    Code (CSharp):
    1. void OnRenderImage(RenderTexture source, RenderTexture destination)
    2.     {      
    3.         fadeCamera.gameObject.SetActive(true);
    4.         fadeCamera.Render();
    5.         fadeCamera.gameObject.SetActive(false);
    6.         mat.SetFloat("_FadeFactor", fadeFactor);
    7.         mat.SetTexture("_MainTex", source);
    8.         mat.SetTexture("_FadeTex", fadeCamera.targetTexture);
    9.         Graphics.Blit(source, destination, mat);
    10.        
    11.     }

    The fade effect of course takes into account the depths of both the scene rendered objects and the ones rendered by the second camera in order to correctly cut occluded areas during the merge. This is done in the following shader:

    Code (CSharp):
    1. Shader "Custom/Fade" {
    2.    
    3.     Properties{
    4.         _MainTex("_MainTex", 2D) = "white" {}
    5.         _FadeTex("_FadeTex", 2D) = "white" {}
    6.         _FadeFactor("Fade Factor", float) = 0.0
    7.     }
    8.    
    9.     SubShader {
    10.    
    11.         Tags { "RenderType"="Opaque" }
    12.        
    13.         Pass{
    14.        
    15.             CGPROGRAM
    16.            
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.             #include "UnityCG.cginc"
    20.            
    21.             sampler2D _CameraDepthTexture, _LastCameraDepthTexture;
    22.             sampler2D _MainTex, _FadeTex;
    23.             half4 _MainTex_ST;
    24.             float _FadeFactor;
    25.            
    26.             struct v2f {
    27.                float4 pos : SV_POSITION;
    28.                float4 scrPos:TEXCOORD1;
    29.             };
    30.            
    31.             //Vertex Shader
    32.             v2f vert (appdata_base v){
    33.                v2f o;
    34.                o.pos = UnityObjectToClipPos (v.vertex);
    35.                o.scrPos = ComputeScreenPos(o.pos);
    36.                return o;
    37.             }
    38.            
    39.             //Fragment Shader
    40.             half4 frag (v2f i) : COLOR{
    41.                
    42.                
    43.                //float4 uv =i.scrPos; // classic uvs, no VR
    44.                
    45.                float4 uv = UnityStereoScreenSpaceUVAdjust(i.scrPos, _MainTex_ST);
    46.            
    47.                float depthMain = Linear01Depth (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos)).r);
    48.                float depthFade = Linear01Depth (tex2Dproj(_LastCameraDepthTexture, UNITY_PROJ_COORD(i.scrPos)).r);
    49.                              
    50.                if(depthFade<depthMain){
    51.                    return lerp(tex2D(_FadeTex, i.scrPos), tex2D(_MainTex, uv), 1-_FadeFactor);
    52.                }
    53.                return tex2D(_MainTex, i.scrPos);
    54.             }
    55.            
    56.             ENDCG
    57.            
    58.         }
    59.        
    60.     }
    61.     FallBack "Diffuse"
    62. }

    And this worked perfectly without VR support (no UnityStereoScreenSpaceUVAdjust in the shader), but when I enabled the VR support (Single Pass Stereo), the fading objects started becoming low-res, sometimes black and they don't blend well with the background scene.

    This is the normal scene: the red object will be faded-out.

    upload_2018-7-2_13-4-43.png


    And this is the object during the fade.

    upload_2018-7-2_13-3-39.png



    Does anyone kno why I get this ugly effect?

    Thanks,

    Francesco