Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CameraMotionBlur.shader 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. Shader "Hidden/Universal Render Pipeline/CameraMotionBlur"
  2. {
  3. HLSLINCLUDE
  4. #pragma vertex VertCMB
  5. #pragma fragment FragCMB
  6. #pragma multi_compile_fragment _ _ENABLE_ALPHA_OUTPUT
  7. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
  8. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Random.hlsl"
  9. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  10. #include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"
  11. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
  12. #if defined(USING_STEREO_MATRICES)
  13. float4x4 _ViewProjMStereo[2];
  14. float4x4 _PrevViewProjMStereo[2];
  15. #define _ViewProjM _ViewProjMStereo[unity_StereoEyeIndex]
  16. #define _PrevViewProjM _PrevViewProjMStereo[unity_StereoEyeIndex]
  17. #else
  18. float4x4 _ViewProjM;
  19. float4x4 _PrevViewProjM;
  20. #endif
  21. half _Intensity;
  22. half _Clamp;
  23. half4 _SourceSize;
  24. TEXTURE2D_X(_MotionVectorTexture);
  25. struct VaryingsCMB
  26. {
  27. float4 positionCS : SV_POSITION;
  28. float4 texcoord : TEXCOORD0;
  29. UNITY_VERTEX_OUTPUT_STEREO
  30. };
  31. VaryingsCMB VertCMB(Attributes input)
  32. {
  33. VaryingsCMB output;
  34. UNITY_SETUP_INSTANCE_ID(input);
  35. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  36. float4 pos = GetFullScreenTriangleVertexPosition(input.vertexID);
  37. float2 uv = GetFullScreenTriangleTexCoord(input.vertexID);
  38. output.positionCS = pos;
  39. output.texcoord.xy = DYNAMIC_SCALING_APPLY_SCALEBIAS(uv);
  40. float4 projPos = output.positionCS * 0.5;
  41. projPos.xy = projPos.xy + projPos.w;
  42. output.texcoord.zw = projPos.xy;
  43. return output;
  44. }
  45. half2 ClampVelocity(half2 velocity, half maxVelocity)
  46. {
  47. half len = length(velocity);
  48. return (len > 0.0) ? min(len, maxVelocity) * (velocity * rcp(len)) : 0.0;
  49. }
  50. half2 GetVelocity(float2 uv)
  51. {
  52. // Unity motion vectors are forward motion vectors in screen UV space
  53. half2 offsetUv = SAMPLE_TEXTURE2D_X(_MotionVectorTexture, sampler_LinearClamp, uv).xy;
  54. return -offsetUv;
  55. }
  56. // Per-pixel camera velocity
  57. half2 GetCameraVelocity(float4 uv)
  58. {
  59. #if UNITY_REVERSED_Z
  60. half depth = SampleSceneDepth(uv.xy).x;
  61. #else
  62. half depth = lerp(UNITY_NEAR_CLIP_VALUE, 1, SampleSceneDepth(uv.xy).x);
  63. #endif
  64. float4 worldPos = float4(ComputeWorldSpacePosition(uv.xy, depth, UNITY_MATRIX_I_VP), 1.0);
  65. float4 prevClipPos = mul(_PrevViewProjM, worldPos);
  66. float4 curClipPos = mul(_ViewProjM, worldPos);
  67. half2 prevPosCS = prevClipPos.xy / prevClipPos.w;
  68. half2 curPosCS = curClipPos.xy / curClipPos.w;
  69. // Backwards motion vectors
  70. half2 velocity = (prevPosCS - curPosCS);
  71. #if UNITY_UV_STARTS_AT_TOP
  72. velocity.y = -velocity.y;
  73. #endif
  74. return ClampVelocity(velocity, _Clamp);
  75. }
  76. half4 GatherSample(half sampleNumber, half2 velocity, half invSampleCount, float2 centerUV, half randomVal, half velocitySign)
  77. {
  78. half offsetLength = (sampleNumber + 0.5h) + (velocitySign * (randomVal - 0.5h));
  79. float2 sampleUV = centerUV + (offsetLength * invSampleCount) * velocity * velocitySign;
  80. return SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_PointClamp, sampleUV);
  81. }
  82. half4 DoMotionBlur(VaryingsCMB input, int iterations, int useMotionVectors)
  83. {
  84. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  85. float2 uv = UnityStereoTransformScreenSpaceTex(input.texcoord.xy);
  86. half2 velocity;
  87. if(useMotionVectors == 1)
  88. {
  89. velocity = GetVelocity(uv) * _Intensity;
  90. // Scale back to -1, 1 from 0..1 to match GetCameraVelocity. A workaround to keep existing visual look.
  91. // TODO: There's bug in GetCameraVelocity, which is using NDC and not UV
  92. velocity *= 2;
  93. }
  94. else
  95. velocity = GetCameraVelocity(float4(uv, input.texcoord.zw)) * _Intensity;
  96. half randomVal = InterleavedGradientNoise(uv * _SourceSize.xy, 0);
  97. half invSampleCount = rcp(iterations * 2.0);
  98. half4 color = 0.0;
  99. UNITY_UNROLL
  100. for (int i = 0; i < iterations; i++)
  101. {
  102. color += GatherSample(i, velocity, invSampleCount, uv, randomVal, -1.0);
  103. color += GatherSample(i, velocity, invSampleCount, uv, randomVal, 1.0);
  104. }
  105. #if _ENABLE_ALPHA_OUTPUT
  106. return color * invSampleCount;
  107. #else
  108. // NOTE: Rely on the compiler to eliminate .w computation above
  109. return half4(color.xyz * invSampleCount, 1.0);
  110. #endif
  111. }
  112. ENDHLSL
  113. SubShader
  114. {
  115. Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline"}
  116. LOD 100
  117. ZTest Always ZWrite Off Cull Off
  118. Pass
  119. {
  120. Name "Camera Motion Blur - Low Quality"
  121. HLSLPROGRAM
  122. half4 FragCMB(VaryingsCMB input) : SV_Target
  123. {
  124. return DoMotionBlur(input, 2, 0);
  125. }
  126. ENDHLSL
  127. }
  128. Pass
  129. {
  130. Name "Camera Motion Blur - Medium Quality"
  131. HLSLPROGRAM
  132. half4 FragCMB(VaryingsCMB input) : SV_Target
  133. {
  134. return DoMotionBlur(input, 3, 0);
  135. }
  136. ENDHLSL
  137. }
  138. Pass
  139. {
  140. Name "Camera Motion Blur - High Quality"
  141. HLSLPROGRAM
  142. half4 FragCMB(VaryingsCMB input) : SV_Target
  143. {
  144. return DoMotionBlur(input, 4, 0);
  145. }
  146. ENDHLSL
  147. }
  148. Pass
  149. {
  150. Name "Camera And Object Motion Blur - Low Quality"
  151. HLSLPROGRAM
  152. half4 FragCMB(VaryingsCMB input) : SV_Target
  153. {
  154. return DoMotionBlur(input, 2, 1);
  155. }
  156. ENDHLSL
  157. }
  158. Pass
  159. {
  160. Name "Camera And Object Motion Blur - Medium Quality"
  161. HLSLPROGRAM
  162. half4 FragCMB(VaryingsCMB input) : SV_Target
  163. {
  164. return DoMotionBlur(input, 3, 1);
  165. }
  166. ENDHLSL
  167. }
  168. Pass
  169. {
  170. Name "Camera And Object Motion Blur - High Quality"
  171. HLSLPROGRAM
  172. half4 FragCMB(VaryingsCMB input) : SV_Target
  173. {
  174. return DoMotionBlur(input, 4, 1);
  175. }
  176. ENDHLSL
  177. }
  178. }
  179. }