SpriteLitMasterNode.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEditor.Graphing;
  5. using UnityEngine;
  6. using UnityEditor.ShaderGraph;
  7. using UnityEditor.ShaderGraph.Internal;
  8. namespace UnityEditor.Experimental.Rendering.Universal
  9. {
  10. [Serializable]
  11. [Title("Master", "Sprite Lit (Experimental)")]
  12. [FormerName("UnityEditor.Experimental.Rendering.LWRP.SpriteLitMasterNode")]
  13. class SpriteLitMasterNode : MasterNode<ISpriteLitSubShader>, IMayRequirePosition, IMayRequireNormal, IMayRequireTangent
  14. {
  15. public const string PositionName = "Vertex Position";
  16. public const string NormalName = "Vertex Normal";
  17. public const string TangentName = "Vertex Tangent";
  18. public const string ColorSlotName = "Color";
  19. public const string MaskSlotName = "Mask";
  20. public const string NormalSlotName = "Normal";
  21. public const int PositionSlotId = 9;
  22. public const int ColorSlotId = 0;
  23. public const int MaskSlotId = 1;
  24. public const int NormalSlotId = 2;
  25. public const int VertNormalSlotId = 10;
  26. public const int VertTangentSlotId = 11;
  27. public SpriteLitMasterNode()
  28. {
  29. UpdateNodeAfterDeserialization();
  30. }
  31. public sealed override void UpdateNodeAfterDeserialization()
  32. {
  33. base.UpdateNodeAfterDeserialization();
  34. name = "Sprite Lit Master";
  35. AddSlot(new PositionMaterialSlot(PositionSlotId, PositionName, PositionName, CoordinateSpace.Object, ShaderStageCapability.Vertex));
  36. AddSlot(new NormalMaterialSlot(VertNormalSlotId, NormalName, NormalName, CoordinateSpace.Object, ShaderStageCapability.Vertex));
  37. AddSlot(new TangentMaterialSlot(VertTangentSlotId, TangentName, TangentName, CoordinateSpace.Object, ShaderStageCapability.Vertex));
  38. AddSlot(new ColorRGBAMaterialSlot(ColorSlotId, ColorSlotName, ColorSlotName, SlotType.Input, Color.white, ShaderStageCapability.Fragment));
  39. AddSlot(new ColorRGBAMaterialSlot(MaskSlotId, MaskSlotName, MaskSlotName, SlotType.Input, Color.white, ShaderStageCapability.Fragment));
  40. AddSlot(new Vector3MaterialSlot(NormalSlotId, NormalSlotName, NormalSlotName, SlotType.Input, new Vector3(0.0f, 0.0f, 1.0f), ShaderStageCapability.Fragment));
  41. RemoveSlotsNameNotMatching(
  42. new[]
  43. {
  44. PositionSlotId,
  45. VertNormalSlotId,
  46. VertTangentSlotId,
  47. ColorSlotId,
  48. MaskSlotId,
  49. NormalSlotId,
  50. });
  51. }
  52. public NeededCoordinateSpace RequiresNormal(ShaderStageCapability stageCapability)
  53. {
  54. List<MaterialSlot> slots = new List<MaterialSlot>();
  55. GetSlots(slots);
  56. List<MaterialSlot> validSlots = new List<MaterialSlot>();
  57. for (int i = 0; i < slots.Count; i++)
  58. {
  59. if (slots[i].stageCapability != ShaderStageCapability.All && slots[i].stageCapability != stageCapability)
  60. continue;
  61. validSlots.Add(slots[i]);
  62. }
  63. return validSlots.OfType<IMayRequireNormal>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresNormal(stageCapability));
  64. }
  65. public NeededCoordinateSpace RequiresPosition(ShaderStageCapability stageCapability)
  66. {
  67. List<MaterialSlot> slots = new List<MaterialSlot>();
  68. GetSlots(slots);
  69. List<MaterialSlot> validSlots = new List<MaterialSlot>();
  70. for (int i = 0; i < slots.Count; i++)
  71. {
  72. if (slots[i].stageCapability != ShaderStageCapability.All && slots[i].stageCapability != stageCapability)
  73. continue;
  74. validSlots.Add(slots[i]);
  75. }
  76. return validSlots.OfType<IMayRequirePosition>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresPosition(stageCapability));
  77. }
  78. public NeededCoordinateSpace RequiresTangent(ShaderStageCapability stageCapability)
  79. {
  80. List<MaterialSlot> slots = new List<MaterialSlot>();
  81. GetSlots(slots);
  82. List<MaterialSlot> validSlots = new List<MaterialSlot>();
  83. for (int i = 0; i < slots.Count; i++)
  84. {
  85. if (slots[i].stageCapability != ShaderStageCapability.All && slots[i].stageCapability != stageCapability)
  86. continue;
  87. validSlots.Add(slots[i]);
  88. }
  89. return validSlots.OfType<IMayRequireTangent>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresTangent(stageCapability));
  90. }
  91. }
  92. }