This problem makes me crazy. Load a .FBX model with lightmaps

Hi everyone! I come to expose this problem that makes me crazy for the last days.

My intention was very simple: create a model with lightmaps in FBX format and load that model in my three.js app. I create a lightmap using different software like Unity, Blender, SimLab, etc. And in all I can get the model and the lightmaps in separate files.

My lightmap:

My three.js render:

My code:

                loader.load(obj.file, (fbx) => {
                    let texture_box = new THREE.TextureLoader().load('textures/crate.gif')
                    fbx.traverse((child) => {
                        if (child.isMesh) {
                            new EXRLoader()
                            .load('models/Lightmap-0_comp_light.exr', ( texture, textureData ) => {
                                var newMaterial = new THREE.MeshStandardMaterial( { map: texture_box } );
                                child.material = newMaterial
                                child.material.lightMap = texture
                                child.material.lightMapIntensity = 10
                            });
 );
                        }
                    })

...

I cant obtain a correct lightmap application and I tryed everything tha all. I changed the channel (0 to 1, 2, 3) attribute, I changed and move the content of UV attribute in runtime because I thought that the problem was in the order of this attributes content, I try change flipY attribute and nothing works!

Does somebody know what I did wrong?

Regards!

I discover that if I use this params provided by Unity I can set correctly the lightmap like this:

image

child.material.lightMap.offset = new THREE.Vector2(0, 0);
child.material.lightMap.repeat.x = 0.78125
child.material.lightMap.repeat.y = 0.78125

But I need that the set be automatically and I can’t find this values in the FBX (ASCII format), I can’t find in the textureData meta-info and anywhere.

I have very limited experience with lightmaps. When they do not appear as expect, usually the problem is because of some discrepancy between UV coordinates in the geometry and the texture.

In the image below, the lightmap in the thumbnail looks like it is applied to the floor as it is (I have marked vertices – A maps to A, B to B, …). However, the thumbnail is rather small and I cannot see it well, but it looks like a lighmap of the whole scene (the floor + the cube). So, my guess is that you apply the lightmap of the whole scene onto just the floor.

I’m sorry that most likely I’ll not be able to help you, but if you do not get a solution and you have no idea what else to do, you may try to investigate the issue with texture/uv mismatch.

For debug purposes, you may manually add contrasting characters in the lightmap texture and visually inspect where they appear in the scene.

1 Like

Thanks for your response.

I think that the main problem is very simple, the model .FBX dont have any information about the lightmap coordinates. See this test: When I change manually the coordinates for the plane mesh, the lightmap is applied correctly:


image

This information is inserted manually copying of the Unity environment.

image

I exported the .FBX in ASCII mode and tryed to find this number “0.78125” in the file to confirm the existence of this value, but this value dont appear, my conclusion is that the model dont have information about the lightmap.

In Three.js, the material offset, or the “repeat” attribute dont have any information.

without information, is impossible to set correctly the lightmap. The curious is that this repeat if I export from Unity or if I export from Blender…

In this order of ideas, I was able to apply the lightmap correctly. But seting manually offset and repeat attributes… How can obtain the offset and repeat data?

image

The best would be to have both textures (the map and the lightmap) consistent with the UV coordinates in the 3D model. Now only the map is consistent with the UV, that’s why you have to manually adjust the lightmap. You should:

  • either fix the lightmap to match the UV that you already have
  • or add a second UV set that matches the lightmap texture
  • or manually modify the mapping (as you do it now)

The software that creates the lightmap knows these offset/repeat parameters, but most likely they are not stored anywhere, because when maps and UVs are consistent, you will not need them.

2 Likes

Hi! Okey, is a good approach for the solution. I will try to create a model with a texture and then execute the light baking and try load the model and only add the lightmap file to the material.lightMap attribute and see the result.

For now, the solution for me has been set manually the lightmap parameters for offset and repeat copying the data from the Unity software.

This is my first result using this technique (Model without textures):

2 Likes

You could write an exporter in unity to copy over the lightmap scale/offset into json. Should allow you to automate this instead of doing it by hand.

1 Like

The offset and repeat is the same for each model coming from unity though yeah?
Is is something like 1 and -1 for each?
Is it just that the texture is flipped perhaps?

1 Like

Hi for all! The idea of write a script for Unity and export the data in json is a very good idea, thanks! I think My problema is solved with this way.

For manthrax, the offset and repeat is not same but I think that the origin of My problem was not used a texture in My model.

1 Like