Three.js MeshPhongMaterial似乎不受非环境光的影响

时间:2015-10-22 17:52:12

标签: javascript three.js webgl

我有一个函数,它将一对数组绘制为双面曲面。我一直使用threejs几周,但从来没有自定义形状,所以我认为这可能与问题有关。我没有找到任何帮助的互联网答案,虽然我尝试了很多,所以我在这里。

我有两段相关的代码,但JsFiddle有完整的东西。

var ambient = new THREE.AmbientLight( 0x444444 );
scene.add( ambient );

var spotLight = new THREE.DirectionalLight( 0xffffff );
spotLight.position.set( 0, 1, 1 ).normalize();
scene.add(spotLight);

function graph(array1, array2)
{

    var particleCount = array1.length*array2.length,
        particles = new THREE.Geometry(),
        pMaterial = new THREE.MeshPhongMaterial({
            side: THREE.DoubleSide,
            shading: THREE.SmoothShading
        });

    // now create the individual particles
    for (var p = 0; p < particleCount; p++) {


    var row = (p%array1.length),
        col = Math.floor(p/array1.length),
        pX = 2*row - array1.length,
        pY = 2*col - array2.length,
        pZ = (array1[row])*(array2[col]),
        particle = new THREE.Vector3(pX, pZ, pY);

    // add it to the geometry
    particles.vertices.push(particle);
    if (row != 0 && col != 0)
    {
        particles.faces.push( new THREE.Face3( col * array1.length + row, col * array1.length + row - 1, (col - 1) * array1.length + row) );
        particles.faces.push( new THREE.Face3( (col - 1) * array1.length + row, col * array1.length + row - 1, (col - 1) * array1.length + row - 1) );
    }
}

// create the particle system
var particleSystem = new THREE.Mesh(
    particles,
    pMaterial);

// add it to the scene
scene.add(particleSystem);

onRenderFcts.push(function(){
    var angle   = Date.now()/10000 * Math.PI;
    particleSystem.rotation.y   = angle;        
})

}

https://jsfiddle.net/6Lp74xdn/1/

我想我已经完成了关于定向光不会照亮表面的原因的想法。

1 个答案:

答案 0 :(得分:0)

生成顶点和面后,需要调用computeFaceNormals()

因此,只需在图表函数的末尾添加particles.computeFaceNormals();即可。