UnlitMasterNode.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEditor.Graphing;
  5. using UnityEditor.ShaderGraph.Drawing;
  6. using UnityEditor.ShaderGraph.Drawing.Controls;
  7. using UnityEditor.ShaderGraph.Internal;
  8. using UnityEngine;
  9. using UnityEngine.UIElements;
  10. namespace UnityEditor.ShaderGraph
  11. {
  12. [Serializable]
  13. [Title("Master", "Unlit")]
  14. class UnlitMasterNode : MasterNode<IUnlitSubShader>, IMayRequirePosition, IMayRequireNormal, IMayRequireTangent
  15. {
  16. public const string ColorSlotName = "Color";
  17. public const string AlphaSlotName = "Alpha";
  18. public const string AlphaClipThresholdSlotName = "AlphaClipThreshold";
  19. public const string PositionName = "Vertex Position";
  20. public const string NormalName = "Vertex Normal";
  21. public const string TangentName = "Vertex Tangent";
  22. public const int ColorSlotId = 0;
  23. public const int AlphaSlotId = 7;
  24. public const int AlphaThresholdSlotId = 8;
  25. public const int PositionSlotId = 9;
  26. public const int VertNormalSlotId = 10;
  27. public const int VertTangentSlotId = 11;
  28. [SerializeField]
  29. SurfaceType m_SurfaceType;
  30. public SurfaceType surfaceType
  31. {
  32. get { return m_SurfaceType; }
  33. set
  34. {
  35. if (m_SurfaceType == value)
  36. return;
  37. m_SurfaceType = value;
  38. Dirty(ModificationScope.Graph);
  39. }
  40. }
  41. [SerializeField]
  42. AlphaMode m_AlphaMode;
  43. public AlphaMode alphaMode
  44. {
  45. get { return m_AlphaMode; }
  46. set
  47. {
  48. if (m_AlphaMode == value)
  49. return;
  50. m_AlphaMode = value;
  51. Dirty(ModificationScope.Graph);
  52. }
  53. }
  54. [SerializeField]
  55. bool m_TwoSided;
  56. public ToggleData twoSided
  57. {
  58. get { return new ToggleData(m_TwoSided); }
  59. set
  60. {
  61. if (m_TwoSided == value.isOn)
  62. return;
  63. m_TwoSided = value.isOn;
  64. Dirty(ModificationScope.Graph);
  65. }
  66. }
  67. [SerializeField]
  68. bool m_AddPrecomputedVelocity = false;
  69. public ToggleData addPrecomputedVelocity
  70. {
  71. get { return new ToggleData(m_AddPrecomputedVelocity); }
  72. set
  73. {
  74. if (m_AddPrecomputedVelocity == value.isOn)
  75. return;
  76. m_AddPrecomputedVelocity = value.isOn;
  77. Dirty(ModificationScope.Graph);
  78. }
  79. }
  80. public UnlitMasterNode()
  81. {
  82. UpdateNodeAfterDeserialization();
  83. }
  84. public sealed override void UpdateNodeAfterDeserialization()
  85. {
  86. base.UpdateNodeAfterDeserialization();
  87. name = "Unlit Master";
  88. AddSlot(new PositionMaterialSlot(PositionSlotId, PositionName, PositionName, CoordinateSpace.Object, ShaderStageCapability.Vertex));
  89. AddSlot(new NormalMaterialSlot(VertNormalSlotId, NormalName, NormalName, CoordinateSpace.Object, ShaderStageCapability.Vertex));
  90. AddSlot(new TangentMaterialSlot(VertTangentSlotId, TangentName, TangentName, CoordinateSpace.Object, ShaderStageCapability.Vertex));
  91. AddSlot(new ColorRGBMaterialSlot(ColorSlotId, ColorSlotName, ColorSlotName, SlotType.Input, Color.grey.gamma, ColorMode.Default, ShaderStageCapability.Fragment));
  92. AddSlot(new Vector1MaterialSlot(AlphaSlotId, AlphaSlotName, AlphaSlotName, SlotType.Input, 1, ShaderStageCapability.Fragment));
  93. AddSlot(new Vector1MaterialSlot(AlphaThresholdSlotId, AlphaClipThresholdSlotName, AlphaClipThresholdSlotName, SlotType.Input, 0.0f, ShaderStageCapability.Fragment));
  94. // clear out slot names that do not match the slots
  95. // we support
  96. RemoveSlotsNameNotMatching(
  97. new[]
  98. {
  99. PositionSlotId,
  100. VertNormalSlotId,
  101. VertTangentSlotId,
  102. ColorSlotId,
  103. AlphaSlotId,
  104. AlphaThresholdSlotId
  105. });
  106. }
  107. protected override VisualElement CreateCommonSettingsElement()
  108. {
  109. return new UnlitSettingsView(this);
  110. }
  111. public NeededCoordinateSpace RequiresNormal(ShaderStageCapability stageCapability)
  112. {
  113. List<MaterialSlot> slots = new List<MaterialSlot>();
  114. GetSlots(slots);
  115. List<MaterialSlot> validSlots = new List<MaterialSlot>();
  116. for (int i = 0; i < slots.Count; i++)
  117. {
  118. if (slots[i].stageCapability != ShaderStageCapability.All && slots[i].stageCapability != stageCapability)
  119. continue;
  120. validSlots.Add(slots[i]);
  121. }
  122. return validSlots.OfType<IMayRequireNormal>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresNormal(stageCapability));
  123. }
  124. public NeededCoordinateSpace RequiresPosition(ShaderStageCapability stageCapability)
  125. {
  126. List<MaterialSlot> slots = new List<MaterialSlot>();
  127. GetSlots(slots);
  128. List<MaterialSlot> validSlots = new List<MaterialSlot>();
  129. for (int i = 0; i < slots.Count; i++)
  130. {
  131. if (slots[i].stageCapability != ShaderStageCapability.All && slots[i].stageCapability != stageCapability)
  132. continue;
  133. validSlots.Add(slots[i]);
  134. }
  135. return validSlots.OfType<IMayRequirePosition>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresPosition(stageCapability));
  136. }
  137. public NeededCoordinateSpace RequiresTangent(ShaderStageCapability stageCapability)
  138. {
  139. List<MaterialSlot> slots = new List<MaterialSlot>();
  140. GetSlots(slots);
  141. List<MaterialSlot> validSlots = new List<MaterialSlot>();
  142. for (int i = 0; i < slots.Count; i++)
  143. {
  144. if (slots[i].stageCapability != ShaderStageCapability.All && slots[i].stageCapability != stageCapability)
  145. continue;
  146. validSlots.Add(slots[i]);
  147. }
  148. return validSlots.OfType<IMayRequireTangent>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresTangent(stageCapability));
  149. }
  150. }
  151. }