RotateNode.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System.Reflection;
  2. using UnityEngine;
  3. using UnityEditor.ShaderGraph.Drawing.Controls;
  4. using UnityEditor.Graphing;
  5. namespace UnityEditor.ShaderGraph
  6. {
  7. enum RotationUnit
  8. {
  9. Radians,
  10. Degrees
  11. };
  12. [Title("UV", "Rotate")]
  13. class RotateNode : CodeFunctionNode
  14. {
  15. [SerializeField]
  16. private RotationUnit m_Unit = RotationUnit.Radians;
  17. [EnumControl("Unit")]
  18. public RotationUnit unit
  19. {
  20. get { return m_Unit; }
  21. set
  22. {
  23. if (m_Unit == value)
  24. return;
  25. m_Unit = value;
  26. Dirty(ModificationScope.Graph);
  27. }
  28. }
  29. public RotateNode()
  30. {
  31. name = "Rotate";
  32. }
  33. protected override MethodInfo GetFunctionToConvert()
  34. {
  35. if (m_Unit == RotationUnit.Radians)
  36. return GetType().GetMethod("Unity_Rotate_Radians", BindingFlags.Static | BindingFlags.NonPublic);
  37. else
  38. return GetType().GetMethod("Unity_Rotate_Degrees", BindingFlags.Static | BindingFlags.NonPublic);
  39. }
  40. static string Unity_Rotate_Radians(
  41. [Slot(0, Binding.MeshUV0)] Vector2 UV,
  42. [Slot(1, Binding.None, 0.5f, 0.5f, 0.5f, 0.5f)] Vector2 Center,
  43. [Slot(2, Binding.None)] Vector1 Rotation,
  44. [Slot(3, Binding.None)] out Vector2 Out)
  45. {
  46. Out = Vector2.zero;
  47. return
  48. @"
  49. {
  50. //rotation matrix
  51. UV -= Center;
  52. $precision s = sin(Rotation);
  53. $precision c = cos(Rotation);
  54. //center rotation matrix
  55. $precision2x2 rMatrix = $precision2x2(c, -s, s, c);
  56. rMatrix *= 0.5;
  57. rMatrix += 0.5;
  58. rMatrix = rMatrix*2 - 1;
  59. //multiply the UVs by the rotation matrix
  60. UV.xy = mul(UV.xy, rMatrix);
  61. UV += Center;
  62. Out = UV;
  63. }";
  64. }
  65. static string Unity_Rotate_Degrees(
  66. [Slot(0, Binding.MeshUV0)] Vector2 UV,
  67. [Slot(1, Binding.None, 0.5f, 0.5f, 0.5f, 0.5f)] Vector2 Center,
  68. [Slot(2, Binding.None)] Vector1 Rotation,
  69. [Slot(3, Binding.None)] out Vector2 Out)
  70. {
  71. Out = Vector2.zero;
  72. return @"
  73. {
  74. //rotation matrix
  75. Rotation = Rotation * (3.1415926f/180.0f);
  76. UV -= Center;
  77. $precision s = sin(Rotation);
  78. $precision c = cos(Rotation);
  79. //center rotation matrix
  80. $precision2x2 rMatrix = $precision2x2(c, -s, s, c);
  81. rMatrix *= 0.5;
  82. rMatrix += 0.5;
  83. rMatrix = rMatrix*2 - 1;
  84. //multiply the UVs by the rotation matrix
  85. UV.xy = mul(UV.xy, rMatrix);
  86. UV += Center;
  87. Out = UV;
  88. }";
  89. }
  90. }
  91. }