I have loaded the GLTF scene which was exported from Sketchup. I am trying to get the meshes actual position in the scene. The console I do not get the mesh positions. If I use child.position.set(1,1,1)
which is reflected in the console log but without using position.set() the position of the mesh not shown.
Below my code
const gltfLoader = new GLTFLoader();
var model = new THREE.Object3D();
gltfLoader.load('images/ann2.gltf', (gltf, el) => {
model = gltf.scene;
model.name = 'model';
// Add gltf model to scene
scene.add(model);
//change color
var newMaterial = new THREE.MeshBasicMaterial({color: 0xffff});
var newMaterial2 = new THREE.MeshStandardMaterial({color: 0xffffff});
scene.traverse(function (child) {
//console.log(child,'sureshhhhh')
if (child.name == "Component#1") {
console.log(child.name, "child_name")
child.material = newMaterial;
}else if (child.name == "Component#2") {
child.material = newMaterial2;
//child.position.set(1,1,1)
var positionA = new THREE.Vector3();
scene.updateMatrixWorld(true);
positionA.setFromMatrixPosition(child.matrixWorld);
console.log(positionA.x+',' +positionA.y+',' +positionA.z);
console.log(child.localToWorld( positionA ),'position comp' );
}
const box1 = new THREE.Mesh(
new THREE.BoxGeometry(2, 2, 2),
new THREE.MeshStandardMaterial({
color: 0xffff00,
name: 'suresh'
})
);
// Retrieves Object3D in the scene
model.add(box1);
//box1.position.set(1, 2, 3);
var axes = new THREE.AxesHelper(15);
model.add( axes );
//this is for get model name
//model = scene.getObjectByName(model.name)
//console.log(model,'dsdddgfdsggfdsgdg')
scene.up
var positionB = new THREE.Vector3();
scene.updateMatrixWorld(true);
positionB.setFromMatrixPosition(box1.matrixWorld);
console.log(positionB.x+',' +positionB.y+',' +positionB.z);
});
const box = new THREE.Box3().setFromObject(model);
const boxSize = box.getSize(new THREE.Vector3()).length();
const boxCenter = box.getCenter(new THREE.Vector3());
console.log(boxSize,'box bounding box')