Bez popisu
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.

UniversalSubTarget.cs 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. VFXTaskCompiledData m_TaskDataVFX;
  25. protected bool TargetsVFX() => m_ContextVFX != null;
  26. public void ConfigureContextData(VFXContext context, VFXTaskCompiledData data)
  27. {
  28. m_ContextVFX = context;
  29. m_TaskDataVFX = 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_TaskDataVFX);
  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.alphaMode = target.alphaMode;
  53. if (shaderID != ShaderID.SG_SpriteLit && shaderID != ShaderID.SG_SpriteUnlit)
  54. {
  55. urpMetadata.allowMaterialOverride = target.allowMaterialOverride;
  56. urpMetadata.surfaceType = target.surfaceType;
  57. urpMetadata.castShadows = target.castShadows;
  58. urpMetadata.hasVertexModificationInMotionVector = target.additionalMotionVectorMode != AdditionalMotionVectorMode.None || graphData.AnyVertexAnimationActive();
  59. }
  60. else
  61. {
  62. //Ignore unsupported settings in SpriteUnlit/SpriteLit
  63. urpMetadata.allowMaterialOverride = false;
  64. urpMetadata.surfaceType = SurfaceType.Transparent;
  65. urpMetadata.castShadows = false;
  66. urpMetadata.hasVertexModificationInMotionVector = false;
  67. }
  68. urpMetadata.isVFXCompatible = graphData.IsVFXCompatible();
  69. return urpMetadata;
  70. }
  71. private int lastMaterialNeedsUpdateHash = 0;
  72. protected virtual int ComputeMaterialNeedsUpdateHash() => 0;
  73. public override object saveContext
  74. {
  75. get
  76. {
  77. int hash = ComputeMaterialNeedsUpdateHash();
  78. bool needsUpdate = hash != lastMaterialNeedsUpdateHash;
  79. if (needsUpdate)
  80. lastMaterialNeedsUpdateHash = hash;
  81. return new UniversalShaderGraphSaveContext { updateMaterials = needsUpdate };
  82. }
  83. }
  84. }
  85. internal static class SubShaderUtils
  86. {
  87. internal static void AddFloatProperty(this PropertyCollector collector, string referenceName, float defaultValue, HLSLDeclaration declarationType = HLSLDeclaration.DoNotDeclare)
  88. {
  89. collector.AddShaderProperty(new Vector1ShaderProperty
  90. {
  91. floatType = FloatType.Default,
  92. hidden = true,
  93. overrideHLSLDeclaration = true,
  94. hlslDeclarationOverride = declarationType,
  95. value = defaultValue,
  96. displayName = referenceName,
  97. overrideReferenceName = referenceName,
  98. });
  99. }
  100. internal static void AddToggleProperty(this PropertyCollector collector, string referenceName, bool defaultValue, HLSLDeclaration declarationType = HLSLDeclaration.DoNotDeclare)
  101. {
  102. collector.AddShaderProperty(new BooleanShaderProperty
  103. {
  104. value = defaultValue,
  105. hidden = true,
  106. overrideHLSLDeclaration = true,
  107. hlslDeclarationOverride = declarationType,
  108. displayName = referenceName,
  109. overrideReferenceName = referenceName,
  110. });
  111. }
  112. // Overloads to do inline PassDescriptor modifications
  113. // NOTE: param order should match PassDescriptor field order for consistency
  114. #region PassVariant
  115. internal static PassDescriptor PassVariant(in PassDescriptor source, PragmaCollection pragmas)
  116. {
  117. var result = source;
  118. result.pragmas = pragmas;
  119. return result;
  120. }
  121. #endregion
  122. }
  123. }