Get Meshes Touched by a Light

Is there a way to get the meshes touched by a light?

For example in this playground:

It’s possible to get an array with the meshes player1 and player3, but not the player3, using the Light element or another one?

I found this in the documentation: Lights - Babylon.js Documentation . There is a section that says the following:
" when a light is created all current meshes will be lit by it. There are two ways to exclude some meshes from being lit. A mesh can be added to the excludedMeshes array or add the ones not to be excluded to the includedOnlyMeshes array. The number of meshes to be excluded can be one factor in deciding which method to use. In the following example two meshes are to be excluded from light0 and twenty three from light1 . Commenting out lines 26 and 27 in turn will show the individual effect.

1 Like

Thanks for answer.
The problem with the light’s excluded and included arrays is that are static. I mean that them are setted by manually, and them not reflect if the light is actually hitting the mesh, instead it shows if the light can hit (included) or not (excluded) the mesh.

In other hand, you say that as soon the light is created, it lit every mesh, so i think that the property i’am searching don’t exist for the Light object.

I been thinking a while and maybe the solution be cast some rays from the player position in the 360 grades and with the range of the light, and the get the players meshes they hitted.

Thanks anyway :slight_smile: maybe someone has another idea.

Unfortunately this is computed in the shaders. the CPU has no idea which meshes is within light reach (And honestly I would love to get that info because it could help a lot)

Thanks @Deltakosh.
Yes, i was very sceptical with this.
Finally i’m implemented it using raycast between the players, that are being updated each time them moves.

I don’t see a simple way to do it without compute shaders…

What you could do:

  • generate a texture A by rendering the scene using a shader that outputs only the shadow property of the fragment shader => for the standard and PBR material you have a variable in the fragment shader named shadow that is 1 if the pixel is fully lit or between 0 and 1 depending on the amount of light received.
  • generate another texture B by rendering the scene using a specific color for each mesh. The material should be unlit so that this color is the same for all pixels of the mesh

Now, scan B and A in parallel: where B is the color of a mesh (and you know which mesh thanks to the value of the color), look for the corresponding value in A. If A is 1 the pixel is fully lit, else not (so it is in shadows). Do this for all pixels of B. If at the end all the pixels for a mesh are fully lit, then this mesh is fully lit, else it is at least partially in shadows.

It’s quite involved because you have two rendering to perform (not a big problem), but more importantly you have to get the two textures back to CPU and you must scan them, which may bit a bit slow if you want to do this in realtime.