This repository has been archived by the owner on Nov 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 747
/
Overlays.shader
369 lines (283 loc) · 11.7 KB
/
Overlays.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
Shader "Hidden/PostProcessing/Debug/Overlays"
{
HLSLINCLUDE
#include "../StdLib.hlsl"
#include "../Colors.hlsl"
#pragma target 3.0
TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture);
TEXTURE2D_SAMPLER2D(_CameraDepthNormalsTexture, sampler_CameraDepthNormalsTexture);
TEXTURE2D_SAMPLER2D(_CameraMotionVectorsTexture, sampler_CameraMotionVectorsTexture);
#if SOURCE_GBUFFER
TEXTURE2D_SAMPLER2D(_CameraGBufferTexture2, sampler_CameraGBufferTexture2);
#endif
float4 _MainTex_TexelSize;
float4 _Params;
// -----------------------------------------------------------------------------
// Depth
float4 FragDepth(VaryingsDefault i) : SV_Target
{
float d = SAMPLE_DEPTH_TEXTURE_LOD(_CameraDepthTexture, sampler_CameraDepthTexture, i.texcoordStereo, 0);
d = lerp(d, Linear01Depth(d), _Params.x);
//#if !UNITY_COLORSPACE_GAMMA
// d = SRGBToLinear(d);
//#endif
return float4(d.xxx, 1.0);
}
// -----------------------------------------------------------------------------
// Normals
float4 FragNormals(VaryingsDefault i) : SV_Target
{
#if SOURCE_GBUFFER
float3 norm = SAMPLE_TEXTURE2D(_CameraGBufferTexture2, sampler_CameraGBufferTexture2, i.texcoordStereo).xyz * 2.0 - 1.0;
float3 n = mul((float3x3)unity_WorldToCamera, norm);
#else
float4 cdn = SAMPLE_TEXTURE2D(_CameraDepthNormalsTexture, sampler_CameraDepthNormalsTexture, i.texcoordStereo);
float3 n = DecodeViewNormalStereo(cdn) * float3(1.0, 1.0, -1.0);
#endif
#if UNITY_COLORSPACE_GAMMA
n = LinearToSRGB(n);
#endif
return float4(n, 1.0);
}
// -----------------------------------------------------------------------------
// Motion vectors
float DistanceToLine(float2 p, float2 p1, float2 p2)
{
float2 center = (p1 + p2) * 0.5;
float len = length(p2 - p1);
float2 dir = (p2 - p1) / len;
float2 rel_p = p - center;
return dot(rel_p, float2(dir.y, -dir.x));
}
float DistanceToSegment(float2 p, float2 p1, float2 p2)
{
float2 center = (p1 + p2) * 0.5;
float len = length(p2 - p1);
float2 dir = (p2 - p1) / len;
float2 rel_p = p - center;
float dist1 = abs(dot(rel_p, float2(dir.y, -dir.x)));
float dist2 = abs(dot(rel_p, dir)) - 0.5 * len;
return max(dist1, dist2);
}
float DrawArrow(float2 texcoord, float body, float head, float height, float linewidth, float antialias)
{
float w = linewidth / 2.0 + antialias;
float2 start = -float2(body / 2.0, 0.0);
float2 end = float2(body / 2.0, 0.0);
// Head: 3 lines
float d1 = DistanceToLine(texcoord, end, end - head * float2(1.0, -height));
float d2 = DistanceToLine(texcoord, end - head * float2(1.0, height), end);
float d3 = texcoord.x - end.x + head;
// Body: 1 segment
float d4 = DistanceToSegment(texcoord, start, end - float2(linewidth, 0.0));
float d = min(max(max(d1, d2), -d3), d4);
return d;
}
float2 SampleMotionVectors(float2 coords)
{
float2 mv = SAMPLE_TEXTURE2D(_CameraMotionVectorsTexture, sampler_CameraMotionVectorsTexture, UnityStereoTransformScreenSpaceTex(coords)).xy;
mv.y *= -1.0;
return mv;
}
float4 FragMotionVectors(VaryingsDefault i) : SV_Target
{
#if UNITY_CAN_READ_POSITION_IN_FRAGMENT_PROGRAM
float3 src = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoordStereo).rgb;
float2 mv = SampleMotionVectors(i.texcoord);
// Background color intensity - keep this low unless you want to make your eyes bleed
const float kIntensity = _Params.x;
// Map motion vector direction to color wheel (hue between 0 and 360deg)
float phi = atan2(mv.x, mv.y);
float hue = (phi / PI + 1.0) * 0.5;
float r = abs(hue * 6.0 - 3.0) - 1.0;
float g = 2.0 - abs(hue * 6.0 - 2.0);
float b = 2.0 - abs(hue * 6.0 - 4.0);
float a = length(mv * kIntensity);
float4 color = saturate(float4(r, g, b, a));
// Grid subdivisions
const float kGrid = _Params.y;
// Arrow grid (aspect ratio is kept)
float rows = floor(kGrid * _MainTex_TexelSize.w / _MainTex_TexelSize.z);
float cols = kGrid;
float2 size = _MainTex_TexelSize.zw / float2(cols, rows);
float body = (min(size.x, size.y) / 1.4142135623730951) * saturate(length(mv * kGrid * 0.25));
float2 texcoord = i.vertex.xy;
float2 center = (floor(texcoord / size) + 0.5) * size;
texcoord -= center;
// Sample the center of the cell to get the current arrow vector
float2 arrow_coord = center / _MainTex_TexelSize.zw;
float2 mv_arrow = SampleMotionVectors(arrow_coord);
// Skip empty motion
float d = 0.0;
if (any(mv_arrow))
{
// Rotate the arrow according to the direction
mv_arrow = normalize(mv_arrow);
float2x2 rot = float2x2(mv_arrow.x, -mv_arrow.y, mv_arrow.y, mv_arrow.x);
texcoord = mul(rot, texcoord);
d = DrawArrow(texcoord, body, 0.25 * body, 0.5, 2.0, 1.0);
d = 1.0 - saturate(d);
}
#if !UNITY_COLORSPACE_GAMMA
src = LinearToSRGB(src);
#endif
color.rgb = lerp(src, color.rgb, color.a);
#if !UNITY_COLORSPACE_GAMMA
color.rgb = SRGBToLinear(color.rgb);
#endif
return float4(color.rgb + d.xxx, 1.0);
#else
// Reading vertex SV_POSITION in a fragment shader is not supported by this platform so just return solid color.
return float4(1.0f, 0.0f, 1.0f, 1.0f);
#endif
}
// -----------------------------------------------------------------------------
// NAN tracker
float4 FragNANTracker(VaryingsDefault i) : SV_Target
{
float4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoordStereo);
if (AnyIsNan(color))
{
color = float4(1.0, 0.0, 1.0, 1.0);
}
else
{
// Dim the color buffer so we can see NaNs & Infs better
color.rgb = saturate(color.rgb) * 0.25;
}
return color;
}
// -----------------------------------------------------------------------------
// Color blindness simulation
float3 RGFilter(float3 color, float k1, float k2, float k3)
{
float3 c_lin = color * 128.498039;
float r_blind = (k1 * c_lin.r + k2 * c_lin.g) / 16448.25098;
float b_blind = (k3 * c_lin.r - k3 * c_lin.g + 128.498039 * c_lin.b) / 16448.25098;
r_blind = saturate(r_blind);
b_blind = saturate(b_blind);
return lerp(color, float3(r_blind, r_blind, b_blind), _Params.x);
}
float4 FragDeuteranopia(VaryingsDefault i) : SV_Target
{
float3 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoordStereo).rgb;
color = saturate(color);
#if UNITY_COLORSPACE_GAMMA
color = SRGBToLinear(color);
#endif
color = RGFilter(color, 37.611765, 90.87451, -2.862745);
#if UNITY_COLORSPACE_GAMMA
color = LinearToSRGB(color);
#endif
return float4(color, 1.0);
}
float4 FragProtanopia(VaryingsDefault i) : SV_Target
{
float3 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoordStereo).rgb;
color = saturate(color);
#if UNITY_COLORSPACE_GAMMA
color = SRGBToLinear(color);
#endif
color = RGFilter(color, 14.443137, 114.054902, 0.513725);
#if UNITY_COLORSPACE_GAMMA
color = LinearToSRGB(color);
#endif
return float4(color, 1.0);
}
float4 FragTritanopia(VaryingsDefault i) : SV_Target
{
float3 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoordStereo).rgb;
color = saturate(color);
float anchor_e0 = 0.05059983 + 0.08585369 + 0.00952420;
float anchor_e1 = 0.01893033 + 0.08925308 + 0.01370054;
float anchor_e2 = 0.00292202 + 0.00975732 + 0.07145979;
float inflection = anchor_e1 / anchor_e0;
float a1 = -anchor_e2 * 0.007009;
float b1 = anchor_e2 * 0.0914;
float c1 = anchor_e0 * 0.007009 - anchor_e1 * 0.0914;
float a2 = anchor_e1 * 0.3636 - anchor_e2 * 0.2237;
float b2 = anchor_e2 * 0.1284 - anchor_e0 * 0.3636;
float c2 = anchor_e0 * 0.2237 - anchor_e1 * 0.1284;
#if UNITY_COLORSPACE_GAMMA
color = SRGBToLinear(color);
#endif
float3 c_lin = color * 128.498039;
float L = (c_lin.r * 0.05059983 + c_lin.g * 0.08585369 + c_lin.b * 0.00952420) / 128.498039;
float M = (c_lin.r * 0.01893033 + c_lin.g * 0.08925308 + c_lin.b * 0.01370054) / 128.498039;
float S = (c_lin.r * 0.00292202 + c_lin.g * 0.00975732 + c_lin.b * 0.07145979) / 128.498039;
float tmp = M / L;
if (tmp < inflection) S = -(a1 * L + b1 * M) / c1;
else S = -(a2 * L + b2 * M) / c2;
float r = L * 30.830854 - M * 29.832659 + S * 1.610474;
float g = -L * 6.481468 + M * 17.715578 - S * 2.532642;
float b = -L * 0.375690 - M * 1.199062 + S * 14.273846;
color = lerp(color, saturate(float3(r, g, b)), _Params.x);
#if UNITY_COLORSPACE_GAMMA
color = LinearToSRGB(color);
#endif
return float4(color, 1.0);
}
ENDHLSL
SubShader
{
Cull Off ZWrite Off ZTest Always
// 0 - Depth
Pass
{
HLSLPROGRAM
#pragma vertex VertDefault
#pragma fragment FragDepth
ENDHLSL
}
// 1 - Normals
Pass
{
HLSLPROGRAM
#pragma vertex VertDefault
#pragma fragment FragNormals
#pragma multi_compile _ SOURCE_GBUFFER
ENDHLSL
}
// 2 - Motion vectors
Pass
{
HLSLPROGRAM
#pragma vertex VertDefault
#pragma fragment FragMotionVectors
ENDHLSL
}
// 3 - Nan tracker
Pass
{
HLSLPROGRAM
#pragma vertex VertDefault
#pragma fragment FragNANTracker
ENDHLSL
}
// 4 - Color blindness (deuteranopia)
Pass
{
HLSLPROGRAM
#pragma vertex VertDefault
#pragma fragment FragDeuteranopia
ENDHLSL
}
// 5 - Color blindness (protanopia)
Pass
{
HLSLPROGRAM
#pragma vertex VertDefault
#pragma fragment FragProtanopia
ENDHLSL
}
// 6 - Color blindness (tritanopia)
Pass
{
HLSLPROGRAM
#pragma vertex VertDefault
#pragma fragment FragTritanopia
ENDHLSL
}
}
}