Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

is _CameraDepthTexture available in surface shaders ?

Discussion in 'Shaders' started by Steven-1, Jan 14, 2013.

  1. Steven-1

    Steven-1

    Joined:
    Sep 11, 2010
    Posts:
    334
    is the _CameraDepthTexture only usable in vert and frag shaders ? not in surface shaders ? Cause I can't get it to work in a surface shader.

    I asked this in the scripting forum, but got no response: http://forum.unity3d.com/threads/98911-How-to-view-Camera-s-Depth-Texture

    I need it for making a fairly simpel water shader but one that fades out close to the shore (for which I need the depth texture), that's what unity's water does, but it also does a lot of other stuff I don't want and it's way too complicated.
     
    Last edited: Jan 15, 2013
  2. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,716
    yes its available.
     
  3. Steven-1

    Steven-1

    Joined:
    Sep 11, 2010
    Posts:
    334
    okay, then what am I doing wrong?

    Edit: I simplified the shader to (almost) only contain the relevant stuff

    Code (csharp):
    1. Shader "Custom/WaterSimple"
    2. {
    3.     Properties
    4.     {
    5.         _WaterColor ("Water color", Color)  = (0.0, 0.0, 0.5, 1.0)
    6.         _SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
    7.         _Shininess ("Shininess", Float) = 0.078125
    8.     }
    9.  
    10.     Subshader
    11.     {
    12.         Tags
    13.         {
    14.             "RenderType" = "Transparent"
    15.             "Queue" = "Transparent-400"
    16.         }
    17.  
    18.         Blend SrcAlpha OneMinusSrcAlpha
    19.         LOD 300
    20.  
    21.         CGPROGRAM
    22.         #pragma exclude_renderers gles
    23.         #pragma surface surf BlinnPhong vertex:vert
    24.         #pragma target 3.0
    25.  
    26.         float4 _WaterColor;
    27.         float _Shininess;
    28.         sampler2D _CameraDepthTexture;
    29.  
    30.         struct Input
    31.         {
    32.             float4 projPos : TEXCOORD1;
    33.         };
    34.        
    35.         void vert (inout appdata_full v, out Input o)
    36.         {
    37.             o.projPos = ComputeScreenPos (mul(UNITY_MATRIX_MVP, v.vertex));
    38.             UNITY_INITIALIZE_OUTPUT(Input,o);
    39.             COMPUTE_EYEDEPTH(o.projPos.z); 
    40.         }
    41.  
    42.         void surf(Input IN, inout SurfaceOutput o)
    43.         {          
    44.             float sceneZ = LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.projPos))));//IN.screenPos))));
    45.             float partZ = IN.projPos.z;//IN.screenPos.z;
    46.             float fade = saturate (0.1 * (sceneZ-partZ));
    47.  
    48.             o.Albedo = _WaterColor;
    49.             o.Specular = _Shininess;
    50.             o.Gloss = 1.0;
    51.             o.Alpha = fade;
    52.         }
    53.  
    54.         ENDCG
    55.     }
    56. }
    the result is a very dark gray, which is because sceneZ and partZ have nearly the same value, which shouldn't be.
    I based this on a number of shaders, but latest version I believe is mainly based on particles-additive shader (atleast for the fade out when too litlle depth diference)
     
    Last edited: Jan 14, 2013
  4. Steven-1

    Steven-1

    Joined:
    Sep 11, 2010
    Posts:
    334
    I would expect this to also work, but instead it also doesn't work:

    Code (csharp):
    1. Shader "Custom/WaterSimple"
    2. {
    3.     Properties
    4.     {
    5.         _WaterColor ("Water color", Color)  = (0.0, 0.0, 0.5, 1.0)
    6.         _SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
    7.         _Shininess ("Shininess", Float) = 0.078125
    8.     }
    9.  
    10.     Subshader
    11.     {
    12.         Tags
    13.         {
    14.             "RenderType" = "Transparent"
    15.             "Queue" = "Transparent-400"
    16.         }
    17.  
    18.         Blend SrcAlpha OneMinusSrcAlpha
    19.         LOD 300
    20.  
    21.         CGPROGRAM
    22.         #pragma exclude_renderers gles
    23.         #pragma surface surf BlinnPhong
    24.         #pragma target 3.0
    25.  
    26.         float4 _WaterColor;
    27.         float _Shininess;
    28.         sampler2D _CameraDepthTexture;
    29.  
    30.         struct Input
    31.         {
    32.             float4 screenPos;
    33.         };
    34.  
    35.         void surf(Input IN, inout SurfaceOutput o)
    36.         {          
    37.             float sceneZ = LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.screenPos))));
    38.             float partZ = IN.screenPos.z;
    39.             float fade = saturate (0.1 * (sceneZ-partZ));
    40.  
    41.             o.Albedo = _WaterColor;
    42.             o.Specular = _Shininess;
    43.             o.Gloss = 1.0;
    44.             o.Alpha = fade;
    45.         }
    46.  
    47.         ENDCG
    48.     }
     
    Last edited: Jan 15, 2013
  5. Steven-1

    Steven-1

    Joined:
    Sep 11, 2010
    Posts:
    334
    No one ?
     
  6. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Is the camera definitely creating a depth texture?

    Camera.depthTextureMode = DepthTextureMode.Depth;
     
  7. Steven-1

    Steven-1

    Joined:
    Sep 11, 2010
    Posts:
    334
    well yes, I've put that line in a script attached to an object in the scene, but the result is the same.
    But I believe the depth texture should be created anyway as I'm using deferred lighting, no ?
    seeing as the particle shaders depth testing works (and they don't work in forward, I assume because the depth texture isn't created then), and a lot of post effects also need the depth texture, so I would expect I don't even need to set the depthTextureMode
    or am I mistaken in how it all works?
    (could be, I find it rather strange that we would have to add that line in code instead of just an extra toggle on the camera)
     
  8. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Hmm, I'm not 100% on the deferred pipeline, but yeah, it sounds like it's set up alright.

    Does Linear01Depth() work compared to LinearEyeDepth () ?
     
  9. Steven-1

    Steven-1

    Joined:
    Sep 11, 2010
    Posts:
    334
    no, I tried them both,
    as I recall it had pretty much (or exactly) the same result.

    So it really should work like this? (the shaders above)
    then it's probably some stupid oversight of me :s
     
    Last edited: Jan 21, 2013
  10. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Dunno, I don't have Pro to test it on :/
     
  11. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,716
    Drop your camera far clip plane to some reasonable value 50-100 and try to visualize the depth texture and check if you see something.
     
  12. Steven-1

    Steven-1

    Joined:
    Sep 11, 2010
    Posts:
    334
    And how do I do that?
    if you mean by writing a posteffect shader, well that would be unnecessary, cause I already use post effects (like ssao for example) that use depth, and they work fine.
     
  13. Steven-1

    Steven-1

    Joined:
    Sep 11, 2010
    Posts:
    334
    Bump

    still not solved.
     
  14. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    i can not too .i want to use renderWithShader function .But get confused too.....
     
  15. RyanFavale

    RyanFavale

    Joined:
    May 30, 2014
    Posts:
    28
    I am able to read it as long as I'm not outputing to the StandardOutput's. If I write to SV_Target with a half4 surface output I can read the depth as long as ZWrite is off.
     
  16. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    I'm having the same issue, can't get the depth texture right in forward rendering using a surface shader. works with frag shader only...

    someone made it work?
     
unityunity