Search Unity

Question How would you fake lights (in 2D) via shaders/blend modes?

Discussion in 'General Graphics' started by JoeStrout, Jan 31, 2023.

  1. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Suppose, in a 2D game, we can't afford to use actual lights. But we want to fake it:



    It seems like this should be doable via shader tricks, maybe involving blend modes, but I can't quite see how to set it up. Any suggestions?

    Follow-up question: can we do it in a way that allows for tinted fake lights?


    Thanks,
    - Joe
     
  2. LethalGenes

    LethalGenes

    Joined:
    Jan 31, 2023
    Posts:
    69
    You would use an additive, multiply or subtractive shader. The default particle effect shader has these options. And a few legacy particle shaders also have this option.
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Thank you, but I still don't see it. What is the structure of objects these shaders would be applied to? In the back we have the "real" sprites (the ground tiles, the player, any enemies, etc.). Then on top of this we have, what? A big quad that renders black for the darkness? And somehow this interacts with round sprites that represent the lights, using some additive, multiply or subtractive shader to somehow cut holes in the blackness?

    More detail would be appreciated.
     
  4. LethalGenes

    LethalGenes

    Joined:
    Jan 31, 2023
    Posts:
    69
    SHAD.png SHAD_2.png

    I imagine in its simplest form if you inspect the above screenshots i used a Subtractive shader on an object in front of my background(leaf) I used the particle effect shader. And I can continue to modify its color opacity or what not, through the sprite renderers color setter.

    LAYERS.png
    This uses a combination of background, subtractive and additive layers in that order. Additive introduces the red lighting effect.
     
    Last edited: Jan 31, 2023
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Subtractive makes a dark spot on the background. I could do the same with ordinary alpha blending, just by drawing a black translucent sprite on top of the other sprites.

    I could even make a single fake light this way, by having a giant sprite that is black/opaque everywhere except for a transparent area around the light source. But I can't support multiple lights this way. And I think the same applies to your subtractive technique above.

    How would you do something like the screen shots in my original post? (Assume those light sources can move about and overlap.)
     
  6. LethalGenes

    LethalGenes

    Joined:
    Jan 31, 2023
    Posts:
    69
    Its_a_trick.png
    Can you see the straight edge between the area's?



    The shader variant is not the same as an alpha overlay. They modify the appearance of the rear image
    I don't have the graphics in this project for the accurate replica, this is just a 5 minute cobble. It'l be as far as I go on the subject but maybe somebody else has another way!

    You could always shine real lit lights on the image
     

    Attached Files: