Best way to create part of a mesh that acts as a light source

Yeah, the problem with this is that the meshes are instanced and the big boss said that you cannot use includedOnlyMeshes with instanced meshes, or that it only works with the main mesh? Creating lights dynamically that effect instanced meshes with includedOnlyMeshes - #2 by Deltakosh
Or so I at least understood :smiley: So I can’t just instantiate the wall piece and try to tinker with what lights it accepts…

Ohhhhh they are instanced as well so, yup I guess you d need a special trick :frowning:

1 Like

Yeap! That’s why I am currently interested in stencil buffer / shaders… But I would need someone wiser than me to tell me if it viable :smiley:

Yup and I hope @PatrickRyan that we summoned to be this person :slight_smile:

1 Like

@Panuchka, this is an interesting problem and I think I have an idea. Give me a little time to mock it up


Ok, @Panuchka, I think I have something that may work for you with some customization on your end. Basically what we have is this:

The scene is made of 4 instances of a plane with the original plane hidden. The textures on the plane are using the PBR lighting model and there is an environment light in the scene that is lighting the planes. They are all using the same shader that was applied to the source plane.

The “lights” are just meshes with an unlit material that are animating back and forth across the length of the planes. The environment light is also rotating every frame so that we can test all of the lights interacting correctly.

The red and green light on the planes is generated in the shader and uses the position of the meshes to determine where the light on any of the instances is rendered. Note that the shape of the light “cast” from each mesh light is controlled by the procedure in the shader, the sphere casting a circular shape and the cylinder casting a line shape.

They are also respecting the normal map on the texture by casting more light on the surfaces that face the light and being culled from faces pointing away. The mask for this looks like this:

And the full shader for the effect looks like this:

I am using the world postion of the instance minus a vector3 that is pumped into the shader which is taken from each mesh light. This shader can handle two “light” inputs, but you can add as many as you want and performance will allow with their own path in the shader. I would suggest designing your assets to handle a couple of light sources and adding that number of inputs into your shader.

The way I would handle which lights and the light positions contribute to any instance would be to add some nulls or abstract meshes to your meshes that will cast your light. These are the positions you will feed to the shader. If you have a lot of lights to cast into your shader, you will want to set up an instance buffer to hold all the positions and distribute the positions of the closest lights to each instance.

The thing to note is that I am only using the X position of the mesh to drive the effect, but you could modify it to your needs. The important thing here is that we are using world positions to determine how far the light is from any pixel and the shape of the light cast is controllable in the shader.

The playground where you can see this in action is below. Feel free to ping me with any questions. And thanks to @sebavan for the interesting challenge!

Custom Lighting on Instances | Babylon.js Playground (