简体   繁体   中英

Three.js MeshPhongMaterial doesn't seem to be affected by non-ambient lights

I have a function which graphs a pair of arrays as a double-sided surface. I have been using threejs for a few weeks, but never with a custom shape so I thought that might be related to the issue. I didn't find any internet answers that helped, although I tried many, so here I am.

I have these two pieces of code which are relevant, but the JsFiddle has the whole thing.

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);

and

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/

I guess I have just run out of ideas on what could be the reason the directional light doesn't illuminate the surface.

After you generated the vertices and faces you need to call computeFaceNormals() .

So, just add particles.computeFaceNormals(); at the end of your graph function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM