Repository: godot
Issue: LightmapGI Environment lighting is polluting closed spaces
Labels: bug,topic:rendering,topic:3d

### Tested versions

- Reproducible in: v4.3.beta.custom_build.daa81bbb7d

### System information

Windows 10 - Godot v4.3.beta.custom_build.daa81bbb7d - Vulcan (Forward+) - Geforce GTX 960

### Issue description

# Info

It seems that when baking lightmaps, random rays miss the geometry and cause the interior of closed spaces to be illuminated by environment light.

Godot with `DEBUG_TEXTURES` enabled in `lightmapper_rd.cpp`.

# Example scene

a) basic 3D scene with solid color for background, ambient and reflected turned off for clarity

![world](https://github.com/godotengine/godot/assets/224215/2458fdba-2dc0-4f6c-b36a-aebd68c17c0b)

b) lightmap gi node with different solid color for environment, denoiser off, use texture off

![lmgi](https://github.com/godotengine/godot/assets/224215/90e1f6f5-d67f-4b3b-adc2-dca92fa5b09e)

c) omnilight with yet another color in the center

d) basic blender cube with normals pointing **inside**

# Results

Before bake:

![direct](https://github.com/godotengine/godot/assets/224215/b2baaf8c-f7fa-4733-b1d1-83d0151b9c90)

After bake:

![with](https://github.com/godotengine/godot/assets/224215/38dc114f-b171-48c4-ae9a-1cde26553260)

Although the geometry is closed on all sides, a lot of light from the environment gets in and hits the faces. This only happens in the “light_secondary” step. The green color of the environment is set from LightmapGI settings.

`2_light_primary_0.exr`

![with_primary](https://github.com/godotengine/godot/assets/224215/47a2ad03-a968-44b5-9d0d-ebd3c13a90cc)

`4_light_secondary_0.exr`

![with_secondary](https://github.com/godotengine/godot/assets/224215/d11c0ebd-4c69-4c18-b8d0-a45bce085821)

# Box inside box

The situation changes a bit when the above cube is placed inside a larger one. In Blender, it looks as follows:

![boxes](https://github.com/godotengine/godot/assets/224215/bc22b2d1-268d-45ab-b268-0efb18098fed)

After bake:

![inside](https://github.com/godotengine/godot/assets/224215/8f575e6b-b970-4dec-8619-de0a837dbd49)

You can see that fewer rays missed the geometry, but despite the thick walls surrounding the cube, environment leaks still occur (green).

Switching Environment in LightmapGI to “Scene” gives this result: environment still appears, but consistent with the WorldEnvironment setting (blue).

![inside_scene](https://github.com/godotengine/godot/assets/224215/42b63f29-69e7-4277-8bb2-f792c65a0129)

Increased exposure:

![inside_scene_up](https://github.com/godotengine/godot/assets/224215/aa5b8c0d-3fa1-4071-be2a-de5477cac6fd)

# Source

This section of `lm_compute.glsl` is most likely responsible for adding color of the environment:

https://github.com/godotengine/godot/blob/40b4130c93d08235a60996d29e5869a22b6ae53d/modules/lightmapper_rd/lm_compute.glsl#L532-L535

When you turn off the line **534** all the green environment from baked ligtmap disappears. Naturally, this causes the environment to be completely ignored, so it is not a solution. The question remains why and in what cases `trace_result == RAY_MISS`, if we are talking about closed spaces (and even closed spaces inside other closed spaces).

No green dots anywhere:

![without](https://github.com/godotengine/godot/assets/224215/f0e04442-1643-4313-a770-59e30cc2b43b)

`4_light_secondary_0.exr` after commenting line 534:

![without_secondary](https://github.com/godotengine/godot/assets/224215/d0b73ac2-f3fc-401a-b932-c93ea04d43ea)



### Steps to reproduce

Minimal reproduction project below.

### Minimal reproduction project (MRP)

[environment-pollute.zip](https://github.com/godotengine/godot/files/15381515/environment-pollute.zip)