Search Unity

Object Depth Shader Help

Discussion in 'Shaders' started by rcalt2vt, Aug 25, 2014.

  1. rcalt2vt

    rcalt2vt

    Joined:
    Jun 6, 2009
    Posts:
    36
    Hey all, I'm running into a really odd issue that I just can't wrap my head around as to why this is happening. I'm writing a very basic object depth shader and for whatever reason, unless I set the Fallback to something other than Off, it will not render correctly. For the life of me I just cant figure out what I am missing from the shader code to make it work without using a fallback. As far as I am aware, UNITY_MATRIX_MVP, UNITY_MATRIX_MV, and _ProjectionParams are all declared and handled in UnityCS.cginc and work with ShaderModel 2.0

    I'm using Windows 7 x64 with Unity Pro 4.5.2f1. Any thoughts would be greatly appreciated!

    Code (csharp):
    1. Shader "Custom/DistanceTest" {
    2.     SubShader {
    3.         Pass {    
    4.             CGPROGRAM          
    5.             #pragma vertex vert
    6.             #pragma fragment frag
    7.            
    8.             #include "UnityCG.cginc"
    9.  
    10.             struct v2f
    11.             {
    12.                 float4 pos : SV_POSITION;
    13.                 float objDepth : TEXCOORD0;
    14.             };
    15.  
    16.             v2f vert(appdata_base v)
    17.             {
    18.                 v2f o;
    19.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    20.                 o.objDepth = -(mul(UNITY_MATRIX_MV, float4(0,0,0,1)).z * _ProjectionParams.w);
    21.                 return o;
    22.             }
    23.  
    24.             fixed4 frag(v2f i) : COLOR
    25.             {
    26.                 return fixed4(i.objDepth, i.objDepth, i.objDepth, 1);
    27.             }
    28.             ENDCG
    29.         }
    30.     }
    31.     Fallback Off
    32. }
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    What do you define as correctly? Without the Fallback, the shader won't cast or receive shadows, sine those passes are not defined here. For the rest it should work fine. I've never set the Fallback to Off myself. (In that case I just omit the Fallback line completely.)
     
  3. rcalt2vt

    rcalt2vt

    Joined:
    Jun 6, 2009
    Posts:
    36
    Correct Behavior: Each object as they move from close to the camera to far away should go progressively from black to white. Shadows / Lighting / Etc are not applied to the objects.

    When Fallback Off is defined, or even omitted, all objects appear as Dark Grey regardless of their distance from the camera. However, when Fallback "VertexLit" is defined, it works correctly: No lights, no shadows, objects appear as black to white as desired.

    Best I've been able to determine is that this part is failing: mul(UNITY_MATRIX_MV, float4(0,0,0,1))
    and really, I just want to know why and what I have to do to fix it as I find it hard to believe that using a Fallback is required to get that line to work correctly.

    I can only hope Aras would grace me with his presence and tell me what I am doing wrong lol.
     
    Last edited: Aug 25, 2014
  4. rcalt2vt

    rcalt2vt

    Joined:
    Jun 6, 2009
    Posts:
    36
    A bit more research and I was able to figure out that Dynamic Batching and Static Batching were causing some problems as when I turned those options off, I was getting a better representation of object depth in the scene, however there were still several issues that I noticed and just could not figure out what was causing them.

    Anyone have any additional thoughts?
     
  5. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Yes, of course, batching changes the pivot of the object, but that's not really related to the shader used.