Search Unity

Multiple Cameras, Transparent Shaders - Depth Buffer Issues

Discussion in 'General Graphics' started by fiveampsoftware, Mar 31, 2020.

  1. fiveampsoftware

    fiveampsoftware

    Joined:
    Feb 9, 2015
    Posts:
    33
    Hello,

    I'm trying to achieve an effect of a disabled land in a bigger set of a level. This part of the level will be grayscale so that the players can't interact with it. Our setup is the follow:

    It's targeting mobile (Android / iOS)

    We have three cameras, all of them are orthographic. Grayscaled camera is at the lowest depth (at 0) that has the grayscale effect. It is applied to a render texture that is already created in the project (for now) and uses OnPostRender to apply the effect. The Grayscale camera has that render texture as it's target texture already as well.

    Code (CSharp):
    1.     void OnPostRender()
    2.     {
    3.         TimeX += Time.deltaTime;
    4.         if (TimeX > 100) TimeX = 0;
    5.         material.SetFloat("_TimeX", TimeX);
    6.         material.SetFloat("_Fade", _Fade);
    7.         material.SetVector("_ScreenResolution", new Vector4(renderTextureToUse.width, renderTextureToUse.height, 0.0f, 0.0f));
    8.  
    9.         Graphics.Blit(renderTextureToUse, renderTextureToUse, material);
    10.     }
    Then the Main Camera has the same target texture that renderers everything but the disabled land (layer named "Grayscale"). The depth is 1 higher than the Grayscale camera.

    All of this is handled but a 3rd camera that is rendering nothing but Graphics.Blit the final RenderTexture - technique learned from - https://forum.unity.com/threads/postprocessing-issues-with-several-cameras.313903/

    Code is:
    Code (CSharp):
    1.     void OnPostRender()
    2.     {
    3.         Graphics.Blit(renderTexture, materialInstance);
    4.     }
    However, the final output looks like the following - doesn't look correct:
    upload_2020-3-31_11-37-21.png

    This is what's actually in the scene
    upload_2020-3-31_11-21-56.png

    The water that you see that is over the tree is being rendered by the MainCamera. The shader that is on the tree is a transparent one - because we need it to fade away slowly if the player zooms in too closely on it. I'm assuming that the transparent shader of the tree is not writing to the depth buffer and thus allowing the main camera to draw in that place (in the buffer)?

    Not sure what the best way to get this resolved would be. The Transparent custom diffuse shader is the following:
    Code (CSharp):
    1. Shader "Mobile/DiffuseXTransparent"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Color",COLOR) = (0.5,0.5,0.5,1.0)
    6.         _MainTex("Base (RGB)", 2D) = "white" {}
    7.     }
    8.  
    9.     SubShader
    10.     {
    11.         Tags{ "RenderType" = "Opaque" }
    12.         LOD 150
    13.         CGPROGRAM
    14.         #pragma surface surf Lambert noforwardadd alpha
    15.  
    16.         sampler2D _MainTex;
    17.         fixed4 _Color;
    18.  
    19.         struct Input
    20.         {
    21.             float2 uv_MainTex;
    22.         };
    23.  
    24.         void surf(Input IN, inout SurfaceOutput o)
    25.         {
    26.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    27.             o.Albedo = c.rgb;
    28.             o.Alpha = c.a;
    29.         }
    30.         ENDCG
    31.     }
    32.  
    33.     Fallback "Mobile/VertexLit"
    34. }
    35.  
    It looks like removing the "alpha" keyword gets it to work correctly however this also removed the point of the shader.

    Not sure exactly how to fix it - any pointers would be greatly appreciated.

    Thanks,
    - Kevin
     

    Attached Files:

    Last edited: Mar 31, 2020