How to use lightmaps with the Node Shader and the PBRMetallicRoughness node

Hey all,
So I have been playing around with node materials and am using a PBRMetallicRoughness node. What I would like to do is plug in a light map for my over all lighting but I am struggling on how best to do that. I have worked a fair amount in the past using PBRMaterial and setting up a light map for those is pretty straight forward. However, as I don’t see a light map attribute for PBRMetallicRoughness I am not sure how best to go about setting things up.

Anyway, I am hoping that someone may be able to point me to an example of how best to take advantage of lightmaps using the node material and PBRMetallicRoughness.

adding @Evgeni_Popov

In the PBR material, the lightmap color is simply either added or multiplied (if lightmap is used as shadow map) to the final color output.

For best results, your lightmap color should be in linear space, so the final output color of the PBRMaterial should also be in linear space but the PBRMetallicRoughness.lighting output is in gamma space.

To work in linear space, instead of using PBRMetallicRoughness.lighting you should add up all the individual outputs of the PBRMetallicRoughness block (from AmbientClr to refraction - you can omit some if you don’t use the corresponding feature - for eg, you don’t need to sum with the sheen outputs if you don’t use sheen) and use the result to add or multiply with your linear lightmap color. You can convert the final result to gamma space by using the Convert to gamma space switch of the FragmentOutput block.

Thanks Evgeni_Popov, I think I understand what you are saying there. Let me give it a shot and I will post an example of what I come up with to make sure I understood you correctly.

@Evgeni_Popov here is that example. I think I got it right. :slight_smile:
https://nme.babylonjs.com/#9PDRGG#2

Yes, that’s fine!

Note that if the lightmap has been designed in gamma space, you need to enable the “Convert to linear space” switch on the lightmap texture.

Also, the texture you plug into the baseColor entry of the PBR block should probably enable the “Convert to linear space” switch as it is normally authored in gamma space.

Ah, thanks good to know. :slight_smile:

Ok, so I tested the shader network I had posted earlier but outside of nme and for the life of me cannot get the shader to compile do to errors connecting the Ind outputs. I then tried to simplyfy my problem by removing the add nodes and connecting the diffuseInd directly into the rgb on the fragment slot. While the shader compiles in nme when I take the resulting code and try to use it, I get an error. However if I connect the dir contribution in to the rgb of the fragment slot is works. I am so confused. :thinking: Here are some links illustrating what I mean:

Links:
nme link: https://nme.babylonjs.com/#9PDRGG#5
playground link: Babylon.js Playground

This PR will fix the problem:

The output names were not ok. Waiting for the PR, you can make your PG work by replacing diffuseInd with diffuseIndirect (line 182):

https://playground.babylonjs.com/#MG1VXG#1

Ah, that is super cool. It works. Thank you so much. :slight_smile: