Reputation: 180
How can I dynamically change material on my .obj model? Each of my model contains .obj, .mtl and .jpg files with textures. Should I change .mtl file somehow?
In the code below I center geometries of each child mesh of my .obj , then I try to give it texture, but neither commented code nor loader.load is not giving it texture.
var loader = new THREE.OBJMTLLoader();
loader.addEventListener( 'load', function ( event ) {
object = event.content;
for (var i = 0; i < object.children.length; i++) {
THREE.GeometryUtils.merge(geometry, object.children[i].geometry);
}
THREE.GeometryUtils.center( geometry );
//var materials = new THREE.ImageUtils.loadTexture("/obj/stol.mtl");
//mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial( material ) );
var material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
mesh = new THREE.Mesh( geometry, material );
mesh.scale.x = 0.25;
mesh.scale.y = 0.25;
mesh.scale.z = 0.25;
mesh.castShadow = true;
scene.add( mesh );
animate();
});
loader.load( '/obj/stol.obj', '/obj/stol.mtl' );
I'd like to have some buttons on my page that trigger texture changes onclick, e.g
$(function(){
$('#txt_01').click(function(){
mesh.textureLoad("/obj/txt_01.mtl");
}
});
Upvotes: 4
Views: 4331
Reputation: 1377
Just check the examples about loading a texture.
And how to update stuff in three.js , see the "material update" section. https://github.com/mrdoob/three.js/wiki/Updates
Upvotes: 1