- Home /
Complex Depth Shader
Hello all, quick question.
Should it be possible for me to do the following?
- Render the depth of the scene out to a RenderTexture, but only if the depth is between two values (one from the shader, the other from the RenderTexture I want to write to). 
- Then render color, but only where the depth matches the pixel I'm about to draw. 
I would think this should be possible. Step one is the one that is giving me trouble... I current have this:
             CGPROGRAM
 
             #pragma vertex vertShadow
             #pragma fragment fragShadow
             
             #pragma target 5.0
 
             #include "UnityCG.cginc"
 
             int    _DepthTexWidth;
             int    _DepthTexHeight;
             float4 _Color;
             sampler2D _TempDepthTex;
               float4 _TempDepthTex_ST;
               
               #ifdef SHADER_API_D3D11
             RWTexture2D<float> CurrentDepthTex : register(u1);
             #endif
             
             struct v2f
             {
                 float4 pos : POSITION;
                 float3 normal : TEXCOORD1;
                 float2 uv : TEXCOORD2;
                 float4 screenPos : WPOS;
             };
 
             struct fout
             {
                 fixed4 color : COLOR;
                 float  depth : DEPTH;
             };
 
             v2f vertShadow(appdata_base v)
             {
                 v2f o;
 
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);     
                 o.screenPos = ComputeScreenPos(o.pos);
                 o.uv = v.texcoord;
                 o.normal = normalize(v.normal).xyz;
                 TRANSFER_VERTEX_TO_FRAGMENT(o);
 
                 return o; 
             }
 
             fout fragShadow(v2f i)
             {    
                 fout fo;
                 
                 i.screenPos.xy /= i.screenPos.w;
                                 
                 float newDepth     = i.pos.z;        
                 float portalDepth  = UNITY_SAMPLE_DEPTH(tex2D(_TempDepthTex, i.screenPos.xy)).x;
                 
                 float currentDepth = CurrentDepthTex.Load(int3(i.screenPos.xy, 0)).x;
                 
                 if ((portalDepth > newDepth) || (newDepth > currentDepth))
                     discard;
                 
                 CurrentDepthTex[i.screenPos.xy] = float4(newDepth, newDepth, newDepth, 1);
                 
                 fo.depth = newDepth;
                 fo.color = newDepth;
 
                 return fo;                    
             }
 
             ENDCG
Unfortunately what is happening is that I'm getting horrible corruption: 
While it should look like this: 
 
                 
                horriblecorruption.png 
                (19.4 kB) 
               
 
                
                 
                nocorruption.png 
                (7.8 kB) 
               
 
              
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                