简体   繁体   English

Three.js objloader + texture

[英]Three.js objloader + texture

I have been using Three.js for a few weeks now, I managed to apply a texture to a cube created directly via the code, but once I tried to load an OBJ file with OBJLoader I noticed that I couldn't use the same method to load simple png or jpg textures with TextureLoader . 我已经使用Three.js几周了,我设法将纹理应用于通过代码直接创建的多维数据集,但是一旦我尝试使用OBJLoader加载OBJ文件,我注意到我无法使用相同的方法使用TextureLoader加载简单的png或jpg纹理。

Now I'm not entirely sure what is going wrong, if either this is not the right way to do it, if I just can't load a simple image fine or if I'm not applying it right. 现在我不完全确定出了什么问题,如果这不是正确的方法,如果我只是无法加载一个简单的图像或者我没有正确应用它。

I've seen that applying a texture seems quite easy to do with an MTL file, but unfortunately I'm not sure how to make one. 我已经看到应用纹理似乎很容易用MTL文件,但不幸的是我不知道如何制作一个。

My current code looks like something like this: 我当前的代码看起来像这样:

        var loader = new THREE.OBJLoader( manager );
        loader.load( 'models/tshirt.obj', function ( object ) {

            object.position.x = 0;
            object.position.y = -200;
            object.scale.x = 10;
            object.scale.y = 10;
            object.scale.z = 10;
            obj = object;
            texture = THREE.TextureLoader('img/crate.png'),
            material = new THREE.MeshLambertMaterial( { map: texture } );
            mesh = new THREE.Mesh( object, material );
            scene.add( mesh );

        } );

But it just doesn't seem to work. 但它似乎没有用。 The model doesn't load and gives me random errors from Three.js. 该模型不加载并从Three.js给我随机错误。 If instead of the code above I change scene.add( obj); 如果不是上面的代码,我改变了scene.add( obj); the model actually loads. 模型实际加载。

What should I be doing here? 我该怎么办? Should I just go ahead and try to make an MTL file? 我应该继续尝试制作MTL文件吗?

My full working code can bee seen at http://creativiii.com/3Dproject/old-index.html 我的完整工作代码可以在http://creativiii.com/3Dproject/old-index.html上看到

EDIT: The error I get when adding mesh instead of obj is 编辑:添加网格而不是obj时得到的错误是

three.min.js:436 Uncaught TypeError: Cannot read property 'center' of undefined

Try this code: 试试这段代码:

var OBJFile = 'my/mesh.obj';
var MTLFile = 'my/mesh.mtl';
var JPGFile = 'my/texture.jpg';

new THREE.MTLLoader()
.load(MTLFile, function (materials) {
    materials.preload();
    new THREE.OBJLoader()
        .setMaterials(materials)
        .load(OBJFile, function (object) {
            object.position.y = - 95;
            var texture = new THREE.TextureLoader().load(JPGFile);

            object.traverse(function (child) {   // aka setTexture
                if (child instanceof THREE.Mesh) {
                    child.material.map = texture;
                }
            });
            scene.add(object);
        });
});

The texture loading changes was taken from: How to apply texture on an object loaded by OBJLoader? 纹理加载更改取自: 如何在OBJLoader加载的对象上应用纹理? .

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

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