22// Licensed under the MIT License. See LICENSE in the project root for license information.
33
44using UnityEngine ;
5- using UnityEngine . VR . WSA ;
65
76namespace HoloToolkit . Unity
87{
@@ -11,20 +10,51 @@ namespace HoloToolkit.Unity
1110 /// </summary>
1211 public partial class GazeManager : Singleton < GazeManager >
1312 {
13+ /// <summary>
14+ /// Maximum gaze distance, in meters, for calculating a hit from a GameOjects Collider.
15+ /// </summary>
1416 [ Tooltip ( "Maximum gaze distance, in meters, for calculating a hit." ) ]
1517 public float MaxGazeDistance = 15.0f ;
1618
19+ /// <summary>
20+ /// Select the layers raycast should target.
21+ /// </summary>
1722 [ Tooltip ( "Select the layers raycast should target." ) ]
1823 public LayerMask RaycastLayerMask = Physics . DefaultRaycastLayers ;
1924
25+ /// <summary>
26+ /// Checking enables SetFocusPointForFrame to set the stabilization plane.
27+ /// </summary>
28+ [ Tooltip ( "Checking enables SetFocusPointForFrame to set the stabilization plane." ) ]
29+ public bool SetStabilizationPlane = true ;
30+
31+ /// <summary>
32+ /// Lerp speed when moving focus point closer.
33+ /// </summary>
34+ [ Tooltip ( "Lerp speed when moving focus point closer." ) ]
35+ public float LerpStabilizationPlanePowerCloser = 4.0f ;
36+
37+ /// <summary>
38+ /// Lerp speed when moving focus point farther away.
39+ /// </summary>
40+ [ Tooltip ( "Lerp speed when moving focus point farther away." ) ]
41+ public float LerpStabilizationPlanePowerFarther = 7.0f ;
42+
43+ /// <summary>
44+ /// Use built in gaze stabilization that utilizes gavity wells.
45+ /// Change to false if you wish to use your own gabilization calculation
46+ /// and extend this class.
47+ /// </summary>
48+ [ Tooltip ( "Use built in gaze stabilization that utilizes gavity wells." ) ]
49+ public bool UseBuiltInGazeStabilization = true ;
50+
2051 /// <summary>
2152 /// Physics.Raycast result is true if it hits a hologram.
2253 /// </summary>
2354 public bool Hit { get ; private set ; }
2455
2556 /// <summary>
26- /// HitInfo property gives access
27- /// to RaycastHit public members.
57+ /// HitInfo property gives access to RaycastHit public members.
2858 /// </summary>
2959 public RaycastHit HitInfo { get ; private set ; }
3060
@@ -43,21 +73,35 @@ public partial class GazeManager : Singleton<GazeManager>
4373 /// </summary>
4474 public GameObject FocusedObject { get ; private set ; }
4575
46- [ Tooltip ( "Checking enables SetFocusPointForFrame to set the stabilization plane." ) ]
47- public bool SetStabilizationPlane = true ;
48- [ Tooltip ( "Lerp speed when moving focus point closer." ) ]
49- public float LerpStabilizationPlanePowerCloser = 4.0f ;
50- [ Tooltip ( "Lerp speed when moving focus point farther away." ) ]
51- public float LerpStabilizationPlanePowerFarther = 7.0f ;
76+ /// <summary>
77+ /// Helper class that stabilizes gaze using gravity wells
78+ /// </summary>
79+ public GazeStabilizer GazeStabilization { get ; private set ; }
5280
5381 private Vector3 gazeOrigin ;
5482 private Vector3 gazeDirection ;
83+ private Quaternion gazeRotation ;
5584 private float lastHitDistance = 15.0f ;
5685
86+ private void Awake ( )
87+ {
88+ if ( UseBuiltInGazeStabilization )
89+ {
90+ GazeStabilization = gameObject . GetComponent < GazeStabilizer > ( ) ??
91+ gameObject . AddComponent < GazeStabilizer > ( ) ;
92+ }
93+ }
94+
5795 private void Update ( )
5896 {
5997 gazeOrigin = Camera . main . transform . position ;
6098 gazeDirection = Camera . main . transform . forward ;
99+ gazeRotation = Camera . main . transform . rotation ;
100+
101+ if ( GazeStabilization != null )
102+ {
103+ GazeStabilization . UpdateHeadStability ( gazeOrigin , gazeRotation ) ;
104+ }
61105
62106 UpdateRaycast ( ) ;
63107 UpdateStabilizationPlane ( ) ;
@@ -70,13 +114,18 @@ private void UpdateRaycast()
70114 {
71115 // Get the raycast hit information from Unity's physics system.
72116 RaycastHit hitInfo ;
73- Hit = Physics . Raycast ( gazeOrigin ,
74- gazeDirection ,
75- out hitInfo ,
76- MaxGazeDistance ,
77- RaycastLayerMask ) ;
117+
118+ if ( GazeStabilization != null )
119+ {
120+ Hit = Physics . Raycast ( GazeStabilization . StableHeadRay , out hitInfo , MaxGazeDistance , RaycastLayerMask ) ;
121+ }
122+ else
123+ {
124+ Hit = Physics . Raycast ( gazeOrigin , gazeDirection , out hitInfo , MaxGazeDistance , RaycastLayerMask ) ;
125+ }
78126
79127 GameObject oldFocusedObject = FocusedObject ;
128+
80129 // Update the HitInfo property so other classes can use this hit information.
81130 HitInfo = hitInfo ;
82131
@@ -127,7 +176,7 @@ private void UpdateStabilizationPlane()
127176 }
128177 }
129178
130- if ( StabilizationPlaneModifier . Instance )
179+ if ( StabilizationPlaneModifier . Instance != null )
131180 {
132181 StabilizationPlaneModifier . Instance . SetStabilizationPlane = SetStabilizationPlane ;
133182 }
0 commit comments