Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*copied from complete version in sample project of "https://developer.microsoft.com/en-us/windows/holographic/holograms_210"*/
using UnityEngine;
namespace Academy.HoloToolkit.Unity
{
/// <summary>
/// GazeManager determines the location of the user's gaze, hit position and normals.
/// </summary>
public class GazeManager : Singleton<GazeManager>
{
[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;
/// <summary>
/// Physics.Raycast result is true if it hits a Hologram.
/// </summary>
public bool Hit { get; private set; }
/// <summary>
/// HitInfo property gives access
/// to RaycastHit public members.
/// </summary>
public RaycastHit HitInfo { get; private set; }
/// <summary>
/// Position of the user's gaze.
/// </summary>
public Vector3 Position { get; private set; }
/// <summary>
/// RaycastHit Normal direction.
/// </summary>
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<GazeStabilizer>();
}
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();
}
/// <summary>
/// Calculates the Raycast hit position and normal.
/// </summary>
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;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment