Search Unity

How to Raycast to point and cloud particle in ARFoundation?

Discussion in 'AR' started by soorya696, Oct 1, 2019.

  1. soorya696

    soorya696

    Joined:
    Dec 13, 2018
    Posts:
    71
    I want to know the exact real-world location of a particular point cloud particle. So, I tried to raycast the point cloud particle, but it gives the wrong results.
    Here's my previous workaround:
    Code (CSharp):
    1.         if (Input.touchCount > 0 )
    2.         {
    3.             Touch touch = Input.GetTouch(0);
    4.          
    5.             var hits = new List<ARRaycastHit>();
    6.             Ray ray = Camera.main.ScreenPointToRay(touch.position);
    7.             raycastManager.Raycast(ray, hits, TrackableType.FeaturePoint);
    8.             if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began && PrefabToPlace != null)
    9.             {
    10.                 Pose p = hits[0].pose;
    11.                 _ShowAndroidToastMessage("Hit at : " + (p.position.z) +  " ciunt L "+ hits.Count);
    12.                 var obj = Instantiate(PrefabToPlace, pose.position, pose.rotation);
    13.                 obj.name = PrefabToPlace.name;
    14.                 obj.transform.parent = ParentAR.transform;
    15.  
    16.                 PrefabToPlace = null;
    17.  
    18.             }
    19.         }
    Please suggest a way to achieve this.
     
  2. selsebil1995

    selsebil1995

    Joined:
    Jul 10, 2019
    Posts:
    3
    hi everyone ,
    i'm still looking for a solution for this problem : knowing the exact real-world location of a particular point cloud particle.
    any idea ?
    thanks
     
  3. KyryloKuzyk

    KyryloKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    1,144
    Here is an example script. Drop references to Raycast Manager and Origin and you're good to go.
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.Linq;
    3. using JetBrains.Annotations;
    4. using UnityEngine;
    5. using UnityEngine.XR.ARFoundation;
    6. using UnityEngine.XR.ARSubsystems;
    7. using Input = UnityEngine.Input;
    8.  
    9.  
    10. namespace ARFoundationRemoteExample.Runtime {
    11.     public class MultiTouchRaycastExample : MonoBehaviour {
    12.         [SerializeField] ARRaycastManager raycastManager = null;
    13.         [SerializeField] ARSessionOrigin origin = null;
    14.         [CanBeNull] [SerializeField] GameObject optionalPointerPrefab = null;
    15.         [SerializeField] bool disablePointersOnTouchEnd = false;
    16.         [SerializeField] TrackableType trackableTypeMask = TrackableType.FeaturePoint;
    17.  
    18.         readonly Dictionary<int, Transform> pointers = new Dictionary<int, Transform>();
    19.  
    20.  
    21.         void Update() {
    22.             for (int i = 0; i < Input.touchCount; i++) {  
    23.                 var touch = Input.GetTouch(i);
    24.                 var pointer = getPointer(touch.fingerId);
    25.                 var touchPhase = touch.phase;
    26.                 if (touchPhase == TouchPhase.Ended || touchPhase == TouchPhase.Canceled) {
    27.                     if (disablePointersOnTouchEnd) {
    28.                         pointer.gameObject.SetActive(false);
    29.                     }
    30.                 } else {
    31.                     var ray = origin.camera.ScreenPointToRay(touch.position);
    32.                     var hits = new List<ARRaycastHit>();
    33.                     var hasHit = raycastManager.Raycast(ray, hits, trackableTypeMask);
    34.                     if (hasHit) {
    35.                         var pose = hits.First().pose;
    36.                         pointer.position = pose.position;
    37.                         pointer.rotation = pose.rotation;
    38.                     }
    39.                    
    40.                     pointer.gameObject.SetActive(hasHit);
    41.                 }
    42.             }
    43.         }
    44.  
    45.         Transform getPointer(int fingerId) {
    46.             if (pointers.TryGetValue(fingerId, out var existing)) {
    47.                 return existing;
    48.             } else {
    49.                 var newPointer = createNewPointer();
    50.                 pointers[fingerId] = newPointer;
    51.                 return newPointer;
    52.             }
    53.         }
    54.        
    55.         Transform createNewPointer() {
    56.             var result = instantiatePointer();
    57.             result.parent = transform;
    58.             return result;
    59.         }
    60.  
    61.         Transform instantiatePointer() {
    62.             if (optionalPointerPrefab != null) {
    63.                 return Instantiate(optionalPointerPrefab).transform;
    64.             } else {
    65.                 var result = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform;
    66.                 result.localScale = Vector3.one * 0.05f;
    67.                 return result;
    68.             }
    69.         }
    70.     }
    71. }
    72.