Esempio n. 1
0
        /// <inheritdoc />
        public override bool HitTest(Vector2 screenPosition, ARMobileHitType hitType, out ARMobileHitTestResult[] results)
        {
            if (this.currentFrame?.Camera == null ||
                this.currentFrame.Camera.TrackingState != Google.AR.Core.TrackingState.Tracking)
            {
                results = null;
                return(false);
            }

            var resultAnchors = new List <ARMobileHitTestResult>();

            foreach (var hit in this.currentFrame.HitTest(screenPosition.X, screenPosition.Y))
            {
                ARMobileHitType?currentHitType = null;
                var             trackable      = hit.Trackable;

                if ((hitType & ARMobileHitType.FeaturePoint) != 0 &&
                    trackable is ARCore.Point)
                {
                    currentHitType = ARMobileHitType.FeaturePoint;
                }
                else if (trackable is ARCore.Plane)
                {
                    if ((hitType & ARMobileHitType.ExistingPlaneUsingExtent) != 0 &&
                        ((ARCore.Plane)trackable).IsPoseInExtents(hit.HitPose))
                    {
                        currentHitType = ARMobileHitType.ExistingPlaneUsingExtent;
                    }
                    else if ((hitType & ARMobileHitType.ExistingPlaneUsingGeometry) != 0 &&
                             ((ARCore.Plane)trackable).IsPoseInPolygon(hit.HitPose))
                    {
                        currentHitType = ARMobileHitType.ExistingPlaneUsingGeometry;
                    }
                    else if ((hitType & ARMobileHitType.ExistingPlane) != 0 ||
                             (hitType & ARMobileHitType.EstimatedHorizontalPlane) != 0)
                    {
                        currentHitType = ARMobileHitType.ExistingPlane;
                    }
                }

                if (currentHitType.HasValue)
                {
                    var trackableId = trackable.GetGuid();
                    var hitResult   = new ARMobileHitTestResult()
                    {
                        Distance = hit.Distance,
                        HitType  = currentHitType.Value,
                        Anchor   = this.FindAnchor(trackableId)
                    };
                    hit.HitPose.ToWave(out hitResult.WorldTransform);

                    resultAnchors.Add(hitResult);
                }
            }

            results = resultAnchors.ToArray();
            return(resultAnchors.Count > 0);
        }
        /// <inheritdoc />
        public override bool HitTest(Vector2 screenPosition, ARMobileHitType hitType, out ARMobileHitTestResult[] results)
        {
            var point = new CGPoint(screenPosition.X / this.platform.ScreenWidth, screenPosition.Y / this.platform.ScreenHeight);
            var arHitTestResultType = hitType.ToARKit();

            var arKitResults = this.arkitSession.CurrentFrame?.HitTest(point, arHitTestResultType);

            if (arKitResults != null)
            {
                results = new ARMobileHitTestResult[arKitResults.Length];

                for (int i = 0; i < arKitResults.Length; i++)
                {
                    var arkitResult = arKitResults[i];

                    var hitResult = new ARMobileHitTestResult()
                    {
                        Distance = (float)arkitResult.Distance,
                        HitType  = arkitResult.Type.ToWave(),
                    };

                    arkitResult.WorldTransform.ToWave(out hitResult.WorldTransform);

                    var anchor = arkitResult.Anchor;
                    if (anchor != null)
                    {
                        var anchorId = anchor.Identifier.ToWave();
                        hitResult.Anchor = this.FindAnchor(anchorId);
                    }

                    results[i] = hitResult;
                }

                return(arKitResults.Length > 0);
            }

            results = null;

            return(false);
        }