Search Unity

How to save 3D object's position and rotation and load them at the exact place using ARkit in unity?

Discussion in 'AR' started by thesanketkale, Aug 29, 2019.

  1. thesanketkale

    thesanketkale

    Joined:
    Dec 14, 2016
    Posts:
    22
    Hi All,

    I am trying to figure out a simple thing to do using ARkit and Unity and I need help with it. I would like to place 3D objects in my room on detected planes and save the 3D objects position and rotation along with the detected feature points on to a file. Then I want to be able to load the saved session again by opening the app and see the 3D objects right where I kept them in AR. Now I have been able to save and load the world map using ARkit plugin in unity and I have managed to place objects where I want to place them using a Tap on screen. But I am not able figure out how to save the position and orientation of the 3D objects placed in my room and load them along with the world map. I tried saving the world position of the 3D objects and reload them after loading world map, but it is highly inaccurate.

    The key part is I want the load the 3D objects at the whatever places I kept them before saving the world map. I keep reading about AR Anchors or World Anchors in blogs but I don't see it with a specific implementation in unity. Could anybody please guide me on how to efficiently and accurately save the position and rotation of the 3D objects in a session so that they could be loaded back later at the same place?

    I am building this app with unity 2019.2.0 and ARkit XR plugin 2.1.1. Look forward to suggestions and advises.

    Regards,
    Sanket.
     
  2. unity_i0YDh9MITpYWmw

    unity_i0YDh9MITpYWmw

    Joined:
    Aug 16, 2019
    Posts:
    3
    Did you tried Map Sdks? To store the locations of the objects?
     
  3. thesanketkale

    thesanketkale

    Joined:
    Dec 14, 2016
    Posts:
    22
    Actually the 3D objects are going to be inside my bedroom. They might be on the top of a table or side of the bed, etc. I am not sure if saving GPS locations would help in this case as this is going to be entirely indoors.

    Now as seen in some videos, Placenote SDK allows to do something like this, but I am trying to figure out if this could be possible only using Anchors in ARkit XR plugin in Unity.

    Again, I have not found any concrete documentation or tutorial to use AR Anchors in Unity. Is it possible to use Anchors for this in some way along with AR world maps? If yes, how to use anchors with unity ARkit plugin?
     
  4. unity_i0YDh9MITpYWmw

    unity_i0YDh9MITpYWmw

    Joined:
    Aug 16, 2019
    Posts:
    3
    Sorry, I just started developing using Unity recently. Still don't have vast knowledge yet. I just said that because I thought it might help. :(
     
  5. thesanketkale

    thesanketkale

    Joined:
    Dec 14, 2016
    Posts:
    22
    Hey everyone, just a heads up, I cracked it and leaving it here for someone trying to accomplish this.

    If you want to save 3D objects at their positions in AR and spawn them right at the correct position and orientation every time, then you can just place the object on a detected plane using ARkit with AR Foundation and keep the object as its child and then save the plane's trackable ID along with the 3D object's position and rotation vector may be in a persistent way like a serialized DTO object or cloud database.

    Just save the world map to save all the detected feature points along with the position, rotation vector and plane trackable id on which the 3D object is placed. For spawning them right where you saved them, just download both (world map and serialized object) and apply the world map to the AR session and simultaneously start a coroutine to keep checking if the desired plane with the saved trackable id is detected in the session. Once found, just place the 3D object as its child and at the saved local position and rotation. And Voila, the 3D objects will be where you saved them previously irrespective of where you start the app from.

    Some notes after working hours on it:
    1) Make sure to place the 3D object as the child of the plane to have more accurate placement instead of storing its world position. You should get the transform of the plane by looping through the ARPlaneManager's trackables. You can place this (https://github.com/Unity-Technologi...er/Assets/Scripts/PlaneDetectionController.cs) script on Gameobject where ARPlaneManager is placed.
    2) Saving world map is an important aspect of this along with plane detection. Saving world map will save the feature points that are detected which will instantaneously spawn the detected planes on verifying if the save feature points are valid for the current environment (like a re-localization). The instantaneous detection of the saved planes in the previous session will ensure quick respawning of your 3D objects where they were saved.
    3) This s right now just possible in ARkit due to its world map feature and not in ARCore. So it will only work in iOS devices with ARkit support and not in android. The world map feature is pretty cool and I hope and wish Google rolls it out for ARcore as well.