Search Unity

_CameraDepthTexture not working with sprites

Discussion in 'Shaders' started by AlexTemina, Nov 16, 2015.

  1. AlexTemina

    AlexTemina

    Joined:
    Feb 19, 2014
    Posts:
    44
    Hi, I'm trying to make fog in my 2D scene, it works well with 3d objects but no sprites... Why is the reason?

    Here is the shader code that is in my camera:

    Code (CSharp):
    1.  
    2. Shader "AlexTest/TestFog" {
    3.     Properties{
    4.         _MainTex("Base (RGB)", 2D) = "black" {}
    5.     }
    6.  
    7.         CGINCLUDE
    8.  
    9.     #include "UnityCG.cginc"
    10.  
    11.     uniform sampler2D _MainTex;
    12.     uniform sampler2D_float _CameraDepthTexture;
    13.  
    14.     struct v2f {
    15.         float4 pos : SV_POSITION;
    16.         float2 uv_MainTex : TEXCOORD0;
    17.     };
    18.  
    19.     float4 _MainTex_ST;
    20.  
    21.     v2f vert(appdata_base v) {
    22.         v2f o;
    23.         o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    24.         o.uv_MainTex = TRANSFORM_TEX(v.texcoord, _MainTex);
    25.         return o;
    26.     }
    27.  
    28.  
    29.     half4 ComputeFog(v2f i) : SV_Target
    30.     {
    31.         float rawDepth = UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, i.uv_MainTex));
    32.         float dpth = Linear01Depth(rawDepth);
    33.        
    34.         return dpth ;
    35.     }
    36.  
    37.         ENDCG
    38.  
    39.         SubShader
    40.     {
    41.        
    42.  
    43.             Pass
    44.         {
    45.             CGPROGRAM
    46. #pragma vertex vert
    47. #pragma fragment frag
    48.             half4 frag(v2f i) : SV_Target{ return ComputeFog(i); }
    49.             ENDCG
    50.         }
    51.     }
    52.  
    53.     Fallback off
    54.  
    55. }
     
  2. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    You need a shadow caster pass.

    Form the doc:
    Depth texture is rendered using the same shader passes as used for shadow caster rendering (“ShadowCaster” pass type). So by extension, if a shader does not support shadow casting (i.e. there’s no shadow caster pass in the shader or any of the fallbacks), then objects using that shader will not show up in the depth texture.

    Note that only “opaque” objects (that which have their materials and shaders setup to use render queue <= 2500) are rendered into the depth texture.
     
    AlexTemina likes this.