PointLight - Possible to have a sharp drop in intensity?

For a personal project, I would like to set up a light with the following characteristics:

1 - Instead of having its intensity decreasing by 1/r2, I’d like to have its intensity at 100% for a radius < x and at 0% for a radius > x.
2 - The point 1 assumes a light constrained by a sphere of radius x. Would it be possible in general to constrain a light inside a solid boundary? eg. Light intensity at 100% inside the solid and 0% outside?

You will have to write custom shaders for this.

Here’s an example for a standard material and for your case 1:

https://playground.babylonjs.com/#YECMXJ

It’s a bit involved, as you have to overwrite the function calculating the light contribution.

For a point light (and directional light), it is the computeLighting function. It is called from the lightFragment include, so basically you have to:

  1. make your own customLightFragment include by copying the content of lightFragment and replace computeLighting by customComputeLighting - lines 72 to 76 in the PG
  2. provide an implementation for customComputeLighting - lines 35 to 69 in the PG. I have copy/pasted the content of the existing computeLighting function and only changed the lines 44-45

I have used a CustomMaterial in the PG that makes this relatively easy.

Regarding your point 2 it’s more difficult, you will have to write code that determines if the point to be shaded (vPositionW) is in the volume or not.

1 Like

Thanks for your answer Evgeni!

Even though it’s involved, at least I know it’s possible to do.

Thanks!