Search Unity

Question Shader depth behaving differently in Scene and Game window

Discussion in 'Shader Graph' started by Zack_C, Jun 28, 2022.

  1. Zack_C

    Zack_C

    Joined:
    Jul 21, 2015
    Posts:
    6
    Hi all,

    I have written a custom shader that would produce the pink edges based on depth proj. But in the game view, instead of the pink edges, it does a weird projection of the whole scene view as in the second image. The mesh with the custom shader is a large plane. The pink is a color selected by me, it is not a missing texture.


    upload_2022-6-28_11-49-23.png
    The mesh with the custom shader is a large plane.

    upload_2022-6-28_11-49-47.png
    This image is taken in scene view and working as intended. The pink edges should only appear when it detects a mesh.

    upload_2022-6-28_11-52-33.png
    This image is taken in game view where the custom shader will project the exact image in my scene view.

    Here's how the code looks
    Code (CSharp):
    1. uniform sampler2D _CameraDepthTexture;
    2.  
    3. v2f vert (appdata v)
    4. {
    5.      o.screenPos = ComputeScreenPos (o.vertex);
    6. }
    7.  
    8. fixed4 frag (v2f i) : SV_Target
    9. {
    10.      half depth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture,UNITY_PROJ_COORD(i.screenPos)));
    11.      half4 foamLine = 1 - saturate(_Foam * (depth.r - i.screenPos.w));              
    12.      col += foamLine * _FoamTint;
    13.  
    14.      return col;
    15. }
    I've tried to change the render queue to a different number but the results are the same.

    Can someone enlighten me why the game and scene views are producing different results? Thanks in advance!