How to use lightmaps for multiple meshes that share the same material

Hi
I have a scene. I baked the lighting using 3dsmax and vray into textures.
I loaded the scene and loaded the textures into the lightmaptexture propriety of each object’s material.
I have some objects in the scene that share the same multimaterial.
There are two problems:

  1. I can’t apply lightmaps to a mutiMaterial.
  2. I can’t apply the lightmaps for each object seprately if they share the same material (submaterial).

How could I apply the lightmaps to those objects?
it could be nice if the lightmap is a propriety of the mesh instead of the material to solve this problem.
here’s my scene in playground
https://playground.babylonjs.com/#XIB3TP#9

The important part for lightmaps will be the uvs so as long as the meshes have their uvs (usually the secondary set) baked in accordingly it should work even if they share the same material.

Could you try to repro in the playground ?

It might be easier to look into it.

Each mesh has two uv channels.
This is the playground
https://playground.babylonjs.com/#XIB3TP#9

Changing the uv maps does not look like it changes the outcome (targetting the second set on the lightmap texture) https://playground.babylonjs.com/#XIB3TP#12

I am wondering what the issue is here probably an export time issue cause the ground looks fine ???

Maybe @PatrickRyan could help here ? (be mindfull it is thanksgiving week so the answer might be delayed)

I tried some changes to the code, that exposed the problem.

https://playground.babylonjs.com/#XIB3TP#15

Am I doing something wrong?
I hope @PatrickRyan could have a solution.

Maybe @Vinc3r @MackeyK24 @Davidae could help as I know they used lightmaps a lot in their workflows ?

1 Like

thank you for the reply.
I tried to clone the shared material for each mesh but I get an error I don’t know why.
https://playground.babylonjs.com/#XIB3TP#16

@billydev, there are a couple of things I see in your original question and playground that need some clarification. You mention that you want to use a multi-material in the question but a multi-material is used to attach several materials to one mesh by identifying sub meshes through the vertex array.

Your scene has several different meshes in the scene so they aren’t typically using a multi-material since you already have several meshes in the scene. Otherwise, you would need to merge all your meshes and identify where in the vertex array each submesh started to assign the multi-materials and get any benefit from it.

Secondly, you are passing a unique light map to each mesh (baked individually per mesh rather than as one overall light map). The PBR materials you are using don’t have a base color texture, so it really doesn’t matter if they have multiple UV sets as you aren’t using the UVs for texture mapping other than the lightmap. Typically light maps will have one UV set for the base textures to increase texel density and then an atlas in a second UV set with the other meshes in the scene so that they all reside on one texture for the light map. That way you have one light map for the whole scene, no matter what material is used (which also reduces texture downloads and results in fewer textures in memory).

I also noticed that you don’t have any environment light in your scene which kind of defeats the purpose of having a metallic-rough PBR material as you are reflecting nothing from the environment. If you really only need punctual lights, you may want to go with a standard material since it will be a lighter light calculation in the scene.

If you really want to style one material and then swap out the light map per material, there’s no need with your scene to use a multi-material. Just create one material with the base parameters that you want to apply to all of the meshes, and then clone the material and assign a unique name and light map to it. Then apply this new material clone to the mesh that matches the light map. You will end up with the same number of meshes and materials as before.

Hope this helps point you in a direction, but let us know if you have more questions.

Thank you for your response, that is clear.
those lightmaps were generated using VRAY and 3ds max. It generate a texture per mesh.

What workflow are you recommending to get the whole scene in one texture?

Do I need to attach all the meshes in the scene in one mesh and unwrap the whole scene in the second UV?

P.S: I already tried to clone the material for each mesh but it didn’t work
here is the PG: https://playground.babylonjs.com/#XIB3TP#16

@billydev, I made a couple of edits to your PG to demonstrate how you can use material clones, commenting out the multi-material parts. To show that the material clones are working, I assign a random base color to each clone for the scene, but you can easily remove that part.

For baking one light map, you will need to create a second UV set that atlases all islands into one layout. You don’t need to combine the meshes to do this, just make sure that your island layout does not overlap in any way for the lightmap UV set. Something like this:

In this way, you have the normal layout for any texture maps you want to bake and then a second set of UVs atlases so each island has unique UV coordinates. You can then bake out as normal, which will bake one lightmap per mesh and then you can use an image editor to combine each lightmap into one texture. I do this with normal maps all the time since baking normal maps can produce errors if the projection cage intersects with another mesh in the scene. I bake out separate pieces of my mesh in isolation to get a clean normal bake and then combine the normal maps together in a texture editor like Substance Designer to produce my final normal map.

Hope this helps unblock you.