- Home /
Finding the center point of a model
I'm automatically downloading 3D models from the web for use in a game project. This means I didn't make the models, and have never seen them before. My Unity project places them automatically within the 3D world. I've noticed that these models often do not have their local origin as the dead center of the model. In fact they can be wildly off.
This answer: http://answers.unity3d.com/questions/359023/is-a-mesh-vertice-position-relative-to-the-center.html seems to suggest that scale and rotation play a part in it. But setting scale to 1,1,1 and rotation to 0, the origin is clearly not the middle of the model I'm looking at right now. I assume this is because the modeller build the model off-origin in their modelling program?
This is a huge problem for me. Is there a way to 'reorient' the origin to be the center of the model's mesh, automatically in code?
Answer by Kiloblargh · Jul 20, 2013 at 03:58 PM
Yes. You need to process mesh.vertices, which is an array of local Vector3 positions. You could average all of them, or just find the max and min x, y, and z values and average those. Then you go through again and subtract the offset of the calculated center point from every vertex.
Don't try to change mesh.vertices[n] directly, do it like in the example, where you copy the array, modify the copy, and then write it back when you're done.
Brilliant! Thank you so much. I know nothing about 3D modelling yet so I'd never have got to this.