You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
6.9 KiB

  1. Shader "Hidden/Post FX/Builtin Debug Views"
  2. {
  3. CGINCLUDE
  4. #include "UnityCG.cginc"
  5. #include "Common.cginc"
  6. #pragma exclude_renderers d3d11_9x
  7. sampler2D_float _CameraDepthTexture;
  8. sampler2D_float _CameraDepthNormalsTexture;
  9. sampler2D_float _CameraMotionVectorsTexture;
  10. float4 _CameraDepthTexture_ST;
  11. float4 _CameraDepthNormalsTexture_ST;
  12. float4 _CameraMotionVectorsTexture_ST;
  13. #if SOURCE_GBUFFER
  14. sampler2D _CameraGBufferTexture2;
  15. float4 _CameraGBufferTexture2_ST;
  16. #endif
  17. // -----------------------------------------------------------------------------
  18. // Depth
  19. float _DepthScale;
  20. float4 FragDepth(VaryingsDefault i) : SV_Target
  21. {
  22. float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, UnityStereoScreenSpaceUVAdjust(i.uv, _CameraDepthTexture_ST));
  23. depth = Linear01Depth(depth) * _DepthScale;
  24. float3 d = depth.xxx;
  25. #if !UNITY_COLORSPACE_GAMMA
  26. d = GammaToLinearSpace(d);
  27. #endif
  28. return float4(d, 1.0);
  29. }
  30. // -----------------------------------------------------------------------------
  31. // Normals
  32. float3 SampleNormal(float2 uv)
  33. {
  34. #if SOURCE_GBUFFER
  35. float3 norm = tex2D(_CameraGBufferTexture2, uv).xyz * 2.0 - 1.0;
  36. return mul((float3x3)unity_WorldToCamera, norm);
  37. #else
  38. float4 cdn = tex2D(_CameraDepthNormalsTexture, uv);
  39. return DecodeViewNormalStereo(cdn) * float3(1.0, 1.0, -1.0);
  40. #endif
  41. }
  42. float4 FragNormals(VaryingsDefault i) : SV_Target
  43. {
  44. float3 n = SampleNormal(UnityStereoScreenSpaceUVAdjust(i.uv, _CameraDepthNormalsTexture_ST));
  45. #if UNITY_COLORSPACE_GAMMA
  46. n = LinearToGammaSpace(n);
  47. #endif
  48. return float4(n, 1.0);
  49. }
  50. // -----------------------------------------------------------------------------
  51. // Motion vectors
  52. float _Opacity;
  53. float _Amplitude;
  54. float4 _Scale;
  55. float4 FragMovecsOpacity(VaryingsDefault i) : SV_Target
  56. {
  57. float4 src = tex2D(_MainTex, i.uv);
  58. return float4(src.rgb * _Opacity, src.a);
  59. }
  60. // Convert a motion vector into RGBA color.
  61. float4 VectorToColor(float2 mv)
  62. {
  63. float phi = atan2(mv.x, mv.y);
  64. float hue = (phi / UNITY_PI + 1.0) * 0.5;
  65. float r = abs(hue * 6.0 - 3.0) - 1.0;
  66. float g = 2.0 - abs(hue * 6.0 - 2.0);
  67. float b = 2.0 - abs(hue * 6.0 - 4.0);
  68. float a = length(mv);
  69. return saturate(float4(r, g, b, a));
  70. }
  71. float4 FragMovecsImaging(VaryingsDefault i) : SV_Target
  72. {
  73. float4 src = tex2D(_MainTex, i.uv);
  74. float2 mv = tex2D(_CameraMotionVectorsTexture, i.uv).rg * _Amplitude;
  75. #if UNITY_UV_STARTS_AT_TOP
  76. mv.y *= -1.0;
  77. #endif
  78. float4 mc = VectorToColor(mv);
  79. float3 rgb = src.rgb;
  80. #if !UNITY_COLORSPACE_GAMMA
  81. rgb = LinearToGammaSpace(rgb);
  82. #endif
  83. rgb = lerp(rgb, mc.rgb, mc.a * _Opacity);
  84. #if !UNITY_COLORSPACE_GAMMA
  85. rgb = GammaToLinearSpace(rgb);
  86. #endif
  87. return float4(rgb, src.a);
  88. }
  89. struct VaryingsArrows
  90. {
  91. float4 vertex : SV_POSITION;
  92. float2 scoord : TEXCOORD;
  93. float4 color : COLOR;
  94. };
  95. VaryingsArrows VertArrows(AttributesDefault v)
  96. {
  97. // Retrieve the motion vector.
  98. float4 uv = float4(v.texcoord.xy, 0.0, 0.0);
  99. #if UNITY_UV_STARTS_AT_TOP
  100. uv.y = 1.0 - uv.y;
  101. #endif
  102. float2 mv = tex2Dlod(_CameraMotionVectorsTexture, uv).rg * _Amplitude;
  103. #if UNITY_UV_STARTS_AT_TOP
  104. mv.y *= -1.0;
  105. #endif
  106. // Arrow color
  107. float4 color = VectorToColor(mv);
  108. // Make a rotation matrix based on the motion vector.
  109. float2x2 rot = float2x2(mv.y, mv.x, -mv.x, mv.y);
  110. // Rotate and scale the body of the arrow.
  111. float2 pos = mul(rot, v.vertex.zy) * _Scale.xy;
  112. // Normalized variant of the motion vector and the rotation matrix.
  113. float2 mv_n = normalize(mv);
  114. float2x2 rot_n = float2x2(mv_n.y, mv_n.x, -mv_n.x, mv_n.y);
  115. // Rotate and scale the head of the arrow.
  116. float2 head = float2(v.vertex.x, -abs(v.vertex.x)) * 0.3;
  117. head *= saturate(color.a);
  118. pos += mul(rot_n, head) * _Scale.xy;
  119. // Offset the arrow position.
  120. pos += v.texcoord.xy * 2.0 - 1.0;
  121. // Convert to the screen coordinates.
  122. float2 scoord = (pos + 1.0) * 0.5 * _ScreenParams.xy;
  123. // Snap to a pixel-perfect position.
  124. scoord = round(scoord);
  125. // Bring back to the normalized screen space.
  126. pos = (scoord + 0.5) * (_ScreenParams.zw - 1.0) * 2.0 - 1.0;
  127. // Color tweaks
  128. color.rgb = GammaToLinearSpace(lerp(color.rgb, 1.0, 0.5));
  129. color.a *= _Opacity;
  130. // Output
  131. VaryingsArrows o;
  132. o.vertex = float4(pos, 0.0, 1.0);
  133. o.scoord = scoord;
  134. o.color = saturate(color);
  135. return o;
  136. }
  137. float4 FragMovecsArrows(VaryingsArrows i) : SV_Target
  138. {
  139. // Pseudo anti-aliasing.
  140. float aa = length(frac(i.scoord) - 0.5) / 0.707;
  141. aa *= (aa * (aa * 0.305306011 + 0.682171111) + 0.012522878); // gamma
  142. return float4(i.color.rgb, i.color.a * aa);
  143. }
  144. ENDCG
  145. SubShader
  146. {
  147. Cull Off ZWrite Off ZTest Always
  148. // (0) - Depth
  149. Pass
  150. {
  151. CGPROGRAM
  152. #pragma vertex VertDefault
  153. #pragma fragment FragDepth
  154. ENDCG
  155. }
  156. // (1) - Normals
  157. Pass
  158. {
  159. CGPROGRAM
  160. #pragma vertex VertDefault
  161. #pragma fragment FragNormals
  162. #pragma multi_compile __ SOURCE_GBUFFER
  163. ENDCG
  164. }
  165. // (2) - Motion vectors - Opacity
  166. Pass
  167. {
  168. CGPROGRAM
  169. #pragma vertex VertDefault
  170. #pragma fragment FragMovecsOpacity
  171. ENDCG
  172. }
  173. // (3) - Motion vectors - Imaging
  174. Pass
  175. {
  176. CGPROGRAM
  177. #pragma vertex VertDefault
  178. #pragma fragment FragMovecsImaging
  179. #pragma multi_compile __ UNITY_COLORSPACE_GAMMA
  180. ENDCG
  181. }
  182. // (4) - Motion vectors - Arrows
  183. Pass
  184. {
  185. Blend SrcAlpha OneMinusSrcAlpha
  186. CGPROGRAM
  187. #pragma vertex VertArrows
  188. #pragma fragment FragMovecsArrows
  189. ENDCG
  190. }
  191. }
  192. }