7

I successfully applied a texture to a cube geometry with this:

var geometry = new THREE.CubeGeometry(10, 10, 10);
var meshMaterial = new THREE.MeshPhongMaterial({ transparent: false, map: THREE.ImageUtils.loadTexture('/app/images/wood.jpg') });
meshMaterial.side = THREE.DoubleSide;
var mesh = new THREE.Mesh(geometry, meshMaterial);

With this I get a nice textured cube like this:

enter image description here

Now I want to apply the same texture (512x512 jpg image) to a custom model I'm loading from an STL and this is what I get (in this case a pyramid):

enter image description here

This is the code:

loader.load(jsonParam.url, function (geometry) {
            var meshMaterial = new THREE.MeshPhongMaterial({ transparent: false, map: THREE.ImageUtils.loadTexture('/app/images/wood.jpg') });
            meshMaterial.side = THREE.DoubleSide;

            var mesh = new THREE.Mesh(geometry, meshMaterial);

            mesh.castShadow = false;
            mesh.receiveShadow = true;
            scene.add(mesh);
        });

Why the texture is not being applied and I get only what seems to be an average of the texture colors?

Andres
  • 6,080
  • 13
  • 60
  • 110

1 Answers1

8

You need UV mapping.
You can either edit the model in modelling software to add UV coordinates or maybe generate them as in the answers posted here.
I suppose another option would be to create your own shader that maps the texture to the model surface without using UV coordinates.

Community
  • 1
  • 1
2pha
  • 9,798
  • 2
  • 29
  • 43
  • 1
    Is there any way to produce the UV Map programmatically as in the link above but for non-planar surfaces? I don't care if the texture get's a little distorded – Andres Jan 22 '16 at 03:06
  • 1
    You would have to apply some other type of [projection method](http://softimage.wiki.softimage.com/xsidocs/tex_basicproc_TypesofTextureProjection.htm) but that could probably not be used as a generic method for all the models you have. This is why so many UV mapping software packages are available. – 2pha Jan 23 '16 at 02:31