VFXCircleGizmos.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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(TCircle))]
  10. class VFXCircleGizmo : VFXSpaceableGizmo<TCircle>
  11. {
  12. IProperty<Vector3> m_CenterProperty;
  13. IProperty<Vector3> m_AnglesProperty;
  14. IProperty<Vector3> m_ScaleProperty;
  15. IProperty<float> m_RadiusProperty;
  16. public override void RegisterEditableMembers(IContext context)
  17. {
  18. m_CenterProperty = context.RegisterProperty<Vector3>("transform.position");
  19. m_AnglesProperty = context.RegisterProperty<Vector3>("transform.angles");
  20. m_ScaleProperty = context.RegisterProperty<Vector3>("transform.scale");
  21. m_RadiusProperty = context.RegisterProperty<float>("radius");
  22. }
  23. private static readonly Vector3[] s_RadiusDirections = new Vector3[] { Vector3.up, Vector3.right, Vector3.down, Vector3.left };
  24. private static readonly int[] s_RadiusDirectionsName = new int[]
  25. {
  26. "VFX_Circle_Up".GetHashCode(),
  27. "VFX_Circle_Right".GetHashCode(),
  28. "VFX_Circle_Down".GetHashCode(),
  29. "VFX_Circle_Left".GetHashCode()
  30. };
  31. public static void DrawCircle(VFXGizmo gizmo, TCircle circle, IProperty<Vector3> centerProperty, IProperty<Vector3> anglesProperty, IProperty<Vector3> scaleProperty, IProperty<float> radiusProperty, int countVisible = int.MaxValue)
  32. {
  33. var radius = circle.radius;
  34. gizmo.PositionGizmo(circle.transform.position, circle.transform.angles, centerProperty, false);
  35. gizmo.RotationGizmo(circle.transform.position, circle.transform.angles, anglesProperty, false);
  36. gizmo.ScaleGizmo(circle.transform.position, circle.transform.angles, circle.transform.scale, scaleProperty, false);
  37. if (radiusProperty.isEditable)
  38. {
  39. using (new Handles.DrawingScope(Handles.matrix * circle.transform))
  40. {
  41. for (int i = 0; i < countVisible && i < s_RadiusDirections.Length; ++i)
  42. {
  43. EditorGUI.BeginChangeCheck();
  44. var dir = s_RadiusDirections[i];
  45. var sliderPos = dir * radius;
  46. var result = Handles.Slider(s_RadiusDirectionsName[i], sliderPos, dir, handleSize * HandleUtility.GetHandleSize(sliderPos), CustomCubeHandleCap, 0.0f);
  47. if (EditorGUI.EndChangeCheck())
  48. {
  49. radius = result.magnitude;
  50. if (float.IsNaN(radius))
  51. radius = 0;
  52. radiusProperty.SetValue(radius);
  53. }
  54. }
  55. }
  56. }
  57. }
  58. public override void OnDrawSpacedGizmo(TCircle circle)
  59. {
  60. using (new Handles.DrawingScope(Handles.matrix * circle.transform))
  61. {
  62. // Draw circle around the arc
  63. Handles.DrawWireDisc(Vector3.zero, -Vector3.forward, circle.radius);
  64. }
  65. DrawCircle(this, circle, m_CenterProperty, m_AnglesProperty, m_ScaleProperty, m_RadiusProperty);
  66. }
  67. public static Bounds GetBoundsFromCircle(TCircle value)
  68. {
  69. return new Bounds(value.transform.position, value.transform.scale * value.radius);
  70. }
  71. public override Bounds OnGetSpacedGizmoBounds(TCircle value)
  72. {
  73. return GetBoundsFromCircle(value);
  74. }
  75. }
  76. [VFXGizmo(typeof(TArcCircle))]
  77. class VFXArcCircleGizmo : VFXSpaceableGizmo<TArcCircle>
  78. {
  79. IProperty<Vector3> m_CenterProperty;
  80. IProperty<Vector3> m_AnglesProperty;
  81. IProperty<Vector3> m_ScaleProperty;
  82. IProperty<float> m_RadiusProperty;
  83. IProperty<float> m_ArcProperty;
  84. public override void RegisterEditableMembers(IContext context)
  85. {
  86. m_CenterProperty = context.RegisterProperty<Vector3>("circle.transform.position");
  87. m_AnglesProperty = context.RegisterProperty<Vector3>("circle.transform.angles");
  88. m_ScaleProperty = context.RegisterProperty<Vector3>("circle.transform.scale");
  89. m_RadiusProperty = context.RegisterProperty<float>("circle.radius");
  90. m_ArcProperty = context.RegisterProperty<float>("arc");
  91. }
  92. public override void OnDrawSpacedGizmo(TArcCircle arcCircle)
  93. {
  94. float radius = arcCircle.circle.radius;
  95. float arc = arcCircle.arc * Mathf.Rad2Deg;
  96. using (new Handles.DrawingScope(Handles.matrix * arcCircle.circle.transform))
  97. {
  98. Handles.DrawWireArc(Vector3.zero, -Vector3.forward, Vector3.up, arc, radius);
  99. ArcGizmo(Vector3.zero, radius, arc, m_ArcProperty, Quaternion.Euler(-90.0f, 0.0f, 0.0f));
  100. }
  101. VFXCircleGizmo.DrawCircle(this, arcCircle.circle, m_CenterProperty, m_AnglesProperty, m_ScaleProperty, m_RadiusProperty, Mathf.CeilToInt(arc / 90));
  102. }
  103. public override Bounds OnGetSpacedGizmoBounds(TArcCircle value)
  104. {
  105. return VFXCircleGizmo.GetBoundsFromCircle(value.circle);
  106. }
  107. }
  108. }