PlaneSurfaceEditor.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using UnityEngine;
  2. using UnityEditor;
  3. using Rokid.UXR.Interaction;
  4. namespace Rokid.UXR.Editor
  5. {
  6. [CustomEditor(typeof(PlaneSurface))]
  7. public class PlaneSurfaceEditor : UnityEditor.Editor
  8. {
  9. private const int NUM_SEGMENTS = 40;
  10. private const float FADE_DISTANCE = 10f;
  11. private static readonly Color Color = EditorConstants.PRIMARY_COLOR_DISABLED;
  12. private static float Interval => FADE_DISTANCE / NUM_SEGMENTS;
  13. public void OnSceneGUI()
  14. {
  15. PlaneSurface plane = target as PlaneSurface;
  16. Draw(plane);
  17. }
  18. public static void Draw(PlaneSurface plane)
  19. {
  20. Vector3 origin = plane.transform.position;
  21. if (SceneView.lastActiveSceneView?.camera != null)
  22. {
  23. Transform camTransform = SceneView.lastActiveSceneView.camera.transform;
  24. if (plane.ClosestSurfacePoint(camTransform.position, out SurfaceHit hit, 0))
  25. {
  26. Vector3 hitDelta = PoseUtils.Delta(plane.transform, new Pose(hit.Point, plane.transform.rotation)).position;
  27. hitDelta.x = Mathf.RoundToInt(hitDelta.x / Interval) * Interval;
  28. hitDelta.y = Mathf.RoundToInt(hitDelta.y / Interval) * Interval;
  29. hitDelta.z = 0f;
  30. origin = PoseUtils.Multiply(plane.transform.GetPose(), new Pose(hitDelta, Quaternion.identity)).position;
  31. }
  32. }
  33. DrawLines(origin, plane.Normal, plane.transform.up, Color);
  34. DrawLines(origin, plane.Normal, -plane.transform.up, Color);
  35. DrawLines(origin, plane.Normal, plane.transform.right, Color);
  36. DrawLines(origin, plane.Normal, -plane.transform.right, Color);
  37. }
  38. private static void DrawLines(in Vector3 origin,
  39. in Vector3 normal,
  40. in Vector3 direction,
  41. in Color baseColor)
  42. {
  43. Color prevColor = Handles.color;
  44. Color color = baseColor;
  45. Vector3 offsetOrigin = origin;
  46. for (int i = 0; i < NUM_SEGMENTS; ++i)
  47. {
  48. Handles.color = color;
  49. Vector3 cross = Vector3.Cross(normal, direction).normalized;
  50. float interval = Interval;
  51. for (int j = -NUM_SEGMENTS; j < NUM_SEGMENTS; ++j)
  52. {
  53. float horizStart = interval * j;
  54. float horizEnd = horizStart + interval;
  55. Vector3 start = offsetOrigin + cross * horizStart;
  56. Vector3 end = offsetOrigin + cross * horizEnd;
  57. color.a = 1f - Mathf.Abs((float)j / NUM_SEGMENTS);
  58. color.a *= 1f - ((float)i / NUM_SEGMENTS);
  59. color.a *= color.a;
  60. Handles.color = color;
  61. Handles.DrawLine(start, end);
  62. }
  63. offsetOrigin += direction * interval;
  64. color = baseColor;
  65. }
  66. Handles.color = prevColor;
  67. }
  68. }
  69. }