Search Unity

[NOW FREE] Light2D - GPU Lighting System.

Discussion in 'Assets and Asset Store' started by SSS135, Mar 14, 2015.

  1. SSS135

    SSS135

    Joined:
    Nov 26, 2012
    Posts:
    62
    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.









    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.​
     
    Last edited: Jan 25, 2017
  2. Kirbychwan

    Kirbychwan

    Joined:
    Aug 14, 2012
    Posts:
    430
    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!
     
  3. SSS135

    SSS135

    Joined:
    Nov 26, 2012
    Posts:
    62
    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.
     
  4. Kirbychwan

    Kirbychwan

    Joined:
    Aug 14, 2012
    Posts:
    430
    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?
     
    Last edited: Mar 18, 2015
  5. Greenwar

    Greenwar

    Joined:
    Oct 11, 2014
    Posts:
    43
    Would it be possible for this to work with normalmaps?
     
  6. Doghelmer

    Doghelmer

    Joined:
    Aug 30, 2014
    Posts:
    112
    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?
     
  7. Kirbychwan

    Kirbychwan

    Joined:
    Aug 14, 2012
    Posts:
    430
    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.
     
  8. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    363
    would also like to know the answer to this. I am using normal maps in conjunction with 2d sprites.
     
  9. SSS135

    SSS135

    Joined:
    Nov 26, 2012
    Posts:
    62
    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.
     
    Last edited: Mar 22, 2015
  10. Kirbychwan

    Kirbychwan

    Joined:
    Aug 14, 2012
    Posts:
    430
    @SSS135

    Sounds great! Eagerly anticipating the perspective camera support. :)
     
  11. SSS135

    SSS135

    Joined:
    Nov 26, 2012
    Posts:
    62
    Version with perspective camera support is pending review in asset store.
     
    Kirbychwan likes this.
  12. BTStone

    BTStone

    Joined:
    Mar 10, 2012
    Posts:
    1,084
    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: http://forum.unity3d.com/threads/ferr2d-terrain-tool.204436/

    How does this work exactly? How's the setup? Is there a need for specific shaders?
     
  13. SSS135

    SSS135

    Joined:
    Nov 26, 2012
    Posts:
    62
    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.
     
    Kirbychwan and BTStone like this.
  14. BTStone

    BTStone

    Joined:
    Mar 10, 2012
    Posts:
    1,084
    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 :/
     
  15. SSS135

    SSS135

    Joined:
    Nov 26, 2012
    Posts:
    62
    No, I have used only vertex and fragment shaders.
     
    BTStone likes this.
  16. Kirbychwan

    Kirbychwan

    Joined:
    Aug 14, 2012
    Posts:
    430
    Saw that the asset store just updated with the perspective camera support; grabbing it right now. :)
     
  17. Kirbychwan

    Kirbychwan

    Joined:
    Aug 14, 2012
    Posts:
    430
    Having some issues with perspective camera... sent you an email.
     
  18. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    363
    First, this asset is awesome!...

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

    ss.png
     
  19. Oijadt

    Oijadt

    Joined:
    Sep 4, 2013
    Posts:
    5
    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 :/
     
    gibernier likes this.
  20. SSS135

    SSS135

    Joined:
    Nov 26, 2012
    Posts:
    62
    You could make color of light obstacles lighter or more transparent.
     
  21. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    363
    Turning the obstacle anti-aliasing off did the trick! I can't get my UI layer to show over the lighting layer though. Any thoughts?
     
  22. SSS135

    SSS135

    Joined:
    Nov 26, 2012
    Posts:
    62
    You could move you UI to another camera with depth higher than on main camera.
     
  23. Doghelmer

    Doghelmer

    Joined:
    Aug 30, 2014
    Posts:
    112
    I am having some pretty large difficulties getting this to work with my game.

    No matter how I adjust the settings, my lights always end up appearing at a different position from my regular camera. Here's where the light should appear (scene view):

    Pic1.png

    And here's where the light actually appears (game view).

    Pic2.png Pic3.png

    Notice how the center of the light is in different positions depending on where my camera is located. As you can see, the light is not following my camera properly. This was about the best I could get it to look after fiddling with the camera and Lighting System settings for awhile.

    I suspect that this has something to do with the fact that I'm using 2D Toolkit. I gave my Lighting Camera a TK2D Camera with the same settings as my normal camera, and this did not make a difference. Any ideas what might cause this sort of thing? Has anyone gotten this to work with 2D Toolkit? I can provide more details if necessary.
     
  24. SSS135

    SSS135

    Joined:
    Nov 26, 2012
    Posts:
    62
    I'm testing 2D Toolkit right now. Lighting seems to work fine, but I found one bug. Light Camera property in Lighting System is referenced to prefab instead of scene object. Try to re assing it to Light Camera from scene. Also, are you working with XY or XZ plane?
     
  25. Doghelmer

    Doghelmer

    Joined:
    Aug 30, 2014
    Posts:
    112
    I tried referencing the Light Camera from the scene as you mentioned, it didn't make a difference. I am working with the XY plane.

    Also of note, my normal camera is using a Pixels per Meter of 100. Not sure if this would make a difference.
     
  26. SSS135

    SSS135

    Joined:
    Nov 26, 2012
    Posts:
    62
    Could you please send me a project? It will greatly speed up solving that problem.
     
  27. Doghelmer

    Doghelmer

    Joined:
    Aug 30, 2014
    Posts:
    112
    OK, just sent you a private message.
     
  28. SSS135

    SSS135

    Joined:
    Nov 26, 2012
    Posts:
    62
    Found some problems with 2DTK integration. Will fix them in next version.
     
  29. ErebosGames

    ErebosGames

    Joined:
    Aug 1, 2012
    Posts:
    6
    I will purchase this once it works with TK2D
     
  30. Doghelmer

    Doghelmer

    Joined:
    Aug 30, 2014
    Posts:
    112
    The creator helped me to fix the issue I was having. I can say that it's definitely worth a purchase, and I'll be writing a review soon that states as much. I've been testing some very complex scenes with a lot of lights, and performance is still solid. Not to mention it looks about as good as you could expect it to.

    SSS135, I actually have another question: Is it possible to have a light that completely ignores all of the obstacles in a scene?
     
    ErebosGames likes this.
  31. Doghelmer

    Doghelmer

    Joined:
    Aug 30, 2014
    Posts:
    112
    ...And, I have yet another question :)

    In this screenshot, notice how when my light hits a wall obstacle, it creates a dark horizontal line which is much darker than the area directly below it:
    Pic4.png

    This is a problem, because it creates these ugly looking black spots whenever a wall is hit by a light. Is there some way of effectively getting rid of this? I haven't been successful in figuring this out.
     
  32. SSS135

    SSS135

    Joined:
    Nov 26, 2012
    Posts:
    62
    This is happening because by default light obstacle is using color of your sprite. To fix that change multiplicative color of light obstacle to something like (0,0,0,1).
     
  33. SSS135

    SSS135

    Joined:
    Nov 26, 2012
    Posts:
    62
    Better 2DTK support will be done within 6-8 days.
     
    ErebosGames likes this.
  34. SSS135

    SSS135

    Joined:
    Nov 26, 2012
    Posts:
    62
    You could use "Light 1 Point" shader and set "Obstacle mul" in material to 0. Actually you could use any other shader, even not "Light xxx" shader to do that.
     
  35. ErebosGames

    ErebosGames

    Joined:
    Aug 1, 2012
    Posts:
    6
    Awesome, looking forward to seeing it.
     
  36. Doghelmer

    Doghelmer

    Joined:
    Aug 30, 2014
    Posts:
    112
    I tried setting Red = 0, Green = 0, Blue = 0, Alpha = 1 (as opposed to 255), and I'm still seeing the black spots. I actually still see them even if I set alpha to 0. I've found that it is possible to make the spots less dark if I play around with the obstacle's Additive Color. Currently, I have my additive set to 128,128,128,64, but this only alleviates the issue, and actually causes lights to bleed slightly through obstacles. By the way, the sprite I am using is solid white (though I've also tried solid black), and I'm using an ambient light set to 107,107,107,255. After a lot of toying with light, ambient and obstacle settings, I think it may not be possible to completely to fix the issue if I use ambient light. Then again, I may be missing something.
     
  37. SSS135

    SSS135

    Joined:
    Nov 26, 2012
    Posts:
    62
    Light2D will be on 50% Level 11 sale from April 1 - April 30.
     
    Last edited: Apr 3, 2015
    tango209 likes this.
  38. SSS135

    SSS135

    Joined:
    Nov 26, 2012
    Posts:
    62
    Is turning off ambient light causes that problem to disappear?
     
  39. Doghelmer

    Doghelmer

    Joined:
    Aug 30, 2014
    Posts:
    112
    Actually, I was mistaken-- it's still something that happens with ambient light.

    To give you a better example of what I'm talking about, here's a scene I'm working with:
    Pic5.png

    The green marks are the centers of my point lights. I've marked in red where the black lines appear. Ideally, I would prefer the areas that are marked in red to display at the same level of lightness as the area at the right side of the screen, instead of displaying as a dark line as they do now. My settings for this scene are as follows:

    Obstacle:
    Multiplicative: 0,0,0,1
    Additive: 0,0,0,64

    Ambient: 130,130,130,255

    In case you are curious, the scene looks like this without ambient light:
    Pic6.png
     
  40. SSS135

    SSS135

    Joined:
    Nov 26, 2012
    Posts:
    62
    Have you tried to set "Light Pixel Size" to 4-6 times lower value?
     
  41. Doghelmer

    Doghelmer

    Joined:
    Aug 30, 2014
    Posts:
    112
    Yeah, unfortunately that doesn't seem to make much of a difference. My light pixel size was already 0.1. I've tried fiddling around with this value to make it both higher and lower.
     
  42. ErebosGames

    ErebosGames

    Joined:
    Aug 1, 2012
    Posts:
    6
  43. Doghelmer

    Doghelmer

    Joined:
    Aug 30, 2014
    Posts:
    112
    ErebosGames likes this.
  44. SSS135

    SSS135

    Joined:
    Nov 26, 2012
    Posts:
    62
    Version 1.2 with better 2DTK support is pending review in Asset Store.
     
  45. Loft

    Loft

    Joined:
    Nov 15, 2014
    Posts:
    2
    Attempting to run this on an iOS device generated a number of AOT exceptions. Is there a way around these or is support limited to other mobile platforms?
     
  46. SSS135

    SSS135

    Joined:
    Nov 26, 2012
    Posts:
    62
    Can you please send me those exceptions so I could fix them?
     
  47. Loft

    Loft

    Joined:
    Nov 15, 2014
    Posts:
    2
    Thanks for quick reply, and for the fix!
     
  48. SSS135

    SSS135

    Joined:
    Nov 26, 2012
    Posts:
    62
    Last edited: Apr 4, 2015
  49. strich

    strich

    Joined:
    Aug 14, 2012
    Posts:
    285
    Would this toolkit work on an isometric game like this one?
     
  50. Kirbychwan

    Kirbychwan

    Joined:
    Aug 14, 2012
    Posts:
    430
    1.2 may fix the perspective camera and 2DTK issues? Consider me completely re-interested!
     
    Last edited: Apr 6, 2015