1 Shader "Hidden/PostProcessing/Debug/Overlays"
5 #include "../StdLib.hlsl"
6 #include "../Colors.hlsl"
9 TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
10 TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture);
11 TEXTURE2D_SAMPLER2D(_CameraDepthNormalsTexture, sampler_CameraDepthNormalsTexture);
12 TEXTURE2D_SAMPLER2D(_CameraMotionVectorsTexture, sampler_CameraMotionVectorsTexture);
15 TEXTURE2D_SAMPLER2D(_CameraGBufferTexture2, sampler_CameraGBufferTexture2);
18 float4 _MainTex_TexelSize;
21 // -----------------------------------------------------------------------------
24 float4 FragDepth(VaryingsDefault i) : SV_Target
26 float d = SAMPLE_DEPTH_TEXTURE_LOD(_CameraDepthTexture, sampler_CameraDepthTexture, i.texcoordStereo, 0);
27 d = lerp(d, Linear01Depth(d), _Params.x);
29 //#if !UNITY_COLORSPACE_GAMMA
30 // d = SRGBToLinear(d);
33 return float4(d.xxx, 1.0);
36 // -----------------------------------------------------------------------------
39 float4 FragNormals(VaryingsDefault i) : SV_Target
42 float3 norm = SAMPLE_TEXTURE2D(_CameraGBufferTexture2, sampler_CameraGBufferTexture2, i.texcoordStereo).xyz * 2.0 - 1.0;
43 float3 n = mul((float3x3)unity_WorldToCamera, norm);
45 float4 cdn = SAMPLE_TEXTURE2D(_CameraDepthNormalsTexture, sampler_CameraDepthNormalsTexture, i.texcoordStereo);
46 float3 n = DecodeViewNormalStereo(cdn) * float3(1.0, 1.0, -1.0);
49 #if UNITY_COLORSPACE_GAMMA
53 return float4(n, 1.0);
56 // -----------------------------------------------------------------------------
59 float DistanceToLine(float2 p, float2 p1, float2 p2)
61 float2 center = (p1 + p2) * 0.5;
62 float len = length(p2 - p1);
63 float2 dir = (p2 - p1) / len;
64 float2 rel_p = p - center;
65 return dot(rel_p, float2(dir.y, -dir.x));
68 float DistanceToSegment(float2 p, float2 p1, float2 p2)
70 float2 center = (p1 + p2) * 0.5;
71 float len = length(p2 - p1);
72 float2 dir = (p2 - p1) / len;
73 float2 rel_p = p - center;
74 float dist1 = abs(dot(rel_p, float2(dir.y, -dir.x)));
75 float dist2 = abs(dot(rel_p, dir)) - 0.5 * len;
76 return max(dist1, dist2);
79 float DrawArrow(float2 texcoord, float body, float head, float height, float linewidth, float antialias)
81 float w = linewidth / 2.0 + antialias;
82 float2 start = -float2(body / 2.0, 0.0);
83 float2 end = float2(body / 2.0, 0.0);
86 float d1 = DistanceToLine(texcoord, end, end - head * float2(1.0, -height));
87 float d2 = DistanceToLine(texcoord, end - head * float2(1.0, height), end);
88 float d3 = texcoord.x - end.x + head;
91 float d4 = DistanceToSegment(texcoord, start, end - float2(linewidth, 0.0));
93 float d = min(max(max(d1, d2), -d3), d4);
97 float2 SampleMotionVectors(float2 coords)
99 float2 mv = SAMPLE_TEXTURE2D(_CameraMotionVectorsTexture, sampler_CameraMotionVectorsTexture, UnityStereoTransformScreenSpaceTex(coords)).xy;
104 float4 FragMotionVectors(VaryingsDefault i) : SV_Target
106 #if UNITY_CAN_READ_POSITION_IN_FRAGMENT_PROGRAM
107 float3 src = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoordStereo).rgb;
108 float2 mv = SampleMotionVectors(i.texcoord);
110 // Background color intensity - keep this low unless you want to make your eyes bleed
111 const float kIntensity = _Params.x;
113 // Map motion vector direction to color wheel (hue between 0 and 360deg)
114 float phi = atan2(mv.x, mv.y);
115 float hue = (phi / PI + 1.0) * 0.5;
116 float r = abs(hue * 6.0 - 3.0) - 1.0;
117 float g = 2.0 - abs(hue * 6.0 - 2.0);
118 float b = 2.0 - abs(hue * 6.0 - 4.0);
119 float a = length(mv * kIntensity);
121 float4 color = saturate(float4(r, g, b, a));
124 const float kGrid = _Params.y;
126 // Arrow grid (aspect ratio is kept)
127 float rows = floor(kGrid * _MainTex_TexelSize.w / _MainTex_TexelSize.z);
129 float2 size = _MainTex_TexelSize.zw / float2(cols, rows);
130 float body = (min(size.x, size.y) / 1.4142135623730951) * saturate(length(mv * kGrid * 0.25));
131 float2 texcoord = i.vertex.xy;
132 float2 center = (floor(texcoord / size) + 0.5) * size;
135 // Sample the center of the cell to get the current arrow vector
136 float2 arrow_coord = center / _MainTex_TexelSize.zw;
137 float2 mv_arrow = SampleMotionVectors(arrow_coord);
143 // Rotate the arrow according to the direction
144 mv_arrow = normalize(mv_arrow);
145 float2x2 rot = float2x2(mv_arrow.x, -mv_arrow.y, mv_arrow.y, mv_arrow.x);
146 texcoord = mul(rot, texcoord);
148 d = DrawArrow(texcoord, body, 0.25 * body, 0.5, 2.0, 1.0);
149 d = 1.0 - saturate(d);
152 #if !UNITY_COLORSPACE_GAMMA
153 src = LinearToSRGB(src);
156 color.rgb = lerp(src, color.rgb, color.a);
158 #if !UNITY_COLORSPACE_GAMMA
159 color.rgb = SRGBToLinear(color.rgb);
162 return float4(color.rgb + d.xxx, 1.0);
164 // Reading vertex SV_POSITION in a fragment shader is not supported by this platform so just return solid color.
165 return float4(1.0f, 0.0f, 1.0f, 1.0f);
169 // -----------------------------------------------------------------------------
172 float4 FragNANTracker(VaryingsDefault i) : SV_Target
174 float4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoordStereo);
178 color = float4(1.0, 0.0, 1.0, 1.0);
182 // Dim the color buffer so we can see NaNs & Infs better
183 color.rgb = saturate(color.rgb) * 0.25;
189 // -----------------------------------------------------------------------------
190 // Color blindness simulation
192 float3 RGFilter(float3 color, float k1, float k2, float k3)
194 float3 c_lin = color * 128.498039;
196 float r_blind = (k1 * c_lin.r + k2 * c_lin.g) / 16448.25098;
197 float b_blind = (k3 * c_lin.r - k3 * c_lin.g + 128.498039 * c_lin.b) / 16448.25098;
198 r_blind = saturate(r_blind);
199 b_blind = saturate(b_blind);
201 return lerp(color, float3(r_blind, r_blind, b_blind), _Params.x);
204 float4 FragDeuteranopia(VaryingsDefault i) : SV_Target
206 float3 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoordStereo).rgb;
207 color = saturate(color);
209 #if UNITY_COLORSPACE_GAMMA
210 color = SRGBToLinear(color);
213 color = RGFilter(color, 37.611765, 90.87451, -2.862745);
215 #if UNITY_COLORSPACE_GAMMA
216 color = LinearToSRGB(color);
219 return float4(color, 1.0);
222 float4 FragProtanopia(VaryingsDefault i) : SV_Target
224 float3 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoordStereo).rgb;
225 color = saturate(color);
227 #if UNITY_COLORSPACE_GAMMA
228 color = SRGBToLinear(color);
231 color = RGFilter(color, 14.443137, 114.054902, 0.513725);
233 #if UNITY_COLORSPACE_GAMMA
234 color = LinearToSRGB(color);
237 return float4(color, 1.0);
240 float4 FragTritanopia(VaryingsDefault i) : SV_Target
242 float3 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoordStereo).rgb;
243 color = saturate(color);
245 float anchor_e0 = 0.05059983 + 0.08585369 + 0.00952420;
246 float anchor_e1 = 0.01893033 + 0.08925308 + 0.01370054;
247 float anchor_e2 = 0.00292202 + 0.00975732 + 0.07145979;
248 float inflection = anchor_e1 / anchor_e0;
250 float a1 = -anchor_e2 * 0.007009;
251 float b1 = anchor_e2 * 0.0914;
252 float c1 = anchor_e0 * 0.007009 - anchor_e1 * 0.0914;
253 float a2 = anchor_e1 * 0.3636 - anchor_e2 * 0.2237;
254 float b2 = anchor_e2 * 0.1284 - anchor_e0 * 0.3636;
255 float c2 = anchor_e0 * 0.2237 - anchor_e1 * 0.1284;
257 #if UNITY_COLORSPACE_GAMMA
258 color = SRGBToLinear(color);
261 float3 c_lin = color * 128.498039;
263 float L = (c_lin.r * 0.05059983 + c_lin.g * 0.08585369 + c_lin.b * 0.00952420) / 128.498039;
264 float M = (c_lin.r * 0.01893033 + c_lin.g * 0.08925308 + c_lin.b * 0.01370054) / 128.498039;
265 float S = (c_lin.r * 0.00292202 + c_lin.g * 0.00975732 + c_lin.b * 0.07145979) / 128.498039;
269 if (tmp < inflection) S = -(a1 * L + b1 * M) / c1;
270 else S = -(a2 * L + b2 * M) / c2;
272 float r = L * 30.830854 - M * 29.832659 + S * 1.610474;
273 float g = -L * 6.481468 + M * 17.715578 - S * 2.532642;
274 float b = -L * 0.375690 - M * 1.199062 + S * 14.273846;
276 color = lerp(color, saturate(float3(r, g, b)), _Params.x);
278 #if UNITY_COLORSPACE_GAMMA
279 color = LinearToSRGB(color);
282 return float4(color, 1.0);
289 Cull Off ZWrite Off ZTest Always
296 #pragma vertex VertDefault
297 #pragma fragment FragDepth
307 #pragma vertex VertDefault
308 #pragma fragment FragNormals
309 #pragma multi_compile _ SOURCE_GBUFFER
314 // 2 - Motion vectors
319 #pragma vertex VertDefault
320 #pragma fragment FragMotionVectors
330 #pragma vertex VertDefault
331 #pragma fragment FragNANTracker
336 // 4 - Color blindness (deuteranopia)
341 #pragma vertex VertDefault
342 #pragma fragment FragDeuteranopia
347 // 5 - Color blindness (protanopia)
352 #pragma vertex VertDefault
353 #pragma fragment FragProtanopia
358 // 6 - Color blindness (tritanopia)
363 #pragma vertex VertDefault
364 #pragma fragment FragTritanopia