Search Unity

detect (or ray cast to) only horizontal planes

Discussion in 'Handheld AR' started by robertoranon, Oct 16, 2018.

  1. robertoranon

    robertoranon

    Joined:
    Feb 21, 2018
    Posts:
    12
    Hi,

    I searched through docs and forums, but could not find an answer: how can I raycast (or detect hits) only to horizontal planes? or even only detect horizontal planes?

    Thanks a lot for any help!
     
    dyuldashev likes this.
  2. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    I think you can check using:

    plane.boundedPlane.Alignment == UnityEngine.Experimental.XR.PlaneAlignment.Horizontal
     
  3. robertoranon

    robertoranon

    Joined:
    Feb 21, 2018
    Posts:
    12
    Ok, thanks. But how do I get the plane from the ray cast?

    The ARRayCastHit I get from the raycast does not include the hit plane, the only thing that seems connected is the
    trackableId field, but how do I use it to get the plane?
     
  4. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    Would this work... (I havent implemented this so its off the top of my head... as Im not at my dev pc)

    m_SessionOrigin.GetComponent<ARPlaneManager>().TryGetPlane(s_Hit[0].trackableId).boundedPlane.Alignment
     
  5. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    Code (CSharp):
    1. if (m_SessionOrigin.Raycast(touch.position, s_Hits, TrackableType.PlaneWithinPolygon) && m_SessionOrigin.GetComponent<ARPlaneManager>().TryGetPlane(s_Hits[0].trackableId).boundedPlane.Alignment == PlaneAlignment.Horizontal)
    Try this out... if it works its prob a good idea to cache the m_SessionOrigin.GetComponent<ARPlaneManager>() and use it instead so you're not doing a GetComponent every Update()

    Let me know if it works out... I'll prob use it myself
     
  6. dyuldashev

    dyuldashev

    Joined:
    Mar 3, 2016
    Posts:
    61
    I have another question. Let's say, we only need to detect the ground place just once, and we place the object on it afterwards. How should we proceed after placing the content? Should we stop detecting planes? If so, does it affect the tracking?
     
  7. dyuldashev

    dyuldashev

    Joined:
    Mar 3, 2016
    Posts:
    61
    This does not really disable detecting vertical planes, does it? You're merely not putting any content on vertical planes this way. It's because you're raycasting to planes and choosing only the horizontal ones, yet that does not stop generating vertical planes. How do you totally disable generating vertical planes? Or, as a matter of fact, stop generating any planes after you place some content on one plane? Thanks.
     
  8. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    No, my response was to raycast to only horizontals..

    To then stop plane detection you disable the ARPlaneManager. And clean up existing planes by iterating through the list returned using GetAllPlanes()
     
  9. dyuldashev

    dyuldashev

    Joined:
    Mar 3, 2016
    Posts:
    61
    Got it. When you disable the ARPlaneManager, does it affect tracking? Let's say, I placed a large environment on a plane. Then, I disable the ARPlaneManager. Does it affect the tracking stability?
     
  10. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    Hi

    I don't think it should affect tracking... it should just stop detecting and generating visual planes. That's my understanding of it anyway, and appears to work ok for me.

    I did have an issue when exiting an AR scene to go back to a non AR menu.. there was a bit of a stall.. approx 5secs, but recent build seems ok.

    https://forum.unity.com/threads/scene-loading-issue-hang-on-android-ar-foundation.546438/

    I havent profiled it yet to see what timings Im getting for the Disable/Destroy AR methods that seemed to be the cause of the lag at the time. But I did update ARCore on my S8... it was 1.3, now 1.5... So its another thing to check. (note: I do have Attempt Update enabled on the AR Session GO)

    Another outstanding issues I have on IOS is that I loose tracking when I place an object and subsequently disable the ARPlaneManager. Its the same project.. just built to IOS and testing on iPad 9.7 2017.

    Have you experienced this in your tests when disabling the plane manager and building to IOS?

    https://forum.unity.com/threads/managing-trackables.564217/

    Cheers
     
    Last edited: Oct 19, 2018
  11. dyuldashev

    dyuldashev

    Joined:
    Mar 3, 2016
    Posts:
    61
    I started a new thread where I explained what was happening when we tried to exit from an AR Scene, or when we try to Reset() an AR Session:
    https://forum.unity.com/threads/multiple-ar-foundation-scenes-performing-poorly.571072/
    And I am discussing a similar issue on this thread(more people there than on my thread):
    https://forum.unity.com/threads/app-crashes-after-arsession-reset.570763/
    So, we could all join the second thread because there is a bug, and we should ask @jimmya and @tdmowrer to fix it.

    I don't think that it's OK just to disable the ARPlaneManager. There should be a safer way of doing this because when we destroy the ARPlaneManager, we cannot be sure whether it is safe for deletion or not.
     
  12. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    Hey
    Yeah, there are many threads here that are talking about similar issues.. that I also feel are related. I am having issues when going back and forth to an AR scene also... ie. NonARMenu > AR Scene > NonARMenu > ARScene... etc.. It doesnt crash, but my frame rate starts to drop.. If I stay in the ARScene it doesnt drop. Only on reload. If I build my game with out AR and mimic the placeonplane, I dont have these issues... so its not a memory leak on my part.

    Re: ARPlaneManager... if you find the Unity responses to disabling/removing planes once your object is placed in this forum, and on github... you will find they all say you turn off plane detection by disabling the ARPlaneManager component on the ARSessionOrigin... I'm not saying that is not causing issues... but its supposed to be the approach as far as I can tell. But currently, there is a lack of response from anyone in Unity here... I actually asked that very question in my initial thread and got no response
    https://forum.unity.com/threads/managing-trackables.564217/

    So hunted down the answer in several other threads.. eg:

    https://forum.unity.com/threads/arf...-visualizer-and-cancel-touch-tracking.565585/
     
    dyuldashev likes this.
  13. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
  14. dyuldashev

    dyuldashev

    Joined:
    Mar 3, 2016
    Posts:
    61
  15. robertoranon

    robertoranon

    Joined:
    Feb 21, 2018
    Posts:
    12
    Hi, and thanks for your suggestion. Been away for a while ...
    This is almost what I was looking for: I need to translate 3D models on the floor, so I need to use the infinite extent of horizontal planes: the problem, with your solution, is that I might hit a vertical plane as the first one, and then the hit is not detected. I modified your solution in the following way:

    Code (CSharp):
    1. if ( m_SessionOrigin.Raycast (screenPoint, s_Hits, TrackableType.Planes)) {
    2.  
    3.            foreach( ARRaycastHit hit in s_Hits ) {
    4.          
    5.                 if ( m_PlaneManager.TryGetPlane(hit.trackableId).boundedPlane.Alignment == PlaneAlignment.Horizontal) {
    6.                     Pose hitPose = s_Hits[0].pose;
    7.                     positions.Add(hitPose.position);
    8.                     return true;
    9.                 }
    10.            }
    11.  
    12.         }
    and with this, I get only hits on horizontal (infinite) planes
     
    GreeneMachine likes this.