AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
LineManager.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 UnityEngine;
5 using HoloToolkit.Unity;
6 using System.Collections.Generic;
8 
9 namespace HoloToolkit.Examples.GazeRuler
10 {
14  public class LineManager : Singleton<LineManager>, IGeometry
15  {
16  // save all lines in scene
17  private Stack<Line> Lines = new Stack<Line>();
18 
19  private Point lastPoint;
20 
21  private const float defaultLineScale = 0.005f;
22 
23  // place point and lines
24  public void AddPoint(GameObject LinePrefab, GameObject PointPrefab, GameObject TextPrefab)
25  {
26 
27  Vector3 hitPoint = GazeManager.Instance.HitPosition;
28 
29  GameObject point = (GameObject)Instantiate(PointPrefab, hitPoint, Quaternion.identity);
30  if (lastPoint != null && lastPoint.IsStart)
31  {
32  Vector3 centerPos = (lastPoint.Position + hitPoint) * 0.5f;
33  Vector3 cameraPosition = CameraCache.Main.transform.position;
34  Vector3 directionFromCamera = centerPos - cameraPosition;
35 
36  float distanceA = Vector3.Distance(lastPoint.Position, cameraPosition);
37  float distanceB = Vector3.Distance(hitPoint, cameraPosition);
38 
39  Debug.Log("A: " + distanceA + ",B: " + distanceB);
40  Vector3 direction;
41  if (distanceB > distanceA || (distanceA > distanceB && distanceA - distanceB < 0.1))
42  {
43  direction = hitPoint - lastPoint.Position;
44  }
45  else
46  {
47  direction = lastPoint.Position - hitPoint;
48  }
49 
50  float distance = Vector3.Distance(lastPoint.Position, hitPoint);
51  GameObject line = (GameObject)Instantiate(LinePrefab, centerPos, Quaternion.LookRotation(direction));
52  line.transform.localScale = new Vector3(distance, defaultLineScale, defaultLineScale);
53  line.transform.Rotate(Vector3.down, 90f);
54 
55  Vector3 normalV = Vector3.Cross(direction, directionFromCamera);
56  Vector3 normalF = Vector3.Cross(direction, normalV) * -1;
57  GameObject tip = (GameObject)Instantiate(TextPrefab, centerPos, Quaternion.LookRotation(normalF));
58 
59  //unit is meter
60  tip.transform.Translate(Vector3.up * 0.05f);
61  tip.GetComponent<TextMesh>().text = distance + "m";
62 
63  GameObject root = new GameObject();
64  lastPoint.Root.transform.parent = root.transform;
65  line.transform.parent = root.transform;
66  point.transform.parent = root.transform;
67  tip.transform.parent = root.transform;
68 
69  Lines.Push(new Line
70  {
71  Start = lastPoint.Position,
72  End = hitPoint,
73  Root = root,
74  Distance = distance
75  });
76 
77  lastPoint = new Point
78  {
79  Position = hitPoint,
80  Root = point,
81  IsStart = false
82  };
83 
84  }
85  else
86  {
87  lastPoint = new Point
88  {
89  Position = hitPoint,
90  Root = point,
91  IsStart = true
92  };
93  }
94  }
95 
96 
97  // delete latest placed lines
98  public void Delete()
99  {
100  if (Lines != null && Lines.Count > 0)
101  {
102  Line lastLine = Lines.Pop();
103  Destroy(lastLine.Root);
104  }
105 
106  }
107 
108  // delete all lines in the scene
109  public void Clear()
110  {
111  if (Lines != null && Lines.Count > 0)
112  {
113  while (Lines.Count > 0)
114  {
115  Line lastLine = Lines.Pop();
116  Destroy(lastLine.Root);
117  }
118  }
119  }
120 
121  // reset current unfinished line
122  public void Reset()
123  {
124  if (lastPoint != null && lastPoint.IsStart)
125  {
126  Destroy(lastPoint.Root);
127  lastPoint = null;
128  }
129  }
130  }
131 
132 
133  public struct Line
134  {
135  public Vector3 Start { get; set; }
136 
137  public Vector3 End { get; set; }
138 
139  public GameObject Root { get; set; }
140 
141  public float Distance { get; set; }
142  }
143 }
The gaze manager manages everything related to a gaze ray that can interact with other objects...
Definition: GazeManager.cs:13
Manages all lines in the scene
Definition: LineManager.cs:14
The purpose of this class is to provide a cached reference to the main camera. Calling Camera...
Definition: CameraCache.cs:12
static T Instance
Returns the Singleton instance of the classes type. If no instance is found, then we search for an in...
Definition: Singleton.cs:26
static Camera Main
Returns a cached reference to the main camera and uses Camera.main if it hasn&#39;t been cached yet...
Definition: CameraCache.cs:20
interface for geometry class
Definition: IGeometry.cs:12
void AddPoint(GameObject LinePrefab, GameObject PointPrefab, GameObject TextPrefab)
Definition: LineManager.cs:24
Singleton behaviour class, used for components that should only have one instance.
Definition: Singleton.cs:14