如何在`OBJLoader` three.js中使用多个纹理?

时间:2018-02-23 09:55:00

标签: javascript three.js

如何在Three.js中使用多个纹理材质?。使用三个纹理和一个3D sofa.obj格式文件。我试了很多次。在我的代码下面。我的代码中有什么错误?

var loader = new THREE.OBJLoader();
              var textureLoader =  new THREE.TextureLoader();   
              threeDTexture = new THREE.ImageUtils.loadTexture( 'models/Sofa/Texturses/paradis_beige.jpg' );
              threeDTexture2 = new THREE.ImageUtils.loadTexture( 'models/Sofa/Texturses/1.jpg' );   
              threeDTexture3 = new THREE.ImageUtils.loadTexture( 'models/Sofa/Texturses/2.jpg' );       
              loader.load('models/Sofa/sofa.obj', function (object) {
                   var geo = object.children[0].geometry;
                   var mats = [threeDTexture, threeDTexture2,threeDTexture3];
                   objct = new THREE.Mesh(geo, mats);
                   object.traverse(function (child) {
                    if (child instanceof THREE.Mesh) {
                        child.material = objct;
                    }
                    });
                    object.position.x = posX;
                    object.position.y = 0;
                    object.position.z = posZ;

                    var size = new THREE.Box3().setFromObject(object).getSize();
                    object.scale.set(width/size.x, height/size.y, depth/size.z);
                    scene1.add(object);

                    console.log(object); 
                    console.log(size);
                    console.log(width/size.x, height/size.y, depth/size.z);

              },
                function ( xhr ) {
                    returnValue = ( xhr.loaded / xhr.total * 100 ) + '% loaded';
                    console.log(returnValue);
                },
                function ( error ) {
                    console.log( 'An error happened' );
                }
            );
            break

1 个答案:

答案 0 :(得分:0)

首先,第2行声明的textureLoader未使用;

第二,垫底垫不是材质阵列,你应该new MeshBasicMaterial(...)来包裹纹理,请参阅文档here

var mats = [threeDTexture, threeDTexture2,threeDTexture3];
objct = new THREE.Mesh(geo, mats); // Mesh will not accept this mats param!