VFXTypesGizmos.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEngine.VFX;
  7. namespace UnityEditor.VFX
  8. {
  9. [VFXGizmo(typeof(Position))]
  10. class VFXPositionGizmo : VFXSpaceableGizmo<Position>
  11. {
  12. IProperty<Position> m_Property;
  13. public override void RegisterEditableMembers(IContext context)
  14. {
  15. m_Property = context.RegisterProperty<Position>("");
  16. }
  17. public override void OnDrawSpacedGizmo(Position position)
  18. {
  19. if (m_Property.isEditable && PositionGizmo(ref position.position, Vector3.zero, true))
  20. {
  21. m_Property.SetValue(position);
  22. }
  23. }
  24. public override Bounds OnGetSpacedGizmoBounds(Position value)
  25. {
  26. return new Bounds(value.position, Vector3.one);
  27. }
  28. }
  29. [VFXGizmo(typeof(DirectionType))]
  30. class VFXDirectionGizmo : VFXSpaceableGizmo<DirectionType>
  31. {
  32. IProperty<DirectionType> m_Property;
  33. public override void RegisterEditableMembers(IContext context)
  34. {
  35. m_Property = context.RegisterProperty<DirectionType>("");
  36. }
  37. public override void OnDrawSpacedGizmo(DirectionType direction)
  38. {
  39. direction.direction.Normalize();
  40. if (direction.direction == Vector3.zero)
  41. {
  42. direction.direction = Vector3.up;
  43. }
  44. Quaternion normalQuat = Quaternion.FromToRotation(Vector3.forward, direction.direction);
  45. Ray ray = HandleUtility.GUIPointToWorldRay(Vector2.one * 200);
  46. var position = ray.origin + ray.direction * 2;
  47. var mat = Handles.matrix;
  48. mat.SetColumn(3, new Vector4(position.x, position.y, position.z, 1));
  49. Handles.matrix = mat;
  50. Handles.ArrowHandleCap(0, Vector3.zero, normalQuat, HandleUtility.GetHandleSize(Vector3.zero) * 1, Event.current.type);
  51. if (m_Property.isEditable && NormalGizmo(Vector3.zero, ref direction.direction, true))
  52. {
  53. direction.direction.Normalize();
  54. m_Property.SetValue(direction);
  55. }
  56. }
  57. Quaternion m_PrevQuaternion;
  58. public static void AngleHandleDrawFunction(int controlID, Vector3 position, Quaternion rotation, float size, EventType eventType)
  59. {
  60. Handles.DrawWireDisc(Vector3.zero, Vector3.forward, size * 10);
  61. Handles.DrawLine(Vector3.zero, position);
  62. }
  63. public override Bounds OnGetSpacedGizmoBounds(DirectionType value)
  64. {
  65. return new Bounds(Vector3.zero, Vector3.zero);
  66. }
  67. }
  68. [VFXGizmo(typeof(Vector))]
  69. class VFXVectorGizmo : VFXSpaceableGizmo<Vector>
  70. {
  71. IProperty<Vector> m_Property;
  72. public override void RegisterEditableMembers(IContext context)
  73. {
  74. m_Property = context.RegisterProperty<Vector>("");
  75. }
  76. public override void OnDrawSpacedGizmo(Vector vector)
  77. {
  78. if (vector.vector == Vector3.zero)
  79. {
  80. vector.vector = Vector3.up;
  81. }
  82. Quaternion normalQuat = Quaternion.FromToRotation(Vector3.forward, vector.vector);
  83. float length = vector.vector.magnitude;
  84. if (m_Property.isEditable && NormalGizmo(Vector3.zero, ref vector.vector, true))
  85. {
  86. m_Property.SetValue(vector);
  87. }
  88. if (m_Property.isEditable)
  89. {
  90. Handles.DrawLine(Vector3.zero, vector.vector);
  91. EditorGUI.BeginChangeCheck();
  92. Vector3 result = Handles.Slider(vector.vector, vector.vector, handleSize * 2 * HandleUtility.GetHandleSize(vector.vector), Handles.ConeHandleCap, 0);
  93. if (EditorGUI.EndChangeCheck())
  94. {
  95. vector.vector = vector.vector.normalized * result.magnitude;
  96. m_Property.SetValue(vector);
  97. }
  98. }
  99. else
  100. {
  101. Handles.ArrowHandleCap(0, Vector3.zero, normalQuat, length, Event.current.type);
  102. }
  103. }
  104. public override Bounds OnGetSpacedGizmoBounds(Vector value)
  105. {
  106. return new Bounds(Vector3.zero, Vector3.one * value.vector.magnitude * 2);
  107. }
  108. }
  109. }