Overlays.shader 12 KB

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