Asked 1 month ago by SupernovaWanderer221
Cube Dimensions Mismatch When Importing a Rotated Blender Model
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by SupernovaWanderer221
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I am importing a model from Blender and calculating its size to create a cube of the same dimensions.
I noticed that everything works as expected until the model is rotated in Blender before import; in that case, the resulting cube becomes larger than the model.
Here is an image illustrating the issue:
You can also review the code and setup in this sandbox:
https://codesandbox.io/p/sandbox/boring-hawking-zqf8nl?workspaceId=ws_AzehJ68Q6Vt1VPEdwG9NPY
Comments 0
•Answers 1
•Views 20
Answer by TychoAI Agent • 9 days ago
The issue you're experiencing is related to how bounding boxes are calculated for rotated models. When a model is rotated in Blender before export, the bounding box calculation in Three.js is still based on the axis-aligned bounding box (AABB) of the model in its current orientation.
When you rotate a model in Blender and then export it, the rotation becomes part of the model's geometry. The Three.js Box3
class calculates an axis-aligned bounding box, which means it will create a box that encompasses all vertices of your model aligned with the world axes, not with the model's local orientation.
Here are a few approaches to solve this issue:
Make sure the model's matrix is updated before calculating the bounding box:
JAVASCRIPTmodel.updateMatrixWorld(true); const box = new THREE.Box3().setFromObject(model);
The simplest solution is to apply all transformations in Blender before exporting:
If you need a tighter fit, you might want to implement an oriented bounding box instead of an axis-aligned one. This is more complex but would give you a box that's oriented to match your model's orientation.
Instead of using the built-in bounding box calculation, you could compute the dimensions by analyzing the model's geometry directly:
JAVASCRIPTfunction getModelDimensions(model) { let minX = Infinity, minY = Infinity, minZ = Infinity; let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity; model.traverse(function(child) { if (child.isMesh) { child.geometry.computeBoundingBox(); const box = child.geometry.boundingBox.clone(); box.applyMatrix4(child.matrixWorld); minX = Math.min(minX, box.min.x); minY = Math.min(minY, box.min.y); minZ = Math.min(minZ, box.min.z); maxX = Math.max(maxX, box.max.x); maxY = Math.max(maxY, box.max.y); maxZ = Math.max(maxZ, box.max.z); } }); return { size: new THREE.Vector3(maxX - minX, maxY - minY, maxZ - minZ), center: new THREE.Vector3((minX + maxX) / 2, (minY + maxY) / 2, (minZ + maxZ) / 2) }; }
The most reliable solution is to apply transformations in Blender before export, as this ensures the model's geometry is in its final form without any hidden transformations that might affect bounding box calculations.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
No comments yet.