Matrix2Node.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using UnityEditor.ShaderGraph.Drawing.Controls;
  2. using UnityEngine;
  3. using UnityEditor.Graphing;
  4. using System.Collections.Generic;
  5. using UnityEditor.ShaderGraph.Internal;
  6. namespace UnityEditor.ShaderGraph
  7. {
  8. [Title("Input", "Matrix", "Matrix 2x2")]
  9. class Matrix2Node : AbstractMaterialNode, IGeneratesBodyCode, IPropertyFromNode
  10. {
  11. public const int OutputSlotId = 0;
  12. const string kOutputSlotName = "Out";
  13. [SerializeField]
  14. Vector2 m_Row0;
  15. [SerializeField]
  16. Vector2 m_Row1;
  17. [MultiFloatControl("", " ", " ", " ", " ")]
  18. public Vector2 row0
  19. {
  20. get { return m_Row0; }
  21. set { SetRow(ref m_Row0, value); }
  22. }
  23. [MultiFloatControl("", " ", " ", " ", " ")]
  24. public Vector2 row1
  25. {
  26. get { return m_Row1; }
  27. set { SetRow(ref m_Row1, value); }
  28. }
  29. void SetRow(ref Vector2 row, Vector2 value)
  30. {
  31. if (value == row)
  32. return;
  33. row = value;
  34. Dirty(ModificationScope.Node);
  35. }
  36. public Matrix2Node()
  37. {
  38. name = "Matrix 2x2";
  39. UpdateNodeAfterDeserialization();
  40. }
  41. public sealed override void UpdateNodeAfterDeserialization()
  42. {
  43. AddSlot(new Matrix2MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output));
  44. RemoveSlotsNameNotMatching(new[] { OutputSlotId });
  45. }
  46. public override void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode)
  47. {
  48. if (!generationMode.IsPreview())
  49. return;
  50. properties.AddShaderProperty(new Vector2ShaderProperty()
  51. {
  52. overrideReferenceName = string.Format("_{0}_m0", GetVariableNameForNode()),
  53. generatePropertyBlock = false,
  54. value = m_Row0
  55. });
  56. properties.AddShaderProperty(new Vector2ShaderProperty()
  57. {
  58. overrideReferenceName = string.Format("_{0}_m1", GetVariableNameForNode()),
  59. generatePropertyBlock = false,
  60. value = m_Row1
  61. });
  62. }
  63. public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
  64. {
  65. if (!generationMode.IsPreview())
  66. {
  67. sb.AppendLine("$precision2 _{0}_m0 = $precision2 ({1}, {2});", GetVariableNameForNode(),
  68. NodeUtils.FloatToShaderValue(m_Row0.x),
  69. NodeUtils.FloatToShaderValue(m_Row0.y));
  70. sb.AppendLine("$precision2 _{0}_m1 = $precision2 ({1}, {2});", GetVariableNameForNode(),
  71. NodeUtils.FloatToShaderValue(m_Row1.x),
  72. NodeUtils.FloatToShaderValue(m_Row1.y));
  73. }
  74. sb.AppendLine("$precision2x2 {0} = $precision2x2 (_{0}_m0.x, _{0}_m0.y, _{0}_m1.x, _{0}_m1.y);", GetVariableNameForNode());
  75. }
  76. public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
  77. {
  78. properties.Add(new PreviewProperty(PropertyType.Vector2)
  79. {
  80. name = string.Format("_{0}_m0", GetVariableNameForNode()),
  81. vector4Value = m_Row0
  82. });
  83. properties.Add(new PreviewProperty(PropertyType.Vector2)
  84. {
  85. name = string.Format("_{0}_m1", GetVariableNameForNode()),
  86. vector4Value = m_Row1
  87. });
  88. }
  89. public override string GetVariableNameForSlot(int slotId)
  90. {
  91. return GetVariableNameForNode();
  92. }
  93. public AbstractShaderProperty AsShaderProperty()
  94. {
  95. return new Matrix2ShaderProperty
  96. {
  97. value = new Matrix4x4()
  98. {
  99. m00 = row0.x,
  100. m01 = row0.y,
  101. m02 = 0,
  102. m03 = 0,
  103. m10 = row1.x,
  104. m11 = row1.y,
  105. m12 = 0,
  106. m13 = 0,
  107. m20 = 0,
  108. m21 = 0,
  109. m22 = 0,
  110. m23 = 0,
  111. m30 = 0,
  112. m31 = 0,
  113. m32 = 0,
  114. m33 = 0,
  115. }
  116. };
  117. }
  118. public int outputSlotId { get { return OutputSlotId; } }
  119. }
  120. }