GLTF - Hijacking "specularColorTexture" channel for Lightmap

So in our project entire environments are contained within gltf or glb files, but it is our understanding that gltf doesn’t have official support for lightmaps. My intention, as demonstrated in the attached gltf file is to feed the lightmap (created in blender) directly into the “specularColorTexture” and add a custom property (“specColorToLightmap”:true) on these meshes to let babylon.js know to extract the lightmap. The question here is on behalf of our coders. How might we instruct babylon to use this channel and feed it to babylon’s lightmap to render appropriately? I’ve attached the gltf below, viewable as json if you open in notepad++ or another viewer.

Line 33 is the custom property

Line 44-46
“specularColorTexture”:{
“index”:0,
“texCoord”:1

Line 123 “uri”:“Lightmap1.webp”

gltf example

Note: It has it’s own UV channel as well (texCoord_1) which is already hooked up before export.

Hello! The PBR Material already has a lightmapTexture property: PBRMaterial | Babylon.js Documentation (babylonjs.com), so after loading the material you could simply assign the texture you baked to the lightmap.

This topic might also be useful: Baking Bake Lightmaps Lightmapping (The Texture Kitchen) - Tutorials and tips - Babylon.js (babylonjs.com)

Just to clarify - you already know how to make it work using the lightmap as separately loaded image texture, so the question relates to GLTF parsing, or I missed something?

You might consider looking at proposed glTF extensions for lightmaps. Here is the most current one: FB_materials_modmap by idorurez · Pull Request #1658 · KhronosGroup/glTF (github.com). Maybe you can create a custom loader extension to handle it?

1 Like

Just to be clear, what we are trying to accomplish is a hack for the moment to get it working on a short timeframe. We want to map the lightmap texture to the “wrong” node in Blender e.g. specular so that the lightmap texture comes into Babylon.js mapped, just in the wrong node. Then, we want to simply say “Oh, we want to take the specular and use that as our lightmap texture in Babylon”, is that a possibility?

You need to know also which meshes would use this lightmap and then assign the lightmap to its materials.

Ahh, didn’t realize this is a quick hack. You can probably do something similar to this and change the order to something smaller such that it takes priority. If you still need KHR_materials_specular to work if specColorToLightmap is not specified, then this is more difficult with the current set up. If you can move the specColorToLightmap property to the KHR_materials_specular extension itself, then the function will have access to it to determine whether to execute it or not. loadMaterialPropertiesAsync of a loader extension can return null which will indicate to the loader that the loader extension is not going to parse the extension.

That all said, all of this trickiness goes away if you can define a separate extension. That will make things easier.