问题
I am loading obj & mtl file using OBJMTLLoader. There are multiple materials in the object. What I want is to apply texture to a particular material within the object.
Eg. I have a Bag object, and it has Handle, Brand name, Wheels as different material. I want to add different texture to each of them. The texture will not be there in the material file. I want to add it using code only.
<pre>
var loader = new THREE.OBJMTLLoader();
loader.load( 'obj/Test.obj', 'obj/Test.mtl', function ( object ) {
var tex = new THREE.MeshLambertMaterial({map:THREE.ImageUtils.loadTexture( 'obj/texture.png')});
object.traverse( function(child) {
if (child instanceof THREE.Mesh) {
// apply custom material
child.material = tex;
// enable casting shadows
child.castShadow = true;
child.receiveShadow = true;
}
});
</pre>
回答1:
You can do this by checking the name of the material that you have written in your mtl file and then assigning the texture.
Your .mtl file looks like this:
newmtl wheels
Ka 1.0 1.0 1.0
Kd 1.0 1.0 1.0
d 1.0
# ...
and in code:
var wheelsTexture = THREE.ImageUtils.loadTexture('obj/wheelTexture.png');
object.traverse( function(child) {
if (child instanceof THREE.Mesh) {
if ( child.material.name === "wheels" ) {
child.material.map = wheelsTexture;
} else if ( child.material.name === "handle" ) {
// ...
}
}
} );
来源:https://stackoverflow.com/questions/30486570/three-js-loading-texture-to-only-specific-mtl