/// <summary> /// If the session is <see cref="isReadyForInput"/> this method checks every frame (<see cref="Update"/>) for a new touch input. /// As soon as there is a touch recognized the <see cref="prefabInstance"/> gets posed at the <see cref="RaycastHit"/>. /// </summary> private void inputToAnchor() { if (isReadyForInput) { const TrackableType trackableTypes = TrackableType.FeaturePoint | TrackableType.PlaneWithinPolygon; if (Input.touchCount == 0) { return; } var touch = Input.GetTouch(0); if (touch.phase != TouchPhase.Began) { return; } if (isTouchOverUIElement(touch)) { return; } var hits = new List <ARRaycastHit>(); aRRaycastManager.Raycast(touch.position, hits, trackableTypes); bool inputFound = hits.Count > 0; if (inputFound) { createOrUpdatePrefabInstance(hits[0].pose); return; } } }
/// <summary> /// Casts a ray originating from <paramref name="screenPoint"/> against trackables specified with <paramref name="trackableTypeMask"/>. /// </summary> /// <param name="screenPoint">A point on the screen in normalized screen coordinates (0, 0) - (1, 1)</param> /// <param name="trackableTypeMask">The types of trackables to test for ray intersections.</param> /// <param name="allocator">The <c>Allocator</c> used to allocate the returned <c>NativeArray</c>.</param> /// <returns>A <c>NativeArray</c> of all the resulting ray intersections.</returns> public NativeArray <XRRaycastHit> Raycast( Vector2 screenPoint, TrackableType trackableTypeMask, Allocator allocator) { return(m_Provider.Raycast(XRRaycastHit.defaultValue, screenPoint, trackableTypeMask, allocator)); }
NativeArray <XRRaycastHit> RaycastRay( Ray ray, TrackableType trackableTypeMask, Allocator allocator) { return(subsystem.Raycast(ray, trackableTypeMask, allocator)); }
/// <summary> /// Casts <paramref name="ray"/> against trackables specified with <paramref name="trackableTypeMask"/>. /// </summary> /// <param name="ray">A ray in session space.</param> /// <param name="trackableTypeMask">The types of trackables to test for ray intersections.</param> /// <param name="allocator">The <c>Allocator</c> used to allocate the returned <c>NativeArray</c>.</param> /// <returns>A <c>NativeArray</c> of all the resulting ray intersections.</returns> public NativeArray <XRRaycastHit> Raycast( Ray ray, TrackableType trackableTypeMask, Allocator allocator) { return(m_Provider.Raycast(XRRaycastHit.defaultValue, ray, trackableTypeMask, allocator)); }
/// <summary> /// Helper function to perform an AR Raycast for the user - by default uses TrackableType.PlaneWithinPolygon which is the most common /// </summary> /// <param name="_touchPosition">Where the user touched</param> /// <param name="hitPose">The location of the impact</param> /// <returns>True if they hit a trackable that fits the specified trackable type</returns> public bool Raycast(Vector2 _touchPosition, out Pose hitPose) { #if UNITY_ANDROID || UNITY_IOS TrackableType trackableType = TrackableType.PlaneWithinPolygon; //OLD: float x, float y, TrackableHitFlags filter, out TrackableHit hitResult List <ARRaycastHit> hitResults = new List <ARRaycastHit>(); m_RaycastManager.Raycast(_touchPosition, hitResults, trackableType); bool foundHit = hitResults.Count > 0; //hitResult = new ARRaycastHit(); hitPose = new Pose(); if (foundHit) { Pose worldPose = _WorldToAnchorPose(hitResults[0].pose); //ARRaycastHit newHit = new ARRaycastHit(new XRRaycastHit(hitResults[0].trackableId, worldPose, hitResults[0].distance, _trackableType), // hitResults[0].distance, null); //Last parameter may be wrong //"The Transform that transforms from session space to world space" hitPose = worldPose; } return(foundHit); #else Debug.LogError("Can only AR Raycast on mobile devices"); hitPose = new Pose(); return(false); #endif }
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); }
public override NativeArray <XRRaycastHit> Raycast(XRRaycastHit defaultRaycastHit, Vector2 screenPoint, TrackableType trackableTypeMask, Allocator allocator) { var hits = new NativeArray <XRRaycastHit>(); return(hits); }
private static extern void Internal_RaycastAsList( Ray ray, float pointCloudRaycastAngleInDegrees, IntPtr depthSubsystem, IntPtr planeSubsystem, TrackableType trackableTypeMask, List <XRRaycastHit> hitResultsOut);
public bool Raycast(Vector2 screenPoint, List <PortalbleHitResult> hitResults, TrackableType trackableTypes = TrackableType.PlaneWithinPolygon) { ARRaycastManager arrm = GetComponent <ARRaycastManager>(); if (arrm != null) { List <ARRaycastHit> hits = new List <ARRaycastHit>(); if (arrm.Raycast(screenPoint, hits, trackableTypes)) { hitResults.Clear(); foreach (ARRaycastHit hit in hits) { hitResults.Add(new PortalbleHitResult(hit)); return(true); } } else { return(false); } } return(false); }
/// <summary> /// Cast a <c>Ray</c> against trackables, i.e., detected features such as planes. /// </summary> /// <param name="ray">The <c>Ray</c>, in Unity world space, to cast.</param> /// <param name="hitResults">Contents are replaced with the raycast results, if successful.</param> /// <param name="trackableTypeMask">(Optional) The types of trackables to cast against.</param> /// <param name="pointCloudRaycastAngleInDegrees">(Optional) Used to define the angle of the cone to use when raycasting against feature points.</param> /// <returns>True if the raycast hit a trackable in the <paramref name="trackableTypeMask"/></returns> public bool Raycast(Ray ray, List <ARRaycastHit> hitResults, TrackableType trackableTypeMask = TrackableType.All, float pointCloudRaycastAngleInDegrees = 5f) { if (trackablesParent == null) { return(false); } if (hitResults == null) { throw new ArgumentNullException("hitResults"); } var rayLocalSpace = trackablesParent.InverseTransformRay(ray); XRRaycastSubsystem.Raycast( rayLocalSpace, ARSubsystemManager.depthSubsystem, ARSubsystemManager.planeSubsystem, s_RaycastHits, trackableTypeMask, pointCloudRaycastAngleInDegrees); TransformRaycastResults(trackablesParent, hitResults, ray.origin); return(hitResults.Count > 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); } }
/// <summary> /// Cast a ray from a point in screen space against trackables, i.e., detected features such as planes. /// </summary> /// <param name="screenPoint">The point, in device screen pixels, from which to cast.</param> /// <param name="hitResults">Contents are replaced with the raycast results, if successful.</param> /// <param name="trackableTypeMask">(Optional) The types of trackables to cast against.</param> /// <returns>True if the raycast hit a trackable in the <paramref name="trackableTypeMask"/></returns> public bool Raycast(Vector3 screenPoint, List <ARRaycastHit> hitResults, TrackableType trackableTypeMask = TrackableType.All) { if (hitResults == null) { throw new ArgumentNullException("hitResults"); } var raycastSubsystem = ARSubsystemManager.raycastSubsystem; if (raycastSubsystem == null) { return(false); } var originTransform = camera != null ? camera.transform : trackablesParent; // Results are in "trackables space" if (raycastSubsystem.Raycast(screenPoint, s_RaycastHits, trackableTypeMask)) { // Transform results back into world space TransformRaycastResults(trackablesParent, hitResults, originTransform.position); return(hitResults.Count > 0); } return(false); }
static unsafe extern void UnityARCore_raycast_acquireHitResultsRay( Vector3 rayOrigin, Vector3 rayDirection, TrackableType filter, out void *hitBuffer, out int hitCount, out int elementSize);
public TrackableType GetTrackableType(UInt64 trackable_handle) { TrackableType trackble_type = TrackableType.TRACKABLE_BASE; NativeApi.NRTrackableGetType(m_NativeInterface.TrackingHandle, trackable_handle, ref trackble_type); return(trackble_type); }
/// <summary> /// Cast a ray from a point in screen space against trackables, i.e., detected features such as planes. /// </summary> /// <param name="screenPoint">The point, in device screen pixels, from which to cast.</param> /// <param name="hitResults">Contents are replaced with the raycast results, if successful.</param> /// <param name="trackableTypes">(Optional) The types of trackables to cast against.</param> /// <returns>True if the raycast hit a trackable in the <paramref name="trackableTypes"/></returns> public static bool Raycast(Vector2 screenPoint, List <ARRaycastHit> hitResults, TrackableType trackableTypes = TrackableType.All) { hitResults.Clear(); if (CheckDependentManagers() && s_ARRaycastManager.Raycast(screenPoint, hitResults, trackableTypes)) { return(true); } // No hits or managers, try debug planes var sessionOrigin = Object.FindObjectOfType <ARSessionOrigin>(); RaycastHit hit; var ray = Camera.main.ScreenPointToRay(screenPoint); if (Physics.Raycast(ray, out hit, float.MaxValue, 1 << 9)) { hitResults.Add(new ARRaycastHit( new XRRaycastHit(TrackableId.invalidId, new Pose(hit.point, Quaternion.LookRotation(Vector3.forward, hit.normal)), hit.distance, TrackableType.PlaneWithinPolygon), hit.distance, sessionOrigin != null ? sessionOrigin.transform : hit.collider.transform)); return(true); } return(false); }
NativeArray <XRRaycastHit> RaycastFallback( Ray ray, TrackableType trackableTypeMask, Allocator allocator) { s_NativeRaycastHits.Clear(); int count = 0; foreach (var raycaster in m_Raycasters) { var hits = raycaster.Raycast(ray, trackableTypeMask, Allocator.Temp); if (hits.IsCreated) { s_NativeRaycastHits.Add(hits); count += hits.Length; } } var allHits = new NativeArray <XRRaycastHit>(count, allocator); int dstIndex = 0; foreach (var hitArray in s_NativeRaycastHits) { NativeArray <XRRaycastHit> .Copy(hitArray, 0, allHits, dstIndex, hitArray.Length); hitArray.Dispose(); dstIndex += hitArray.Length; } return(allHits); }
XRRaycastSubsystemDescriptor(Cinfo cinfo) { id = cinfo.id; subsystemImplementationType = cinfo.subsystemImplementationType; supportsViewportBasedRaycast = cinfo.supportsViewportBasedRaycast; supportsWorldBasedRaycast = cinfo.supportsWorldBasedRaycast; supportedTrackableTypes = cinfo.supportedTrackableTypes; }
/// <summary> /// Performs a raycast from the camera against the types /// specified by <paramref name="trackableTypeMask"/>. Results /// should be sorted by distance from the ray origin. /// </summary> /// <param name="defaultRaycastHit">The default raycast hit that should be used as a template when populating the returned <c>NativeArray</c>.</param> /// <param name="screenPoint">A point on the screen in normalized (0...1) coordinates</param> /// <param name="trackableTypeMask">The types to raycast against.</param> /// <param name="allocator">The allocator with which to allocate the returned <c>NativeArray</c>.</param> /// <returns>A <c>NativeArray</c> of all the resulting ray intersections.</returns> public virtual NativeArray <XRRaycastHit> Raycast( XRRaycastHit defaultRaycastHit, Vector2 screenPoint, TrackableType trackableTypeMask, Allocator allocator) { throw new NotSupportedException("Raycasting using a screen point is not supported."); }
public void UpdateTrackables(UInt64 trackable_list_handle, TrackableType trackable_type) { if (m_TrackingHandle == 0) { return; } NativeApi.NRTrackingUpdateTrackables(m_NativeInterface.TrackingHandle, trackable_type, trackable_list_handle); }
/// <summary> /// Performs a raycast from an arbitrary ray against the types /// specified by <paramref name="trackableTypeMask"/>. Results /// should be sorted by distance from the ray origin. /// </summary> /// <param name="defaultRaycastHit">The default raycast hit that should be used as a template when populating the returned <c>NativeArray</c>.</param> /// <param name="ray">A ray in session space from which to raycast.</param> /// <param name="trackableTypeMask">The types to raycast against.</param> /// <param name="allocator">The allocator with which to allocate the returned <c>NativeArray</c>.</param> /// <returns>A <c>NativeArray</c> of all the resulting ray intersections.</returns> public virtual NativeArray <XRRaycastHit> Raycast( XRRaycastHit defaultRaycastHit, Ray ray, TrackableType trackableTypeMask, Allocator allocator) { throw new NotSupportedException("Raycasting using a Ray is not supported."); }
/// <summary> /// Get the tracking type of current trackable. /// </summary> public TrackableType GetTrackableType() { if (NRFrame.SessionStatus != SessionState.Running) { return(trackableType); } trackableType = NativeInterface.NativeTrackable.GetTrackableType(TrackableNativeHandle); return(trackableType); }
NativeArray <XRRaycastHit> RaycastViewport( Vector2 screenPoint, TrackableType trackableTypeMask, Allocator allocator) { screenPoint.x = Mathf.Clamp01(screenPoint.x / Screen.width); screenPoint.y = Mathf.Clamp01(screenPoint.y / Screen.height); return(subsystem.Raycast(screenPoint, trackableTypeMask, allocator)); }
public static unsafe void UnityARKit_raycast_acquireHitResults( Vector2 screenPoint, TrackableType filter, out void *hitResults, out int hitCount) { hitResults = null; hitCount = 0; }
static unsafe void UnityARCore_raycast_acquireHitResults( Vector2 screenPoint, TrackableType filter, out void *hitBuffer, out int hitCount, out int elementSize) { hitBuffer = null; hitCount = elementSize = 0; }
/// <summary> /// Constructs an <see cref="XRRaycastHit"/>. /// </summary> /// <param name="trackableId">The <see cref="TrackableId"/> of the trackable which was hit.</param> /// <param name="pose">The session-space <c>Pose</c> of the intersection.</param> /// <param name="distance">The session-space distance from the raycast origin to the intersection point.</param> /// <param name="hitType">The type(s) of trackables which were hit by the ray.</param> public XRRaycastHit( TrackableId trackableId, Pose pose, float distance, TrackableType hitType) { m_TrackableId = trackableId; m_Pose = pose; m_Distance = distance; m_HitType = hitType; }
// Update is called once per frame void Update() { // Exit the app when the 'back' button is pressed. if (Input.GetKey(KeyCode.Escape)) { Application.Quit(); } if (Input.touchCount == 0) { return; } var touch = Input.GetTouch(0); if (touch.phase != TouchPhase.Began) { return; } Debug.Log("AFWK: New touch !!!"); // Raycast against planes and feature points const TrackableType trackableTypes = TrackableType.PlaneWithinPolygon; // Perform the raycast if (m_RaycastManager.Raycast(touch.position, s_Hits, trackableTypes)) { // Raycast hits are sorted by distance, so the first one will be the closest hit. var hit = s_Hits[0]; // Create a new anchor var anchor = CreateAnchor(hit); if (anchor) { //turn on portal portal.gameObject.SetActive(false); portal.gameObject.SetActive(true); portal.transform.parent = anchor.transform; portal.transform.localPosition = Vector3.zero; portal.transform.localRotation = Quaternion.Euler(0, 180.0f, 0); if (previousAnchor) { Destroy(previousAnchor.gameObject); } previousAnchor = anchor; } else { Debug.LogWarning("AFW: Error creating portal"); } } }
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)); }
public bool Raycast(Vector3 screenPoint, List <XRRaycastHit> hitResults, TrackableType trackableTypeMask = TrackableType.All) { if (hitResults == null) { throw new ArgumentNullException("hitResults"); } float screenX = Mathf.Clamp01(screenPoint.x / Screen.width); float screenY = Mathf.Clamp01(screenPoint.y / Screen.height); return(Internal_ScreenRaycastAsList(screenX, screenY, trackableTypeMask, hitResults)); }
public void ClearTrackableList(TrackableType type) { for (int i = 0; i < trackables.Count; i++) { if (trackables[i].type.Equals(type)) { trackables[i].items.Clear(); return; } } Debug.LogWarning("There is no trackable list of the type " + type); }
/// @endcond private NRTrackable Create(UInt64 trackable_handle, NativeInterface nativeInterface) { if (trackable_handle == 0) { return(null); } NRTrackable result; if (m_TrackableDict.TryGetValue(trackable_handle, out result)) { return(result); } if (nativeInterface == null) { return(null); } TrackableType trackableType = nativeInterface.NativeTrackable.GetTrackableType(trackable_handle); if (trackableType == TrackableType.TRACKABLE_PLANE) { result = new NRTrackablePlane(trackable_handle, nativeInterface); } else if (trackableType == TrackableType.TRACKABLE_IMAGE) { result = new NRTrackableImage(trackable_handle, nativeInterface); } else { throw new NotImplementedException( "TrackableFactory::No constructor for requested trackable type."); } if (result != null) { m_TrackableDict.Add(trackable_handle, result); if (!m_TrackableTypeDict.ContainsKey(trackableType)) { m_TrackableTypeDict.Add(trackableType, new Dictionary <ulong, NRTrackable>()); } Dictionary <ulong, NRTrackable> trackbletype_dict = null; m_TrackableTypeDict.TryGetValue(trackableType, out trackbletype_dict); if (!trackbletype_dict.ContainsKey(trackable_handle)) { trackbletype_dict.Add(trackable_handle, result); m_AllTrackables.Add(result); } } return(result); }