Unity-Technologies / arfoundation-samples

Example content for Unity projects based on AR Foundation
Other
3.02k stars 1.12k forks source link

How to get the world camera position ? #964

Closed alexs7 closed 2 years ago

alexs7 commented 2 years ago

How do I get the world camera position ?

Is it as simple as getting the position and rotation of the camera game object that is a child of the AR Session Origin?

If so which one will give me the world position ?

Vector3 wPos = arSessionOrigin.camera.transform.position;
Vector3 lPos = arSessionOrigin.camera.transform.localPosition;

Do I need to attach an AR Pose Driver script to that camera game object ? Does it make a difference ?

Thanks!

DavidMohrhardt commented 2 years ago

Assuming you mean real world then you should use the localPosition of the camera that is the child of the AR Session Origin that will get you the camera position relative to the session origin and handle any cases where the session origin has been translated, rotated. If any scaling has been applied then you will need some math to undo the scaling to properly determine the real world position.

Edit: Misused phrasing when describing the relationship between AR Session Origin and AR Camera.

alexs7 commented 2 years ago

Does that pose correspond to https://developers.google.com/ar/reference/java/com/google/ar/core/Camera#getDisplayOrientedPose() in ARCore or https://developers.google.com/ar/reference/java/com/google/ar/core/Camera#getPose() ?

alexs7 commented 2 years ago

@DavidMohrhardt You meant the camera has to be a child of ARSessionOrigin right ? I am confused because you said "camera parented to the AR Session Origin", which means the camera is the parent of AR Session Origin.

DavidMohrhardt commented 2 years ago

Yes, I mis-phrased it. Edited the comment to reflect the proper phrasing.

DavidMohrhardt commented 2 years ago

Does that pose correspond to https://developers.google.com/ar/reference/java/com/google/ar/core/Camera#getDisplayOrientedPose() in ARCore or https://developers.google.com/ar/reference/java/com/google/ar/core/Camera#getPose() ?

It would be the DisplayOrientedPose

alexs7 commented 2 years ago

From debugging, it seems that z-axis in unity is flipped compared to the android poses.. What is the expected coordinate system in Unity ? I mean for the local camera one (not the world.)

DavidMohrhardt commented 2 years ago

Unity is left-handed, Y-Up whereas ARCore is right-handed Y-Up so a conversion must happen to get the ARCore pose in Unity space.

alexs7 commented 2 years ago

@DavidMohrhardt Ok this makes sense! So this arSessionOrigin.camera.transform will be a left-handed coordinate system but https://developers.google.com/ar/reference/java/com/google/ar/core/Camera#getDisplayOrientedPose() this in a right-handed coordinate system ?

DavidMohrhardt commented 2 years ago

That is correct. Under the hood the ARCore Provider Plug-In performs the conversion before reporting to the input subsystem. The input subsystem then surfaces that information to the Unity input system from which the XROrigin TrackedPoseDriver or the legacy ARPoseDriver will drive the camera pose in the scene.

alexs7 commented 2 years ago

@DavidMohrhardt ARPoseDriver is not legacy is it ? TrackedPoseDrive is if I recall ?

alexs7 commented 2 years ago

@DavidMohrhardt btw are all of this documented somewhere ? Dor example what you just mentioned, is it written online somewhere ? I found ARFoundation docs to only provide the basics.

DavidMohrhardt commented 2 years ago

@DavidMohrhardt ARPoseDriver is not legacy is it ? TrackedPoseDrive is if I recall ?

XROrigin contains a new TrackedPoseDriver that is not legacy. ARPoseDriver is now legacy.

@DavidMohrhardt btw are all of this documented somewhere ? Dor example what you just mentioned, is it written online somewhere ? I found ARFoundation docs to only provide the basics.

The ARFoundation documentation for XR Origin and ARInputManager are the key pieces of documentation for input in terms of ARFoundation.

XRInputSubsystem is a Unity core API and can be found in the main Unity documentation. For platform specific details you should refer to the documentation on those platforms.

alexs7 commented 2 years ago

@DavidMohrhardt Ok XROrigin is a 5.0 version class. I am still at 4.1, makes sense.

Great! Thanks a lot for the links! :)