1

I have a human body mesh loaded with OBJMTLLoader, and I want to apply skin.jpg on it.

Loading the texture and OBJ file:

var texloader = new THREE.TextureLoader();
var skinTexture = texloader.load('/test/skin.jpg', function (tex) {
    skinTexture = tex;
    skinTexture.needsUpdate = true;
});
skinTexture.wrapS = THREE.RepeatWrapping;
skinTexture.wrapT = THREE.RepeatWrapping;
skinTexture.repeat.set( 4, 4 );


var loader = new THREE.OBJMTLLoader();

loader.load( '/test/body.obj', 'test/body.mtl', function ( res ) {
    body = res;

    var _body = body.children[0].children[1]
    _body.material = new THREE.MeshLambertMaterial( {
        map: skinTexture
    });
    _body.geometry.computeVertexNormals();

    scene.add( body );

}, onProgress, onError );

and I have a simple cube that I render the skin on:

    var cubeGeometry = new THREE.BoxGeometry( 1, 1, 1 );
    var cubeMaterial = new THREE.MeshPhongMaterial({
        map: skinTexture
    });
    cube = new THREE.Mesh( cubeGeometry, cubeMaterial );
    scene.add( cube );

What I see is this:

enter image description here

The cube has the skin applied, but body only gets the colour of it somehow. If I change map: skinTexture to color: 0xff0000 the body changes colour, so parameter should be the correct one.

This is a closeup of the head, and cube on the background:

enter image description here

skin.jpg dimensions are 256x256.

Any ideas why the body doesn't get the same texture as the cube? Thank you

gman
  • 100,619
  • 31
  • 269
  • 393
Deniz Ozger
  • 2,565
  • 23
  • 27

1 Answers1

1

The problem was the UVs. Thanks to @alex for mentioning, and @Tamlyn for posting and answer about how to generate them. The code I used is in this answer.

It looks like this:

function assignUVs(geometry) {

    geometry.faceVertexUvs[0] = [];

    geometry.faces.forEach(function(face) {

        var components = ['x', 'y', 'z'].sort(function(a, b) {
            return Math.abs(face.normal[a]) > Math.abs(face.normal[b]);
        });

        var v1 = geometry.vertices[face.a];
        var v2 = geometry.vertices[face.b];
        var v3 = geometry.vertices[face.c];

        geometry.faceVertexUvs[0].push([
            new THREE.Vector2(v1[components[0]], v1[components[1]]),
            new THREE.Vector2(v2[components[0]], v2[components[1]]),
            new THREE.Vector2(v3[components[0]], v3[components[1]])
        ]);

    });

    geometry.uvsNeedUpdate = true;
}
Community
  • 1
  • 1
Deniz Ozger
  • 2,565
  • 23
  • 27