I encountered an issue while trying to dynamically clone a loaded GLB model and allow the user to position it in the scene. Despite using the model.clone() method, the cloned model ends up appearing at the same position as the original, causing changes in position to affect both models. Moreover, I noticed that both the original and cloned models share the same ID. This made me question whether my understanding of the clone() method is correct, prompting me to consider alternative approaches. I came across the SceneBuilder extension, but extracting the geometry of the GLB model for it remains a challenge.
Here's the code snippet:
class FireExtinguisher extends Autodesk.Viewing.Extension {
constructor(viewer, options){
super(viewer, options)
this.model = null
}
load(){
const modelUrl = './GLTF/fireExtinguisher.glb'
const loadOptions = {
keepCurrentModels: true,
applyRefPoint: true,
}
this.viewer.loadModel(modelUrl, loadOptions, this.onSuccessCallback.bind(this), this.onErrorCallback)
return true
}
unload(){
return false
}
onSuccessCallback(model){
this.model = model
const cloned1 = model.clone()
cloned1.id = 14
const clonedPosition1 = { x: 10, y: 0, z: 0 };
const clonedMatrix1 = new THREE.Matrix4();
const clonedTranslation1 = new THREE.Matrix4().makeTranslation(clonedPosition1.x, clonedPosition1.y, clonedPosition1.z);
clonedMatrix1.multiply(clonedTranslation1);
cloned1.setPlacementTransform(clonedMatrix1);
this.viewer.impl.addModel(cloned1);
this.viewer.impl.invalidate(true);
const cloned2 = model.clone()
const clonedPosition2 = { x: -10, y: 3, z: 0 };
const clonedMatrix2 = new THREE.Matrix4()
const clonedTranslation2 = new THREE.Matrix4().makeTranslation(clonedPosition2.x,clonedPosition2.y,clonedPosition2.z)
clonedMatrix2.multiply(clonedTranslation2)
cloned2.setPlacementTransform(clonedMatrix2)
this.viewer.impl.addModel(cloned2);
this.viewer.impl.invalidate(true);
const newModel = this.clone(model)
console.log(newModel);
}
clone(obj){
var clone = {};
for(var i in obj) {
if(typeof(obj[i])=="object" && obj[i] != null)
clone[i] = this.clone(obj[i]);
else
clone[i] = obj[i];
}
return clone;
}
}
I attempted various methods to break the reference between translation matrices and models, but encountered no success. Efforts to sever ties between the original and cloned models led to other errors.
Your assistance is greatly appreciated.