title: Shader - Object depth or thickness
body: <p>I am using <strong>Unity</strong> and would like to render the <strong>thickness</strong> of an object.<br>My first thought was to use the <strong>ZBuffer</strong> and the <strong>depth</strong> but it is always depend to the camera, it seems legit.<br>But I would like to avoid the change of the z position of the camera. The aim is to "normalize" the camera position but I don't know how to do that.<br>Maybe a simple matrix trick ?</p>
  
  <p>I am not focused on any front or back face operation but simply retrieve or fake the thickness of the object maybe using depth (but without being view position depend but to keep the camera orientation effects)</p>
  
  <p>Here are two simple shaders that retrieve depth for Unity.</p>
  
  <p><strong>Unity Vertex and Fragment shader</strong></p>
  
  <pre><code>#pragma vertex vert
  #pragma fragment frag
  #include "UnityCG.cginc"
  
  struct v2f {
      float4 pos : SV_POSITION;
      float2 depth : TEXCOORD0;
  };
  
  v2f vert (appdata_base v) {
      v2f o;
      o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
      o.depth = o.pos.zw;
      return o;
  }
  
  half4 frag(v2f i) : COLOR {
      #if SHADER_API_FLASH
          return EncodeFloatRGBA(i.depth.x/i.depth.y);
      #else
          return i.depth.x/i.depth.y;
      #endif
  }
  </code></pre>
  
  <p><strong>Unity Surface shader</strong></p>
  
  <pre><code>void vert(inout appdata_full v, out Input o)
  {
      UNITY_INITIALIZE_OUTPUT(Input, o);
      o.screenPos = ComputeScreenPos(o.pos);
  }
  
  void surf(Input IN, inout IceOutput OUT)
  {
      /* Some code */
  
      float Depth =  1.0 / (_ZBufferParams.x * tex2Dproj(_CameraDepthTexture, IN.screenPos.xyw).r + _ZBufferParams.y);
      // Adjust depth manually to math camera distance to the object
      half4 edgeBlendFactors = half4(1.0, 0.0, 0.0, 0.0);
      edgeBlendFactors = saturate((Depth - IN.screenPos.w + _A) * _B); // _A = 7 and _B = 0.2
      Depth = lerp(1.0, 0.0, edgeBlendFactors);
  
      /* Some code */
  }
  </code></pre>
  
  <p>I also found those interesting articles but I can't achieve the same results.</p>
  
  <ul>
  <li><a href="http://eraser85.wordpress.com/2008/02/28/make-it-translucid/" rel="nofollow">Make it Translucid !</a></li>
  <li><a href="http://prideout.net/blog/?p=51" rel="nofollow">Thick glass</a></li>
  <li><a href="http://http.developer.nvidia.com/GPUGems/gpugems_ch16.html" rel="nofollow">GPU Gems</a></li>
  </ul>