[NOW FREE] Light2D - GPU Lighting System.

Hi, Unity Community.

Light2D is a 2D lighting system that performing computations on the GPU. That allows to get better lighting quality and use more lights, compared to other 2D lighting systems.

Now free and open source. GitHub.

Asset Store link**.**

Tutorial video

How it works

Light is computed on small textures, 160x88 px size is used in this images. Quality of lighting is mostly depending on that resolution. When you increase it lighting will became more precise and will affect smaller light obstacles, but it will make computations more expensive.
Each frame system is rendering light obstacles, light sources, ambient light to RenderTextures. Then system mixes light sources and ambient light together and draws them as post effect.

Light obstacles.

Light obstacles texture (RGB). That and following textures is upscaled to 400%.

Black areas are fully transparent for light, white is fully opaque. There is support for colors too, for example, fully red pixels would absorb red part of light and allow passing green and blue components unchanged.

Light sources.

Light sources texture.

Light sources is a main part of lighting system. They are using GPU ray tracking on light obstacle texture to cast shadows. MeshRenderer draws light source with generated mesh of four vertices. Light emits from light origin, which could have point or line shape. Position of light origin is configurable.
Different shaders is used to draw light sources, with GPU ray tracking or without it. Shaders with ray tracking could have fixed or variable point count. Shader without ray tracking is used with particle systems.

Ambient light.


Opposite to light source, ambient light is emitting from whole area. For example, you could use ambient light to create minimal light level in caves without light sources. See areas unaffected by light sources in Rocket Example. They would be completely black without ambient light.

Overall pros and cons.
Pros:

  • Sprites is used as light sources. That means lights can have any shape.

  • Light source is just a MeshRenderer with 4 vertices.

  • Dynamic batching works with light sources.

  • Point and line light emission types.

  • Mobile device support. Rocket Example is using 8-10 ms per frame on Nexus 4 with profiler attached (where lighting itself taking 2-3 ms).

  • Fully dynamic lighting system. That means you could create or destroy lights or light obstacles without a performance overhead (except Instantiate/Destroy itself).

  • Ambient light support.

  • Colored light sources and light obstacles.

  • Variable light obstacles transparency.

  • Support for light sources emission in Unity particle system.

  • Flexible quality settings.

  • Works with OpenGL ES 2.0 / DirectX 9.

  • Supports Unity 5, 2D Toolkit.

Cons:

  • Perspective camera mode is partially supported.

  • Lights could ignore some small obstacles. Increasing resolution of render texture fixes that, however, can have a performance hit.

  • System is creating 6-10 DrawCalls, depending on settings.

  • Size of lighting camera must be bigger than of game camera by x1-x1.5. This is required to compute off screen lights correctly.

  • It still consuming about 1 - 2ms per frame (on Nexus 4) when there is no lights on screen (if system is not disabled).

F.A.Q.

  1. What is wrong with perspective camera?
    All light obstacles should be at the same Z-depth.
  2. How performance is compared to non GPU lighting systems?
    Light2D will be faster when you use many lights, need big/high quality lights or when it’s used on standalone/webplayer build.

Looks interesting, and with perspective camera support it may be exactly what I need.

I was going to adopt the 2D Mesh Based Volumetric Light asset, but unfortunately, the current Dev has had to put it on the backburner and it’s not currently in a state where it would be useful to me.

Some quick questions:

Is it compatible with 2DTK Shaders and Tilemapping?

I’m a little unclear on how light obstruction works here. It does not obstruct based on physics colliders? Would a project that uses 3D physical colliders, a perspective camera, and 2DTK sprites have trouble getting the intended effects?

I see ambient, point and line light sources; are there plans to add more, such as directional (for lighting large outdoor-type environments), area lights and cones?

Is there support for layers/obstructing only certain layers/tags but not others?

Here’s a shot of a debug area using lighting similar to what I’d want. (Using the aforementioned 2DVLS asset). Do you think I would be able to easily reproduce this effect?

Thanks!

Light obstacles could be rendered with 2DTK. To render light obstacles, Light2D is changing culling mask of camera used for light rendering to obstacles layer and calling Camra.Render(). So anything that could be rendered at specific layer will work for light obstacles. But for light sources and ambient light only Unity sprites are supported.

No, colliders is not used at all. Light obstruction works through GPU ray tracking on light obstacles rendered to texture.

Cone or any other form of light is supported by point light. Point light is just a light that have a one light emission point. Form of light is defined by sprite, so it could be cone, box, elliptic, triangular or whatewer else. Line light have emission line instead of one point. Shape of line light defined by sprite too. You could see a cone light in this webplayer demo.

Yes, it is. You should select layers for light obstacles, light sources and ambient light in light settings.

I think it could be easily reproduced with some point and ambient lights.

Sounds good, although I thought ambient lights required Unity sprites? I think I understand, I might just be confused on how the lights are specifically set up using Unity Sprites; seeing a few example images showing basic light setup would be super useful.

Thanks for all the prompt responses. I’d definitely like to purchase and try Light2D out as soon as perspective camera support is available. Is there an ETA?

Would it be possible for this to work with normalmaps?

I’m not quite sure I understand this, could you elaborate a bit? (I’m also using TK2D). Can you give me a specific example of some things that would not be possible using TK2D?

I believe that light sources themselves are only represented as Unity sprites. So, you would create a rectangular or circular sprite that is tagged as a Light Source, and it will show up in-game as a rectangular/circular light source, but without showing the actual Unity sprite.

@SSS135 Is this correct? I’m not quite sure how ambient lights work in that context, though.

would also like to know the answer to this. I am using normal maps in conjunction with 2d sprites.

Normal maps are not supported. That may be fixed in future updates.

Yes, it’s correct. Shape of ambient light is defined same way as a shape of light sources, except you are not restricted to Unity sprites.

@SSS135

Sounds great! Eagerly anticipating the perspective camera support. :slight_smile:

Version with perspective camera support is pending review in asset store.

1 Like

Hm, I would really like to test this out. We’re developing a 2D Platformer/Adventure with bigger sprites than 160x88 with 2DToolkit and Ferr2D for the PS Vita. If it isn’t compatible with our plugins nor the PS Vita itself we’d lost 50$

Our Sprites vary from 200x200 to max 1000x1000. Would that be a problem for this Light-System? Is it compatible to Ferr2D: Ferr2D Terrain Tool - Community Showcases - Unity Discussions

How does this work exactly? How’s the setup? Is there a need for specific shaders?

There is no limitation on sprite size. 160x88 is example size of light grid and it’s related only to lighting quality and could be adjusted. I think it’s compatible with Ferr2D and almost anything else. Only problem that you will need to place light obstacles by yourself.

It works by rendering texture like light map and then overlaying it to screen texture in OnRenderImage function.

There is a readme file with setup info.

There is no need to change shaders in your game. Only light sources should be rendered with specific shaders which is included in package.

2 Likes

Ah, the light sources are rendered with specific shaders, I see. I hope these shaders aren’t fixed-function shaders, because the PS Vita doesn’t support them :confused:

No, I have used only vertex and fragment shaders.

1 Like

Saw that the asset store just updated with the perspective camera support; grabbing it right now. :slight_smile:

Having some issues with perspective camera… sent you an email.

First, this asset is awesome!..

is there a way I can make the shadows not so dark without altering the ambient lighting?

Great asset!
Is there a possibility to draw obstacle shadow “under” the sprite but still light the sprite by the light source?
It looks that it can’t be done but it is the only one feature that is missing to implement this awesome light system in our game :confused:

1 Like

You could make color of light obstacles lighter or more transparent.