8 #if UNITY_2017_2_OR_NEWER 22 [Tooltip(
"Checking enables SetFocusPointForFrame to set the stabilization plane.")]
23 public bool SetStabilizationPlane =
true;
25 [Tooltip(
"When lerping, use unscaled time. This is useful for games that have a pause mechanism or otherwise adjust the game timescale.")]
26 public bool UseUnscaledTime =
true;
28 [Tooltip(
"Lerp speed when moving focus point closer.")]
29 public float LerpStabilizationPlanePowerCloser = 4.0f;
31 [Tooltip(
"Lerp speed when moving focus point farther away.")]
32 public float LerpStabilizationPlanePowerFarther = 7.0f;
34 [SerializeField, Tooltip(
"Used to temporarily override the location of the stabilization plane.")]
35 private Transform targetOverride;
36 public Transform TargetOverride
40 return targetOverride;
44 if (targetOverride != value)
46 targetOverride = value;
49 targetOverridePreviousPosition = targetOverride.position;
55 [SerializeField, Tooltip(
"Keeps track of position-based velocity for the target object.")]
56 private bool trackVelocity;
57 public bool TrackVelocity
65 trackVelocity = value;
68 targetOverridePreviousPosition = TargetOverride.position;
73 [Tooltip(
"Use the GazeManager class to set the plane to the gazed upon hologram. If disabled, the plane will always be at a constant distance.")]
74 public bool UseGazeManager =
true;
76 [Tooltip(
"Default distance to set plane if plane is gaze-locked or if no object is hit.")]
77 public float DefaultPlaneDistance = 2.0f;
79 [Tooltip(
"Visualize the plane at runtime.")]
85 private Vector3 planePosition;
91 private float currentPlaneDistance = 4.0f;
96 private Vector3 targetOverridePreviousPosition;
101 private void LateUpdate()
103 if (SetStabilizationPlane)
105 float deltaTime = UseUnscaledTime
106 ? Time.unscaledDeltaTime
109 if (TargetOverride != null)
111 ConfigureTransformOverridePlane(deltaTime);
113 else if (UseGazeManager)
115 ConfigureGazeManagerPlane(deltaTime);
119 ConfigureFixedDistancePlane(deltaTime);
128 private void OnValidate()
130 TrackVelocity = trackVelocity;
131 TargetOverride = targetOverride;
137 private Vector3 GazeOrigin
152 private Vector3 GazeNormal
169 private bool TryGetGazeHitPosition(out Vector3 hitPosition)
176 hitPosition = Vector3.zero;
183 private void ConfigureTransformOverridePlane(
float deltaTime)
185 planePosition = TargetOverride.position;
187 Vector3 velocity = Vector3.zero;
190 velocity = UpdateVelocity(deltaTime);
195 HolographicSettings.SetFocusPointForFrame(planePosition, -GazeNormal, velocity);
202 private void ConfigureGazeManagerPlane(
float deltaTime)
204 Vector3 gazeOrigin = GazeOrigin;
205 Vector3 gazeDirection = GazeNormal;
208 float focusPointDistance;
209 Vector3 gazeHitPosition;
210 if (TryGetGazeHitPosition(out gazeHitPosition))
212 focusPointDistance = (gazeOrigin - gazeHitPosition).magnitude;
216 focusPointDistance = DefaultPlaneDistance;
219 float lerpPower = focusPointDistance > currentPlaneDistance ? LerpStabilizationPlanePowerFarther
220 : LerpStabilizationPlanePowerCloser;
223 currentPlaneDistance = Mathf.Lerp(currentPlaneDistance, focusPointDistance, lerpPower * deltaTime);
225 planePosition = gazeOrigin + (gazeDirection * currentPlaneDistance);
228 HolographicSettings.SetFocusPointForFrame(planePosition, -gazeDirection, Vector3.zero);
235 private void ConfigureFixedDistancePlane(
float deltaTime)
237 Vector3 gazeOrigin = GazeOrigin;
238 Vector3 gazeNormal = GazeNormal;
240 float lerpPower = DefaultPlaneDistance > currentPlaneDistance ? LerpStabilizationPlanePowerFarther
241 : LerpStabilizationPlanePowerCloser;
244 currentPlaneDistance = Mathf.Lerp(currentPlaneDistance, DefaultPlaneDistance, lerpPower * deltaTime);
246 planePosition = gazeOrigin + (gazeNormal * currentPlaneDistance);
248 HolographicSettings.SetFocusPointForFrame(planePosition, -gazeNormal, Vector3.zero);
255 private Vector3 UpdateVelocity(
float deltaTime)
258 Vector3 velocity = (TargetOverride.position - targetOverridePreviousPosition) / deltaTime;
259 targetOverridePreviousPosition = TargetOverride.position;
266 private void OnDrawGizmos()
268 if (Application.isPlaying && DrawGizmos)
270 Vector3 focalPlaneNormal = -GazeNormal;
271 Vector3 planeUp = Vector3.Cross(Vector3.Cross(focalPlaneNormal, Vector3.up), focalPlaneNormal);
272 Gizmos.matrix = Matrix4x4.TRS(planePosition, Quaternion.LookRotation(focalPlaneNormal, planeUp),
new Vector3(4.0f, 3.0f, 0.01f));
274 Color gizmoColor = Color.magenta;
276 Gizmos.color = gizmoColor;
278 Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
279 Gizmos.DrawCube(Vector3.zero, Vector3.one);