Proper lightmap workflow and formats

I am trying to bake lightmaps in blender and then use them in babylon app. As it turns out it’s not that straightforward. I am using PBR materials and I specifically want to have separate albedo and lightmap image on the objects.
I have already read this thread on the topic but I still feel like not having full picture.

Let’s say I can generate lightmaps in OpenEXR or HDR as I know that producing PNG lightmaps from Blender does not work as expected (it normalizes channels and uses only RGB channels).
What is the transformation of OpenEXR/HDR to texture format that I can:

  1. Load into Babylon
  2. Use as lightmap rightaway (without tranformations in babylon)

My idea is to have Blender output something that Babylon expects without any additional transformations later.

1 Like

Hey @sebavan @Evgeni_Popov. You mind explaining lightmaps a bit further?

The easiest way to do this is to bake in blender with cycles. Then paste the map to the self-luminous channel and specify the uv.

No code is needed, but it will break the glow. Later in the code you can move it to the lightmap channel to eliminate the effect on the glow.

That’s almost exactly my flow currently. The problem is not how to get lightmap from Blender to Babylon but rather what should be the format of that lightmap coming out from Blender. Right now I can see that Babylon is using proprietary RGBD format which Blender does not generate. I even tried reproducing this as post process step in Blender but it results in banding in the output textures.
I can’t really see a streamlined process to have lightmaps working correctly without hacks that involve manual work.

I actually don’t think it’s a good idea to use RGBD.
Because currently the lightmap channel in babylonjs does nothing more than superimpose the lightmap colors onto the final output color.

That sounds like lightmaps are just broken then? I can see some good looking results with using RGBD though here

But he doesn’t give a comparison, and in reality the RGBD improvement may not be that great.

Later he himself complains about overexposure of the environment

I agree. In my opinion, the topic is still open. I think it’s worth following (I do) but I wouldn’t dare using it in a productivity environment with a strong requisit about colors/exposure. At least, not just yet. Of course, my opinion only.

But that’s not a problem, because the difference between RGBD is simply the use of the A channel to increase the color range.

The existing process remains exactly the same, except for a single sentence texture.isRGBD = true.

Even if you paste the rgbd texture to the self-luminous channel, it should be perfectly fine.

Okay. What is proper way of exporting blender lightmaps which when pasted in babylon will give me exactly the same results as usual Blender render?

RGBD is simply the use of the A channel to increase the color range.

It’s not “simply”, this is the only proper way to have results that look exactly the same as my Blender renders AFAIK yet RGBD is not documented anywhere except for this piece of code, but I still cant produce nice RGBD textures just because I don’t know what exactly does this function expect.

You can basically convert .hdr to RGBD format, which is essentially an image with a transparent channel.

I’m not sure how the native blender baked mapping outputs hdr.
Some baking plugins export in hdr by default or support setting rgbd encoding.

A .rgbd map generally looks just like a normal png map.

But the A channel is used to scale the RGB values. The actual output value is (rgb)/a.

When you set texture.isRGBD = true, this function is automatically used by babylonjs, and the rest is left to babylonjs.

He will populate these functions in the shader.

However, from the definition can be seen, babylonjs in the shader will only reflectionTexture, refractionTexture and lightmapTexture automatically do processing

I am asking about specifically this. How to convert HDR to RGBD? I don’t know what this piece of code expects.

  1. Does it expect vec3 color to be in linear color space?
  2. Does it expect color to be clamped from 0 to 255 or 0 to 1?
  3. When converting .hdr (RGBE) to RGBD with that function, do I take only RGB values or I do something with E from hdr too?

cc @PatrickRyan

  1. The problem is indeed a bit subtle, because I am seeing the lightmapTexture sampled directly in the standard material.

But in pbrMaterial, the conversion from sRGB to linear space is done.

This seems to imply that the LightMap in pbrMateiral requires srgb, while the standard material requires linear. Maybe someone can answer this.

  1. the color is 0-255 there is nothing to say about this.
    Because he is not really high dynamic range, he is using “A” channel to simulate high dynamic range.

  2. I don’t know much about RGBE, but I think their final output values are the same anyway.

Translated with DeepL

I think I know what is going on.
The standard materials are probably always in sRGB space, so there is no need to convert them. So both expect sRGB lightmap.
I don’t know if I’m right


1 Like

Note that you can use this piece of code to convert a .hdr to a RGBD .png:

1 Like

I am aware that I can use that piece of code to convert .hdr to RGBD .png but I’d like to do that already in blender so that I can pass ready .gltf + lightmaps to babylon and have that working rightaway. Do you have any tips on how to convert image to RGBD?

Unfortunately not… cc @PatrickRyan in case he would know.

Maybe we should support a more widespread format for floating point textures, like .hdr or .exr…