Unity-Technologies / arfoundation-samples

Example content for Unity projects based on AR Foundation
Other
2.98k stars 1.11k forks source link

How to spawn real size prefabs at Image tracking? #1047

Closed marck0zz closed 1 year ago

marck0zz commented 1 year ago

Hello all,

I'm making an app that show some 3D desings when its corresponding image is tracked with ARFoundation in android but the thing is that it should be at real world size.

I'm traying to figuring out how the prefab is scaled and spawned once the image is tracked. If I put the same prefab in an simple AR scene (just scan for a plain and instantiated it) it spawn with its real size.

to assign the corresponding prefab I'm making:

imagen

And I'm changing the scale by "playing" with "scale" variable in the next code: imagen

But it is more like a "guess" rather than a actual solution.

My Questions: 1.- How the prefab is scaled and instantiated once the image is tracked? 2.- Is there a way to calculate or set the actual size of the prefab once is spawned? 3.- Is there a way to spawn the prefab at real world size in the Image Tracking system?

My question is very similar or quite the same as this case #565 but it doesn't work for me, the prefab is showed with smaller size always.

I'm using: Unity 2021.3.12 ARFoundation 5.0.2

Any advice or even point me to the right direction will be appreciated. Thanks in advance.

andyb-unity commented 1 year ago

Hi @marck0zz,

AR Foundation does not affect the scale of your prefab at all when you Instantiate it. Note that you are simply calling Unity's Instantiate API in your code above. You might want to consider:

Hierarchy. If any Transforms in the hierarchy above your prefab have non-identity local scale values, these values will affect the scale of your prefab. Make sure that the scales of all Transforms from the scene root to your Prefab are set as you want them.

Distance. AR objects might look smaller when they are actually further away. You may want to log some debug depth calculations or otherwise ensure that your object is indeed rendering at your desired distance from the camera.

Image Size. You can use the Physical Size property of reference images in your reference image library to ensure that ARCore detects your image at the correct scale: https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@5.0/manual/features/image-tracking.html#create-a-reference-image-library

Hope that helps!

marck0zz commented 1 year ago

Thanks for the response!,

I made some test based on your suggestion.

I realized that I'm was instantiated the prefab as a child of the "ARTrackedImage" been tracked. So I Logged the Local Scale of my ARTrackedImage at runtime and It has the Physical Size that I assign to the image in the reference library (Divided by the "scale" factor I put in the code above). I though this "ARTrackedImage.size" was just the size of the texture and serve as reference for the image to be tracked.

So if I put the width of my image at let say ".10m", and a "scale" factor of "2" then by the above code my ARTrackedImage will have a local scale of (.05, .05, .05) wich make my prefab, as a child of that ARTrackedImage, so small. Assign an identity local scale to may ARTrackedImage make my prefab more "real size" looking. I still need to check the Distance depth that you mentioned but this seems to fix my issue.

ankur-unity commented 1 year ago

Closing this as it has been resolved. Feel free to reopen the issue if you have follow-up questions.