설명 없음
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.

VFXCommon.hlsl 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
  2. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
  3. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Texture.hlsl"
  4. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  5. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
  6. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/AmbientOcclusion.hlsl"
  7. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
  8. #include "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/ShaderPass.hlsl"
  9. float3 _LightDirection;
  10. #ifdef VFX_VARYING_PS_INPUTS
  11. void VFXTransformPSInputs(inout VFX_VARYING_PS_INPUTS input) {}
  12. float4 VFXApplyPreExposure(float4 color, float exposureWeight)
  13. {
  14. return color;
  15. }
  16. float4 VFXApplyPreExposure(float4 color, VFX_VARYING_PS_INPUTS input)
  17. {
  18. return color;
  19. }
  20. #endif
  21. float2 VFXGetNormalizedScreenSpaceUV(float4 clipPos)
  22. {
  23. return GetNormalizedScreenSpaceUV(clipPos);
  24. }
  25. void VFXEncodeMotionVector(float2 velocity, out float4 outBuffer)
  26. {
  27. outBuffer = float4(velocity.xy, 0, 0);
  28. }
  29. float4x4 VFXGetObjectToWorldMatrix()
  30. {
  31. // NOTE: If using the new generation path, explicitly call the object matrix (since the particle matrix is now baked into UNITY_MATRIX_M)
  32. #if defined(HAVE_VFX_MODIFICATION) && !defined(SHADER_STAGE_COMPUTE)
  33. return GetSGVFXUnityObjectToWorld();
  34. #else
  35. return GetObjectToWorldMatrix();
  36. #endif
  37. }
  38. float4x4 VFXGetWorldToObjectMatrix()
  39. {
  40. // NOTE: If using the new generation path, explicitly call the object matrix (since the particle matrix is now baked into UNITY_MATRIX_I_M)
  41. #if defined(HAVE_VFX_MODIFICATION) && !defined(SHADER_STAGE_COMPUTE)
  42. return GetSGVFXUnityWorldToObject();
  43. #else
  44. return GetWorldToObjectMatrix();
  45. #endif
  46. }
  47. float4 VFXTransformPositionWorldToClip(float3 posWS)
  48. {
  49. return TransformWorldToHClip(posWS);
  50. }
  51. float4 VFXTransformPositionWorldToNonJitteredClip(float3 posWS)
  52. {
  53. return mul(_NonJitteredViewProjMatrix, float4(posWS, 1.0f));
  54. }
  55. float4 VFXTransformPositionWorldToPreviousClip(float3 posWS)
  56. {
  57. return mul(_PrevViewProjMatrix, float4(posWS, 1.0f));
  58. }
  59. float4 VFXTransformPositionObjectToClip(float3 posOS)
  60. {
  61. float3 posWS = mul(VFXGetObjectToWorldMatrix(), float4(posOS,1)).xyz;
  62. return VFXTransformPositionWorldToClip(posWS);
  63. }
  64. float4 VFXTransformPositionObjectToNonJitteredClip(float3 posOS)
  65. {
  66. float3 posWS = mul(VFXGetObjectToWorldMatrix(), float4(posOS,1)).xyz;
  67. return VFXTransformPositionWorldToNonJitteredClip(posWS);
  68. }
  69. float3 VFXTransformPreviousObjectToWorld(float3 posOS)
  70. {
  71. return mul(GetPrevObjectToWorldMatrix(), float4(posOS, 1.0)).xyz;
  72. }
  73. float4 VFXTransformPositionObjectToPreviousClip(float3 posOS)
  74. {
  75. float3 posWS = VFXTransformPreviousObjectToWorld(posOS);
  76. return VFXTransformPositionWorldToPreviousClip(posWS);
  77. }
  78. float3 VFXTransformPositionWorldToView(float3 posWS)
  79. {
  80. return TransformWorldToView(posWS);
  81. }
  82. float3 VFXTransformPositionWorldToCameraRelative(float3 posWS)
  83. {
  84. #if SHADEROPTIONS_CAMERA_RELATIVE_RENDERING
  85. #error VFX Camera Relative rendering isn't supported in URP.
  86. #endif
  87. return posWS;
  88. }
  89. //Compatibility functions for the common ShaderGraph integration
  90. float4x4 ApplyCameraTranslationToMatrix(float4x4 modelMatrix)
  91. {
  92. return modelMatrix;
  93. }
  94. float4x4 ApplyCameraTranslationToInverseMatrix(float4x4 inverseModelMatrix)
  95. {
  96. return inverseModelMatrix;
  97. }
  98. //End of compatibility functions
  99. float3x3 VFXGetWorldToViewRotMatrix()
  100. {
  101. return (float3x3)GetWorldToViewMatrix();
  102. }
  103. float3 VFXGetViewWorldPosition()
  104. {
  105. return GetCurrentViewPosition();
  106. }
  107. float4x4 VFXGetViewToWorldMatrix()
  108. {
  109. return UNITY_MATRIX_I_V;
  110. }
  111. #ifdef USING_STEREO_MATRICES
  112. float3 GetWorldStereoOffset()
  113. {
  114. return unity_StereoWorldSpaceCameraPos[0].xyz - unity_StereoWorldSpaceCameraPos[1].xyz;
  115. }
  116. float4x4 GetNonJitteredViewProjMatrix(int eye)
  117. {
  118. return _NonJitteredViewProjMatrixStereo[eye];
  119. }
  120. #endif
  121. float VFXSampleDepth(float4 posSS)
  122. {
  123. float2 screenUV = GetNormalizedScreenSpaceUV(posSS.xy);
  124. // In URP, the depth texture is optional and could be 4x4 white texture, Load isn't appropriate in that case.
  125. //float depth = LoadSceneDepth(screenUV * _ScreenParams.xy);
  126. float depth = SampleSceneDepth(screenUV);
  127. return depth;
  128. }
  129. void VFXApplyShadowBias(inout float4 posCS, inout float3 posWS, float3 normalWS)
  130. {
  131. posWS = ApplyShadowBias(posWS, normalWS, _LightDirection);
  132. posCS = VFXTransformPositionWorldToClip(posWS);
  133. }
  134. void VFXApplyShadowBias(inout float4 posCS, inout float3 posWS)
  135. {
  136. posWS = ApplyShadowBias(posWS, _LightDirection, _LightDirection);
  137. posCS = VFXTransformPositionWorldToClip(posWS);
  138. }
  139. float4 VFXApplyAO(float4 color, float4 posCS)
  140. {
  141. #if defined(_SCREEN_SPACE_OCCLUSION) && !defined(_SURFACE_TYPE_TRANSPARENT)
  142. float2 normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(posCS);
  143. AmbientOcclusionFactor aoFactor = GetScreenSpaceAmbientOcclusion(normalizedScreenSpaceUV);
  144. color.rgb *= aoFactor.directAmbientOcclusion;
  145. #endif
  146. return color;
  147. }
  148. float4 VFXTransformFinalColor(float4 color, float4 posCS)
  149. {
  150. if (IsOnlyAOLightingFeatureEnabled())
  151. {
  152. color.rgb = (float3)1.0f;
  153. color = VFXApplyAO(color, posCS);
  154. }
  155. return color;
  156. }
  157. float4 VFXApplyFog(float4 color,float4 posCS,float3 posWS)
  158. {
  159. float4 fog = (float4)0;
  160. fog.rgb = unity_FogColor.rgb;
  161. float fogFactor = ComputeFogFactor(posCS.z * posCS.w);
  162. fog.a = ComputeFogIntensity(fogFactor);
  163. #if VFX_BLENDMODE_ALPHA || IS_OPAQUE_PARTICLE
  164. color.rgb = lerp(fog.rgb, color.rgb, fog.a);
  165. #elif VFX_BLENDMODE_ADD
  166. color.rgb *= fog.a;
  167. #elif VFX_BLENDMODE_PREMULTIPLY
  168. color.rgb = lerp(fog.rgb * color.a, color.rgb, fog.a);
  169. #endif
  170. return color;
  171. }
  172. float3 VFXGetCameraWorldDirection()
  173. {
  174. return unity_CameraToWorld._m02_m12_m22;
  175. }
  176. #if defined(_GBUFFER_NORMALS_OCT)
  177. #define VFXComputePixelOutputToNormalBuffer(i,normalWS,uvData,outNormalBuffer) \
  178. { \
  179. float2 octNormalWS = PackNormalOctQuadEncode(normalWS); /*values between [-1, +1], must use fp32 on some platforms*/ \
  180. float2 remappedOctNormalWS = saturate(octNormalWS * 0.5 + 0.5); /*values between [ 0, 1]*/ \
  181. half3 packedNormalWS = PackFloat2To888(remappedOctNormalWS); /*values between [ 0, 1]*/ \
  182. outNormalBuffer = float4(packedNormalWS, 0.0); \
  183. }
  184. #else
  185. #define VFXComputePixelOutputToNormalBuffer(i,normalWS,uvData,outNormalBuffer) \
  186. { \
  187. outNormalBuffer = float4(normalWS, 0.0); \
  188. }
  189. #endif