I’m trying to load GLTF objects and change their pivot values using object traverse and geometry values. But when centered the object gets disassembled and won’t look like it anymore. Here is my code:
is there any way to get the exact desired pivot position of an object without changing original properties of an object loaded?
check this codepen https://codepen.io/saiviru/pen/abbWQpX
I’m trying to translate values and gltf object child elements are changing.
I think you should confirm a same translation for every geometry. For example, remove child.geometry.center();, just translate geometries with same value.
Hey, thanks for the reply.
I tried translate, it’s the same and it won’t work on all the models.
And also (0,0,0) doesn’t center on the object. Need an efficient solution that works on almost all the gltf and glb models.
Thanks.
I can’t see any image in that codepen.
I think you should compute the bounding box of the whole object. And then translate the geomteries by the center of the bounding box.
I used Box3 to get the center and I tried applying that center to translate but still the same issue. Updated the code in the codepen https://codepen.io/saiviru/pen/abbWQpX
I still can’t see any object int the codepen.
But I would do like this(not tested) to set the pivot of an object to (0,0,0):
var bbox = new THREE.Box3();
var cent = new THREE.Vector3();
bbox.setFromObject(object);
bbox.getCenter(cent);
console.log("actual center ", cent);
object.traverse(function(child) {
if (child instanceof THREE.Mesh) {
child.geometry.translate(-cent.x, -cent.y, -cent.z);
}
});
I test it and find that it really flies. I have to admit that I have no experience on gltf loading. But if anyone else wants to help and can’t see any object in saiviru's codepen, please check this: https://codepen.io/toniff/pen/MWWvKRN
@saiviru
Hmm! I’m sure everybody that can help are also busy with other stuffs, but it’s pretty easy if you just remove all the parent objects and attach it to a singular pivotMesh, and move it instead:
Hey, thanks for taking time and replying, I tried adding controls to pivotMesh but I get transform controls added at one of the corners of the object not at the center.