SubGraphOutputNode.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEditor.ShaderGraph.Drawing;
  6. using UnityEngine;
  7. using UnityEditor.Graphing;
  8. using UnityEditor.Rendering;
  9. using UnityEngine.Rendering.ShaderGraph;
  10. using UnityEngine.UIElements;
  11. namespace UnityEditor.ShaderGraph
  12. {
  13. class SubGraphOutputNode : AbstractMaterialNode, IHasSettings
  14. {
  15. static string s_MissingOutputSlot = "A Sub Graph must have at least one output slot";
  16. public SubGraphOutputNode()
  17. {
  18. name = "Output";
  19. }
  20. // Link to the Sub Graph overview page instead of the specific Node page, seems more useful
  21. public override string documentationURL => Documentation.GetPageLink("Sub-graph");
  22. void ValidateShaderStage()
  23. {
  24. List<MaterialSlot> slots = new List<MaterialSlot>();
  25. GetInputSlots(slots);
  26. foreach(MaterialSlot slot in slots)
  27. slot.stageCapability = ShaderStageCapability.All;
  28. var effectiveStage = ShaderStageCapability.All;
  29. foreach (var slot in slots)
  30. {
  31. var stage = NodeUtils.GetEffectiveShaderStageCapability(slot, true);
  32. if (stage != ShaderStageCapability.All)
  33. {
  34. effectiveStage = stage;
  35. break;
  36. }
  37. }
  38. foreach(MaterialSlot slot in slots)
  39. slot.stageCapability = effectiveStage;
  40. }
  41. void ValidateSlotName()
  42. {
  43. List<MaterialSlot> slots = new List<MaterialSlot>();
  44. GetInputSlots(slots);
  45. foreach (var slot in slots)
  46. {
  47. var error = NodeUtils.ValidateSlotName(slot.RawDisplayName(), out string errorMessage);
  48. if (error)
  49. {
  50. owner.AddValidationError(tempId, errorMessage);
  51. break;
  52. }
  53. }
  54. }
  55. public override void ValidateNode()
  56. {
  57. ValidateShaderStage();
  58. if (!this.GetInputSlots<MaterialSlot>().Any())
  59. {
  60. owner.AddValidationError(tempId, s_MissingOutputSlot, ShaderCompilerMessageSeverity.Warning);
  61. }
  62. base.ValidateNode();
  63. }
  64. protected override void OnSlotsChanged()
  65. {
  66. base.OnSlotsChanged();
  67. ValidateNode();
  68. }
  69. public int AddSlot(ConcreteSlotValueType concreteValueType)
  70. {
  71. var index = this.GetInputSlots<ISlot>().Count() + 1;
  72. name = NodeUtils.GetDuplicateSafeNameForSlot(this, index, "Out_" + concreteValueType.ToString());
  73. AddSlot(MaterialSlot.CreateMaterialSlot(concreteValueType.ToSlotValueType(), index, name,
  74. NodeUtils.GetHLSLSafeName(name), SlotType.Input, Vector4.zero));
  75. return index;
  76. }
  77. public VisualElement CreateSettingsElement()
  78. {
  79. PropertySheet ps = new PropertySheet();
  80. ps.Add(new ReorderableSlotListView(this, SlotType.Input));
  81. return ps;
  82. }
  83. public override bool canDeleteNode => false;
  84. public override bool canCopyNode => false;
  85. }
  86. }