using System; using UnityEngine.XR.ARSubsystems; namespace UnityEngine.XR.ARFoundation { /// /// Represents the result of a raycast intersection with a trackable. /// public struct ARRaycastHit : IEquatable, IComparable { /// /// Constructor invoked by /// and . /// /// Session-relative raycast hit data. /// The distance, in Unity world space, of the hit. /// The Transform that transforms from session space to world space. /// Thrown if is `null`. [Obsolete("Use ARRaycastHit(XRRaycastHit, float, Transform, ARTrackable) instead. (2020-10-09)")] public ARRaycastHit(XRRaycastHit hit, float distance, Transform transform) { if (transform == null) throw new ArgumentNullException(nameof(transform)); m_Hit = hit; this.distance = distance; m_Transform = transform; trackable = null; } /// /// Constructor invoked by /// and . /// /// Session-relative raycast hit data. /// The distance, in Unity world space, of the hit. /// The Transform that transforms from session space to world space. /// The trackable that was hit by this raycast, or `null` if no trackable was hit. /// Thrown if is `null`. public ARRaycastHit(XRRaycastHit hit, float distance, Transform transform, ARTrackable trackable) { if (transform == null) throw new ArgumentNullException(nameof(transform)); m_Hit = hit; this.distance = distance; m_Transform = transform; this.trackable = trackable; } /// /// The distance, in Unity world space, from the ray origin to the intersection point. /// public float distance { get; } /// /// The type of trackable hit by the raycast. /// public TrackableType hitType => m_Hit.hitType; /// /// The Pose, in Unity world space, of the intersection point. /// public Pose pose => m_Transform.TransformPose(sessionRelativePose); /// /// The session-unique identifier for the trackable that was hit. /// public TrackableId trackableId => m_Hit.trackableId; /// /// The Pose, in local (session) space, of the intersection point. /// public Pose sessionRelativePose => m_Hit.pose; /// /// The distance, in local (session) space, from the ray origin to the intersection point. /// public float sessionRelativeDistance => m_Hit.distance; /// /// The that this raycast hit, or `null` if no was hit. /// See to determine what type of trackable, if any, was hit. /// public ARTrackable trackable { get; } /// /// Generates a hash suitable for use with containers like `HashSet` and `Dictionary`. /// /// A hash code generated from this object's fields. public override int GetHashCode() => HashCodeUtil.Combine( m_Hit.GetHashCode(), distance.GetHashCode(), HashCodeUtil.ReferenceHash(m_Transform), HashCodeUtil.ReferenceHash(trackable)); /// /// Tests for equality. /// /// The `object` to compare against. /// `True` if is of type and /// also returns `true`; otherwise `false`. public override bool Equals(object obj) => obj is ARRaycastHit other && Equals(other); /// /// Tests for equality. /// /// The other to compare against. /// `True` if every field in is equal to this , otherwise false. public bool Equals(ARRaycastHit other) { return m_Hit.Equals(other.m_Hit) && distance.Equals(other.distance) && m_Transform == other.m_Transform && trackable == other.trackable; } /// /// Tests for equality. Same as . /// /// The left-hand side of the comparison. /// The right-hand side of the comparison. /// `True` if is equal to , otherwise `false`. public static bool operator ==(ARRaycastHit lhs, ARRaycastHit rhs) => lhs.Equals(rhs); /// /// Tests for inequality. Same as `!`. /// /// The left-hand side of the comparison. /// The right-hand side of the comparison. /// `True` if is not equal to , otherwise `false`. public static bool operator !=(ARRaycastHit lhs, ARRaycastHit rhs) => !lhs.Equals(rhs); /// /// Used for sorting two raycast hits by distance. Uses [CompareTo](https://docs.microsoft.com/en-us/dotnet/api/system.icomparable.compareto?view=netframework-4.8) /// on the raycasts' distance properties. /// /// The other to compare against. /// A value less than zero if this raycast hit is closer than , zero if the distances /// are equal, and a positive value if is closer. public int CompareTo(ARRaycastHit other) => m_Hit.distance.CompareTo(other.m_Hit.distance); XRRaycastHit m_Hit; Transform m_Transform; } }