没有绘制Three.js三角形

时间:2014-06-17 02:20:34

标签: javascript three.js webgl

这里是完整的代码:

http://jsfiddle.net/YzR33/

基本上,我试图绘制三角形(并为每个顶点设置颜色),当我设置某些坐标时,它们突然不被绘制。

// Draw 20 triangles, only 5 show up
for (var i=0; i<20; i++) {
    drawTriangle(
        [[4, 10], [1, 5 + i], [10, 10]], // coords
        [0x00ff00, 0xaa0000 + i*256*100, 0x555555 + i*256*100] // colors
    );
}

var triangles = new THREE.Mesh( geometry, material );
scene.add(triangles);    
renderer.render(scene, camera);

...

function drawTriangle(coords, colors) {
    // Triangle counter
    if (typeof this.cnt == 'undefined')
        this.cnt = 0;

    var p1  = new THREE.Vector3(coords[0][0], coords[0][1], 1);
    var p2  = new THREE.Vector3(coords[1][0], coords[1][1], 1);
    var p3  = new THREE.Vector3(coords[2][0], coords[2][1], 1);
    geometry.vertices.push( p1 );
    geometry.vertices.push( p2 );       
    geometry.vertices.push( p3 );

    geometry.faces.push( new THREE.Face3( this.cnt*3, this.cnt*3+1, this.cnt*3+2 ) );

    geometry.faces[this.cnt++].vertexColors = [
        new THREE.Color(colors[0]),
        new THREE.Color(colors[1]),
        new THREE.Color(colors[2])
    ];
}

1 个答案:

答案 0 :(得分:3)

您正在绘制的一些三角形是背面,这是由三角形法线确定的。将side: THREE.DoubleSide添加到您的素材中,您将看到所有三角形。