using UnityEngine;
namespace Academy.HoloToolkit.Unity
{
///
/// GazeManager determines the location of the user's gaze, hit position and normals.
///
public class GazeManager : Singleton
{
[Tooltip("Maximum gaze distance for calculating a hit.")]
public float MaxGazeDistance = 5.0f;
[Tooltip("Select the layers raycast should target.")]
public LayerMask RaycastLayerMask = Physics.DefaultRaycastLayers;
///
/// Physics.Raycast result is true if it hits a Hologram.
///
public bool Hit { get; private set; }
///
/// HitInfo property gives access
/// to RaycastHit public members.
///
public RaycastHit HitInfo { get; private set; }
///
/// Position of the user's gaze.
///
public Vector3 Position { get; private set; }
///
/// RaycastHit Normal direction.
///
public Vector3 Normal { get; private set; }
private GazeStabilizer gazeStabilizer;
private Vector3 gazeOrigin;
private Vector3 gazeDirection;
void Awake()
{
/* TODO: DEVELOPER CODING EXERCISE 3.a */
// 3.a: GetComponent GazeStabilizer and assign it to gazeStabilizer.
gazeStabilizer = GetComponent();
}
private void Update()
{
// 2.a: Assign Camera's main transform position to gazeOrigin.
gazeOrigin = Camera.main.transform.position;
// 2.a: Assign Camera's main transform forward to gazeDirection.
gazeDirection = Camera.main.transform.forward;
// 3.a: Using gazeStabilizer, call function UpdateHeadStability.
// Pass in gazeOrigin and Camera's main transform rotation.
gazeStabilizer.UpdateHeadStability(gazeOrigin, Camera.main.transform.rotation);
// 3.a: Using gazeStabilizer, get the StableHeadPosition and
// assign it to gazeOrigin.
gazeOrigin = gazeStabilizer.StableHeadPosition;
UpdateRaycast();
}
///
/// Calculates the Raycast hit position and normal.
///
private void UpdateRaycast()
{
/* TODO: DEVELOPER CODING EXERCISE 2.a */
// 2.a: Create a variable hitInfo of type RaycastHit.
RaycastHit hitInfo;
// 2.a: Perform a Unity Physics Raycast.
// Collect return value in public property Hit.
// Pass in origin as gazeOrigin and direction as gazeDirection.
// Collect the information in hitInfo.
// Pass in MaxGazeDistance and RaycastLayerMask.
Hit = Physics.Raycast(gazeOrigin,
gazeDirection,
out hitInfo,
MaxGazeDistance,
RaycastLayerMask);
// 2.a: Assign hitInfo variable to the HitInfo public property
// so other classes can access it.
HitInfo = hitInfo;
if (Hit)
{
// If raycast hit a hologram...
// 2.a: Assign property Position to be the hitInfo point.
Position = hitInfo.point;
// 2.a: Assign property Normal to be the hitInfo normal.
Normal = hitInfo.normal;
}
else
{
// If raycast did not hit a hologram...
// Save defaults ...
// 2.a: Assign Position to be gazeOrigin plus MaxGazeDistance times gazeDirection.
Position = gazeOrigin + (gazeDirection * MaxGazeDistance);
// 2.a: Assign Normal to be the user's gazeDirection.
Normal = gazeDirection;
}
}
}
}