Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "Custom/logZ"
- {
- Properties {
- _MainTex ("Albedo (RGB)", 2D) = "white" {}
- _Color("Color", Color) = (1,1,1,1)
- }
- SubShader
- {
- Pass
- {
- Tags { "RenderType" = "Opaque" }
- LOD 200
- CGPROGRAM
- #pragma target 2.0
- #pragma vertex vert
- #pragma fragment frag
- #include "UnityCG.cginc"
- sampler2D _MainTex;
- fixed4 _Color;
- struct a2v
- {
- float4 vertex : POSITION;
- float4 texcoord : TEXCOORD0;
- };
- struct v2f
- {
- float4 pos : POSITION;
- float flogz : TEXCOORD2;
- };
- struct Output
- {
- float4 col:COLOR;
- float dep : DEPTH;
- };
- v2f vert(a2v v)
- {
- v2f o;
- float C = 1.0;
- o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
- //http://outerra.blogspot.co.uk/2009/08/logarithmic-z-buffer.html
- //z = log(C*w + 1) / log(C*Far + 1) * w //DirectX with depth range 0..1
- //or
- //z = (2*log(C*w + 1) / log(C*Far + 1) - 1) * w //OpenGL, depth range -1..1
- o.pos.z = (log(C * o.pos.w + 1.0)) / log(C * _ProjectionParams.z + 1.0);
- o.pos.z *= o.pos.w;
- o.flogz = 1.0 + o.pos.w;
- return o;
- }
- Output frag(v2f i)
- {
- Output o;
- o.col = _Color;
- o.dep = log2(i.flogz) * (0.5 * (2.0 / log2(_ProjectionParams.z + 1.0)));
- return o;
- }
- ENDCG
- }
- }
- FallBack "Diffuse"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement