AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
Rectangle.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 
7 namespace HoloToolkit.Unity.UX
8 {
9  public class Rectangle : LineBase
10  {
11  public override int NumPoints
12  {
13  get
14  {
15  return 8;
16  }
17  }
18 
19  public override bool Loops
20  {
21  get
22  {
23  // Force to loop
24  loops = true;
25  return loops;
26  }
27  }
28 
29  [Header("Rectangle Settings")]
30  [SerializeField]
31  private Vector3[] points;
32 
33  [EditableProp]
34  public float XSize
35  {
36  get { return xSize; }
37  set
38  {
39  if (xSize != value)
40  {
41  xSize = value;
42  BuildPoints();
43  }
44  }
45  }
46  [EditableProp]
47  public float YSize
48  {
49  get { return ySize; }
50  set
51  {
52  if (ySize != value)
53  {
54  ySize = value;
55  BuildPoints();
56  }
57  }
58  }
59  [EditableProp]
60  public float ZOffset
61  {
62  get
63  {
64  return zOffset;
65  }
66  set
67  {
68  if (zOffset != value)
69  {
70  zOffset = value;
71  BuildPoints();
72  }
73  }
74  }
75 
76  [SerializeField]
78  private float xSize = 1f;
79  [SerializeField]
81  private float ySize = 1f;
82  [SerializeField]
84  private float zOffset = 0f;
85 
91  protected override Vector3 GetPointInternal(float normalizedDistance)
92  {
93  if (points == null || points.Length != 8)
94  {
95  points = new Vector3[8];
96  }
97 
98  BuildPoints();
99 
100  return LineUtils.InterpolateVectorArray(points, normalizedDistance);
101  }
102 
103  protected override void SetPointInternal(int pointIndex, Vector3 point)
104  {
105  if (points == null || points.Length != 8)
106  {
107  points = new Vector3[8];
108  }
109 
110  if (pointIndex <= 7 && pointIndex >= 0)
111  {
112  points[pointIndex] = point;
113  }
114  }
115 
116  protected override Vector3 GetPointInternal(int pointIndex)
117  {
118  if (points == null || points.Length != 8)
119  {
120  points = new Vector3[8];
121  }
122 
123  if (pointIndex <= 7 && pointIndex >= 0)
124  {
125  return points[pointIndex];
126  }
127 
128  return Vector3.zero;
129  }
130 
131  protected override float GetUnclampedWorldLengthInternal()
132  {
133  BuildPoints();
134 
135  Vector3 last = points[0];
136  float distance = 0f;
137  for (int i = 1; i < points.Length; i++)
138  {
139  distance += Vector3.Distance(last, points[i]);
140  last = points[i];
141  }
142  return distance;
143  }
144 
145  protected override Vector3 GetUpVectorInternal(float normalizedLength)
146  {
147  // Rectangle 'up' vector always points out from center
148  return (GetPoint(normalizedLength) - transform.position).normalized;
149  }
150 
151  private void BuildPoints()
152  {
153  Vector3 offset = Vector3.forward * ZOffset;
154  Vector3 top = (Vector3.up * YSize * 0.5f);
155  Vector3 bot = (Vector3.down * YSize * 0.5f);
156  Vector3 left = (Vector3.left * XSize * 0.5f);
157  Vector3 right = (Vector3.right * XSize * 0.5f);
158 
159  SetPointInternal(0, top + left + offset);
160  SetPointInternal(1, top + offset);
161  SetPointInternal(2, top + right + offset);
162  SetPointInternal(3, right + offset);
163  SetPointInternal(4, bot + right + offset);
164  SetPointInternal(5, bot + offset);
165  SetPointInternal(6, bot + left + offset);
166  SetPointInternal(7, left + offset);
167  }
168 
169 #if UNITY_EDITOR
170  [UnityEditor.CustomEditor(typeof(Rectangle))]
171  public class CustomEditor : LineBaseEditor
172  {
173  // Use FromSource step mode for rectangles since interpolated looks weird
174  protected override StepModeEnum EditorStepMode { get { return StepModeEnum.FromSource; } }
175  }
176 
177  protected override void OnDrawGizmos()
178  {
179  // Show gizmos if this object is not selected
180  // (SceneGUI will display it otherwise)
181 
182  if (Application.isPlaying)
183  {
184  return;
185  }
186 
187  if (UnityEditor.Selection.activeGameObject == this.gameObject)
188  {
189  return;
190  }
191 
192  // Only draw a gizmo if we don't have a line renderer
193  LineRendererBase lr = gameObject.GetComponent<LineRendererBase>();
194  if (lr != null)
195  {
196  return;
197  }
198 
199  Vector3 firstPos = GetPoint(0);
200  Vector3 lastPos = firstPos;
201  Gizmos.color = Color.Lerp(LineBaseEditor.DefaultDisplayLineColor, Color.clear, 0.25f);
202 
203  for (int i = 1; i < NumPoints; i++)
204  {
205  Vector3 currentPos = GetPoint(i);
206  Gizmos.DrawLine(lastPos, currentPos);
207  lastPos = currentPos;
208  }
209 
210  Gizmos.DrawLine(lastPos, firstPos);
211  }
212 #endif
213  }
214 }
StepModeEnum
Default options for how to generate points in a line renderer
Definition: LineUtility.cs:41
override void SetPointInternal(int pointIndex, Vector3 point)
Definition: Rectangle.cs:103
override float GetUnclampedWorldLengthInternal()
Get the UNCLAMPED world length of the line
Definition: Rectangle.cs:131
static Vector3 InterpolateVectorArray(Vector3[] points, float normalizedLength)
Definition: LineUtility.cs:193
override Vector3 GetPointInternal(int pointIndex)
Get a point based on point index Point index will be pre-clamped
Definition: Rectangle.cs:116
override Vector3 GetUpVectorInternal(float normalizedLength)
Gets the up vector at a normalized length along line (used for rotation)
Definition: Rectangle.cs:145
override Vector3 GetPointInternal(float normalizedDistance)
When we get interpolated points we subdivide the square so our sampling has more to work with ...
Definition: Rectangle.cs:91