2D lighting system for Unity3D, running on a GPU

2D lighting system for Unity3D, running on a GPU

https://t.me/game_development

Hello. As you know, Unity3D does not support lighting for 2D games. In Asset Store you can find an example system, but it has one drawback - it runs on the CPU and consumes a lot of resources (64-4096 raycasts per frame for each light source). So I decided to make my own lighting, the performance of which would be enough for mobile devices. For this, the calculations were transferred to the GPU. It turned out something like a lights in Terraria or Starbound.

Link to the demo. Arrows - movement, space - chassis, R - restart. Screenshots are taken from it.

All lighting is spited on small textures, in the example we use 160x88 pixels. When you increase the resolution, you can achieve a very small grid, which will be difficult to notice, although this is no longer for mobile platforms. Due to the fact that the calculations are made on such small textures, you can use rather heavy shaders.

For the lighting work 3 cameras are used, each of which is responsible for its part of the system: light sources, light barriers, ambient light. Then, the light sources and ambient light are mixed and superimposed on the game chamber.

Now in more detail, in the order of rendering.

Obstacles for light

Texture of obstacles for light. RGB channels. This and subsequent similar textures have a scale of 400%

This is the texture of the camera. Black areas are completely transparent, white - completely opaque. Also, colored areas are supported, for example, a completely red pixel will block the red part of the light and let the green and blue pass through.

Ambient Light

Light sources
Sources of ambient light. Alpha channel
Iteratively generated ambient light texture
This is a bit reinforced ambient light, without conventional lighting sources

Here everything is somewhat more complicated. I create this type of lighting in order to add a weak light to that space where there are no light sources. In the example, it uses a soft backlight of the entire free space. The RGB channel controls the color, the alpha channel - glowing. The main difference between this type of light and conventional sources is that it is considered iteratively and has no direction.

The algorithm of calculation for one pixel:

  • We take the initial value of the pixel from the previous iterative texture.
  • We subtract from the pixel the strength of the obstacle from the texture of the obstacles.
  • Add to the pixel the strength of the glow from the texture of the light sources of the environment.
  • Add to the pixel the average value of the neighboring pixels

Sources of light

Sources of light

Conventional sources are the main part of the lighting system. To draw them, something similar to sprites is used. The whole color comes from the center, although you can transfer the point anywhere, if you want.

For light sources, there are several shaders with path tracing and one without it. Shaders with tracing differ in the number of traced points. I use two such ones - one for 9 points, working with Shader Model 2.0, another 20 points for Shader Model 3.0. A shader without path tracing is used for particle systems, since it does not need any additional information to operate.

Route trace algorithm:

  • Take the brightness of the pixel from the texture.
  • Find the position of the light source and the current pixel in the obstacle texture.
  • Reduce the current brightness by the pixel values ​​of the obstacles that lie between the two points from the previous step.

Mixing and applying light

Light sources + ambient light

After the light sources and ambient light is rendered, you can mix them with each other. For this, the textures are multiplied by their alpha and added to each other. Then all this is superimposed on the image of the game and displayed on the screen.

Screenshots of the result, higher resolution on the click.

Pros:

  • The calculations take place on the GPU.
  • The sources of light are ordinary sprites, respectively, you can make a light source of any shape.
  • Each light source consumes very few resources.
  • Works on mobile devices, consuming ~ 8 ms per frame on the Nexus 4.
  • Fully dynamic lighting. Obstacles can be created and destroyed on the fly without any loss of productivity.
  • Support for ambient light.
  • By itself, the system generates 6 DrawCalls, all light sources can fit one plus one another for ambient light.
  • Multicolored light sources and obstacles.
  • The ability to emit light sources in a particle system. The productivity is almost the same as the usual particles.
  • Flexible quality settings.

Minuses:

  • The system illuminates the grid, and as a result small obstacles can be ignored. On powerful platforms, you can make the grid very shallow.
  • It is necessary to generate meshes for ambient light and obstacles.
  • The size of the cameras in which the lighting is created must be larger than the size of the game chamber, so that the light sources behind the screen are correctly displayed.
  • The computational complexity of the system is almost independent of the number of sources. This means that if it consumes 8 ms per frame with 10 light sources, then without sources it will consume about 8 ms.

Download

Open Source




Report Page