VFXSphereGizmos.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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(TSphere))]
  10. class VFXTSphereGizmo : VFXSpaceableGizmo<TSphere>
  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.right, Vector3.up, Vector3.forward };
  24. private static readonly int[] s_RadiusName = { "VFX_Radius_Right".GetHashCode(), "VFX_Radius_Up".GetHashCode(), "VFX_Radius_Forward".GetHashCode() };
  25. public static void DrawSphere(VFXGizmo gizmo, TSphere sphere, IProperty<Vector3> centerProperty, IProperty<Vector3> anglesProperty, IProperty<Vector3> scaleProperty, IProperty<float> radiusProperty)
  26. {
  27. gizmo.PositionGizmo(sphere.transform.position, sphere.transform.angles, centerProperty, false);
  28. gizmo.RotationGizmo(sphere.transform.position, sphere.transform.angles, anglesProperty, false);
  29. gizmo.ScaleGizmo(sphere.transform.position, sphere.transform.angles, sphere.transform.scale, scaleProperty, false);
  30. // Radius controls
  31. if (radiusProperty.isEditable)
  32. {
  33. using (new Handles.DrawingScope(Handles.matrix * sphere.transform))
  34. {
  35. for (int i = 0; i < s_RadiusDirections.Length; ++i)
  36. {
  37. EditorGUI.BeginChangeCheck();
  38. var dir = s_RadiusDirections[i];
  39. var sliderPos = dir * sphere.radius;
  40. var result = Handles.Slider(s_RadiusName[i], sliderPos, dir, handleSize * HandleUtility.GetHandleSize(sliderPos), CustomCubeHandleCap, 0);
  41. if (EditorGUI.EndChangeCheck())
  42. {
  43. float newRadius = result.magnitude;
  44. if (float.IsNaN(newRadius))
  45. newRadius = 0;
  46. radiusProperty.SetValue(newRadius);
  47. }
  48. }
  49. }
  50. }
  51. }
  52. static public void DrawSpaceSphere(VFXGizmo gizmo, TSphere sphere, IProperty<Vector3> centerProperty, IProperty<Vector3> anglesProperty, IProperty<Vector3> scaleProperty, IProperty<float> radiusProperty)
  53. {
  54. using (new Handles.DrawingScope(Handles.matrix * sphere.transform))
  55. {
  56. Handles.DrawWireDisc(Vector3.zero, Vector3.forward, sphere.radius);
  57. Handles.DrawWireDisc(Vector3.zero, Vector3.up, sphere.radius);
  58. Handles.DrawWireDisc(Vector3.zero, Vector3.right, sphere.radius);
  59. }
  60. DrawSphere(gizmo, sphere, centerProperty, anglesProperty, scaleProperty, radiusProperty);
  61. }
  62. public override void OnDrawSpacedGizmo(TSphere sphere)
  63. {
  64. DrawSpaceSphere(this, sphere, m_CenterProperty, m_AnglesProperty, m_ScaleProperty, m_RadiusProperty);
  65. }
  66. static public Bounds GetBoundsFromSphere(TSphere sphere)
  67. {
  68. return new Bounds(sphere.transform.position, sphere.transform.scale * sphere.radius);
  69. }
  70. public override Bounds OnGetSpacedGizmoBounds(TSphere value)
  71. {
  72. return GetBoundsFromSphere(value);
  73. }
  74. }
  75. [VFXGizmo(typeof(TArcSphere))]
  76. class VFXArcSphereGizmo : VFXSpaceableGizmo<TArcSphere>
  77. {
  78. IProperty<Vector3> m_CenterProperty;
  79. IProperty<Vector3> m_AnglesProperty;
  80. IProperty<Vector3> m_ScaleProperty;
  81. IProperty<float> m_RadiusProperty;
  82. IProperty<float> m_ArcProperty;
  83. public override void RegisterEditableMembers(IContext context)
  84. {
  85. m_CenterProperty = context.RegisterProperty<Vector3>("sphere.transform.position");
  86. m_AnglesProperty = context.RegisterProperty<Vector3>("sphere.transform.angles");
  87. m_ScaleProperty = context.RegisterProperty<Vector3>("sphere.transform.scale");
  88. m_RadiusProperty = context.RegisterProperty<float>("sphere.radius");
  89. m_ArcProperty = context.RegisterProperty<float>("arc");
  90. }
  91. public override void OnDrawSpacedGizmo(TArcSphere arcSphere)
  92. {
  93. float radius = arcSphere.sphere.radius;
  94. float arc = arcSphere.arc * Mathf.Rad2Deg;
  95. using (new Handles.DrawingScope(Handles.matrix * arcSphere.sphere.transform))
  96. {
  97. // Draw semi-circles at 90 degree angles
  98. for (int i = 0; i < 4; i++)
  99. {
  100. float currentArc = (float)(i * 90);
  101. if (currentArc <= arc)
  102. Handles.DrawWireArc(Vector3.zero, Matrix4x4.Rotate(Quaternion.Euler(0.0f, 180.0f, currentArc)) * Vector3.right, Vector3.forward, 180.0f, radius);
  103. }
  104. // Draw an extra semi-circle at the arc angle
  105. if (arcSphere.arc < Mathf.PI * 2.0f)
  106. Handles.DrawWireArc(Vector3.zero, Matrix4x4.Rotate(Quaternion.Euler(0.0f, 180.0f, arc)) * Vector3.right, Vector3.forward, 180.0f, radius);
  107. // Draw 3rd circle around the arc
  108. Handles.DrawWireArc(Vector3.zero, -Vector3.forward, Vector3.up, arc, radius);
  109. ArcGizmo(Vector3.zero, radius, arc, m_ArcProperty, Quaternion.Euler(-90.0f, 0.0f, 0.0f));
  110. }
  111. VFXTSphereGizmo.DrawSphere(this, arcSphere.sphere, m_CenterProperty, m_AnglesProperty, m_ScaleProperty, m_RadiusProperty);
  112. }
  113. public override Bounds OnGetSpacedGizmoBounds(TArcSphere value)
  114. {
  115. return VFXTSphereGizmo.GetBoundsFromSphere(value.sphere);
  116. }
  117. }
  118. [VFXGizmo(typeof(Sphere))]
  119. class VFXSphereGizmo : VFXSpaceableGizmo<Sphere>
  120. {
  121. IProperty<Vector3> m_CenterProperty;
  122. IProperty<float> m_RadiusProperty;
  123. public override void RegisterEditableMembers(IContext context)
  124. {
  125. m_CenterProperty = context.RegisterProperty<Vector3>("center");
  126. m_RadiusProperty = context.RegisterProperty<float>("radius");
  127. }
  128. public override void OnDrawSpacedGizmo(Sphere sphere)
  129. {
  130. var tSphere = (TSphere)sphere;
  131. VFXTSphereGizmo.DrawSpaceSphere(this, tSphere, m_CenterProperty, null, null, m_RadiusProperty);
  132. }
  133. public override Bounds OnGetSpacedGizmoBounds(Sphere value)
  134. {
  135. var tSphere = (TSphere)value;
  136. return VFXTSphereGizmo.GetBoundsFromSphere(tSphere);
  137. }
  138. }
  139. }