Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Tricks for 2D lighting?

Discussion in '2D' started by Clicksurfer, Sep 13, 2020.

  1. Clicksurfer

    Clicksurfer

    Joined:
    Aug 17, 2014
    Posts:
    50
    Hi everyone :)

    I've been working on a new mobile game for a week or so now. I've been using URP's 2D lighting system to give my game nice lighting and shadows, but unfortunately I've noticed that this has caused a major framerate drop in my game (from 60FPS to 15FPS). I rather have a smooth game than a pretty one, so I took out the lights.

    I wanted to ask - are there any other ways to incorporate lighting into my 2D game, that isn't as CPU-intensive? It could be a trick or a workaround as well. Anything to make my game look prettier without making it unplayable.

    Thanks in advance!
     
  2. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    295
    yes using shaders you can simulate lighting with good performance
     
    Clicksurfer likes this.
  3. justLC

    justLC

    Joined:
    Oct 16, 2018
    Posts:
    37
    Depending on your project, this workaround may work for you.

    You could use white sprites like this

    with a transparent background and use the alpha for intensity.
    For shadows a simple elipse shape might be a good enough approximation.

    Edit: Grammar.
     
  4. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    440
    Some things to try
    • Replace lights with semi-transparent sprites.
    • Avoid shadows or fake them with semi-transparent dark sprites
    • Fake lights with emission. Use an emissive shader with bloom. Remove bloom if emission itself sufficient. Bake "emission" into MainTex's color to avoid special shader altogether. Fake bloom by baking that in as well.
    • Use only global light, or a few real lights, the others as fakes.
    • No normals.
    • Limit size of both real and fake lights you have.
     
    Clicksurfer and xiangtingsu like this.
  5. Clicksurfer

    Clicksurfer

    Joined:
    Aug 17, 2014
    Posts:
    50
    This seems like the simplest solution to implement, however the shadow part is a bit lacking as far as I understand...
    Assuming I want to make a point light in my scene and I want the objects it hits to cast shadows, is there any way to replicate this effect with white sprites like this?
     
  6. Clicksurfer

    Clicksurfer

    Joined:
    Aug 17, 2014
    Posts:
    50
    That's cool! I studied up a bit on shaders and looked for a bunch, I don't see any modern example which uses shaders for lighting.

    Is there an example you could point me to perhaps?
    Thanks in advance!
     
  7. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    295
    no problem, here are 2 shaders you can use for lighting, these work like blend modes in photoshop add and multiply.

    if you use these shaders on an image like @justLC posted, if you use the ADD shader it will look like a flash of light(all the white in the image counts as light source and all the black as transparent), if you use the Multiply shader it will simulate darkness( the blacks in the image will get rendered and the whites will be transparent.)

    if you apply the multiply shader on this image and you overlay it on top of your scene it will look similar to the image you posted.

    mEoBP4n.jpg

    Of course now you would need a way to dynamically generate this black and white image, but if you can figure out a way to optimize that it will be much cheaper than normal lights and look the same.
     

    Attached Files:

  8. Clicksurfer

    Clicksurfer

    Joined:
    Aug 17, 2014
    Posts:
    50
    Thank you very much for the shaders! They're pretty cool :)

    Unfortunately, my problem is precisely with the part you described in the end - I am not sure how to generate such a black/white image dynamically in a way that won't kill the CPU.
    In my head, just the raycasts needed to calculate where the light goes and doesn't go alone would be too much for a mobile build if my predictions are correct :(

    Thanks anyway though!
     
  9. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    295
    ye if your light moves around and changes the shape of the shadows you are better off using the lights
     
  10. juanmabonet

    juanmabonet

    Joined:
    Jan 12, 2017
    Posts:
    23
  11. DrunkRacoon

    DrunkRacoon

    Joined:
    Aug 18, 2020
    Posts:
    13
    You can give this method a crack (github location to project in description):
     
unityunity