AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
LineBaseEditor.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using HoloToolkit.Unity;
5 using UnityEngine;
6 using HoloToolkit.Unity.UX;
7 #if UNITY_EDITOR
8 using UnityEditor;
9 
13 public class LineBaseEditor : MRTKEditor
14 {
15  // Const editor properties
16  public static readonly Color DefaultDisplayLineColor = Color.white;
17  protected static readonly Color lineVelocityColor = new Color(0.9f, 1f, 0f, 0.8f);
18  protected const int linePreviewResolutionUnselected = 16;
19 
20  // Static editor properties
21  protected static int linePreviewResolutionSelected = 16;
22  protected static bool drawLinePoints = false;
23  protected static bool drawDottedLine = true;
24  protected static bool drawLineRotations = false;
25  protected static bool drawLineManualUpVectors = false;
26  protected static float lineRotationLength = 0.5f;
27  protected static float lineManualUpVectorLength = 10f;
28 
29  protected virtual StepModeEnum EditorStepMode { get { return StepModeEnum.Interpolated; } }
30 
31  protected override void DrawCustomSceneGUI()
32  {
33  LineBase line = (LineBase)target;
34  bool selected = Selection.activeGameObject == line.gameObject;
35 
36  int previewResolution = Mathf.Min(linePreviewResolutionSelected, linePreviewResolutionUnselected);
37  if (selected)
38  {
39  previewResolution = linePreviewResolutionSelected;
40  }
41 
42  // Draw dotted lines regardless of selection
43  if (drawDottedLine)
44  {
45  DrawDottedLine(line, previewResolution);
46  }
47 
48  // Draw rotations only on selected object
49  if (drawLineRotations && selected)
50  {
51  DrawLineRotations(line, previewResolution);
52  }
53 
54  // Draw up vectors only on selected object
55  if (drawLineManualUpVectors && selected)
56  {
57  DrawManualUpVectorHandles(line);
58  }
59 
60  if (drawLinePoints)
61  {
62  DrawLinePoints(line);
63  }
64 
65  // Since lines are constantly updating themselves
66  // just repaint all by default
67  }
68 
69  protected override void DrawCustomFooter()
70  {
71  LineBase line = (LineBase)target;
72 
73  if (DrawSectionStart(line.name + " Editor", "Editor Settings"))
74  {
75  linePreviewResolutionSelected = EditorGUILayout.IntSlider("Preview resolution", linePreviewResolutionSelected, 3, 100);
76 
77  drawDottedLine = EditorGUILayout.Toggle("Draw Dotted Line", drawDottedLine);
78  drawLinePoints = EditorGUILayout.Toggle("Draw Line Points", drawLinePoints);
79  drawLineRotations = EditorGUILayout.Toggle("Draw Line Rotations", drawLineRotations);
80  drawLineManualUpVectors = EditorGUILayout.Toggle("Draw Manual Up Vectors", drawLineManualUpVectors);
81 
82  if (drawLineRotations)
83  {
84  lineRotationLength = EditorGUILayout.Slider("Rotation Arrow Length", lineRotationLength, 0.01f, 5f);
85  }
86 
87  if (drawLineManualUpVectors)
88  {
89  lineManualUpVectorLength = EditorGUILayout.Slider("Manual Up Vector Length", lineManualUpVectorLength, 1f, 10f);
90  if (GUILayout.Button("Normalize Up Vectors"))
91  {
92  for (int i = 0; i < line.ManualUpVectors.Length; i++)
93  {
94  Vector3 upVector = line.ManualUpVectors[i];
95  if (upVector == Vector3.zero)
96  {
97  upVector = Vector3.up;
98  }
99 
100  line.ManualUpVectors[i] = upVector.normalized;
101  }
102  }
103  }
104  }
105  DrawSectionEnd();
106 
107  SceneView.RepaintAll();
108  }
109 
110  protected void DrawDottedLine(LineBase line, int numSteps)
111  {
112  Vector3 firstPos = Vector3.zero;
113  Vector3 lastPos = Vector3.zero;
114 
115  switch (EditorStepMode)
116  {
117  case StepModeEnum.FromSource:
118  firstPos = line.GetPoint(0);
119  lastPos = firstPos;
120 
121  for (int i = 1; i < line.NumPoints; i++)
122  {
123  Vector3 currentPos = line.GetPoint(i);
124  Handles.DrawDottedLine(lastPos, currentPos, MRTKEditor.DottedLineScreenSpace);
125  lastPos = currentPos;
126  }
127 
128  if (line.Loops)
129  {
130  Handles.DrawDottedLine(lastPos, firstPos, MRTKEditor.DottedLineScreenSpace);
131  }
132  break;
133 
134  case StepModeEnum.Interpolated:
135  default:
136  firstPos = line.GetPoint(0f);
137  lastPos = firstPos;
138  Handles.color = DefaultDisplayLineColor;
139 
140  for (int i = 1; i < numSteps; i++)
141  {
142  float normalizedLength = (1f / (numSteps - 1)) * i;
143  Vector3 currentPos = line.GetPoint(normalizedLength);
144  Handles.DrawDottedLine(lastPos, currentPos, MRTKEditor.DottedLineScreenSpace);
145  lastPos = currentPos;
146  }
147 
148  if (line.Loops)
149  {
150  Handles.DrawDottedLine(lastPos, firstPos, MRTKEditor.DottedLineScreenSpace);
151  }
152  break;
153  }
154  }
155 
156  protected void DrawLinePoints(LineBase line)
157  {
158  Handles.color = DefaultDisplayLineColor;
159  float dotSize = HandleUtility.GetHandleSize(line.transform.position) * 0.025f;
160  for (int i = 0; i < line.NumPoints; i++)
161  {
162  Handles.DotHandleCap(0, line.GetPoint(i), Quaternion.identity, dotSize, EventType.Repaint);
163  }
164  }
165 
166  protected void DrawLineRotations(LineBase line, int numSteps)
167  {
168  Handles.color = lineVelocityColor;
169  float arrowSize = HandleUtility.GetHandleSize(line.transform.position) * lineRotationLength;
170 
171  for (int i = 1; i < numSteps; i++)
172  {
173  float normalizedLength = (1f / (numSteps - 1)) * i;
174  Vector3 currentPos = line.GetPoint(normalizedLength);
175  Quaternion rotation = line.GetRotation(normalizedLength);
176 
177  Handles.color = Color.Lerp(lineVelocityColor, Handles.zAxisColor, 0.75f);
178  Handles.ArrowHandleCap(0, currentPos, Quaternion.LookRotation(rotation * Vector3.forward), arrowSize, EventType.Repaint);
179  Handles.color = Color.Lerp(lineVelocityColor, Handles.xAxisColor, 0.75f);
180  Handles.ArrowHandleCap(0, currentPos, Quaternion.LookRotation(rotation * Vector3.right), arrowSize, EventType.Repaint);
181  Handles.color = Color.Lerp(lineVelocityColor, Handles.yAxisColor, 0.75f);
182  Handles.ArrowHandleCap(0, currentPos, Quaternion.LookRotation(rotation * Vector3.up), arrowSize, EventType.Repaint);
183  }
184  }
185 
186  protected void DrawManualUpVectorHandles(LineBase line)
187  {
188  if (line.ManualUpVectors == null || line.ManualUpVectors.Length < 2)
189  line.ManualUpVectors = new Vector3[2];
190 
191  for (int i = 0; i < line.ManualUpVectors.Length; i++)
192  {
193  float normalizedLength = (1f / (line.ManualUpVectors.Length - 1)) * i;
194  Vector3 currentPoint = line.GetPoint(normalizedLength);
195  Vector3 currentUpVector = line.ManualUpVectors[i];
196  line.ManualUpVectors[i] = VectorHandle(currentPoint, currentUpVector, false, lineManualUpVectorLength);
197  }
198  }
199 }
200 #endif
StepModeEnum
Default options for how to generate points in a line renderer
Definition: LineUtility.cs:41
Quaternion GetRotation(float normalizedLength, RotationTypeEnum rotationType=RotationTypeEnum.None)
Gets the rotation of a point along the line at the specified length
Definition: LineBase.cs:232
Vector3 GetPoint(float normalizedLength)
Gets a point along the line at the specified length
Definition: LineBase.cs:291