If you change the texture/map of a material, it is important to set material.needsUpdate = true
.
.needsUpdate : Boolean
Specifies that the material needs to be updated at the WebGL level. Set it to true if you made changes that need to be reflected in WebGL.
This property is automatically set to true when instancing a new material.
Here is some code, how I would implement it. I would also use TextureLoader
instead of ImageLoader
.
// init
var obj;
var objLoader = new THREE.OBJLoader(manager);
var textureLoader = new THREE.TextureLoader(manager);
// load model
objLoader.load('models/obj/dla/dla.obj', function(object) {
obj = object;
loadTexture('models/obj/dla/dla.jpg', obj)
});
// load texture
function loadTexture(path, object) {
textureLoader.load(path, function(texture) {
object.traverse(function(child) {
if (child instanceof THREE.Mesh) {
child.material.map = texture;
child.material.needsUpdate = true;
}
});
});
}
// onclick handler
function onClick() {
loadTexture('models/obj/dla/anotherTexture.jpg', obj);
}
If you don't use the old texture anymore, you might want to dispose it from memory.
...
if (child.material.map) child.material.map.dispose();
child.material.map = texture;
child.material.needsUpdate = true;
...