Object position issue in threejs when replacing object in a scene

31 views Asked by At

I Want to replace an object (objectToBeReplace) from the threejs scene, with the newObject.

I have written the function below.

export const replaceObject = async (
  objectToBeReplace,
  newObj,
  scene
) => {

  //update matrix world of the object
  objectToBeReplace.updateMatrixWorld(true);
  objectToBeReplace.updateMatrix();


    //replace object with the newobject with the same position and rotation
   newObj.position.copy(objectToBeReplace.position.clone());
   newObj.rotation.copy(objectToBeReplace.rotation.clone());
   newObj.scale.copy(objectToBeReplace.scale);
   newObj.quaternion.copy(object.quaternion);
 

  objectToBeReplace.parent.add(newObj);
  objectToBeReplace.parent.remove(objectToBeReplace);
  newObj.updateMatrixWorld(true);
  newObj.updateMatrix();
 

  console.log("object replaced");
};

I want to replace the cooker from the scene with the newCooker.
Before Replacement
Before Replacement

After Replacement After Replacement

Link for the model https://o.convrse.ai/pgLMzNwabk-0/low/W0a17ghen08x/armchair-cooker_DFSZa8q2zf95wQ6eFdZTD_optimized.glb


I am able to replace the object, buts it's position and orientation is getting changed. I want to retain its position and orientation.
Are there any extra parameters in the Mesh or Geometry which affects the position and orientation of the object ?.

0

There are 0 answers