Unity-Technologies / arfoundation-samples

Example content for Unity projects based on AR Foundation
Other
3.03k stars 1.13k forks source link

How to efficiently read an ARFace mesh's vertices #638

Closed FrankSpalteholz closed 3 years ago

FrankSpalteholz commented 3 years ago

Hello, I'm trying to get access to the vertex-positions of the ARFaceMesh during runtime (on iOS), but I'm facing a memory-leak (grows on and on). Here is a small snippet from my code inside a loop. Would be great if you could help me pointing out what I'm doing wrong. Thanks so much in advance!

const int m_EyeCornerVertRT = 1193; (decleration outside loop in Start()) Vector3 m_EyeCornerVecRT = transform.TransformPoint(m_FaceMeshFilter.mesh.vertices[m_EyeCornerVertRT]);

I've tried to follow this link. But the ARFace-object doesn't have a "TryGetFaceMeshVertices(List)"-method. In the description below the object refers to an "XRFace" type. Do i have to cast from ARFace to XRFace somehow?

https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@1.0/api/UnityEngine.XR.ARFoundation.ARFace.html#UnityEngine_XR_ARFoundation_ARFace_TryGetFaceMeshVertices_List_Vector3__

tdmowrer commented 3 years ago
m_FaceMeshFilter.mesh.vertices

Will copy the mesh vertices into a new managed array each time you access it (see the docs here). It will eventually be garbage collected, but not right away, so you will see memory grow for a bit. This is an inefficient way to access to vertices in the face mesh.

The ARFace has a vertices property which gives you access to a NativeArray (not a copy) containing the vertices.

FrankSpalteholz commented 3 years ago

Tim! That was so obvious and I could swear, that I've already tried that. But it works now! I really want to take the chance and thank you for your (always) quick and helpful hints and guidance. Much appreciated!