An elementary strategy
Looking at it in a simplistic way, the phrase "illuminated by" could also be interpreted as "within or crossing over, and directly facing the cone."
Initially, establish whether the face is located inside or intersecting the cone. To achieve this, gather all three vertices of the triangle and form a Vector3
indicating the direction from the spotlight.position
to each vertex.
// Keep in mind: Extracting the vertices of a face varies based on whether it is indexed.
// Consider "vertex1", "vertex2", and "vertex3" as the face's vertices.
// Converting the vertices into World Coordinates
mesh.localToWorld( vertex1 )
mesh.localToWorld( vertex2 )
mesh.localToWorld( vertex3 )
// Obtaining the spotlight's direction of focus
const spotLook = new Vector3().subVectors( spotlight.target.position, spotlight.position )
// Adjust the vertex vectors relative to the spotlight
vertex1.sub( spotlight.position )
vertex2.sub( spotlight.position )
vertex3.sub( spotlight.position )
// Calculating the angles between the vectors
const angle1 = spotLook.angleTo( vertex1 )
const angle2 = spotLook.angleTo( vertex2 )
const angle3 = spotLook.angleTo( vertex3 )
If ANY of these angles is below the spotlight.angle
threshold, then that specific vertex is situated within the spotlight's cone. If all angles exceed the spotlight's angle, then they are all outside the cone.
Next, determine if the face is oriented towards the spotlight by normalizing the vectors between the vertices and finding their cross product.
// These represent the original values of the vertices
vertex1.sub( vertex2 )
vertex1.normalize()
vertex3.sub( vertex2 )
vertex3.normalize()
const crossed = new Vector3().crossVectors( vertex3, vertex1 )
This yields the "face normal," denoting the direction in which the face is pointing. Again, leverage angleTo
to ascertain the angle against the spotlight's direction. If the angle exceeds Math.PI/2
(90°), then the face leans towards the spotlight. If greater than that value, the face tilts away from the spotlight.
If a face meets both criteria—facing towards the spotlight AND having at least one vertex inside the cone—then it can be presumed illuminated.
Considerations
Naturally, this method is rudimentary and delivers fundamental outcomes.
Situations may arise where portions of your shape obscure its own faces (self-shadowing).
The actual normals of the face might hinder its light reception. Even if the face points towards the spotlight, if all the normals are angled away, the shader would not illuminate the face despite being technically within acceptable boundaries.
Instances may occur where the penumbra
of your spotlight prevents a face from being illuminated, even when parts of it lie within the spotlight cone.
These scenarios necessitate consideration to attain your desired results.