Advertisement
cybercritic

Logarithmic Depth Buffer for Unity

Nov 16th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. Shader "Custom/logZ"
  2. {
  3. Properties {
  4. _MainTex ("Albedo (RGB)", 2D) = "white" {}
  5. _Color("Color", Color) = (1,1,1,1)
  6. }
  7. SubShader
  8. {
  9. Pass
  10. {
  11. Tags { "RenderType" = "Opaque" }
  12. LOD 200
  13.  
  14. CGPROGRAM
  15. #pragma target 2.0
  16. #pragma vertex vert
  17. #pragma fragment frag
  18. #include "UnityCG.cginc"
  19.  
  20. sampler2D _MainTex;
  21. fixed4 _Color;
  22.  
  23. struct a2v
  24. {
  25. float4 vertex : POSITION;
  26. float4 texcoord : TEXCOORD0;
  27. };
  28.  
  29. struct v2f
  30. {
  31. float4 pos : POSITION;
  32. float flogz : TEXCOORD2;
  33. };
  34.  
  35. struct Output
  36. {
  37. float4 col:COLOR;
  38. float dep : DEPTH;
  39. };
  40.  
  41. v2f vert(a2v v)
  42. {
  43. v2f o;
  44. float C = 1.0;
  45. o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  46.  
  47. //http://outerra.blogspot.co.uk/2009/08/logarithmic-z-buffer.html
  48. //z = log(C*w + 1) / log(C*Far + 1) * w //DirectX with depth range 0..1
  49. //or
  50. //z = (2*log(C*w + 1) / log(C*Far + 1) - 1) * w //OpenGL, depth range -1..1
  51. o.pos.z = (log(C * o.pos.w + 1.0)) / log(C * _ProjectionParams.z + 1.0);
  52. o.pos.z *= o.pos.w;
  53. o.flogz = 1.0 + o.pos.w;
  54. return o;
  55. }
  56.  
  57. Output frag(v2f i)
  58. {
  59. Output o;
  60. o.col = _Color;
  61. o.dep = log2(i.flogz) * (0.5 * (2.0 / log2(_ProjectionParams.z + 1.0)));
  62. return o;
  63. }
  64.  
  65. ENDCG
  66. }
  67. }
  68. FallBack "Diffuse"
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement