Dynamic lighting and shadows can elevate the visual quality and gameplay experience of your Godot games. They add depth, atmosphere, and realism, making your game world more immersive and engaging.
With Godot’s powerful features like CanvasModulate and PointLight2D, creating and customizing dynamic lighting and shadows is an easy task.

Setting Up the Godot Game
Before diving into dynamic lighting, set up a basic 2D game inthe Godot game engine. Create a new scene and add aCharacterBody2Dfor the player.
The code used in this article is available in thisGitHub repositoryand is free for you to use under the MIT license.

Inside theCharacterBody2D, add aCollisionShape2Dwith a rectangular shape and aSprite2Dto represent the player’s character. Also, create some platforms usingStaticBody2Dnodes.
Now, implement the player movement and collision with the screen boundaries using GDScript:
With this code, the player can move left, right, up, and down while being constrained within the screen boundaries.
Darken the Rest of the Scene
To achieve the effect of dynamic lighting, you need to darken the rest of the scene except for the areas illuminated by the light sources. You can do this using Godot’sCanvasModulatefeature.
Attach this script to a newNode2Dadded at the root of the scene. TheCanvasModulatenode will apply a semi-transparent black overlay, darkening the entire scene except for areas illuminated by lights.
Create a constant variableDARKNESS, which determines the intensity of the darkness applied to the scene. By adjusting the value ofDARKNESS, you can control how dark the scene becomes when not illuminated by lights. A higher value will result in a darker scene, while a lower value will make the scene less dark.
Cast Light Shadow
To cast shadows, you need to set upLightOccluder2Dnodes on obstacles that should block light. For example, if you have walls, platforms, or other objects that should cast shadows, add aLightOccluder2Dto each one.
Create a newLightOccluder2Dnode, and adjust its shape to match the obstacle’s shape in your game. You can also adjust the occluder’s properties likepolygonsandsegmentsto fine-tune the shadow-casting behavior.
Add Omnidirectional Lights
Now, add aPointLight2Dnode with a texture to represent your light source. Point lights emit light in all directions and create a realistic illumination effect.
You can customize the properties to control the range and color of the light emitted by thePointLight2D.
You can also create or import a custom light texture to define the shape and appearance of your light source. It could be a simple circle or a more complex shape, depending on the atmosphere you want to create.
To apply the texture to thePointLight2Dnode, follow these steps:
Now, your light source will emit light with the defined shape and color, adding a visually appealing touch to your dynamic lighting and shadows.
Including Additional Features
In addition to the basic setup of dynamic lighting and shadows, you can introduce various additional features to elevate the visual appeal and gameplay experience further. Here are some ideas to consider:
Light Animations
Bring your light sources to life by adding animations to them. For example, you’re able to create flickering lights for torches or candles, simulate the movement of a campfire, or even add a pulsating effect to magical orbs.
Day-Night Cycle
Implement a dynamic day-night cycle that gradually changes the intensity and color of lights as the in-game time progresses. This feature can enhance the immersion and create different moods depending on the time of day.
Light Fading
Create smooth transitions for light intensity changes to avoid abrupt shifts that may break the immersion. By gradually fading lights in and out, you can achieve a more natural and visually pleasing effect.
Dynamic Shadows
Instead of using static shadows, consider adding dynamic shadows to moving objects or characters. you’re able to do this by using additional light sources that follow the movement of the characters, giving the illusion of real-time shadow updates.
Copyright-Free Sound Effects
Sound effects play a crucial role in making your game world feel alive and dynamic. Consider incorporatingcopyright-free sound effectsto complement your dynamic lighting and shadows.
Best Practices for Dynamic Lighting and Shadows
While dynamic lighting and shadows can greatly enhance your game, improper implementation may negatively impact performance and visual quality. Here are some best practices to keep in mind:
Optimize Light Sources
Limit the number of active light sources to the minimum necessary for your desired visual effect. Excessive lights can significantly impact performance, especially on lower-end devices.
Use Light Baking
For static scenes or objects, consider baking the lighting information to improve performance. Light baking precomputes the lighting data, reducing the need for real-time calculations during gameplay.
Opt for Simple Occluders
When setting upLightOccluder2Dnodes, opt for simple shapes wherever possible. Complex occluders with many vertices can lead to performance issues. Use simple polygons or line segments for occluders whenever feasible.
Culling and Layering
Utilize Godot’s visibility and layering system to ensure that lights and shadows are only calculated for visible areas. Culling off-screen lights and shadows can improve performance significantly.
By following these best practices and carefully implementing additional features, you can create stunning and captivating dynamic lighting and shadows that elevate the overall quality ofyour platformer game.
Making Godot Games More Engaging With Dynamic Lighting and Shadows
Dynamic lighting and shadows breathe life into your game world. They create a more immersive and atmospheric environment, drawing players deeper into the gameplay experience. The interplay of light and shadow can invoke emotions, set the tone, and enhance the visual storytelling of your game.
Whether it’s a mysterious dark dungeon or a serene moonlit night, dynamic lighting enriches the gameplay and leaves a lasting impression on players.