Пример #1
0
            public override unsafe NativeArray <XRRaycastHit> Raycast(
                XRRaycastHit defaultRaycastHit,
                Vector2 screenPoint,
                TrackableType trackableTypeMask,
                Allocator allocator)
            {
                void *hitBuffer;
                int   hitCount, elementSize;

                UnityARCore_raycast_acquireHitResults(
                    screenPoint,
                    trackableTypeMask,
                    out hitBuffer,
                    out hitCount,
                    out elementSize);

                try
                {
                    return(NativeCopyUtility.PtrToNativeArrayWithDefault <XRRaycastHit>(
                               defaultRaycastHit,
                               hitBuffer, elementSize,
                               hitCount, allocator));
                }
                finally
                {
                    UnityARCore_raycast_releaseHitResults(hitBuffer);
                }
            }
            public override unsafe NativeArray <XRRaycastHit> Raycast(
                XRRaycastHit defaultRaycastHit,
                Vector2 screenPoint,
                TrackableType trackableTypeMask,
                Allocator allocator)
            {
                void *hitResults;
                int   count;

                NativeApi.UnityARKit_raycast_acquireHitResults(
                    screenPoint,
                    trackableTypeMask,
                    out hitResults,
                    out count);

                var results = new NativeArray <XRRaycastHit>(count, allocator);

                NativeApi.UnityARKit_raycast_copyAndReleaseHitResults(
                    UnsafeUtility.AddressOf(ref defaultRaycastHit),
                    UnsafeUtility.SizeOf <XRRaycastHit>(),
                    hitResults,
                    results.GetUnsafePtr());

                return(results);
            }
Пример #3
0
            public override NativeArray <XRRaycastHit> Raycast(XRRaycastHit defaultRaycastHit, Vector2 screenPoint,
                                                               TrackableType trackableTypeMask, Allocator allocator)
            {
                var hits = new NativeArray <XRRaycastHit>();

                return(hits);
            }
Пример #4
0
        /// <summary>
        /// Constructor invoked by <see cref="ARRaycastManager.Raycast(Vector2, System.Collections.Generic.List{ARRaycastHit}, TrackableType)"/>
        /// and <see cref="ARRaycastManager.Raycast(Ray, System.Collections.Generic.List{ARRaycastHit}, TrackableType)"/>.
        /// </summary>
        /// <param name="hit">Session-relative raycast hit data.</param>
        /// <param name="distance">The distance, in Unity world space, of the hit.</param>
        /// <param name="transform">The <c>Transform</c> that transforms from session space to world space.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="transform"/> is `null`.</exception>
        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;
        }
Пример #5
0
            public override NativeArray <XRRaycastHit> Raycast(XRRaycastHit defaultRaycastHit, Vector2 screenPoint,
                                                               TrackableType trackableTypeMask, Allocator allocator)
            {
                // ARF normalizes to the Screen dimensions, so convert back to pixels.
                screenPoint.x *= Screen.width;
                screenPoint.y *= Screen.height;

                var camera = MarsRuntimeUtils.GetActiveCamera(true);
                var ray    = camera.ScreenPointToRay(screenPoint);

                return(Raycast(defaultRaycastHit, ray, trackableTypeMask, allocator));
            }
Пример #6
0
            public override NativeArray <XRRaycastHit> Raycast(XRRaycastHit defaultRaycastHit, Ray ray,
                                                               TrackableType trackableTypeMask, Allocator allocator)
            {
                k_Hits.Clear();
                foreach (var marsPlane in m_Planes.Values)
                {
                    var plane = new Plane(marsPlane.pose.up, marsPlane.pose.position);
                    if (plane.Raycast(ray, out var distance))
                    {
                        var worldSpacePoint = ray.GetPoint(distance);
                        var planeSpacePoint =
                            Quaternion.Inverse(marsPlane.pose.rotation) * (worldSpacePoint - marsPlane.pose.position);

                        var hitType = trackableTypeMask & TrackableType.PlaneWithinInfinity;

                        if (marsPlane.vertices != null &&
                            trackableTypeMask.HasFlag(TrackableType.PlaneWithinPolygon) &&
                            GeometryUtils.PointInPolygon(planeSpacePoint, marsPlane.vertices))
                        {
                            hitType |= TrackableType.PlaneWithinPolygon;
                        }

                        if (trackableTypeMask.HasFlag(TrackableType.PlaneWithinBounds) &&
                            PointInExtents(planeSpacePoint, marsPlane.extents))
                        {
                            hitType |= TrackableType.PlaneWithinBounds;
                        }

                        if (hitType != 0)
                        {
                            k_Hits.Add(new XRRaycastHit
                            {
                                trackableId = new TrackableId(),
                                pose        = new Pose(worldSpacePoint, marsPlane.pose.rotation),
                                distance    = distance,
                                hitType     = hitType
                            });
                        }
                    }
                }

                if (k_Hits.Count > 1)
                {
                    k_Hits.Sort((a, b) => a.distance.CompareTo(b.distance));
                }

                return(new NativeArray <XRRaycastHit>(k_Hits.ToArray(), allocator));
            }
