I'm currently attempting to switch between multiple 3D models (loaded with OBJMTLLoader.js) that are rendered in my three.js scene. Using dat.gui, I've created a dropdown list of model names where selecting one will add the corresponding obj model to the scene and remove the original one.
In this scenario, I have loaded 2 separate models and added the second one to the scene before setting up the dat.gui controls:
var loader = new THREE.OBJMTLLoader();
loader.load("../assets/models/boardlego.obj", "../assets/models/boardlego.mtl", function (obj2) {
obj2.name = 'lego2';
});
loader.load("../assets/models/boardlego2.obj", "../assets/models/boardlego.mtl", function (obj) {
obj.name = 'lego';
scene.add(obj);
});
camControl = new THREE.OrbitControls(camera, renderer.domElement);
// call the render function
render();
//set up dat.gui controls
control = new function () {
this.Templates = 'shortboard';
}
addControls(control);
}
Next, we have the addControls function:
function addControls(controlObject) {
var gui = new dat.GUI();
gui.add(controlObject, 'Templates', ['shortboard', 'longboard', 'fish', 'funboard', 'simmons', 'parallel', 'gun']).listen();
Both models are successfully loaded, but only one is added to the scene. Is there a way to add the other model when the 'Templates' control is changed? I attempted to create a separate function called updateboard() and invoked it in the render function, like this:
function updateboard() {
if(controlObject.Templates === "longboard"){
scene.add(obj2);
}
}
Unfortunately, this approach didn't yield the desired outcome. Additionally, I tried implementing an if statement within the render function:
function render() {
renderer.render(scene, camera);
if (scene.getObjectByName('lego')) {
scene.getObjectByName('lego').scale.set(control.Length, control.Thickness, control.Width);
}
if(control.Templates === "longboard"){
scene.add(obj2);
}
However, this method also proved unsuccessful. Any assistance or guidance would be highly appreciated! Alternatively, if you can point me towards a relevant example, that would be wonderful too! Thank you in advance.