top of page

Dynamic Ambient Lighting 2D in DARUMA

The map of DARUMA has been designed to eliminate loading screens during platform gameplay phases. I searched a method for smoothly change ambient lighting (fog and sunlight) between different areas.

Blending from ambiant lighting to another in editor

 

The tool :

The LightingRect tool enables you to set up an ambient lighting area using handles, and to position it within a 2D space. The red rectangle represents the light/fog area, while the blue rectangle symbolizes the blend distance.

2D light rect using using Unity handles.

The LightingRect component allows you to select the main light color, the fog color, the fog start and end distances, and the fog density. When the camera enters the rect, the main light and fog are automatically set to the desired values.

2D light rect component

 

Global pipeline :

Each LightingRect subscribes to a static script, the DynamicLightManager. The manager uses the properties of the rectangles and the camera's position to send light/fog values to the sprite shaders. If necessary, the manager will blend between multiple LightingRect values based on the camera position to provide a smooth transition between different lighting ambiances. The manager is updated each frame in the 2D Light Pass of the render pipeline, before the creation of the 2D lights."


Dynamic Light Manager sending shader properties

 

Making the fog :

The fog shader calculates the final color of the sprite by interpolating (using lerp) between the default lit color and the fog color, based on the sprite's Z position and the fog properties that are sent from the manager.

The fog shader function

Next, I added a few lines to the default sprite shader of the URP, to apply the fog to every sprite in the game.

The fragment shader of Sprite Lit shader
Sprite fog color from Z position

 

What about the main light color? :

The main light color is not utilized in the default sprite shader. To override the URP 2D system, we need to modify the 2D Light Pass in the render pipeline. The main light color is used as the default clear color at the beginning of each frame in the light pass. All we need to do is set a custom color value from the manager in this stage.

Replacing the main light color with custom light color in URP 2D lights pass

 

The tool work in editor view and allow me te work on many levels wihout taking care of global light settings. At runtime, the transition are seamless, and work fine with the asset streaming system.






Comments


bottom of page