Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect the number of lit faces on a mesh?

Tags:

three.js

webgl

I have a number of objects arranged in a THREE.scene, and I want to calculate or retrieve a relative value indicating how much light each object is receiving from a single PointLight source. Simplified example:

block example

With the light positioned at the camera, Block 1's value might be 0.50 since 3 of 6 faces are completely exposed, while 2 is ~0.33 and 3 is ~1.67.

I could probably do this the hard way by drawing a ray from the light toward the center of each face and looking at the intersects, but I'm assuming it's possible to directly retrieve the light level of each face.

like image 718
robots.jpg Avatar asked Dec 16 '22 23:12

robots.jpg


1 Answers

This code takes the object's global matrix in consideration.

var amount = 0;

var rotationMatrix = new THREE.Matrix4();
var vector = new THREE.Vector3();
var centroid = new THREE.Vector3();
var normal = new THREE.Vector3();

for ( var i = 0; i < objects.length; i ++ ) {

    var object = objects[ i ];

    rotationMatrix.extractRotation( object.matrixWorld );

    for ( var j = 0; j < object.geometry.faces.length; j ++ ) {

        var face = object.geometry.faces[ j ];

        centroid.copy( face.centroid );
        object.matrixWorld.multiplyVector3( centroid );

        normal.copy( face.normal );
        rotationMatrix.multiplyVector3( normal );

        vector.sub( light.position, centroid ).normalize();

        if ( normal.dot( vector ) > 0 ) amount ++;

    }

}
like image 158
mrdoob Avatar answered Jan 19 '23 03:01

mrdoob