暫無描述
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.

VFXDecal.template 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include "Packages/com.unity.render-pipelines.universal/Runtime/VFXGraph/Shaders/VFXLit.hlsl"
  2. #if !defined(SHADERPASS)
  3. #error SHADERPASS_is_not_define
  4. #endif
  5. #ifdef _DECAL_LAYERS
  6. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareRenderingLayerTexture.hlsl"
  7. #endif
  8. #if defined(DECAL_LOAD_NORMAL)
  9. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareNormalsTexture.hlsl"
  10. #endif
  11. #if defined(DECAL_PROJECTOR) || defined(DECAL_RECONSTRUCT_NORMAL)
  12. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
  13. #endif
  14. void GetSurfaceDataFromSurfaceDecalData(DecalSurfaceData decalSurfaceData, inout SurfaceData surfaceData)
  15. {
  16. surfaceData.albedo = decalSurfaceData.baseColor.rgb;
  17. surfaceData.metallic = saturate(decalSurfaceData.metallic);
  18. surfaceData.specular = 0;
  19. surfaceData.smoothness = saturate(decalSurfaceData.smoothness);
  20. surfaceData.occlusion = decalSurfaceData.occlusion;
  21. surfaceData.emission = decalSurfaceData.emissive;
  22. surfaceData.alpha = saturate(decalSurfaceData.baseColor.w);
  23. surfaceData.clearCoatMask = 0;
  24. surfaceData.clearCoatSmoothness = 1;
  25. }
  26. void VFXGetSurfaceDecalData(out DecalSurfaceData surfaceData, out PositionInputs posInputs, out half3 receiverNormalWS, VFX_VARYING_PS_INPUTS i)
  27. {
  28. ZERO_INITIALIZE(DecalSurfaceData, surfaceData);
  29. VFXTransformPSInputs(i);
  30. float2 positionCS = i.pos.xy;
  31. // Only screen space needs flip logic, other passes do not setup needed properties so we skip here
  32. #if SHADERPASS == SHADERPASS_DECAL_SCREEN_SPACE_PROJECTOR
  33. TransformScreenUV(positionCS, _ScreenSize.y);
  34. #endif
  35. //Check Rendering layer
  36. #ifdef _DECAL_LAYERS
  37. #ifdef _RENDER_PASS_ENABLED
  38. uint surfaceRenderingLayer = DecodeMeshRenderingLayer(LOAD_FRAMEBUFFER_X_INPUT(GBUFFER4, positionCS.xy).r);
  39. #else
  40. uint surfaceRenderingLayer = LoadSceneRenderingLayer(positionCS.xy);
  41. #endif
  42. ${VFXLoadParameter:{decalLayerMask}}
  43. clip((surfaceRenderingLayer & decalLayerMask) - 0.1);
  44. #endif
  45. #if _RENDER_PASS_ENABLED && !defined(DECAL_RECONSTRUCT_NORMAL)
  46. float depth = LOAD_FRAMEBUFFER_X_INPUT(GBUFFER3, positionCS.xy).x;
  47. #else
  48. float depth = LoadSceneDepth(positionCS.xy);
  49. #endif
  50. #if !UNITY_REVERSED_Z
  51. depth = lerp(UNITY_NEAR_CLIP_VALUE, 1.0f, depth);
  52. #endif
  53. float2 positionSS = i.pos.xy * _ScreenSize.zw;
  54. float3 positionWS = ComputeWorldSpacePosition(positionSS, depth, UNITY_MATRIX_I_VP);
  55. posInputs = GetPositionInput(positionSS, _ScreenSize.zw, positionWS);
  56. float4x4 worldToElement;
  57. worldToElement[0] = i.worldToDecal[0];
  58. worldToElement[1] = i.worldToDecal[1];
  59. worldToElement[2] = -i.worldToDecal[2];
  60. worldToElement[3] = float4(0,0,0,1);
  61. float3 positionDS = mul(worldToElement, float4(positionWS,1.0f)).xyz;
  62. clip(0.5f - abs(positionDS));
  63. float3x3 normalToWorld = transpose(float3x3(
  64. VFXSafeNormalize(worldToElement[0].xyz),
  65. VFXSafeNormalize(worldToElement[1].xyz),
  66. VFXSafeNormalize(worldToElement[2].xyz)));
  67. float2 uv = positionDS.xy + float2(0.5, 0.5);
  68. VFXUVData uvData = GetUVData(i,uv);
  69. float angleFadeFactor = 1.0f;
  70. receiverNormalWS = half3(0,1,0);
  71. #if defined(DECAL_RECONSTRUCT_NORMAL)
  72. #if defined(_DECAL_NORMAL_BLEND_HIGH)
  73. receiverNormalWS = half3(ReconstructNormalTap9(i.pos.xy));
  74. #elif defined(_DECAL_NORMAL_BLEND_MEDIUM)
  75. receiverNormalWS = half3(ReconstructNormalTap5(i.pos.xy));
  76. #else
  77. receiverNormalWS = half3(ReconstructNormalDerivative(i.pos.xy));
  78. #endif
  79. #elif defined(DECAL_LOAD_NORMAL)
  80. receiverNormalWS = half3(LoadSceneNormals(i.pos.xy));
  81. #endif
  82. #ifdef DECAL_ANGLE_FADE
  83. if (i.VFX_VARYING_ANGLEFADE.y < 0.0f) // if angle fade is enabled
  84. {
  85. float3 decalNormal = float3(normalToWorld[0].z, normalToWorld[1].z, normalToWorld[2].z);
  86. float dotAngle = dot(receiverNormalWS, decalNormal);
  87. float2 angleFade = i.VFX_VARYING_ANGLEFADE;
  88. angleFadeFactor = saturate(angleFade.x + angleFade.y * (dotAngle * (dotAngle - 2.0)));
  89. }
  90. #endif
  91. float fadeFactor = i.VFX_VARYING_FADEFACTOR;
  92. fadeFactor *= angleFadeFactor;
  93. //Compute color even for emissive, to have the correct opacity
  94. float4 color = float4(1,1,1,1);
  95. #if URP_USE_BASE_COLOR
  96. color *= VFXGetParticleColor(i);
  97. #elif URP_USE_ADDITIONAL_BASE_COLOR
  98. #if defined(VFX_VARYING_COLOR)
  99. color.xyz *= i.VFX_VARYING_COLOR;
  100. #endif
  101. #if defined(VFX_VARYING_ALPHA)
  102. color.a *= i.VFX_VARYING_ALPHA;
  103. #endif
  104. #endif
  105. #if URP_USE_BASE_COLOR_MAP
  106. float4 colorMap = SampleTexture(VFX_SAMPLER(baseColorMap),uvData);
  107. #if URP_USE_BASE_COLOR_MAP_COLOR
  108. color.xyz *= colorMap.xyz;
  109. #endif
  110. #if URP_USE_BASE_COLOR_MAP_ALPHA
  111. color.a *= colorMap.a;
  112. #endif
  113. #endif
  114. color.a *= fadeFactor;
  115. VFXClipFragmentColor(color.a,i);
  116. #if (SHADERPASS == SHADERPASS_DBUFFER_PROJECTOR) || (SHADERPASS == SHADERPASS_DECAL_SCREEN_SPACE_PROJECTOR) || (SHADERPASS == SHADERPASS_DECAL_GBUFFER_PROJECTOR)
  117. surfaceData.baseColor.rgb = saturate(color.rgb);
  118. surfaceData.baseColor.a = color.a;
  119. float albedoMapBlend = surfaceData.baseColor.a;
  120. float maskMapBlend = fadeFactor;
  121. surfaceData.metallic = 0.0f;
  122. surfaceData.occlusion = 1.0f;
  123. #ifdef VFX_VARYING_METALLIC
  124. surfaceData.metallic = i.VFX_VARYING_METALLIC;
  125. #endif
  126. #ifdef VFX_VARYING_AMBIENT_OCCLUSION
  127. surfaceData.occlusion *= i.VFX_VARYING_AMBIENT_OCCLUSION;
  128. #endif
  129. surfaceData.smoothness = 0.5f;
  130. #ifdef VFX_VARYING_SMOOTHNESS
  131. surfaceData.smoothness = i.VFX_VARYING_SMOOTHNESS;
  132. #endif
  133. float4 metallicMapSample = (float4)1.0f;
  134. float4 specularMapSample = (float4)1.0f;
  135. #if URP_USE_METALLIC_MAP
  136. metallicMapSample = SampleTexture(VFX_SAMPLER(metallicMap), uvData);
  137. surfaceData.metallic *= metallicMapSample.r;
  138. maskMapBlend *= metallicMapSample.b;
  139. #endif
  140. #if URP_USE_OCCLUSION_MAP
  141. float4 mask = SampleTexture(VFX_SAMPLER(occlusionMap),uvData);
  142. surfaceData.occlusion *= mask.g;
  143. #endif
  144. #if URP_USE_SMOOTHNESS_IN_ALBEDO
  145. surfaceData.smoothness *= albedoMapBlend;
  146. #elif URP_USE_SMOOTHNESS_IN_METALLIC
  147. surfaceData.smoothness *= metallicMapSample.a;
  148. #elif URP_USE_SMOOTHNESS_IN_SPECULAR
  149. surfaceData.smoothness *= albedoMapBlend;//TODO: Not implemented yet;
  150. #endif
  151. #if VFX_MAOS_BLEND_BASE_COLOR_ALPHA
  152. surfaceData.MAOSAlpha = albedoMapBlend;
  153. #elif VFX_MAOS_BLEND_METALLIC_BLUE
  154. surfaceData.MAOSAlpha = maskMapBlend;
  155. #endif
  156. float normalAlpha = 1.0f;
  157. //No Decal Surface Gradient in URP implementation of Decals
  158. #if USE_NORMAL_MAP
  159. float3 normalTS = SampleNormalMap(VFX_SAMPLER(normalMap),uvData);
  160. #else //USE_NORMAL_MAP
  161. float3 normalTS = float3(0.0f,0.0f,1.0f);
  162. #endif //USE_NORMAL_MAP
  163. float3 normalWS = mul(normalToWorld, normalTS);
  164. normalWS = normalize(normalWS);
  165. surfaceData.normalWS.xyz = normalWS;
  166. #ifdef VFX_VARYING_NORMALALPHA
  167. surfaceData.normalWS.w = i.VFX_VARYING_NORMALALPHA;
  168. #else
  169. surfaceData.normalWS.w = 0.0f;
  170. #endif
  171. #if VFX_NORMAL_BLEND_BASE_COLOR_ALPHA
  172. surfaceData.normalWS.w *= albedoMapBlend;
  173. #elif VFX_NORMAL_BLEND_METALLIC_BLUE
  174. surfaceData.normalWS.w *= maskMapBlend;
  175. #endif
  176. #endif
  177. #if (SHADERPASS == SHADERPASS_FORWARD_EMISSIVE_PROJECTOR) || (SHADERPASS == SHADERPASS_DECAL_SCREEN_SPACE_PROJECTOR) || (SHADERPASS == SHADERPASS_DECAL_GBUFFER_PROJECTOR)
  178. surfaceData.baseColor.a = color.a;
  179. surfaceData.emissive = float3(1,1,1) * fadeFactor;
  180. #if defined(VFX_VARYING_EMISSIVE) && (URP_USE_EMISSIVE_COLOR || URP_USE_ADDITIONAL_EMISSIVE_COLOR)
  181. surfaceData.emissive *= i.VFX_VARYING_EMISSIVE;
  182. #else
  183. surfaceData.emissive = float3(0,0,0);
  184. #endif
  185. #ifdef URP_USE_EMISSIVE_MAP
  186. float emissiveScale = 1.0f;
  187. #ifdef VFX_VARYING_EMISSIVESCALE
  188. emissiveScale = i.VFX_VARYING_EMISSIVESCALE;
  189. #endif
  190. surfaceData.emissive *= SampleTexture(VFX_SAMPLER(emissiveMap), uvData).rgb * emissiveScale;
  191. #endif
  192. #endif
  193. }
  194. void PrepareSurfaceAndInputData(VFX_VARYING_PS_INPUTS i, inout SurfaceData surfaceData, inout DecalSurfaceData decalSurfaceData, inout InputData inputData)
  195. {
  196. PositionInputs posInput;
  197. ZERO_INITIALIZE(PositionInputs, posInput);
  198. half3 receiverNormalWS;
  199. VFXGetSurfaceDecalData(decalSurfaceData, posInput, receiverNormalWS, i);
  200. GetSurfaceDataFromSurfaceDecalData(decalSurfaceData, surfaceData);
  201. #if (SHADERPASS == SHADERPASS_DECAL_SCREEN_SPACE_PROJECTOR)
  202. decalSurfaceData.normalWS.xyz = normalize(lerp(receiverNormalWS.xyz, decalSurfaceData.normalWS.xyz, decalSurfaceData.normalWS.w));
  203. #endif
  204. inputData = VFXGetInputData(i, posInput, decalSurfaceData.normalWS.xyz, true);
  205. }