- Home /
"Recalculate" the mesh bounds to the objects rotation?
I have played around with mesh.bounds, renderer.bounds, and collider.bounds but none of them really do what i want them to do. the mesh bounds is accurate to the objects bounding box but only at rotation 0, however this is only in local space and not the world space that i want.
The renderer and collider bounds are the axis aligned bounding boxes so when an object is rotated, the "bounding-box's bounding-box" per-se is taken. this results in bounding boxes that are deceiving when an object is rotated and doesn't represent the apparent size of the object.
Is there any way to get, or at least imitate, a bounding box but where the box is essentially the smallest AABB box that can contain the object with its current rotation?
I'd like to know this too, as I've been struggling with Bounds .. Here
Answer by tomekkie2 · Dec 02, 2016 at 09:31 AM
If you would't care about the performance, you could iterate over all the mesh vertices contained in the GameObject, including their position into the bounds.
Bounds bounds;
MeshFilter[] meshes = GetComponentsInChildren<MeshFilter>();
for(int i = 0; i < meshes.Length; i++) {
Mesh ms = meshes[i].mesh;
int vc = ms.vertexCount;
for(int j = 0; j < vc; j++) {
if(i==0&&j==0){
bounds = new Bounds(ms.transform.TransformPoint(vertices[j]), Vector3.zero);
}else{
bounds.Encapsulate(ms.transform.TransformPoint(vertices[j]);
}
}
}
Note that there are cases where you can't use lossyScale like that. A mesh could have been sheard due to non-uniform scaling and a parent rotation. lossyScale (as teh name suggests) is only an approximation of the world space scale and you shouldn't rely on it. Also meshes[i].gameObject.transform
is redundant, simply use meshes[i].transform
.
Thanks for pointing that out. I have updated the above script.
Answer by Bunny83 · Dec 02, 2016 at 10:27 AM
This question has been asked several times in the past and i wrote already several answers like this one.
ps: If you have several meshes in a rigid configuration (that only moves / rotates as a whole) or a single very complex mesh you might want to pre-calculate the convex hull-points of the mesh / meshes and only calculate the bounds on those vertices. This would reduce / simplify the per-frame overhead.
Answer by CiberX15 · Apr 16, 2019 at 01:05 AM
What I ended up doing to solve this problem for my project was to have a function that stored the target object's rotation, then zeroed it, grabbed the meshs's bounding box while rotation was zero, then restored the rotation.
It's a kind of hacky solution, and I have no idea what it would do if the target was a physics object, but it worked for my needs.
Other potential work around might be adding a box collider then getting the bounds from that, then removing the box collider, though that might come with a greater performance hitch.
This makes no sense. You said you grabbed the mesh's bounding box. The "mesh.bounds" value doesn't change when the object rotates since the bounds are given in local space coordinates. Renderer.bounds and Collider.bounds on the other hand do change when the object is rotated. However if the object is rotated to (0,0,0) it's actually the same size as mesh.bounds, just that mesh.bounds has it's center defined in localspace and renderer.bounds in worldspace.
Your answer
Follow this Question
Related Questions
Size of collider bounding box regardless of rotation 1 Answer
Skinned Mesh Renderers Bounds rotated incorrectly when importing from Maya. 0 Answers
Is the bounds.size and the GetComponent.BoxCollider2D.size of a GameObject meant to be the same? 1 Answer
Flip over an object (smooth transition) 3 Answers
How to project the position of a point relative to a rotated collider using its bounds? 1 Answer