Search Unity

Faking 2D lights with z-sorting

Discussion in '2D' started by tchassin, Apr 8, 2021.

  1. tchassin

    tchassin

    Joined:
    Jun 28, 2015
    Posts:
    3
    Hi everyone,

    I'm working on a top-down 2D RPG with pixel art and I'm trying to add fake lights to enhance the visuals.
    Here's what the game looks like at the moment:


    We use isometric depth sorting for all sprites and I'd like to be able to do the same with lights. I wanted to use the URP 2D Lights package but it seems depth sorting is not supported:
    https://forum.unity.com/threads/2d-...-place-it-behind-sprites.899012/#post-5966354

    Since I'm not sure it will be implemented in the future, I'd like to have my own system in place. I made a shader with some additive blending to fake the lights for now but it's not great... I'm also not sure how to properly make this work with ambient lighting.

    This is what I want to achieve (the halo is a light source):


    I don't need normal maps, shadow casting or anything too fancy, all I need is a way to make some sprites look like the URP 2D lights and add fake global lights.

    What would be the best way to achieve this? Can it be done using only shaders or do I need a render texture for my lights?
     
  2. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    you seem to be on the right track

    your problem will be if you want to create darkness ( multiply shader ) or if your lights overlap and you dont want them to add over eachother, in that instance you might need to create a render texture just with fake lights ( only white black and grey sprites moving around ) though that gets complicated with the sorting you want, since you would also have to include copies of the objects that are blocking the light...
     
  3. tchassin

    tchassin

    Joined:
    Jun 28, 2015
    Posts:
    3
    I want lights to add over each other since it's the natural behavior and is useful for having concentric lights like this:



    Using a stencil buffer in my gave pretty decent results but unfortunately it doesn't work in this case :(