0
\$\begingroup\$

I have OBJ files that I'm loading into my scene at runtime. They all have positions offset from zero.

These positions appear to be inherent to the .obj file as they were set in the 3D modelling program that they were exported (from Rhino).

Unity seems blind to these offsets: if I upload all .obj's to Unity's worldspace position 0,0,0, they'll each be positioned differently based on their inherent offsets but Unity only seems to understand their positions being at 0,0,0.

Is there any way to calculare the distances between these objects in Unity, in this case?

I need both the objects to have their offsets from the modelling software but I also need to measure the distance between them.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The "position" of an object is the location of the (0,0,0) point in the object's local "object space" coordinate system — the local origin.

If the mesh has been exported so that all its vertices are located somewhere away from (0,0,0), then you'll see this effect where the visible part of the object is offset from what the engine thinks of as its position. Those offsets are just part of the positions of each vertex, measured from the local origin — there's no intermediate layer of hierarchy between object space and the mesh within object space that stores where the nesh "really is". Each vertex is allowed to tell a different story here.

So you can't really recover where the mesh is "supposed" to be relative to the local origin. Any point is as valid a candidate as any other, and picking a good one requires some human interpretation. Common choices might be taking the centroid of all the vertices, the center of an axis aligned bounding box, etc., but some objects will need this point hand-placed at a structural feature that sits-off center.

What you can do is re-export your objects so their local origins are where you want them to be within the visible mesh, then place them inside container objects, offset from the container by the displacement you need to put the mesh in the right place in your scene. That level of hierarchy will let you get two special points: the spawn point in the world (origin of the top/container level) and the object's logical position for measurement purposes (origin of the bottom/mesh object level).

If keeping your export as-is is important, then you can reverse this, and hand-place a marker object as a child of each mesh, positioning it where you want to measure their positions from, relative to their visible mesh. When you want to do your measurements, take the world space positions of these marker objects as your inputs, rather than the positions of the mesh objects themselves with their offset origins.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for this detailed response. I was afraid this would be the case. I'm unable to zero the models before exporting for reasons, but thankfully they're simple rect geometries so I'm able to use objTransform.GetComponent<MeshFilter>().mesh.vertices.Distinct().ToArray() to understand their worldspace position \$\endgroup\$
    – A__
    Commented Aug 1, 2023 at 14:33
  • \$\begingroup\$ I think posting an answer showing that process would be beneficial to future readers. I'd definitely upvote it, if you'd be willing to take the time to document it to share. \$\endgroup\$
    – DMGregory
    Commented Aug 1, 2023 at 17:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .