Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

2D Lighting?

Discussion in '2D' started by Marscaleb, Mar 9, 2015.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    977
    I'm thinking that I would like to add a lighting effect to a level in my game (which is a 2D platformer.) I am trying to figure out the most reasonable way to go about it.

    My first thought was that I would just make a large sprite which is mostly dark with a bright spot in the center. I could then have this follow the player and/or attach it to any "lights" I want in my scene.
    However, this would take up a bit of memory, since I'd need a large screen-sized alpha-channeled texture or two. It might be acceptable on PC but I want to keep my game running smoothly on mobile devices if I can.

    So, I'm wondering if there are some better and more-efficient ideas as to how to create lighting. I imagine there could be something set up with shaders, but I don't know what options are available there; I wouldn't know where to begin.

    UPDATE:
    I am only looking for some simple light effect, like with these examples:

    Simple, although this one looks a little blocky, like there are exacting squares of values.


    Very much what I'd like to do, but with multiple light sources and perhaps a stronger effect; make the darks darker.


    Way more lights than I want in my level, but that is pretty much exactly what I would like to be able to do.

    This demo: http://www.killstar.eu/demos/platformer2/Build.html shows a nice colored lighting effect following the player. The dark areas have a blue-purple color to them, and the lit areas have a yellow-orange tint to them. I want to be able to apply a color effect, if it isn't too far out of reach/ (But only has that one light, when I would like to have multiple lights, if I can.)
     
    Last edited: Mar 9, 2015
    theANMATOR2b likes this.
  2. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
  3. sleekdigital

    sleekdigital

    Joined:
    May 31, 2012
    Posts:
    133
    @Marscaleb
    I've seen your general idea used and suggested in several places except, instead of using alpha, they often use a black and white radial gradient and then use the particle additive shader.

    You might also be interested in this free item in the asset store...
    https://www.assetstore.unity3d.com/en/#!/content/24083
     
  4. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    977
    Those are some pretty awesome examples, but they are WAAAAY more advanced that what I'm looking for!
    I don't want shadows, occlusion, normal maps, or any of that stuff. I just want a dark screen where I can add brighter spots, preferably with some color.

    I am updating my original post with some examples.
     
    Last edited: Mar 9, 2015
  5. sleekdigital

    sleekdigital

    Joined:
    May 31, 2012
    Posts:
    133
    Ferr2D has some shaders that have some optimizations for using lights in 2D.



    You could bake the static lights and use the shader for any dynamic lights.
     
  6. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    977
    Oooh, those are some pretty lights!
    Hmm, but that requires a custom shader, and I have no knowledge of how to create such a shader.

    ...

    I've been experimenting today with making some lights and using the particle additive shader to make them lighten my scene. But I never got it to look right.
    I had to cover the whole screen with a dark material using a multiply shader. It successfully made the scene dark. But the additive shaders never worked right with it. The color of the darkness would bleed into the color in the light. And the additive lights would make the scene underneath super-bright, and would wash out the detail within the scene. But if I lowered the intensity of the lights, the colors would wash out. I never found a proper balance that looked right.
     
  7. sleekdigital

    sleekdigital

    Joined:
    May 31, 2012
    Posts:
    133
    If you search hard enough you might find a similar shader for free, otherwise the shader is included with the asset...
    https://www.assetstore.unity3d.com/en/#!/content/11653
     
  8. sleekdigital

    sleekdigital

    Joined:
    May 31, 2012
    Posts:
    133
    You could also try the built in sprite/diffuse shader. Depending on your scene and your target devices, the performance might not be too bad.