1- using UnityEngine ;
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 ;
25using HoloToolkit . Unity ;
36using System . Collections . Generic ;
47using HoloToolkit . Unity . InputModule ;
58
6- /// <summary>
7- /// mananger all lines in the scene
8- /// </summary>
9- public class LineManager : Singleton < LineManager > , IGeometry
9+ namespace HoloToolkit . Examples . GazeRuler
1010{
11- // save all lines in scene
12- private Stack < Line > Lines = new Stack < Line > ( ) ;
13-
14- private Point lastPoint ;
15-
16- private const float defaultLineScale = 0.005f ;
17-
18- // place point and lines
19- public void AddPoint ( GameObject LinePrefab , GameObject PointPrefab , GameObject TextPrefab )
11+ /// <summary>
12+ /// mananger all lines in the scene
13+ /// </summary>
14+ public class LineManager : Singleton < LineManager > , IGeometry
2015 {
16+ // save all lines in scene
17+ private Stack < Line > Lines = new Stack < Line > ( ) ;
2118
22- var hitPoint = GazeManager . Instance . HitPosition ;
19+ private Point lastPoint ;
2320
24- var point = ( GameObject ) Instantiate ( PointPrefab , hitPoint , Quaternion . identity ) ;
25- if ( lastPoint != null && lastPoint . IsStart )
26- {
27- var centerPos = ( lastPoint . Position + hitPoint ) * 0.5f ;
21+ private const float defaultLineScale = 0.005f ;
2822
29- var directionFromCamera = centerPos - Camera . main . transform . position ;
23+ // place point and lines
24+ public void AddPoint ( GameObject LinePrefab , GameObject PointPrefab , GameObject TextPrefab )
25+ {
3026
31- var distanceA = Vector3 . Distance ( lastPoint . Position , Camera . main . transform . position ) ;
32- var distanceB = Vector3 . Distance ( hitPoint , Camera . main . transform . position ) ;
27+ Vector3 hitPoint = GazeManager . Instance . HitPosition ;
3328
34- Debug . Log ( "A: " + distanceA + ",B: " + distanceB ) ;
35- Vector3 direction ;
36- if ( distanceB > distanceA || ( distanceA > distanceB && distanceA - distanceB < 0.1 ) )
29+ GameObject point = ( GameObject ) Instantiate ( PointPrefab , hitPoint , Quaternion . identity ) ;
30+ if ( lastPoint != null && lastPoint . IsStart )
3731 {
38- direction = hitPoint - lastPoint . Position ;
32+ Vector3 centerPos = ( lastPoint . Position + hitPoint ) * 0.5f ;
33+
34+ Vector3 directionFromCamera = centerPos - Camera . main . transform . position ;
35+
36+ float distanceA = Vector3 . Distance ( lastPoint . Position , Camera . main . transform . position ) ;
37+ float distanceB = Vector3 . Distance ( hitPoint , Camera . main . transform . position ) ;
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+
3984 }
4085 else
4186 {
42- direction = lastPoint . Position - hitPoint ;
87+ lastPoint = new Point
88+ {
89+ Position = hitPoint ,
90+ Root = point ,
91+ IsStart = true
92+ } ;
4393 }
94+ }
4495
45- var distance = Vector3 . Distance ( lastPoint . Position , hitPoint ) ;
46- var line = ( GameObject ) Instantiate ( LinePrefab , centerPos , Quaternion . LookRotation ( direction ) ) ;
47- line . transform . localScale = new Vector3 ( distance , defaultLineScale , defaultLineScale ) ;
48- line . transform . Rotate ( Vector3 . down , 90f ) ;
49-
50- var normalV = Vector3 . Cross ( direction , directionFromCamera ) ;
51- var normalF = Vector3 . Cross ( direction , normalV ) * - 1 ;
52- var tip = ( GameObject ) Instantiate ( TextPrefab , centerPos , Quaternion . LookRotation ( normalF ) ) ;
53-
54- //unit is meter
55- tip . transform . Translate ( Vector3 . up * 0.05f ) ;
56- tip . GetComponent < TextMesh > ( ) . text = distance + "m" ;
57-
58- var root = new GameObject ( ) ;
59- lastPoint . Root . transform . parent = root . transform ;
60- line . transform . parent = root . transform ;
61- point . transform . parent = root . transform ;
62- tip . transform . parent = root . transform ;
63-
64- Lines . Push ( new Line
65- {
66- Start = lastPoint . Position ,
67- End = hitPoint ,
68- Root = root ,
69- Distance = distance
70- } ) ;
71-
72- lastPoint = new Point
73- {
74- Position = hitPoint ,
75- Root = point ,
76- IsStart = false
77- } ;
7896
79- }
80- else
97+ // delete latest placed lines
98+ public void Delete ( )
8199 {
82- lastPoint = new Point
100+ if ( Lines != null && Lines . Count > 0 )
83101 {
84- Position = hitPoint ,
85- Root = point ,
86- IsStart = true
87- } ;
88- }
89- }
90-
102+ Line lastLine = Lines . Pop ( ) ;
103+ Destroy ( lastLine . Root ) ;
104+ }
91105
92- // delete latest placed lines
93- public void Delete ( )
94- {
95- if ( Lines != null && Lines . Count > 0 )
96- {
97- var lastLine = Lines . Pop ( ) ;
98- Destroy ( lastLine . Root ) ;
99106 }
100107
101- }
102-
103- // delete all lines in the scene
104- public void Clear ( )
105- {
106- if ( Lines != null && Lines . Count > 0 )
108+ // delete all lines in the scene
109+ public void Clear ( )
107110 {
108- while ( Lines . Count > 0 )
111+ if ( Lines != null && Lines . Count > 0 )
109112 {
110- var lastLine = Lines . Pop ( ) ;
111- Destroy ( lastLine . Root ) ;
113+ while ( Lines . Count > 0 )
114+ {
115+ Line lastLine = Lines . Pop ( ) ;
116+ Destroy ( lastLine . Root ) ;
117+ }
112118 }
113119 }
114- }
115120
116- // reset current unfinished line
117- public void Reset ( )
118- {
119- if ( lastPoint != null && lastPoint . IsStart )
121+ // reset current unfinished line
122+ public void Reset ( )
120123 {
121- Destroy ( lastPoint . Root ) ;
122- lastPoint = null ;
124+ if ( lastPoint != null && lastPoint . IsStart )
125+ {
126+ Destroy ( lastPoint . Root ) ;
127+ lastPoint = null ;
128+ }
123129 }
124130 }
125- }
126131
127132
128- public struct Line
129- {
130- public Vector3 Start { get ; set ; }
133+ public struct Line
134+ {
135+ public Vector3 Start { get ; set ; }
131136
132- public Vector3 End { get ; set ; }
137+ public Vector3 End { get ; set ; }
133138
134- public GameObject Root { get ; set ; }
139+ public GameObject Root { get ; set ; }
135140
136- public float Distance { get ; set ; }
141+ public float Distance { get ; set ; }
142+ }
137143}
0 commit comments