is returning inaccurate values. I will demonstrate my process in understanding this issue:THREE.Box3.setFromObject(*object*)
I am creating 2 meshes using vertices. The first one using the triangle()
function, and the other using trapezoidForm()
.
var triangle = function (base, height) {
return [
new THREE.Vector2(0, -height / 2),
new THREE.Vector2(-base / 2, height / 2),
new THREE.Vector2(base / 2, height / 2)
];
}
var trapezoidForm = function (base, upperBase, height) {
return [
new THREE.Vector2(-base / 2, height / 2),
new THREE.Vector2(-upperBase / 2, -height / 2),
new THREE.Vector2(upperBase / 2, -height / 2),
new THREE.Vector2(base / 2, height / 2),
];
}
The returned value from the functions are used to create the mesh:
var material = new THREE.MeshPhongMaterial({ color: 0x666666, /*specular: 0x101010*//*, shininess: 200*/ });
var shape = new THREE.Shape(vertices);
var mesh = new THREE.Mesh(new THREE.ShapeGeometry(shape), material);
The mesh is then positioned in the scene, and a bounding box is created:
mesh.position.set(posX, 0, posZ);
mesh.rotation.set(-Math.PI / 2, 0, 0);
boundingBox.setFromObject(mesh);
Next, I aim to find the center of both shapes by calculating it using the bounding box:
var centerX = (boundingBox.max.x + boundingBox.min.x) * 0.5;
var centerZ = (boundingBox.max.z + boundingBox.min.z) * 0.5;
The calculation seems accurate for the triangle but incorrect for the trapezoid.
A console screenshot displays the vertices of both shapes along with their respective bounding boxes. X-coordinate is represented by the first number, and Z-coordinate by the second.
https://i.sstatic.net/g3vqs.png
Expected outcome: The bounding box for the trapezoid should ideally display:
max: X: 200
Z: 200
min: X: -200
Z: -100
An image depicting the current state (triangle shows a minus sign at its center, while the trapezoid does not):