#if BURST_PRESENT using Unity.Burst; #endif using Unity.Mathematics; namespace UnityEngine.XR.Interaction.Toolkit.Utilities { /// /// Provides utility methods for physics calculations in Burst-compiled code. /// #if BURST_PRESENT [BurstCompile] #endif public static class BurstPhysicsUtils { /// /// Computes sphere overlap parameters given the start and end positions of the overlap. /// /// The starting position of the sphere overlap. /// The ending position of the sphere overlap. /// Output parameter containing the normalized overlap direction vector. /// Output parameter containing the square of the magnitude of the overlap vector. /// Output parameter containing the distance of the overlap. #if BURST_PRESENT [BurstCompile] #endif public static void GetSphereOverlapParameters(in Vector3 overlapStart, in Vector3 overlapEnd, out Vector3 normalizedOverlapVector, out float overlapSqrMagnitude, out float overlapDistance) { Vector3 overlapDirectionVector = overlapEnd - overlapStart; overlapSqrMagnitude = math.distancesq(overlapStart, overlapEnd); overlapDistance = math.sqrt(overlapSqrMagnitude); normalizedOverlapVector = overlapDirectionVector / overlapDistance; } /// /// Computes conecast parameters given the angle radius, offset, and direction. /// /// How wide the cone should be at a given distance. /// How far from the origin this conecast will be starting from. /// The maximum distance this conecast will be allowed to travel. /// The direction the conecast is traveling. /// How much to offset the origin of the conecast. /// The maximum radius this conecast should cover. /// The distance this conecast should travel, taking sphere size into account. #if BURST_PRESENT [BurstCompile] #endif public static void GetConecastParameters(float angleRadius, float offset, float maxOffset, in Vector3 direction, out Vector3 originOffset, out float radius, out float castMax) { castMax = math.clamp(offset, 0.125f, maxOffset); radius = angleRadius * (offset + castMax); originOffset = direction * (offset - radius); } /// /// Computes conecast parameters given the angle radius, offset, and direction. This function supports conecasts comprised of multiple segments to support both straight and curved lines. /// /// How wide the cone should be at a given distance. /// How far from the starting point of this segment this conecast will be starting from. /// How far from the origin (segment point 0) this conecast will be starting from. /// The maximum distance this conecast will be allowed to travel relative to the segment. This is typically the segment length. /// The direction the conecast is traveling. /// How much to offset the origin of the conecast to account for the radius. /// The target radius at the end of this segment. /// The distance this conecast should travel, taking sphere size into account. #if BURST_PRESENT [BurstCompile] #endif internal static void GetMultiSegmentConecastParameters(float angleRadius, float segmentOffset, float offsetFromOrigin, float maxOffset, in Vector3 direction, out Vector3 originOffset, out float radius, out float castMax) { castMax = math.clamp(segmentOffset, 0.125f, maxOffset); // If the segment offset cast max will be greater than the total segment length, set the target cast distance to the remaining length of the segment. if (segmentOffset + castMax > maxOffset) castMax = math.clamp(maxOffset - segmentOffset, 0.125f, castMax); radius = angleRadius * (offsetFromOrigin + castMax); originOffset = direction * (segmentOffset - radius); } /// /// Gets the perpendicular distance from the given point to the nearest point on the given line. /// /// The starting point of the line. /// The point to calculate horizontal distance to. /// The direction of the line. /// The horizontal distance from to the nearest point on the line defined by and . #if BURST_PRESENT [BurstCompile] #endif public static void GetConecastOffset(in float3 origin, in float3 conePoint, in float3 direction, out float coneOffset) { var hitToOrigin = conePoint - origin; var distance = math.dot(hitToOrigin, direction); var hitToRay = hitToOrigin - (direction * distance); coneOffset = math.length(hitToRay); } } }