1
votes

I have created a scene with THREE.js. Most of the surfaces are BoxGeometries with zero width and a texture applied. Everything works well, including transparent windows on the house. When I went to add roof, I needed to make angled flat panels and triangular spaces. I decided to use a technique borrowed from another StackOverflow page (How to create a custom mesh on THREE.JS?). The mesh is rendering, but not the texture. I've tried with different texture images and it does change the COLOR of the rendered panel, but still no visible texture. I'm missing something. If I can get this to work, I'll start using more vertex-based meshes to fill in my building. Why isn't the texture rendering?

    //texture
    var texture = new THREE.ImageUtils.loadTexture("shingles.jpg");
    texture["shingles"] = new THREE.MeshBasicMaterial({ map:texture, side:THREE.DoubleSide});

    //roof
    var v1 = new THREE.Vector3(farRight,level+height,back);
    var v2 = new THREE.Vector3(farRight,level+height,front);
    var v3 = new THREE.Vector3((farRight+farLeft)/2,level+(3*height/2),front-(3*width/2));
    var v4 = new THREE.Vector3((farRight+farLeft)/2,level+(3*height/2),back+(3*width/2));

    var geom = new THREE.Geometry(); 
    geom.vertices.push(v1);
    geom.vertices.push(v2);
    geom.vertices.push(v3);
    geom.vertices.push(v4);
    geom.faces.push( new THREE.Face3( 0, 1, 2 ) );
    geom.faces.push( new THREE.Face3( 3, 0, 2 ) );
    var object = new THREE.Mesh( geom, textures["shingles"] );
    scene.add(object);

enter image description here

----------------Updated with full answer-------------------------------

As noted in the accepted answer, I missed adding the UV vectors. After some reading I was able to figure out how they work and get the textures to map appropriately. I'm including the full solution here for future reference. First two snippets of code.

    var v3 = new THREE.Vector3((farRight+farLeft)/2,level+(3*height/2),front-(3*width/2));
    var v4 = new THREE.Vector3((farRight+farLeft)/2,level+(3*height/2),back+(3*width/2));
    var v5 = new THREE.Vector3(farLeft,level+height,back);
    var v6 = new THREE.Vector3(farLeft,level+height,front);
    geom = new THREE.Geometry(); 
    geom.vertices.push(v3);
    geom.vertices.push(v4);
    geom.vertices.push(v5);
    geom.vertices.push(v6);
    geom.faces.push( new THREE.Face3( 0, 1, 2 ) );
    geom.faces.push( new THREE.Face3( 3, 0, 2 ) );
    geom.faceVertexUvs[0].push([new THREE.Vector2(.3, 1),
                                new THREE.Vector2(.7, 1),
                                new THREE.Vector2(1, 0)]);
    geom.faceVertexUvs[0].push([new THREE.Vector2(0, 0),
                                new THREE.Vector2(.3, 1),
                                new THREE.Vector2(1, 0)]);
    object = new THREE.Mesh( geom, textures["shingles"] );
    scene.add(object);

    var v1 = new THREE.Vector3(farRight,level+height,back);
    var v4 = new THREE.Vector3((farRight+farLeft)/2,level+(3*height/2),back+(3*width/2));
    var v5 = new THREE.Vector3(farLeft,level+height,back);
    geom = new THREE.Geometry(); 
    geom.vertices.push(v1);
    geom.vertices.push(v4);
    geom.vertices.push(v5);
    geom.faces.push( new THREE.Face3( 0, 1, 2 ) );
    geom.faceVertexUvs[0].push([new THREE.Vector2(0, 0),
                                new THREE.Vector2(.5, 1),
                                new THREE.Vector2(1, 0)]);
    object = new THREE.Mesh( geom, textures["shingles"] );
    scene.add(object);

Now a picture and an explanation. The two geometries in the code above are highlighted in the picture. Because I got fancy, vertices 3 and 4 are slightly inward making a trapezoid shape. Likewise the UV vertices that correspond to those points are .3 and .7 inward from the corners to make a trapezoid. The triangular geometry has two points at the base and one at the top-middle.

Basically, create the vertices, add the vertices, define the faces, and (this is the part I missed) add UV vertices corresponding to the locations in the texture.

enter image description here

1
You might find this article helpful.gman
@gman Yes, that will be very helpful.BSD
Thanks for filling out your solution. Future folks will appreciate it. :Dmanthrax

1 Answers

1
votes

You will need to add texture coordinates as well.. (aka uv coordinates). There is a field called faceVertexUVs that has to be filled out. You will need to set 0,0 for the top left corner of each face, 1,0 for top right, 0,1 for bottom left and 1,1 for bottom right corner.