Skip to content

2D light for Unity Urp with raytracing

License

Notifications You must be signed in to change notification settings

damian-666/GiLight2D

 
 

Repository files navigation

GiLight2D

Unity Urp Render Feature for 2D Global Illumination.

Demo

What is it

In practice, this is an approach to lighting with rays, in which glow and shadows are created naturally, and the number of light sources is not limited.
Implemented quite enough options, tested with unity 2021 and 2022 versions.

Some of the options

  • fixed resolution for pixel art.
  • render to texture to use it later in the shader or in the camera to apply built-in post effects.
  • padding the screen borders to display objects outside the camera.
  • different noise settings and resolution.
  • etc.

Installation and use

Install via PackageManager https://github.com/NullTale/GiLight2D.git

Add GiLight2DFeature to the Urp Renderer (urp must be configured).
If the scene is empty this should already be enough to see the effect.


Three sprites on an empty scene.

Then configure the object mask and the output texture, which can then be used in the shader (the name must match).

image

Now the texture can be used from the shader.

UrpRenderer is configured so that it doesn't draw Gi objects by mask.
Texture Exposed checkbox must be unchecked.

image
The sprite of the square uses the Gi texture from the screen coordinates.

How it works

The general idea is that from each pixel rays are released in all directions, from the sum of the rays that hit the light source is the final color of the pixel.
In the end a lighting texture should be calculated which can then be used.
With this approach glow and shadow are formed naturally.

Rays

In order to effectively calculate the rays a sdf map is created, in fact it is a distance map to the nearest light source, it is used to get the offset distance of the ray when searching for the light source. So the whole rendering process consists of three steps: rendering objects into buffer, creating a distance map using jfa algorithm, applying gi shader. Ray bounces are counted by calculating the illumination for the pixels on the border of the objects.

Pipeline

Almost all the calculations are performed in a fragment shader, from which the number of objects does not affect performance, but strongly depends on the resolution.

About

2D light for Unity Urp with raytracing

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 77.2%
  • ShaderLab 20.6%
  • HLSL 2.2%