UniversalSubTarget.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using UnityEngine;
  2. using UnityEditor.ShaderGraph;
  3. using static Unity.Rendering.Universal.ShaderUtils;
  4. using UnityEditor.ShaderGraph.Internal;
  5. #if HAS_VFX_GRAPH
  6. using UnityEditor.VFX;
  7. #endif
  8. namespace UnityEditor.Rendering.Universal.ShaderGraph
  9. {
  10. abstract class UniversalSubTarget : SubTarget<UniversalTarget>, IHasMetadata
  11. #if HAS_VFX_GRAPH
  12. , IRequireVFXContext
  13. #endif
  14. {
  15. static readonly GUID kSourceCodeGuid = new GUID("92228d45c1ff66740bfa9e6d97f7e280"); // UniversalSubTarget.cs
  16. public override void Setup(ref TargetSetupContext context)
  17. {
  18. context.AddAssetDependency(kSourceCodeGuid, AssetCollection.Flags.SourceDependency);
  19. }
  20. protected abstract ShaderID shaderID { get; }
  21. #if HAS_VFX_GRAPH
  22. // VFX Properties
  23. VFXContext m_ContextVFX = null;
  24. VFXContextCompiledData m_ContextDataVFX;
  25. protected bool TargetsVFX() => m_ContextVFX != null;
  26. public void ConfigureContextData(VFXContext context, VFXContextCompiledData data)
  27. {
  28. m_ContextVFX = context;
  29. m_ContextDataVFX = data;
  30. }
  31. #endif
  32. protected SubShaderDescriptor PostProcessSubShader(SubShaderDescriptor subShaderDescriptor)
  33. {
  34. #if HAS_VFX_GRAPH
  35. if (TargetsVFX())
  36. return VFXSubTarget.PostProcessSubShader(subShaderDescriptor, m_ContextVFX, m_ContextDataVFX);
  37. #endif
  38. return subShaderDescriptor;
  39. }
  40. public override void GetFields(ref TargetFieldContext context)
  41. {
  42. #if HAS_VFX_GRAPH
  43. if (TargetsVFX())
  44. VFXSubTarget.GetFields(ref context, m_ContextVFX);
  45. #endif
  46. }
  47. public virtual string identifier => GetType().Name;
  48. public virtual ScriptableObject GetMetadataObject(GraphDataReadOnly graphData)
  49. {
  50. var urpMetadata = ScriptableObject.CreateInstance<UniversalMetadata>();
  51. urpMetadata.shaderID = shaderID;
  52. urpMetadata.allowMaterialOverride = target.allowMaterialOverride;
  53. urpMetadata.alphaMode = target.alphaMode;
  54. urpMetadata.castShadows = target.castShadows;
  55. return urpMetadata;
  56. }
  57. private int lastMaterialNeedsUpdateHash = 0;
  58. protected virtual int ComputeMaterialNeedsUpdateHash() => 0;
  59. public override object saveContext
  60. {
  61. get
  62. {
  63. int hash = ComputeMaterialNeedsUpdateHash();
  64. bool needsUpdate = hash != lastMaterialNeedsUpdateHash;
  65. if (needsUpdate)
  66. lastMaterialNeedsUpdateHash = hash;
  67. return new UniversalShaderGraphSaveContext { updateMaterials = needsUpdate };
  68. }
  69. }
  70. }
  71. internal static class SubShaderUtils
  72. {
  73. internal static void AddFloatProperty(this PropertyCollector collector, string referenceName, float defaultValue, HLSLDeclaration declarationType = HLSLDeclaration.DoNotDeclare)
  74. {
  75. collector.AddShaderProperty(new Vector1ShaderProperty
  76. {
  77. floatType = FloatType.Default,
  78. hidden = true,
  79. overrideHLSLDeclaration = true,
  80. hlslDeclarationOverride = declarationType,
  81. value = defaultValue,
  82. displayName = referenceName,
  83. overrideReferenceName = referenceName,
  84. });
  85. }
  86. internal static void AddToggleProperty(this PropertyCollector collector, string referenceName, bool defaultValue, HLSLDeclaration declarationType = HLSLDeclaration.DoNotDeclare)
  87. {
  88. collector.AddShaderProperty(new BooleanShaderProperty
  89. {
  90. value = defaultValue,
  91. hidden = true,
  92. overrideHLSLDeclaration = true,
  93. hlslDeclarationOverride = declarationType,
  94. displayName = referenceName,
  95. overrideReferenceName = referenceName,
  96. });
  97. }
  98. // Overloads to do inline PassDescriptor modifications
  99. // NOTE: param order should match PassDescriptor field order for consistency
  100. #region PassVariant
  101. internal static PassDescriptor PassVariant(in PassDescriptor source, PragmaCollection pragmas)
  102. {
  103. var result = source;
  104. result.pragmas = pragmas;
  105. return result;
  106. }
  107. #endregion
  108. }
  109. }