# Material lightmap texture atlas support

**URL:** <https://forum.babylonjs.com/t/material-lightmap-texture-atlas-support/10522>\
**Category:** Questions\
**Created:** [May 3, 2020, 9:39pm UTC](https://forum.babylonjs.com/t/material-lightmap-texture-atlas-support/10522 "2020-05-03T21:39:02Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [May 3, 2020, 9:39pm UTC](https://forum.babylonjs.com/t/material-lightmap-texture-atlas-support/10522/1 "2020-05-03T21:39:02Z")

</div>

Yo @Deltakosh / @sebavan … Do you think we can add support for lightmap texture atlas. ???

Right now the lightmapTexture **seems** to only support a single full size texture for the lightmap texture2D call… Unity exports lightmaps as a texture atlas that it packs as many material lightmap textures as it can into a single lightmap texture for all the materials in the scene (provided the texture atlas is large enough… otherwise it splits into more than one texture atlas)…

The point is, i dont think the shaders support **sampling** just a portion of the **lightmapTexture**.

So basically all unity exported lightmaps are usesless.

Can we please add support for lightmap texture atlas (or if its in there already … how do i use it ???)

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [May 3, 2020, 10:09pm UTC](https://forum.babylonjs.com/t/material-lightmap-texture-atlas-support/10522/2 "2020-05-03T22:09:50Z")

</div>

Use the uv2 set for the lightmap:

[https://playground.babylonjs.com/#89D6W1](https://playground.babylonjs.com/#89D6W1)

In this sample, the plane with the checkerboard is using the normal uv coordinates, whereas the other one is using the uv2 coordinates calculated so that only a part of the lightmap is used (namely, the part with a yellow/red/purple gradient).

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [May 3, 2020, 10:23pm UTC](https://forum.babylonjs.com/t/material-lightmap-texture-atlas-support/10522/3 "2020-05-03T22:23:05Z")

</div>

i see where you going… you are manually altering the uv2 coord of the geometry… i think…

i would have to alter the uv2 coord on EVERY exported geometry from unity…I dont think that would be very good… Dunno … will have to play with that idea…

i think unoty handles in the shader… probobly fract or something

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [May 4, 2020, 12:11am UTC](https://forum.babylonjs.com/t/material-lightmap-texture-atlas-support/10522/4 "2020-05-04T00:11:54Z")

</div>

Unity must put the lightmap coordinates somewhere too…

As far as I know, using a specific set of uv coordinates is the standard way to support lightmaps.

[https://docs.unity3d.com/Manual/LightingGiUvs-GeneratingLightmappingUVs.html](https://docs.unity3d.com/Manual/LightingGiUvs-GeneratingLightmappingUVs.html)

It seems that’s what Unity is doing, it is putting the coordinates in uv2, so it should be easy for you: just export uv2 as well as uv when you export a mesh.

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [May 4, 2020, 12:42am UTC](https://forum.babylonjs.com/t/material-lightmap-texture-atlas-support/10522/5 "2020-05-04T00:42:39Z")

</div>

I believe i am using the Unity UV2 channel in my GLTF export:

```auto
AccessorId aPosition = null, aNormal = null, aTangent = null, aTexcoord0 = null, aTexcoord1 = null, aColor0 = null;

aPosition = ExportAccessor(meshObj.vertices, true, "vertices");

if (meshObj.normals.Length != 0) aNormal = ExportAccessor(meshObj.normals, true, "normals");

if (meshObj.tangents.Length != 0) aTangent = ExportAccessor(meshObj.tangents, true, "tangents");

if (meshObj.uv.Length != 0) aTexcoord0 = ExportAccessor(GLTFSchemaAddons.FlipTexCoordArray(meshObj.uv), "uv");

if (meshObj.uv2.Length != 0) aTexcoord1 = ExportAccessor(meshObj.uv2, "uv2");
else if (meshObj.uv.Length != 0) aTexcoord1 = ExportAccessor(meshObj.uv, "uv2");

```

But i dont think the UV2’s (mesh.uv2) are offset with the lightmap texture atlas coordinates. I was under the impression that need to be handled in the shader… But you are saying the physical **mesh.uv2** should be offset with the lightmap texture atlas coordinates… If unity is not already doing that for us in the mesh.uv2 … Is that right ???

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [May 4, 2020, 1:03pm UTC](https://forum.babylonjs.com/t/material-lightmap-texture-atlas-support/10522/6 "2020-05-04T13:03:00Z")

</div>

> [@MackeyK24](#):
>
> But you are saying the physical **mesh.uv2** should be offset with the lightmap texture atlas coordinates… If unity is not already doing that for us in the mesh.uv2 … Is that right ???

Yes, I think uv2 are the lightmap coordinates you need. So, if you already export them, then you only need to do `coordinatesIndex = 1` on Babylonjs side on the lightmap textures to get the right display.

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [May 4, 2020, 3:19pm UTC](https://forum.babylonjs.com/t/material-lightmap-texture-atlas-support/10522/7 "2020-05-04T15:19:39Z")

</div>

I had to actually apply the renderer.lightmapSacleOffest to the mesh.uv2 **after** the light baking process

Also I had some mesh instances and shared materials issues… basically if the mesh is using a lightmap via its material.lightmapTexture … it cannot be an instanced mesh

Working pretty good now… looks beautiful out the box unity baked light on complex detailed scenes… great looking baked shadows to

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [May 5, 2020, 4:30am UTC](https://forum.babylonjs.com/t/material-lightmap-texture-atlas-support/10522/8 "2020-05-05T04:30:40Z")

</div>

You @Evgeni_Popov … What about higher quality lightmap texture formats RGBM encoded png files… Or would we have to modify the shader to decodeRGBM packed color after texture2D call for lightmapTexture ???

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [May 5, 2020, 11:41am UTC](https://forum.babylonjs.com/t/material-lightmap-texture-atlas-support/10522/9 "2020-05-05T11:41:15Z")

</div>

I don’t think Babylon supports RGBM encoding, but it does support RGBD.

After you create a texture by loading a RGBD encoded png file, you can do:

```javascript
texture.isRGBD = true;
RGBDTextureTools.ExpandRGBDTexture(texture);

```

Then you can use your texture as usual, no need to decode it in the shader code, **IF** the GPU does support half/full float encoding. If not, the `ExpandRGBDTexture` will do nothing and you will indeed need to handle the decoding in the shader. To help in that matter, you have the `fromRGBD` (to decode) (and `toRGBD` to encode in case you would need to do that) functions in the `ShadersInclude/helperFunctions.fx` file.

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [May 6, 2020, 6:56am UTC](https://forum.babylonjs.com/t/material-lightmap-texture-atlas-support/10522/10 "2020-05-06T06:56:09Z")

</div>

Yo @Evgeni_Popov … What about Skybox CubeTexture made with 6 Sided PNG encoded as RGBD… Is that supported as well… I would love to encode the six sided hdr skybox textures as RGBD 🙂

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [May 6, 2020, 11:11am UTC](https://forum.babylonjs.com/t/material-lightmap-texture-atlas-support/10522/11 "2020-05-06T11:11:08Z")

</div>

As I can see it, you can’t use RGBD encoded files for reflection textures in the standard material.

However, you can easily support it by updating the material from [https://nme.babylonjs.com/?#AT7YY5#6](https://nme.babylonjs.com/?#AT7YY5#6), which is a re-creation of the standard material in the nme.

For PBR materials, you simply have to set `reflectionTexture.isRGBD=true` (`reflectionTexture` being either the reflection texture defined at the material or at the scene level) and the shader will decode the value after sampling the cube texture.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [May 6, 2020, 1:19pm UTC](https://forum.babylonjs.com/t/material-lightmap-texture-atlas-support/10522/12 "2020-05-06T13:19:26Z")

</div>

Also Lightmaps should supports RGBD natively on pbr mat 😉

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [May 6, 2020, 6:54pm UTC](https://forum.babylonjs.com/t/material-lightmap-texture-atlas-support/10522/13 "2020-05-06T18:54:20Z")

</div>

> [@Evgeni\_Popov](#):
>
> For PBR materials, you simply have to set `reflectionTexture.isRGBD=true` ( `reflectionTexture` being either the reflection texture defined at the material or at the scene level) and the shader will decode the value after sampling the cube texture.

Do we still need some sort of **RGBDTextureTools.ExpandRGBDTexture(texture)** call ???

How do we do that for a **CubeTexture** … ???

Or does the PBR shader need to use **fromRGBD**???

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [May 6, 2020, 7:53pm UTC](https://forum.babylonjs.com/t/material-lightmap-texture-atlas-support/10522/14 "2020-05-06T19:53:11Z")

</div>

Just set `material.yourTexture.isRGBD = true` (`yourTexture` can be the reflection, refraction or lightmap texture - reflection and refraction can be a cube texture) and the shader will do the decoding, no need to call `ExpandRGBDTexture`.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [May 6, 2020, 7:56pm UTC](https://forum.babylonjs.com/t/material-lightmap-texture-atlas-support/10522/15 "2020-05-06T19:56:06Z")

</div>

Calling it is a plus cause interpolation will be on linear data and not rgbd which could have artifacts but it will convert only if it can.

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [May 7, 2020, 6:18pm UTC](https://forum.babylonjs.com/t/material-lightmap-texture-atlas-support/10522/16 "2020-05-07T18:18:27Z")

</div>

FYI… Babylon.js now supports RGBD for reflection, refraction and lightmaps automatically when setting texture.isRGBD = true for Standard Materials just like PBR Materials 🙂

No need to expandRGBDTextures (unless you want to like @sebavan says if artifacts are a problem for you )
