Lately I've been working with ThreeJS models. I managed to implement a custom function which loads each needed .obj once, and if there are more objects using the same model, clone them instead of loading them again.
The issue is that I need to animate each of the cloned meshes in a specific way.
I created an array of objects, containing a function for each of the meshes that needs to be animated which will be looped and executed when needed. The issue is that only the first model is actually being animated for some reason.
Here's the code I use for animating the meshes.
function loadModel(m){
for(var i = 0; i<objects.length; i++){
if(m.name === objects[i].modelData.model){
mesh = m.mesh.clone();
mesh.position.x = objects[i].positionX * distanceAmplifier;
mesh.position.y = objects[i].positionY * distanceAmplifier;
mesh.position.z = objects[i].positionZ * distanceAmplifier;
mesh.scale.set(1000,1000,1000);
//Adding animation
animations.push(function(){
mesh.rotation.z += 1.0005;
});
scene.add(mesh);
}
}
}
While in the animation I do this:
if(animations.length === objects.length){
animations.map(function(anim){
anim();
});
}
If I load different obj meshes, the first instance of each of them is being animated,but not the clone itself.
I hope somebody who had the same issue could give me a hand in sorting this out