How to reset the position of BufferGeometry of gltf model

Hi there,

I’m trying to put all the parts of the gltf model to the center while keeping the placement ot the parts each other and the original offsets between parts each other.

However, my code below which is recursive function put the whole parts to the center (or origin) like the image.

let uri = '../pointclouds/gltf/LoopBridge/';
		let filename = 'Updated Kawazu Nanadaru Loop Bridge_detached.gltf' // this one was adjusted to reset to origin by dimension.
		let loader = new GLTFLoader().setPath( uri );
		loader.load( filename, function ( gltf ) {

			let object = gltf.scene;
			let obj3d = object.children[0];
			gltf.scene.position.set(position.x, position.y, position.z);

			centerAllGeometry(obj3d);

			function centerAllGeometry(obj) {

				obj.children.forEach(e => {

					if (e.type == "Mesh"){
						console.log("centered")
						return e.geometry.center();
					}

					else if(e.type == "Object3D" | e.type == "Group" | e.type == "Scene"){
						console.log("object3D or group")
						return centerAllGeometry(e)
					}
					else {
						return console.log("unknown type", e);
					}

				});
				return obj3d
			};

			viewer.scene.scene.add(object);

Ideally, it should be like this to the center(sorry about that it is dark):

The hierarchy example:


I’m very thankful for this forum.
Please give any advise you have.
Thank you in advance. :grinning:

You could either set the pivot point already in your 3D software. In blender you can move your objs around and then apply the transforms. That means the objs stays at the same position, but the pivot point is set to the center of the scene.
Or you could put all your objs inside another empty Object3D and use that as a pivot point.
so the outer obj is set to 0, 0, 0, but the actual mesh is positioned as desired.

1 Like

Thank you for answering.
I want to be able to transforms to the origin by code not software.
I think even you put an object to an empty Object3D, the Geometry object still has the position so the distance from the pivot point is still the same. Actually I already tried it.

I think even you put an object to an empty Object3D, the Geometry object still has the position so the distance from the pivot point is still the same. Actually I already tried it.

Yes the geometry obj still has a position of for example (10, 0, 0), but the empty obj is at (0, 0, 0).
The geometry obj will move with the empty obj because its nested. So you have to treat the empty obj as your actual geometry obj.

If thats not what you want, you have to be a little more specific. Its always best to make a live demo, to show the problem, using jsFiddle i.e.

1 Like

I understand what you meant.
And also you help me get idea of the code!

            const box3 = new THREE.Box3();
			box3.setFromObject(object.children[0]);

			console.log("box3 center", box3.getCenter());
			let pos_box = box3.getCenter()

			object.position.set(-pos_box.x, -pos_box.y, -pos_box.z);

			let modelParent = new THREE.Object3D();
			modelParent.add(object);

			console.log("parent of gltf model",modelParent);
			viewer.scene.scene.add(modelParent);

I appreciate a lot @Fluqz :slight_smile:

1 Like