BooleanNode.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System.Collections.Generic;
  2. using UnityEditor.ShaderGraph.Drawing.Controls;
  3. using UnityEngine;
  4. using UnityEditor.Graphing;
  5. using UnityEditor.ShaderGraph.Internal;
  6. namespace UnityEditor.ShaderGraph
  7. {
  8. [Title("Input", "Basic", "Boolean")]
  9. class BooleanNode : AbstractMaterialNode, IGeneratesBodyCode, IPropertyFromNode
  10. {
  11. [SerializeField]
  12. private bool m_Value;
  13. public const int OutputSlotId = 0;
  14. private const string kOutputSlotName = "Out";
  15. public BooleanNode()
  16. {
  17. name = "Boolean";
  18. UpdateNodeAfterDeserialization();
  19. }
  20. public sealed override void UpdateNodeAfterDeserialization()
  21. {
  22. AddSlot(new BooleanMaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, false));
  23. RemoveSlotsNameNotMatching(new[] { OutputSlotId });
  24. }
  25. [ToggleControl("")]
  26. public ToggleData value
  27. {
  28. get { return new ToggleData(m_Value); }
  29. set
  30. {
  31. if (m_Value == value.isOn)
  32. return;
  33. m_Value = value.isOn;
  34. Dirty(ModificationScope.Node);
  35. }
  36. }
  37. public override void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode)
  38. {
  39. if (!generationMode.IsPreview())
  40. return;
  41. properties.AddShaderProperty(new BooleanShaderProperty()
  42. {
  43. overrideReferenceName = GetVariableNameForNode(),
  44. generatePropertyBlock = false,
  45. value = m_Value
  46. });
  47. }
  48. public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
  49. {
  50. if (generationMode.IsPreview())
  51. return;
  52. sb.AppendLine("$precision {0} = {1};", GetVariableNameForNode(), (m_Value ? 1 : 0));
  53. }
  54. public override string GetVariableNameForSlot(int slotId)
  55. {
  56. return GetVariableNameForNode();
  57. }
  58. public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
  59. {
  60. properties.Add(new PreviewProperty(PropertyType.Boolean)
  61. {
  62. name = GetVariableNameForNode(),
  63. booleanValue = m_Value
  64. });
  65. }
  66. public AbstractShaderProperty AsShaderProperty()
  67. {
  68. return new BooleanShaderProperty { value = m_Value };
  69. }
  70. public int outputSlotId { get { return OutputSlotId; } }
  71. }
  72. }