Skip to content

Commit 7584f97

Browse files
committed
Clean up code in StabilizationPlaneModifier to not rely directly on the GazeManager
1 parent 12dd767 commit 7584f97

File tree

1 file changed

+61
-15
lines changed

1 file changed

+61
-15
lines changed

Assets/HoloToolkit/Utilities/Scripts/StabilizationPlaneModifier.cs

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,6 @@ public bool TrackVelocity
8383
/// </summary>
8484
private Vector3 targetOverridePreviousPosition;
8585

86-
private GazeManager gazeManager;
87-
88-
private void Start()
89-
{
90-
gazeManager = GazeManager.Instance;
91-
}
92-
9386
/// <summary>
9487
/// Updates the focus point for every frame after all objects have finished moving.
9588
/// </summary>
@@ -122,6 +115,55 @@ private void OnValidate()
122115
TargetOverride = targetOverride;
123116
}
124117

118+
/// <summary>
119+
/// Gets the origin of the gaze for purposes of placing the stabilization plane
120+
/// </summary>
121+
private Vector3 GazeOrigin
122+
{
123+
get
124+
{
125+
if (GazeManager.Instance != null)
126+
{
127+
return GazeManager.Instance.GazeOrigin;
128+
}
129+
return Camera.main.transform.position;
130+
}
131+
}
132+
133+
/// <summary>
134+
/// Gets the direction of the gaze for purposes of placing the stabilization plane
135+
/// </summary>
136+
private Vector3 GazeNormal
137+
{
138+
get
139+
{
140+
if (GazeManager.Instance != null)
141+
{
142+
return GazeManager.Instance.GazeNormal;
143+
}
144+
return Camera.main.transform.forward;
145+
}
146+
}
147+
148+
/// <summary>
149+
/// Gets the position hit on the object the user is gazing at, if gaze tracking is supported.
150+
/// </summary>
151+
/// <param name="hitPosition">The position at which gaze ray intersects with an object.</param>
152+
/// <returns>True if gaze is supported and an object was hit by gaze, otherwise false.</returns>
153+
private bool TryGetGazeHitPosition(out Vector3 hitPosition)
154+
{
155+
if (GazeManager.Instance != null)
156+
{
157+
hitPosition = GazeManager.Instance.HitPosition;
158+
return true;
159+
}
160+
else
161+
{
162+
hitPosition = Vector3.zero;
163+
return false;
164+
}
165+
}
166+
125167
/// <summary>
126168
/// Configures the stabilization plane to update its position based on an object in the scene.
127169
/// </summary>
@@ -136,22 +178,23 @@ private void ConfigureTransformOverridePlane()
136178
}
137179

138180
// Place the plane at the desired depth in front of the user and billboard it to the gaze origin.
139-
HolographicSettings.SetFocusPointForFrame(planePosition, -gazeManager.GazeNormal, velocity);
181+
HolographicSettings.SetFocusPointForFrame(planePosition, -GazeNormal, velocity);
140182
}
141183

142184
/// <summary>
143185
/// Configures the stabilization plane to update its position based on what your gaze intersects in the scene.
144186
/// </summary>
145187
private void ConfigureGazeManagerPlane()
146188
{
147-
Vector3 gazeOrigin = GazeManager.Instance.GazeOrigin;
148-
Vector3 gazeDirection = GazeManager.Instance.GazeNormal;
189+
Vector3 gazeOrigin = GazeOrigin;
190+
Vector3 gazeDirection = GazeNormal;
149191

150192
// Calculate the delta between gaze origin's position and current hit position. If no object is hit, use default distance.
151193
float focusPointDistance;
152-
if (gazeManager.IsGazingAtObject)
194+
Vector3 gazeHitPosition;
195+
if (TryGetGazeHitPosition(out gazeHitPosition))
153196
{
154-
focusPointDistance = (gazeManager.GazeOrigin - GazeManager.Instance.HitPosition).magnitude;
197+
focusPointDistance = (gazeOrigin - gazeHitPosition).magnitude;
155198
}
156199
else
157200
{
@@ -174,14 +217,17 @@ private void ConfigureGazeManagerPlane()
174217
/// </summary>
175218
private void ConfigureFixedDistancePlane()
176219
{
220+
Vector3 gazeOrigin = GazeOrigin;
221+
Vector3 gazeNormal = GazeNormal;
222+
177223
float lerpPower = DefaultPlaneDistance > currentPlaneDistance ? LerpStabilizationPlanePowerFarther
178224
: LerpStabilizationPlanePowerCloser;
179225

180226
// Smoothly move the focus point from previous hit position to new position.
181227
currentPlaneDistance = Mathf.Lerp(currentPlaneDistance, DefaultPlaneDistance, lerpPower * Time.deltaTime);
182228

183-
planePosition = gazeManager.GazeOrigin + (gazeManager.GazeNormal * currentPlaneDistance);
184-
HolographicSettings.SetFocusPointForFrame(planePosition, -gazeManager.GazeNormal, Vector3.zero);
229+
planePosition = gazeOrigin + (gazeNormal * currentPlaneDistance);
230+
HolographicSettings.SetFocusPointForFrame(planePosition, -gazeNormal, Vector3.zero);
185231
}
186232

187233
/// <summary>
@@ -202,7 +248,7 @@ private void OnDrawGizmos()
202248
{
203249
if (UnityEngine.Application.isPlaying && DrawGizmos)
204250
{
205-
Vector3 focalPlaneNormal = -gazeManager.GazeNormal;
251+
Vector3 focalPlaneNormal = -GazeNormal;
206252
Vector3 planeUp = Vector3.Cross(Vector3.Cross(focalPlaneNormal, Vector3.up), focalPlaneNormal);
207253
Gizmos.matrix = Matrix4x4.TRS(planePosition, Quaternion.LookRotation(focalPlaneNormal, planeUp), new Vector3(4.0f, 3.0f, 0.01f));
208254

0 commit comments

Comments
 (0)