IntegerNode.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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", "Integer")]
  9. class IntegerNode : AbstractMaterialNode, IGeneratesBodyCode, IPropertyFromNode
  10. {
  11. [SerializeField]
  12. private int m_Value;
  13. public const int OutputSlotId = 0;
  14. private const string kOutputSlotName = "Out";
  15. public IntegerNode()
  16. {
  17. name = "Integer";
  18. UpdateNodeAfterDeserialization();
  19. }
  20. public sealed override void UpdateNodeAfterDeserialization()
  21. {
  22. AddSlot(new Vector1MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, 0));
  23. RemoveSlotsNameNotMatching(new[] { OutputSlotId });
  24. }
  25. [IntegerControl("")]
  26. public int value
  27. {
  28. get { return m_Value; }
  29. set
  30. {
  31. if (m_Value == value)
  32. return;
  33. m_Value = value;
  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 Vector1ShaderProperty()
  42. {
  43. overrideReferenceName = GetVariableNameForNode(),
  44. generatePropertyBlock = false,
  45. value = value,
  46. floatType = FloatType.Integer
  47. });
  48. }
  49. public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
  50. {
  51. if (generationMode.IsPreview())
  52. return;
  53. sb.AppendLine(string.Format("$precision {0} = {1};", GetVariableNameForNode(), m_Value));
  54. }
  55. public override string GetVariableNameForSlot(int slotId)
  56. {
  57. return GetVariableNameForNode();
  58. }
  59. public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
  60. {
  61. properties.Add(new PreviewProperty(PropertyType.Vector1)
  62. {
  63. name = GetVariableNameForNode(),
  64. floatValue = m_Value
  65. });
  66. }
  67. public AbstractShaderProperty AsShaderProperty()
  68. {
  69. return new Vector1ShaderProperty { value = value, floatType = FloatType.Integer };
  70. }
  71. public int outputSlotId { get { return OutputSlotId; } }
  72. }
  73. }