Пример #7
0
            public void Execute()
            {
                var hitIndex = 0;

                for (int i = 0; i < points.Length; ++i)
                {
                    if (infos[i].cosineAngleWithRay >= cosineThreshold)
                    {
                        hits[hitIndex++] = new XRRaycastHit(
                            trackableId,
                            new Pose(pose.rotation * points[i] + pose.position, Quaternion.identity),
                            infos[i].distance,
                            TrackableType.FeaturePoint);
                    }
                }

                count[0] = hitIndex;
            }
        /// <summary>
        /// Performs a raycast against all currently tracked planes.
        /// </summary>
        /// <param name="ray">The ray, in Unity world space, to cast.</param>
        /// <param name="trackableTypeMask">A mask of raycast types to perform.</param>
        /// <param name="allocator">The <c>Allocator</c> to use when creating the returned <c>NativeArray</c>.</param>
        /// <returns>
        /// A new <c>NativeArray</c> of raycast results allocated with <paramref name="allocator"/>.
        /// The caller owns the memory and is responsible for calling <c>Dispose</c> on the <c>NativeArray</c>.
        /// </returns>
        /// <seealso cref="ARRaycastManager.Raycast(Ray, List{ARRaycastHit}, TrackableType)"/>
        /// <seealso cref="ARRaycastManager.Raycast(Vector2, List{ARRaycastHit}, TrackableType)"/>
        public NativeArray <XRRaycastHit> Raycast(
            Ray ray,
            TrackableType trackableTypeMask,
            Allocator allocator)
        {
            // No plane types requested; early out.
            if ((trackableTypeMask & TrackableType.Planes) == TrackableType.None)
            {
                return(new NativeArray <XRRaycastHit>(0, allocator));
            }

            var trackableCollection = trackables;

            // Allocate a buffer that is at least large enough to contain a hit against every plane
            var hitBuffer = new NativeArray <XRRaycastHit>(trackableCollection.count, Allocator.Temp);

            try
            {
                int count = 0;
                foreach (var plane in trackableCollection)
                {
                    TrackableType trackableTypes = TrackableType.None;

                    var   normal        = plane.transform.localRotation * Vector3.up;
                    var   infinitePlane = new Plane(normal, plane.transform.localPosition);
                    float distance;
                    if (!infinitePlane.Raycast(ray, out distance))
                    {
                        continue;
                    }

                    // Pose in session space
                    var pose = new Pose(
                        ray.origin + ray.direction * distance,
                        plane.transform.localRotation);

                    if ((trackableTypeMask & TrackableType.PlaneWithinInfinity) != TrackableType.None)
                    {
                        trackableTypes |= TrackableType.PlaneWithinInfinity;
                    }

                    // To test the rest, we need the intersection point in plane space
                    var hitPositionPlaneSpace3d = Quaternion.Inverse(plane.transform.localRotation) * (pose.position - plane.transform.localPosition);
                    var hitPositionPlaneSpace   = new Vector2(hitPositionPlaneSpace3d.x, hitPositionPlaneSpace3d.z);

                    var estimatedOrWithinBounds = TrackableType.PlaneWithinBounds | TrackableType.PlaneEstimated;
                    if ((trackableTypeMask & estimatedOrWithinBounds) != TrackableType.None)
                    {
                        var differenceFromCenter = hitPositionPlaneSpace - plane.centerInPlaneSpace;
                        if ((Mathf.Abs(differenceFromCenter.x) <= plane.extents.x) &&
                            (Mathf.Abs(differenceFromCenter.y) <= plane.extents.y))
                        {
                            trackableTypes |= (estimatedOrWithinBounds & trackableTypeMask);
                        }
                    }

                    if ((trackableTypeMask & TrackableType.PlaneWithinPolygon) != TrackableType.None)
                    {
                        if (WindingNumber(hitPositionPlaneSpace, plane.boundary) != 0)
                        {
                            trackableTypes |= TrackableType.PlaneWithinPolygon;
                        }
                    }

                    if (trackableTypes != TrackableType.None)
                    {
                        hitBuffer[count++] = new XRRaycastHit(
                            plane.trackableId,
                            pose,
                            distance,
                            trackableTypes);
                    }
                }

                // Finally, copy to return value
                var hitResults = new NativeArray <XRRaycastHit>(count, allocator);
                NativeArray <XRRaycastHit> .Copy(hitBuffer, hitResults, count);

                return(hitResults);
            }
            finally
            {
                hitBuffer.Dispose();
            }
        }