private void _DrawARLogo(Touch touch) { List <ARHitResult> hitResults = ARFrame.HitTest(touch); ARHitResult hitResult = null; ARTrackable trackable = null; Boolean hasHitFlag = false; ARDebug.LogInfo("_DrawARLogo hitResults count {0}", hitResults.Count); foreach (ARHitResult singleHit in hitResults) { trackable = singleHit.GetTrackable(); ARDebug.LogInfo("_DrawARLogo GetTrackable {0}", singleHit.GetTrackable()); if ((trackable is ARPlane && ((ARPlane)trackable).IsPoseInPolygon(singleHit.HitPose)) || (trackable is ARPoint)) { hitResult = singleHit; hasHitFlag = true; if (trackable is ARPlane) { break; } } } if (hasHitFlag != true) { ARDebug.LogInfo("_DrawARLogo can't hit!"); return; } if (addedAnchors.Count > 16) { ARAnchor toRemove = addedAnchors[0]; toRemove.Detach(); addedAnchors.RemoveAt(0); } GameObject prefab; trackable = hitResult.GetTrackable(); if (trackable is ARPlane) { prefab = arDiscoveryLogoPlanePrefabs; } else { prefab = arDiscoveryLogoPointPrefabs; } /* * ARAnchor anchor = hitResult.CreateAnchor(); * var logoObject = Instantiate(prefab, anchor.GetPose().position, anchor.GetPose().rotation); * logoObject.GetComponent<ARDiscoveryLogoVisualizer>().Initialize(anchor); * addedAnchors.Add(anchor); */ }
private ARHitResult HitTest4Result(ARFrame frame, ARCamera camera, MotionEvent mEvent) { ARHitResult hitResult = null; IList <ARHitResult> hitTestResults = frame.HitTest(mEvent); for (int i = 0; i < hitTestResults.Count; i++) { // Determine whether the hit point is within the plane polygon. ARHitResult hitResultTemp = hitTestResults.ElementAt(i); if (hitResultTemp == null) { continue; } IARTrackable trackable = hitResultTemp.Trackable; Java.Lang.Object PointOrPlane = null; bool isPlanHitJudge = false; bool isPointHitJudge = false; try { PointOrPlane = trackable.JavaCast <ARPlane>(); isPlanHitJudge = PointOrPlane.GetType() == typeof(ARPlane) && ((ARPlane)PointOrPlane).IsPoseInPolygon(hitResultTemp.HitPose) && (CalculateDistanceToPlane(hitResultTemp.HitPose, camera.Pose) > 0); } catch (Exception e) { PointOrPlane = trackable.JavaCast <ARPoint>(); isPointHitJudge = PointOrPlane.GetType() == typeof(ARPoint) && ((ARPoint)PointOrPlane).GetOrientationMode() == ARPoint.OrientationMode.EstimatedSurfaceNormal; }; // Determine whether the point cloud is clicked and whether the point faces the camera. // Select points on the plane preferentially. if (isPlanHitJudge) { hitResult = hitResultTemp; } } return(hitResult); }
private void DoWhenEventTypeSingleTap(ARHitResult hitResult) { // The hit results are sorted by distance. Only the nearest hit point is valid. // Set the number of stored objects to 10 to avoid the overload of rendering and AR Engine. if (mVirtualObjects.Count >= 16) { mVirtualObjects.ElementAt(0).GetAnchor().Detach(); mVirtualObjects.RemoveAt(0); } IARTrackable currentTrackable = hitResult.Trackable; Java.Lang.Object PointOrPlane = null; bool isPlanHitJudge = false; bool isPointHitJudge = false; try { PointOrPlane = currentTrackable.JavaCast <ARPlane>(); isPlanHitJudge = PointOrPlane.GetType() == typeof(ARPlane); } catch (Exception e) { PointOrPlane = currentTrackable.JavaCast <ARPoint>(); isPointHitJudge = PointOrPlane.GetType() == typeof(ARPoint); }; if (isPointHitJudge) { mVirtualObjects.Add(new VirtualObject(hitResult.CreateAnchor(), BLUE_COLORS)); } else if (isPlanHitJudge) { mVirtualObjects.Add(new VirtualObject(hitResult.CreateAnchor(), GREEN_COLORS)); } else { Log.Info(TAG, "Hit result is not plane or point."); } }
private void HandleGestureEvent(ARFrame arFrame, ARCamera arCamera, float[] projectionMatrix, float[] viewMatrix) { GestureEvent mEvent = mQueuedSingleTaps.Poll().JavaCast <GestureEvent>(); if (mEvent == null) { return; } // Do not perform anything when the object is not tracked. if (arCamera.TrackingState != ARTrackableTrackingState.Tracking) { return; } int eventType = mEvent.GetType(); switch (eventType) { case GestureEvent.GESTURE_EVENT_TYPE_DOUBLETAP: DoWhenEventTypeDoubleTap(viewMatrix, projectionMatrix, mEvent); break; case GestureEvent.GESTURE_EVENT_TYPE_SCROLL: { if (mSelectedObj == null) { break; } ARHitResult hitResult = HitTest4Result(arFrame, arCamera, mEvent.GetEventSecond()); if (hitResult != null) { mSelectedObj.SetAnchor(hitResult.CreateAnchor()); } break; } case GestureEvent.GESTURE_EVENT_TYPE_SINGLETAPCONFIRMED: { // Do not perform anything when an object is selected. if (mSelectedObj != null) { mSelectedObj.SetIsSelected(false); mSelectedObj = null; } MotionEvent tap = mEvent.GetEventFirst(); ARHitResult hitResult = null; hitResult = HitTest4Result(arFrame, arCamera, tap); if (hitResult == null) { break; } DoWhenEventTypeSingleTap(hitResult); break; } default: Log.Info(TAG, "Unknown motion event type, and do nothing."); break; } }