My last post was a few months ago, which means I haven't hit my post-each-month target. One of the reasons is that my blog is too large now (likely too many images), and I can't back it up on Google. Another reason is because I was mostly working on procedural buildings/cities optimizations, bug fixes, and minor features. I didn't have a single larger topic (with screenshots) to write a blog post about. Now I have a bit more time with Thanksgiving break coming up. I decided that indirect lighting required enough effort that I could write a few pages of text on the topic.
Background
I wrote a post on 3DWorld's procedural building indirect lighting a few years ago. I use a fixed 128x128x128 3D grid of voxels to store RGB indirect lighting values for the current part of the building the player is in. The main building area, extended basement, backrooms, and malls are all represented by different lighting volumes to keep the bounds small and reduce the amount of light leaking through walls thinner than a single voxel. Each light is added incrementally to the grid by firing random rays from random points on each light source (ceiling light, wall light, window, lamp, etc.) These rays reflect from the walls, ceilings, floors, and room objects and refract through glass surfaces, accumulating in the voxels along their paths. I have to write values for the entire path rather than only the hit points so that dynamic objects such as people and animals that didn't contribute to indirect lighting could still benefit from it. The fragment shader then uses this voxel grid as a 3D texture to look up for lighting each pixel.
There were a variety of problems with that initial lighting solution, which is why I had it disabled by default and only enabled with a custom key toggle. I put quite a bit of effort into optimizing lighting last year, but there were still problems with noise in low light areas and a lack of lighting updates when the scene changed. That's no good because I want the world to be interactive rather than static. I made another pass at fixing some of these problems while I was waiting to come up with new features to add.
Moving Objects and Doors
The first feature I wanted was to have indirect lighting update when objects were picked up, dropped, or moved by the player. It was too expensive to clear and re-add every light when any object was moved. Fortunately, I already had a system for removing lights by adding them with a negative weight that cancels out the existing positive weight. The tricky part is that I had to remove them with the old geometry and then re-add them with the new geometry. This required me to run the negative light removal step in the same frame on the main thread (while still using OpenMP worker threads), update the object at the end, and then schedule the new light add to be run on background threads.
The volume texture wasn't updated until the new lighting was complete. This avoided a temporary darkening of the room when the light was removed, but caused a few frames of delay for the new lighting to be updated. The result was acceptable to me though. Everything is a trade-off.
However, the fact that the removal step had to be run in the current frame meant that it needed to be fast. For this reason I decided to only update the light(s) in the room the object was in, and only if there were at most two enabled light sources. This is reasonable because rooms with many lights such as retail stores and parking garages don't produce significant indirect shadows from objects as the light comes from many directions. The most significant changes in indirect lighting came from basement rooms with no windows, which usually have a single light. I didn't attempt to update window lights in this case because there may be several windows to a room.
That was enough to handle object updates. The next task was to extend this to work with doors that are opened or closed. Doors are a bit more difficult for a few reasons. First, they separate two rooms, so it makes sense that I have to remove and re-add the lights in the rooms on both sides of the door. Well, only the lights that are turned on. I limited this to two lights as well, either one per room or two in one room and none (all off) in the other room. The second problem is that doors open and close slowly over many frames rather than changing immediately like object pickups and drops. I decided to update the lighting on the frame where the door state first starts to change and let the re-add run in the background as the door is moving. This worked pretty well, but it didn't handle lights that bounced through the door from two rooms away.
Now I can have nice indirect shadows from objects on the floor that are responsive to changes made by the player. See the example screenshot below.
![]() |
| Dark basement room lit only by light reflected through the doorway. The boxes and barrels cast indirect shadows. The person does not contribute to indirect lighting. |
Window Blinds
The next problem to tackle was windows. While I haven't found a way to efficiently update window light when objects move, I was able to update them when the player opens and closes bedroom window blinds. This works because each blind covers exactly one window (or half a window when moving horizontally), which means it's only a single light to remove and re-add. It's different from objects though because blinds can be moved horizontally or vertically by different amounts, which means that the player can change the area of the window that's blocked vs. open. This effectively changes which light ray locations on the surface of the window can pass through to the room interior. I could probably have implemented this by only adding or removing rays in the region the edge of blinds passed through during their movement. But this seemed too complex and difficult, so I used the same approach as moving objects.
Except, it wasn't so simple. The problem is that closed blinds don't just block light from entering the room; they also reflect light back into the room and prevent it from leaving. If there are two blinds on opposite sides of the room, and I close one and then the other, the room becomes too dark. Why? Because the negative light added when removing/closing the second blinds reflects off the closed first blinds back into the room, adding too much negative light. The object state is stale after the first update.
The fix for this was to store the original state of all the room objects when the light was first added, and restore the interior to this state again before removing it. Here the state is the BVH (bounding volume hierarchy acceleration structure) of all objects that contribute to indirect lighting of this room. That's a lot of data to store with each light! Each light area can potentially have thousands of room object contributing to reflections. I can reconstruct the BVH from the objects quickly though, so it doesn't need to be stored. The objects themselves tend to be mostly duplicates: The objects list is exactly the same for every window and light that was initially added to the building. So all I really need to do is track all of the unique object states and store one object list per state rather than one per light, and have the lights point to these. This means that I only have to add a new object list when the player moves something, and any lights added at that point keep this state. This can be done incrementally as the player is making changes and makes little difference to frame time and memory usage.
There's another advantage to this object tracking: I no longer need to run the light removal step in the current frame before the object is updated. I can update the object at any point and let the light restore its own object list when removing itself. However, that does cause the lighting to visibly flicker, so I haven't enabled this change. It could possibly be added as a config file option for users with slower computers where waiting for light removal causes too much lag.
I'll add another screenshot with some indirect window lighting. I also have to recorded videos of lighting updates on YouTube that are linked near the end of this post.
![]() |
| A bright room lit by a round ceiling light and windows. |
Day/Night Cycle
We're not done yet. The next problem I ran into was related to the dynamic day/night cycle that can be enabled by the user. This will move the sun and moon over time, which varies the exterior light color and intensity coming through the windows. The current code will repeatedly update the window lighting by adding and removing it every few frames, which leads to low framerate and frame lag. I didn't even think about this case when designing the lighting solution.
The fix for this was to split the lighting volume into two separate grids, one for interior lighting and the other for exterior (sun/moon/sky) lighting. Exterior lighting is always added as pure white light. Both are accumulated once when the player first enters that floor of the building. Then, each frame the exterior lighting color changes, I only have to re-combine the two volumes with the equation light_total = light_interior + exterior_color*light_exterior. This can be spread across multiple frames by splitting the volume into multiple slices and updating them incrementally, sending only the newly combined slices to the GPU each frame. The result is a wave of minor incremental lighting updates that runs over the voxel grid across a number of frames.
Reducing Noise
With lighting updates solved, it was time to work on reducing noise in the lighting results. This was particularly problematic for dark basement rooms where the only light was reflected through doorways from other rooms. These rare rays of bounced light left bright streaks through the otherwise dark rooms. This produced bright spots on walls and blobs of different shades/colors. Using more rays would have helped here, but that also makes lighting take longer to run, leading to reduced framerate and a longer time for lighting to update when the player enters a new area or something changes.
I don't have much background in lighting equations, so I asked Copilot (AI) for suggestions in this area. It recommended replacing both my random ray starting point locations and spherical random ray directions with a a Halton sequence. This produces a more uniformly spaced sample pattern that avoids large sample gaps and clusters of similar samples that resulted in lighting blobs. I used multiple Halton variants for room lights, windows, initial directions, and bounce directions. This was several iterations of code changes that improved lighting quality incrementally.
The next step was to replace my random hemisphere reflection directions with a proper cosine weighted distribution that more accurately models real light behavior. I had to ask Copilot to generate the math for this distribution. This change helped to fix the bright borders of windows and directed more of the light into the room, reducing noise. I compared the old vs. new images of the same interiors side-by-side for all of these changes to judge which looked best. Unfortunately, I didn't think to save these comparison screenshots, and it's too difficult to get the old code back, so I can only show the final results here.
My main test scene was the restaurant with ceiling lights turned off so that all light comes through the windows. The interior space is very open with large flat areas of uniformly colored walls, ceilings, and floors. It's particularly easy to spot noise on the right side wall.
![]() |
| Restaurant lit only by indirect light from the sun and sky that comes through the windows. |
I recorded a video showing how house indirect lighting updates based on the player taking and moving items, opening and closing doors, and changing the state of window blinds. There are still a few minor issues, some of which I've fixed since creating this video.
This second video shows light updates in the large rooms found in malls. There is definitely still some noise in the lighting volume in areas with low light, but it's unclear how to reduce it without impacting framerate or making the lighting values update too slowly.
Finally, here is a static screenshot of that furniture store seen in the mall video above. Lamps produce a very small amount of indirect lighting compared to ceiling lights. I'm not sure if the noise on the walls and ceiling is due to the lamps or the light coming through the store windows from the mall concourse in the back right.
![]() |
| Screenshot of a mall furniture store with the lights off, lit only by lamps and indirect lighting. |
I'm sure there are more improvements to be made in the future. I have to get back to adding new building/room content for now.









































