#if BURST_PRESENT using Unity.Burst; #else using System.Runtime.CompilerServices; #endif using Unity.Mathematics; namespace UnityEngine.XR.Interaction.Toolkit.Utilities { /// /// Provides utility functions related to calculations for determining if things can be seen from a viewpoint. /// #if BURST_PRESENT [BurstCompile] #endif public static class BurstGazeUtility { /// /// Checks if a given position is outside of a specific gaze viewpoint. /// /// The position of the viewer /// The direction the viewer is facing /// The position of the object being viewed /// How wide a field of view the viewer has /// Returns if is outside gaze angle threshold. public static bool IsOutsideGaze(in float3 gazePosition, in float3 gazeDirection, in float3 targetPosition, float angleThreshold) { var outsideThreshold = false; var testVector = math.normalize(targetPosition - gazePosition); outsideThreshold = !IsAlignedToGazeForward(gazeDirection, testVector, angleThreshold); return outsideThreshold; } /// /// Checks if a given direction is aligned with a viewer (looking at it). /// /// The direction the viewer is facing /// The direction the target is facing /// How far the viewer and target can diverge and still be considered looking at one another /// Returns if is within gaze angle threshold. public static bool IsAlignedToGazeForward(in float3 gazeDirection, in float3 targetDirection, float angleThreshold) { var insideThreshold = false; var angleThresholdConvertedToDot = math.cos(math.radians(angleThreshold)); var angularComparison = math.dot(targetDirection, gazeDirection); insideThreshold = angularComparison > angleThresholdConvertedToDot; return insideThreshold; } /// /// Checks if a given position is outside of a given view range /// /// The position of the viewer /// The position of the target /// How far away a target can be before it is outside the viewing range /// Returns if is outside the gaze distance threshold. public static bool IsOutsideDistanceRange(in float3 gazePosition, in float3 targetPosition, float distanceThreshold) { return math.length(targetPosition - gazePosition) > distanceThreshold; } } }