Search Unity

Create ARReferencePoint

Discussion in 'Handheld AR' started by DreamEnder, Aug 8, 2018.

  1. DreamEnder

    DreamEnder

    Joined:
    Apr 12, 2011
    Posts:
    190
    How would you go about creating a reference point/anchor from script?
     
  2. rxmarccall

    rxmarccall

    Joined:
    Oct 13, 2011
    Posts:
    301
    I've wondered this as well!
     
  3. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    547
  4. Saicopate

    Saicopate

    Joined:
    Sep 25, 2017
    Posts:
    58
    I am also interested how I can anchor a gameobject using AR Reference Point.
    Is there a scripting example somewhere? I guess the AR Foundation Sample scripts are not setting Reference Points.

    One way is surely to insert the desired prefab to AR Reference Point Manager and instantiate it with the methods that Tim listed above, but then I guess the gameobject changes its name and parent. I'm not sure if that's the right way.

    I was thinking about spawning an gameobject normally, setting an ARReferencePoint in the same place and utilizing ARReferencePointUpdatedEventArgs to get the updated pose to update the gameobject's transform.
    Is that a good way to go?
     
  5. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    547
    I would suggest parenting your GameObject to the returned ARReferencePoint (if you don't specify a prefab, it creates a GameObject with only an ARReferencePoint component).

    This would also work, and may be desirable if you plane to scale the ARSessionOrigin.
     
    Saicopate likes this.
  6. theDawckta

    theDawckta

    Joined:
    Jun 16, 2014
    Posts:
    48
    Curious what the difference is between ARReferencePointManager.TryAddReferencePoint
    andARReferencePointManager.TryAttachReferencePoint? I was using TryAttachReferencePoint thinking that, by referencing a plane, the reference point would automatically update it's position when the plane updates its bounds/position.

    TryAttachReferencePoint is returning null for me though which seems like a bug. I have verified that my plane and pose are not null.

    Code (CSharp):
    1. _stageReferencePoint = ReferencePointManager.TryAttachReferencePoint(_planeHit, _hitPose);
    After the above code is run _stageReferencePoint is still null;

    EDIT:
    Just tried;
    Code (CSharp):
    1. _stageReferencePoint = ReferencePointManager.TryAddReferencePoint(_hitPose);
    This does return a ReferencePoint for me instead of null, so this one appears to be working.
     
    Last edited: Aug 10, 2018
  7. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    547
    Arkit or ARCore?
     
  8. theDawckta

    theDawckta

    Joined:
    Jun 16, 2014
    Posts:
    48
    I am testing on iOS.
     
  9. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    547
    It definitely works, but some features will break if you don't include this link.xml file in your project. Attach to plane is one such feature.
     
  10. theDawckta

    theDawckta

    Joined:
    Jun 16, 2014
    Posts:
    48
    Thanks fixed it, thank you. ReferencePointUpdated started firing after the fix when it was not while using TryAddReferencePoint, not sure if this is intended or not.

    Can you answer my other question above?

    "Curious what the difference is between ARReferencePointManager.TryAddReferencePoint
    andARReferencePointManager.TryAttachReferencePoint?"

    In my app I am placing a single item and want it to appear as if it is in the room with you. I have accomplished this with both calls but I am unsure of the "correct" way to do it. Both of these look like they do the same thing, wonder what are the differences/pros/cons?
     
  11. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    547
    Add vs Attach maps closely to these concepts in ARCore: https://developers.google.com/ar/develop/developer-guides/anchors#pick_an_anchor_context

    When you "attach" a reference point to a plane, it maintains that distance from the plane. For example, if the plane is horizontal, then the reference point only moves up and down as the plane's Y position changes. The X and Z coordinates should not change. This is useful for "sticking" objects on or near planes.

    If you "add" a reference point, then you're asking the system to track a specific location in space. It may update if the overall environmental understanding of the session changes.
     
  12. theDawckta

    theDawckta

    Joined:
    Jun 16, 2014
    Posts:
    48
    Thanks for the help it is appreciated, that was a great explanation. It sounds like i should use TryAttachReferencePoint since I am trying to place something on a table or flat surface.

    Last question, if I attach to a horizontal plane using TryAttachReferencePoint does the returned ReferencePoint take care of it's y position itself? Is there ever any need to do any repositioning with ReferencePointUpdated?
     
  13. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    547
    Yes it takes care of the y position. The purpose of reference points is for the device to track and update them. You should not update them manually.
     
  14. theDawckta

    theDawckta

    Joined:
    Jun 16, 2014
    Posts:
    48
    Ok I lied, one more. After I place my ReferencePoint do I need to keep ARPlaneManager enabled for it to keep updating it's y position.

    Just wondering if shutting down ARPlaneManager after placing a ReferencePoint would improve performance (not that perf is an issue at this point, just trying to be efficient).
     
  15. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    547
    Disabling the ARPlaneManager will tell the device to stop detecting planes, so I wouldn't expect any further updates to the ReferencePoint.
     
  16. theDawckta

    theDawckta

    Joined:
    Jun 16, 2014
    Posts:
    48
    Thanks for the help, greatly appreciated.
     
  17. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    @tdmowrer Hey, I am wondering if I should be making use of a reference point to improve tracking, or just better practice.

    I am currently placing my content using standard place on plane method, turning off plane manager and making use of makecontentappearat for scaling.

    All is working well, but sometimes I suffer floating,jittering or relocating, and reference points have been mentioned as a way to improve tracking.

    My understanding is I am placing my content at a specific world space coord, and the arsession and trackposedriver looks after the tracking relative to the cameras movement after that etc.

    What would a reference point add? Can it be used in this case? Would it be better?

    Would you mind providing a simple explanation/comparison of a simple point in world space vs a referencepoint(anchor) to help my understanding of things.

    Thanks a lot

    Bump
     
    Last edited: Mar 27, 2019
    marie-hmm and viemr like this.
  18. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    @tdmowrer If you get time, can you offer any insight to my question above.
    Thanks a lot
     
  19. Blenderik

    Blenderik

    Joined:
    May 14, 2013
    Posts:
    68
    What I don't get:
    If the reference points are essential, why are they not included in the sample scenes? I had to google my way over here with a lot of detours until I found out.
    It's the same with remoting:
    You guys updated the thread with a video, but kinda forgot to mention that they are broken in 2019, took me 2h to find that out.
    Sorry, had to say this, I´'ll try the reference points now.
     
  20. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    547
    Sorry, what's broken in 2019? Can you link me to the video?

    I agree a sample would be helpful. I'll see what we can do about that.
     
  21. Blenderik

    Blenderik

    Joined:
    May 14, 2013
    Posts:
    68
  22. ArmanUnity

    ArmanUnity

    Joined:
    Nov 29, 2015
    Posts:
    21
    hello.. @tdmowrer ..Help me understand the advantage of adding a reference point(anchor point) to a object
    for example,1. we can place a cube by simply after a hit test, like instantiating a cube after a raycast hit on the trackable type plane and setting the position and rotation of the cube to the returned hit pos and rotation ,device can start tracking that cube........ does it improve/ make any difference in tracking, if we add a reference point to that hit position ???
    2. we can place a cube(not hit test) just 1 unit in front of the camera at the start itself and it will start tracking that cube relative to the world origin(position and rotation of the device when session starts), does it improve tracking if we add a reference point to that position ??????( for the test case i have done i cant observe much difference)
    3.ar core doc says using anchors improves realism and performance, but when the environment understanding changes in each frame position and rotation of these anchor also may changes ,so in effect the position of the objects using this anchor/reference also prone to the changes .... a possibility of drift may occur like a an object without anchor ryt?????
     
  23. Blenderik

    Blenderik

    Joined:
    May 14, 2013
    Posts:
    68
    I have used anchors only in ARCore, but I assume they are similar here.
    If the track of the scene has been less than optimal before you place your object, there is a chance that the point cloud and therefore the transforms of the trackables change. An Anchor will be notified of the changes and change its pose relative to the the new tracking points' location.
     
    ArmanUnity likes this.
  24. Tarrag

    Tarrag

    Joined:
    Nov 7, 2016
    Posts:
    121
    Hey @ArmanUnity I'm testing anchors with ARF2.1 and frankly not yet seeing much difference on tracking. Where I can see the benefit of anchors is if two objects are to maintain a specific distance to each other - by anchoring both objects as child of the anchor it would help be "more realistic" because both objects can keep their position relative to one another constant cos they're both child of the parent anchor; that's the example in arcore https://developers.google.com/ar/develop/developer-guides/anchors

    That's my interpretation to date.
     
  25. ArmanUnity

    ArmanUnity

    Joined:
    Nov 29, 2015
    Posts:
    21
    yes @Tarrag i understand the advantage if we need to maintain distance between two objects ... but in ar core doc they saying that .... you should place an anchor on the hit position place ...ryt????
    "For example, imagine that your app lets users place virtual furniture pieces around a room. A user opens the app and ARCore begins tracking a tabletop and a floor in the room as Planes. The user positions a virtual lamp on the tabletop. Then, the user puts a virtual chair on the floor.
    At this point, your scene should have one anchor attached to the tabletop Plane and another attached to the floor Plane."
    so for the normal hit test on a plane ... we can change the objects position to hit position and and start tracking, And we can also add a reference point in there and make object as child of reference point game object.. does the second way improves tracking .....???? i cant see much difference
     
  26. Blenderik

    Blenderik

    Joined:
    May 14, 2013
    Posts:
    68
    @ArmanUnity Check my answer above. The anchor points stay fixed in world space (what you see through the phone), while the planes may change position in the Unity scene. Connect your phone via google instant preview and track a not-so-optimal environment. You may run into this situation. The anchors are for "safety".
     
  27. ArmanUnity

    ArmanUnity

    Joined:
    Nov 29, 2015
    Posts:
    21
    @Blenderik ,i have seen your answers ... i will debug it with optimal environment and confirm.... but i think when the environment understanding changes anchor position also changes ,so is the position of the child game objects also experience a shift/drift in position ryt??
     
  28. AlbyDj90

    AlbyDj90

    Joined:
    Feb 7, 2015
    Posts:
    28
    The best usage for anchor is to "anchor" them directly with the plane you just found...this is because if you move in place and the plane become more and more precise with your real world enviroment this could help your object to stay in the right spot.
     
  29. ArmanUnity

    ArmanUnity

    Joined:
    Nov 29, 2015
    Posts:
    21
    @AlbyDj90 ,ok good .... does it improves tracking with out planes..... ie. like a floating cube in air with a anchor point associated with it
     
  30. skywalker887

    skywalker887

    Joined:
    May 24, 2017
    Posts:
    12
    That’s a great thread and covers many of the concepts about anchoring

    @tdmowrer

    I’m adding anchors and my purpose is for them to stay in place without shifting or relocation when I move about .
    However ,the anchors shift/drift .Id my understanding is correct ,the device will start tracking the anchors and update its position as the system understands more about the environment .
    Do you suggest tracking the reference point change event to place back the anchor to the original pose if the system updates its position ? What would be the bet practice to fix an anchor without shifting/drifting due to movement or system update

    thanks
     
  31. skywalker887

    skywalker887

    Joined:
    May 24, 2017
    Posts:
    12
    @tdmowrer

    can you please check my question in above and let me what you think .

    Thanks
     
  32. skywalker887

    skywalker887

    Joined:
    May 24, 2017
    Posts:
    12
    Is there anyone who can help answer my inquiry .

    Appreciate any help

    Thanks
     
    Last edited: Oct 11, 2019
  33. BuoDev

    BuoDev

    Joined:
    Nov 28, 2018
    Posts:
    45
    To me, your question doesn't make much sense. If you intend to place objects in 3d space, wouldn't you want their true positions to be maintained for the entire ARSession? You could use the referencePointChanged event to re-adjust the object attached to an anchor. You would need to store it's original position in a variable to re-adjust it whenever the ARReferencePoint is updated. But honestly, I think this is partially defeating the purpose of anchors.

    I'll break down the logic and purpose of anchors a bit, just in case it helps.

    The anchors should not drift, instead they should "snap" to their original positions (with the 3d objects attached) as spacial understanding is improved. Anchors are intended for situations where tracking is lost or interrupted, or re-localisation fails, OR to adjust for accumulative tracking error. However, this is all related to spacial understanding, or more specifically, the lack of it.

    Typical situations of re-localisation failure when:
    -You move or rotate the camera so quickly, that tracking is interrupted and placed objects are seen in different places then where you originally placed them.
    -You cover up the lens, or accidentally shut off the screen of the device.
    -You place the device in your pocket and continue in a different space.
    -Tracking large spaces that can cause tracking error to accumulate over time.

    1. When you start an AR session, the spacial understanding is obviously very limited. You may start scanning for surfaces and indeed find a surface. If you decide to place a 3d object, you SHOULD create an anchor using the hit's pose (which can be a hit on any type of trackable) so that the system will remember this location. Think of anchors as screenshots, you place an anchor so that the system has a visual reference of what to look for later on.

    2. Then you need to place your object as a child of the
    ARReferencePoint
    that you have created.

    You can do steps 1 and 2 like this:
    //create the ARReferencePoint
    ARReferencePoint newReferencePoint = ar_ReferencePointManager.AddReferencePoint(thisHitPose);

    //place the 3d object as child
    spawnedGameObject.transform.SetParent(newReferencePoint.transform);


    3. Now that you have anchored the object, you could cover up the lens of the device, and walk into an entirely different room or space. Since the system remembers where you attached the anchor, you can go on and place more anchors with objects in the new room you have walked into (repeating steps 1 and 2). When you resumed tracking in room #2, you may have noticed that the 1st object you placed in room #1 may be floating somewhere incorrectly nearby. This is because the system does not have spacial understanding between the rooms and the system is actively working to re-localise with the 1st anchor you placed in room #1. You could create a logic to hid the object until the 1st anchor in room #1 is re-localised.

    4. Now to demonstrate how the anchors work together, after anchoring an object in room #2, move back to room #1 without interrupting the tracking. You will see, when you return to room #1, the first anchor you placed will adjust and your object will be visible where you originally placed it. Also, you can now freely move between the rooms and the anchors and respective objects will have their correct positions.

    5. It's normal for the the anchors and attached object to slightly adjust over time. This is normal, and be happy, because the system is just making sure that your objects aren't drifting about as spacial understanding is accumulated.

    6. If you continuously move or delete objects, you need to create a logic that removes and creates new anchors. You don't want unused anchors around since it will have an affect on performance.
    For example:
    -When an object is moved e.g. TouchPhase.Moved, you can remove the anchor
    -When the movement ends e.g. TouchPhase.Ended, you can place a new anchor
    -You may also want to consider that it isn't necessary to place an anchor if there are other anchors close nearby. You could loop through the distances from the anchors to the object you are placing, and choose not to place an anchor if the nearest anchor is less than 2 meters from the object you have placed.

    Hope this helps!
     
    skywalker887 likes this.
  34. BuoDev

    BuoDev

    Joined:
    Nov 28, 2018
    Posts:
    45
    Here's what I use to handle the updating and removing of anchors.
    Note:
    data.types[typeNumber].spawnedObjects[ofTypeNumber]
    is a class where I keep track of spawned objects and other things like anchors that I frequently access. You need to create your own way to access anchors.

    Code (CSharp):
    1. private void CreateOrUpdateAnchor()
    2.     {
    3.         //get the old reference point
    4.         ARReferencePoint previousReferencePoint = data.types[typeNumber].spawnedObjects[ofTypeNumber].anchor;
    5.  
    6.         if (previousReferencePoint != null)
    7.         {
    8.             //add a new reference point
    9.             ARReferencePoint newReferencePoint = ar_ReferencePointManager.AddReferencePoint(thisHitPose);
    10.             //add the AR object to it
    11.             selectedGameObjectRoot.transform.SetParent(newReferencePoint.transform);
    12.             //update anchor (data)
    13.             data.types[typeNumber].spawnedObjects[ofTypeNumber].anchor = newReferencePoint;
    14.  
    15.             //remove the old reference point
    16.             ar_ReferencePointManager.RemoveReferencePoint(previousReferencePoint);
    17.         }
    18.         else
    19.         {
    20.             //add a new reference point
    21.             ARReferencePoint newReferencePoint = ar_ReferencePointManager.AddReferencePoint(thisHitPose);
    22.             //add the AR object to it
    23.             selectedGameObjectRoot.transform.SetParent(newReferencePoint.transform);
    24.             //add anchor (data)
    25.             data.types[typeNumber].spawnedObjects[ofTypeNumber].anchor = newReferencePoint;
    26.         }
    27.         shouldCreateAnchor = false;
    28.     }
     
    skywalker887 likes this.
  35. skywalker887

    skywalker887

    Joined:
    May 24, 2017
    Posts:
    12
    Thanks a lot @BuoDev ,it is much clearer now ,appreciate it .
     
  36. skywalker887

    skywalker887

    Joined:
    May 24, 2017
    Posts:
    12
    Hello @BuoDev

    Is it possible to add ARplane programmatically ..I want to save information from an AR experience and then play it back when the user opens the app .To clarify my point ,suppose a user moves around a place and scans surfaces and adds objects .For the same area the user will open the app and retrieve all the information that was saved from the previous scene .I know ARKit has worldmap but was wondering if we can control ARPlanes creation .
     
    Last edited: Oct 16, 2019 at 7:37 PM