简体   繁体   English

three.js使用按钮更改.obj文件的材料

[英]three.js change material for .obj file using a button

I'm having trouble creating a button to change the .mtl file of my .obj to another .mtl using three.js. 我无法创建一个按钮来使用three.js将.obj的.mtl文件更改为另一个.mtl。 Any thoughts on how to do that would be awesome! 关于如何做到这一点的任何想法都很棒!

Here is the original code, adapted from the objmtl loader example on threejs.org. 这是原始代码,改编自threejs.org上的objmtl加载程序示例。 So far, I've only been able to make the obj visible / invisible using a button, but I'd love to add more buttons to change the .mtl files to reflect .mtls with other colors and properties. 到目前为止,我只能使用按钮使obj​​可见/不可见,但是我很想添加更多按钮来更改.mtl文件,以使用其他颜色和属性来反映.mtls。

var mtlLoader = new THREE.MTLLoader();
                mtlLoader.setBaseUrl( 'examples/obj/male02/' );
                mtlLoader.setPath( 'examples/obj/male02/' );
                mtlLoader.load( 'male02_dds.mtl', function( materials ) {

                    materials.preload();

                    var objLoader = new THREE.OBJLoader();
                    objLoader.setMaterials( materials );
                    objLoader.setPath( 'examples/obj/male02/' );
                    objLoader.load( 'male02.obj', function ( object ) {


                        object.position.y = - 95;
                        dude = object;
                        scene.add( dude );

    info.innerHTML += '<br/><br/><input id=pants2 type="button" onclick="dude.visible = false" value="Dude: OFF"/>';
    info.innerHTML += '<input id=pants2 type="button" onclick="dude.visible = true" value="Dude: ON"/>';

If you can work without the .mtl file, you can use something like that: 如果没有.mtl文件就可以工作,则可以使用类似的方法:

texture = new THREE.TextureLoader().load('dir/to/texture.jpg');
sectexture =  new THREE.TextureLoader().load('dir/to/second_texture.jpg');
lambert = new THREE.MeshLambertMaterial({color: 0xffffff, map: texture});
objLoader = new THREE.OBJLoader();
objLoader.setPath( 'examples/obj/male02/' );
objLoader.load( 'male02.obj', function ( object ) {
    object.traverse(function(child) {
        if (child instanceof THREE.Mesh){
            child.material = lambert;
        }
    });
    scene.add( object );
}, onProgress, onError );

With that, you can load your texture into the lambert material, so if you want to change it you can use lambert.map=texture or lambert.map=sectexture. 这样,您可以将纹理加载到lambert材质中,因此,如果要更改它,可以使用lambert.map = texture或lambert.map = sectexture。

[EDIT] I have fixed the mistake on child.material [编辑]我更正了child.material上的错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM