cyclopsLevelBuilder icon indicating copy to clipboard operation
cyclopsLevelBuilder copied to clipboard

Generate UV2 and define a lightmap size hint to allow lightmap baking

Open Calinou opened this issue 2 years ago • 7 comments

  • Related to https://github.com/blackears/cyclopsLevelBuilder/issues/14.

Currently, lightmaps can't be baked on a Cyclops level (although VoxelGI, SDFGI and ReflectionProbe all work as expected). This is because no UV2 is generated for the shapes, and no lightmap size hint is set for the meshes either.

Godot doesn't expose xatlas (which is internally used to create UV2 for imported 3D scenes), so a custom method must be used here. Fortunately, shapes are usually quite simple and guaranteed to be convex, so this should be less complex than xatlas.

A lightmap scale project setting or CyclopsGlobalScene node should also be added to control the lightmap size multiplier. This can be used to make lightmaps more detailed or more coarse, depending your needs (in terms of bake times and file size).

Calinou avatar May 02 '23 15:05 Calinou

The latest head should have code that generates lightmap coordinates on UV2, but I can't get lightmapping to work in Godot. Not just for Cyclops, but even with ordinary mesh objects. Could you test to see if lightmap generation is working?

blackears avatar May 08 '23 15:05 blackears

Could you test to see if lightmap generation is working?

I can get lightmapping to work in the following scene with only primitive meshes in 4.0.2: test_lightmap.zip

Make sure to use the Forward+ rendering method, as baking lightmaps with the Compatibility rendering method is not supported and lightmap sampling is broken in the Forward Mobile rendering method. Also, if using primitive meshes like I did in the test project, you'll have to enable Add UV2 in the PrimitiveMesh inspector.

Calinou avatar May 08 '23 16:05 Calinou

Looks like LightmapGI only works in Forward+ mode. I'm using ImmediateMesh to generate the geometry, so I don't think the UV2 layer needs to be explicitly enabled. I'm not sure what to specify for lightmap_size_hint. It looks like the lightmap system uses a default if you leave it blank, so maybe that's good enough?

blackears avatar May 08 '23 20:05 blackears

I was able to get your test scene to work. Looks like I can only use Forward+ to generate lightmapGI data.

I don't seem to be able to bake with my blocks, though. I'm getting an error that says No meshes to bake. Make sure they contain an UV2 channel and that the 'Bake Light' flag is on. I can't find a Bake Light flag anywhere and I'm sure that the UV2s are there. Is this a problem with ImmediateMesh? Should I try an ArrayMesh?

blackears avatar May 08 '23 21:05 blackears

Is this a problem with ImmediateMesh? Should I try an ArrayMesh?

Is the MeshInstance's bake mode set to Static? This is the default since 4.0, but double-check to make sure.

That said, I don't know if baking lightmaps onto ImmediateMeshes is supported. Logically, it should work if they have UV2, but I don't know if something else in the engine would prevent lightmaps to appear on them.

Calinou avatar May 09 '23 00:05 Calinou

I just rewrote the mesh generation to use ArrayMesh instead of ImmediateMesh. LightmapGI baking is still failing. The MeshInstance3D has bake mode set to static and I compared both the MeshInstance3D and the ArrayMesh to an imported mesh from Blender to make them as similar as possible. Nothing is working. The only thing close to a Bake Light flag I can find is in the import tab. And I can't apply an import setting to a procedurally generated mesh.

blackears avatar May 09 '23 02:05 blackears

https://github.com/godotengine/godot/issues/76886

Looks like lightmaps cannot be baked to nodes unless their MeshIntance3D exists in the scene at edit time.

blackears avatar May 09 '23 19:05 blackears