Three js, loading texture to only specific mtl

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 23:25:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!