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

VFXDecalURPOutput.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #if HAS_VFX_GRAPH
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace UnityEditor.VFX.URP
  6. {
  7. [VFXHelpURL("Context-OutputParticleURPLitDecal")]
  8. [VFXInfo(name = "Output Particle|URP Lit|Decal", category = "#4Output Advanced")]
  9. internal class VFXDecalURPOutput : VFXAbstractParticleURPLitOutput
  10. {
  11. public override string name => "Output Particle".AppendLabel("URP Lit").AppendLabel("Decal");
  12. public override string codeGeneratorTemplate => RenderPipeTemplate("VFXParticleURPDecal");
  13. public override VFXTaskType taskType => VFXTaskType.ParticleHexahedronOutput;
  14. public override bool supportsUV => GetOrRefreshShaderGraphObject() == null;
  15. public override void OnEnable()
  16. {
  17. base.OnEnable();
  18. blendMode = BlendMode.Opaque;
  19. workflowMode = WorkflowMode.Metallic;
  20. }
  21. public enum BlendSource
  22. {
  23. BaseColorMapAlpha,
  24. MetallicMapBlue,
  25. }
  26. [VFXSetting(VFXSettingAttribute.VisibleFlags.InInspector), Header("Opacity Channels"), SerializeField,
  27. Tooltip("Specifies the source this Material uses as opacity for its Normal Map.")]
  28. BlendSource normalOpacityChannel = BlendSource.BaseColorMapAlpha;
  29. [VFXSetting(VFXSettingAttribute.VisibleFlags.InInspector), SerializeField, InspectorName("MAOS Opacity Channel"),
  30. Tooltip("Specifies the source this Material uses as opacity for its Mask Map.")]
  31. BlendSource MAOSOpacityChannel = BlendSource.BaseColorMapAlpha;
  32. [VFXSetting(VFXSettingAttribute.VisibleFlags.InInspector), SerializeField, Tooltip("Enables fading the decal based on the angle between the decal backward direction and the receiving surface normal.")]
  33. internal bool angleFade = true;
  34. [VFXSetting(VFXSettingAttribute.VisibleFlags.InInspector), Header("Surface options"), SerializeField,
  35. Tooltip("When enabled, modifies the base color of the surface it projects onto.")]
  36. private bool affectBaseColor = true;
  37. [VFXSetting(VFXSettingAttribute.VisibleFlags.InInspector), SerializeField,
  38. Tooltip("When enabled, modifies the metallic, ambient occlusion and smoothness of the surface it projects onto. The ambient occlusion slider is available when using an Occlusion Map.")]
  39. private bool affectMAOS = true;
  40. [VFXSetting(VFXSettingAttribute.VisibleFlags.InInspector), SerializeField, Tooltip("Specify the layer mask for the decals. Unity renders decals on all meshes where at least one Rendering Layer value matches.")]
  41. private uint decalLayer = ~0u;
  42. protected override bool useSmoothness => affectMAOS;
  43. protected override bool useMetallic => affectMAOS;
  44. protected override bool useNormalScale => false;
  45. public override bool HasSorting() => (sort == SortActivationMode.On) || (sort == SortActivationMode.Auto);
  46. public class FadeFactorProperty
  47. {
  48. [Range(0, 1), Tooltip("Controls the transparency of the decal.")]
  49. public float fadeFactor = 1.0f;
  50. }
  51. public class AngleFadeProperty
  52. {
  53. [Tooltip("Use the min-max slider to control the fade out range of the decal based on the angle between the Decal backward direction and the vertex normal of the receiving surface."), MinMax(0.0f, 180.0f)]
  54. public Vector2 angleFade = new Vector2(0.0f, 180.0f);
  55. }
  56. public class NormalAlphaProperty
  57. {
  58. [Tooltip("Controls the blending factor of the normal map."), Range(0, 1)]
  59. public float normalAlpha = 1.0f;
  60. }
  61. public class AmbientOcclusionProperty
  62. {
  63. [Tooltip("Controls the scale factor for the particle’s ambient occlusion."), Range(0, 1)]
  64. public float ambientOcclusion = 1.0f;
  65. }
  66. protected override IEnumerable<VFXPropertyWithValue> inputProperties
  67. {
  68. get
  69. {
  70. var properties = Enumerable.Empty<VFXPropertyWithValue>();
  71. properties = properties.Concat(PropertiesFromType(nameof(FadeFactorProperty)));
  72. if(angleFade)
  73. properties = properties.Concat(PropertiesFromType(nameof(AngleFadeProperty)));
  74. foreach (var prop in base.inputProperties)
  75. {
  76. //Inserts slots in the correct order
  77. properties = properties.Append(prop);
  78. if(prop.property.name == "normalMap")
  79. properties = properties.Concat(PropertiesFromType(nameof(NormalAlphaProperty)));
  80. if(affectMAOS && useOcclusionMap && prop.property.name == "occlusionMap")
  81. properties = properties.Concat(PropertiesFromType(nameof(AmbientOcclusionProperty)));
  82. }
  83. return properties;
  84. }
  85. }
  86. protected override IEnumerable<VFXNamedExpression> CollectGPUExpressions(
  87. IEnumerable<VFXNamedExpression> slotExpressions)
  88. {
  89. foreach (var exp in base.CollectGPUExpressions(slotExpressions))
  90. {
  91. yield return exp;
  92. }
  93. if (GetOrRefreshShaderGraphObject() == null)
  94. {
  95. yield return slotExpressions.First(o => o.name == nameof(FadeFactorProperty.fadeFactor));
  96. if (angleFade)
  97. {
  98. var angleFadeExp = slotExpressions.First(o => o.name == nameof(AngleFadeProperty.angleFade));
  99. yield return new VFXNamedExpression(AngleFadeSimplification(angleFadeExp.exp),
  100. nameof(AngleFadeProperty.angleFade));
  101. }
  102. if (affectMAOS && useOcclusionMap)
  103. yield return slotExpressions.First(o => o.name == nameof(AmbientOcclusionProperty.ambientOcclusion));
  104. if (useNormalMap)
  105. yield return slotExpressions.First(o => o.name == nameof(NormalAlphaProperty.normalAlpha));
  106. yield return new VFXNamedExpression(VFXValue.Constant(decalLayer), "decalLayerMask");
  107. }
  108. }
  109. //URP uses the old angle fade simplification
  110. VFXExpression AngleFadeSimplification(VFXExpression angleFadeExp)
  111. {
  112. angleFadeExp = angleFadeExp / VFXValue.Constant(new Vector2(180.0f, 180.0f));
  113. var angleStart = new VFXExpressionExtractComponent(angleFadeExp, 0);
  114. var angleEnd = new VFXExpressionExtractComponent(angleFadeExp, 1);
  115. var range = new VFXExpressionMax(VFXValue.Constant(0.0001f), angleEnd - angleStart);
  116. var simplifiedAngleFade = new VFXExpressionCombine(
  117. VFXValue.Constant(1.0f) - (VFXValue.Constant(0.25f) - angleStart) / range,
  118. VFXValue.Constant(-0.25f) / range);
  119. return simplifiedAngleFade;
  120. }
  121. public override IEnumerable<string> additionalDefines
  122. {
  123. get
  124. {
  125. foreach (var def in base.additionalDefines)
  126. yield return def;
  127. if (GetOrRefreshShaderGraphObject() == null)
  128. {
  129. if (angleFade)
  130. yield return "DECAL_ANGLE_FADE";
  131. if (affectBaseColor)
  132. yield return "AFFECT_BASE_COLOR";
  133. if (affectMAOS)
  134. {
  135. yield return "AFFECT_METALLIC";
  136. yield return "AFFECT_SMOOTHNESS";
  137. if (MAOSOpacityChannel == BlendSource.BaseColorMapAlpha)
  138. yield return "VFX_MAOS_BLEND_BASE_COLOR_ALPHA";
  139. else
  140. yield return "VFX_MAOS_BLEND_METALLIC_BLUE";
  141. }
  142. if (affectMAOS && useOcclusionMap)
  143. yield return "AFFECT_AMBIENT_OCCLUSION";
  144. if (useEmissive /*TODO: add useEmissiveColor like in HDRP */ || useEmissiveMap)
  145. {
  146. yield return "AFFECT_EMISSIVE";
  147. }
  148. if (useNormalMap)
  149. {
  150. yield return "AFFECT_NORMAL";
  151. if (normalOpacityChannel == BlendSource.BaseColorMapAlpha)
  152. yield return "VFX_NORMAL_BLEND_BASE_COLOR_ALPHA";
  153. else
  154. yield return "VFX_NORMAL_BLEND_METALLIC_BLUE";
  155. }
  156. }
  157. }
  158. }
  159. protected override IEnumerable<string> filteredOutSettings
  160. {
  161. get
  162. {
  163. foreach (var setting in base.filteredOutSettings)
  164. yield return setting;
  165. yield return nameof(workflowMode);
  166. yield return nameof(zTestMode);
  167. yield return nameof(zWriteMode);
  168. yield return nameof(doubleSided);
  169. yield return nameof(castShadows);
  170. yield return nameof(blendMode);
  171. yield return nameof(shaderGraph);
  172. if (!affectBaseColor)
  173. yield return nameof(useBaseColorMap);
  174. if (!affectMAOS)
  175. yield return nameof(useMetallicMap);
  176. if (!useEmissive)
  177. yield return nameof(useEmissiveMap);
  178. if (!affectMAOS)
  179. {
  180. yield return nameof(useOcclusionMap);
  181. yield return nameof(MAOSOpacityChannel);
  182. }
  183. }
  184. }
  185. protected override IEnumerable<string> untransferableSettings
  186. {
  187. get
  188. {
  189. foreach (var setting in base.untransferableSettings)
  190. {
  191. yield return setting;
  192. }
  193. yield return nameof(blendMode);
  194. }
  195. }
  196. protected VFXShaderWriter GetDBufferMaskColor(int maskIndex)
  197. {
  198. var rs = new VFXShaderWriter();
  199. switch (maskIndex)
  200. {
  201. case 0:
  202. rs.Write(affectBaseColor ? "RBGA" : "0");
  203. break;
  204. case 1:
  205. rs.Write(useNormalMap ? "RGBA" : "0");
  206. break;
  207. case 2:
  208. rs.Write(affectMAOS ? "RGBA" : "0");
  209. break;
  210. }
  211. return rs;
  212. }
  213. protected VFXShaderWriter GetGBufferDecalMaskColor(int maskIndex)
  214. {
  215. var rs = new VFXShaderWriter();
  216. switch (maskIndex)
  217. {
  218. case 0:
  219. rs.Write(affectBaseColor ? "RBG" : "0");
  220. break;
  221. case 1:
  222. rs.Write("0");
  223. break;
  224. case 2:
  225. rs.Write(useNormalMap ? "RGB" : "0");
  226. break;
  227. case 3:
  228. {
  229. rs.Write("RGB");
  230. break;
  231. }
  232. }
  233. return rs;
  234. }
  235. public override IEnumerable<KeyValuePair<string, VFXShaderWriter>> additionalReplacements
  236. {
  237. get
  238. {
  239. foreach (var rep in base.additionalReplacements)
  240. yield return rep;
  241. for (int i = 0; i < 3; i++)
  242. {
  243. yield return new KeyValuePair<string, VFXShaderWriter>("${VFXDBufferColorMask" + i + "}",
  244. GetDBufferMaskColor(i));
  245. }
  246. for (int i = 0; i < 4; i++)
  247. {
  248. yield return new KeyValuePair<string, VFXShaderWriter>("${VFXGBufferDecalColorMask" + i + "}",
  249. GetGBufferDecalMaskColor(i));
  250. }
  251. }
  252. }
  253. public override void OnSettingModified(VFXSetting setting)
  254. {
  255. if (setting.name == nameof(affectMAOS))
  256. {
  257. if (!affectMAOS)
  258. {
  259. useOcclusionMap = false;
  260. useMetallicMap = false;
  261. }
  262. }
  263. if (setting.name == nameof(affectBaseColor))
  264. {
  265. if (!affectBaseColor)
  266. {
  267. useBaseColorMap = BaseColorMapMode.Alpha;
  268. }
  269. else
  270. {
  271. useBaseColorMap = BaseColorMapMode.ColorAndAlpha;
  272. }
  273. }
  274. }
  275. }
  276. }
  277. #endif