VFXBoxGizmos.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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(OrientedBox))]
  10. class VFXOrientedBoxGizmo : VFXSpaceableGizmo<OrientedBox>
  11. {
  12. IProperty<Vector3> m_CenterProperty;
  13. IProperty<float> m_SizeXProperty;
  14. IProperty<float> m_SizeYProperty;
  15. IProperty<float> m_SizeZProperty;
  16. IProperty<Vector3> m_AnglesProperty;
  17. public override void RegisterEditableMembers(IContext context)
  18. {
  19. m_CenterProperty = context.RegisterProperty<Vector3>("center");
  20. m_SizeXProperty = context.RegisterProperty<float>("size.x");
  21. m_SizeYProperty = context.RegisterProperty<float>("size.y");
  22. m_SizeZProperty = context.RegisterProperty<float>("size.z");
  23. m_AnglesProperty = context.RegisterProperty<Vector3>("angles");
  24. }
  25. public override void OnDrawSpacedGizmo(OrientedBox box)
  26. {
  27. Matrix4x4 rotate = Matrix4x4.Rotate(Quaternion.Euler(box.angles));
  28. Matrix4x4 fullTranform = Matrix4x4.Translate(box.center) * rotate * Matrix4x4.Translate(-box.center);
  29. VFXAABoxGizmo.DrawBoxSizeDataAnchorGizmo(new AABox() { center = box.center, size = box.size }, component, this, m_CenterProperty, m_SizeXProperty, m_SizeYProperty, m_SizeZProperty, fullTranform);
  30. RotationGizmo(box.center, box.angles, m_AnglesProperty, true);
  31. }
  32. public override Bounds OnGetSpacedGizmoBounds(OrientedBox value)
  33. {
  34. return new Bounds(value.center, value.size); //TODO take orientation in account
  35. }
  36. }
  37. [VFXGizmo(typeof(AABox))]
  38. class VFXAABoxGizmo : VFXSpaceableGizmo<AABox>
  39. {
  40. IProperty<Vector3> m_CenterProperty;
  41. IProperty<float> m_SizeXProperty;
  42. IProperty<float> m_SizeYProperty;
  43. IProperty<float> m_SizeZProperty;
  44. public override void RegisterEditableMembers(IContext context)
  45. {
  46. m_CenterProperty = context.RegisterProperty<Vector3>("center");
  47. m_SizeXProperty = context.RegisterProperty<float>("size.x");
  48. m_SizeYProperty = context.RegisterProperty<float>("size.y");
  49. m_SizeZProperty = context.RegisterProperty<float>("size.z");
  50. }
  51. public override void OnDrawSpacedGizmo(AABox box)
  52. {
  53. DrawBoxSizeDataAnchorGizmo(box, component, this, m_CenterProperty, m_SizeXProperty, m_SizeYProperty, m_SizeZProperty, Matrix4x4.identity);
  54. }
  55. static bool TwoSidedSizeHandle(Color color, Vector3 otherMiddle, Vector3 middle, Vector3 center, IProperty<float> sizeProperty, IProperty<Vector3> centerProperty)
  56. {
  57. bool result = false;
  58. var savedColor = Handles.color;
  59. Handles.color = color;
  60. if (sizeProperty.isEditable)
  61. {
  62. result = SizeHandle(otherMiddle, middle, center, sizeProperty, centerProperty);
  63. result = SizeHandle(middle, otherMiddle, center, sizeProperty, centerProperty) || result;
  64. }
  65. Handles.color = savedColor;
  66. return result;
  67. }
  68. static bool SizeHandle(Vector3 otherMiddle, Vector3 middle, Vector3 center, IProperty<float> sizeProperty, IProperty<Vector3> centerProperty)
  69. {
  70. EditorGUI.BeginChangeCheck();
  71. Vector3 middleResult = Handles.Slider(middle, (middle - center), handleSize * HandleUtility.GetHandleSize(middle), Handles.CubeHandleCap, 0);
  72. if (EditorGUI.EndChangeCheck())
  73. {
  74. sizeProperty.SetValue((middleResult - otherMiddle).magnitude);
  75. if (centerProperty.isEditable)
  76. {
  77. centerProperty.SetValue((middleResult + otherMiddle) * 0.5f);
  78. }
  79. return true;
  80. }
  81. return false;
  82. }
  83. public static bool DrawBoxSizeDataAnchorGizmo(AABox box,
  84. VisualEffect component,
  85. VFXGizmo gizmo,
  86. IProperty<Vector3> centerProperty,
  87. IProperty<float> sizeXProperty,
  88. IProperty<float> sizeYProperty,
  89. IProperty<float> sizeZProperty,
  90. Matrix4x4 centerMatrix)
  91. {
  92. Vector3[] points = new Vector3[8];
  93. Vector3 center = box.center;
  94. Vector3 size = box.size;
  95. points[0] = center + new Vector3(size.x * 0.5f, size.y * 0.5f, size.z * 0.5f);
  96. points[1] = center + new Vector3(size.x * 0.5f, -size.y * 0.5f, size.z * 0.5f);
  97. points[2] = center + new Vector3(-size.x * 0.5f, size.y * 0.5f, size.z * 0.5f);
  98. points[3] = center + new Vector3(-size.x * 0.5f, -size.y * 0.5f, size.z * 0.5f);
  99. points[4] = center + new Vector3(size.x * 0.5f, size.y * 0.5f, -size.z * 0.5f);
  100. points[5] = center + new Vector3(size.x * 0.5f, -size.y * 0.5f, -size.z * 0.5f);
  101. points[6] = center + new Vector3(-size.x * 0.5f, size.y * 0.5f, -size.z * 0.5f);
  102. points[7] = center + new Vector3(-size.x * 0.5f, -size.y * 0.5f, -size.z * 0.5f);
  103. for (int i = 0; i < points.Length; ++i)
  104. {
  105. points[i] = centerMatrix.MultiplyPoint(points[i]);
  106. }
  107. Handles.DrawLine(points[0], points[1]);
  108. Handles.DrawLine(points[2], points[3]);
  109. Handles.DrawLine(points[4], points[5]);
  110. Handles.DrawLine(points[6], points[7]);
  111. Handles.DrawLine(points[0], points[2]);
  112. Handles.DrawLine(points[0], points[4]);
  113. Handles.DrawLine(points[1], points[3]);
  114. Handles.DrawLine(points[1], points[5]);
  115. Handles.DrawLine(points[2], points[6]);
  116. Handles.DrawLine(points[3], points[7]);
  117. Handles.DrawLine(points[4], points[6]);
  118. Handles.DrawLine(points[5], points[7]);
  119. bool changed = false;
  120. Vector3 xFaceMiddle = (points[0] + points[1] + points[4] + points[5]) * 0.25f;
  121. Vector3 minusXFaceMiddle = (points[2] + points[3] + points[6] + points[7]) * 0.25f;
  122. changed = TwoSidedSizeHandle(Color.red, xFaceMiddle, minusXFaceMiddle, center, sizeXProperty, centerProperty);
  123. Vector3 yFaceMiddle = (points[0] + points[2] + points[4] + points[6]) * 0.25f;
  124. Vector3 minusYFaceMiddle = (points[1] + points[3] + points[5] + points[7]) * 0.25f;
  125. changed = TwoSidedSizeHandle(Color.green, yFaceMiddle, minusYFaceMiddle, center, sizeYProperty, centerProperty) || changed;
  126. Vector3 zFaceMiddle = (points[0] + points[1] + points[2] + points[3]) * 0.25f;
  127. Vector3 minusZFaceMiddle = (points[4] + points[5] + points[6] + points[7]) * 0.25f;
  128. changed = TwoSidedSizeHandle(Color.blue, zFaceMiddle, minusZFaceMiddle, center, sizeZProperty, centerProperty) || changed;
  129. changed = gizmo.PositionGizmo(box.center, Vector3.zero, centerProperty, true) || changed;
  130. return changed;
  131. }
  132. public override Bounds OnGetSpacedGizmoBounds(AABox value)
  133. {
  134. return new Bounds(value.center, value.size);
  135. }
  136. }
  137. }