Search Unity

2D lighting to effect sprites based on y position for top down games

Discussion in '2D' started by Sersh, Mar 3, 2021.

  1. Sersh

    Sersh

    Joined:
    Oct 25, 2015
    Posts:
    23
    Is it possible meanwhile to realise a similar effect to this with the build in lighting of Unity:



    Meaning: Objects that are lower on the y axis (In front) of the light source appear dark, despite both trees have the same distance to the light source. (Image From Article: https://www.gamasutra.com/blogs/Svy..._Keeper_How_the_graphics_effects_are_made.php )

    Is this something you can achieve with shaders or shadergraph?
     
    Last edited: Mar 3, 2021
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Looks like something you'd want to use shadow casters for. Best to look into them. You can make your own custom lighting shader, but would probably be a pain. That said, having tinkered with shadow casters, the look they give might not work for you so you might have to.
     
  3. Sersh

    Sersh

    Joined:
    Oct 25, 2015
    Posts:
    23
    Yeah Shadowcasters is not a solution. I already assumed that it should be possible to modify the Unity shader in a way to get this result - are there any good tutorials on shaders?
     
  4. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,513
    You might want to first examine how lighting works in the 2D Renderer. In particular check out the file RendererLighting.cs (search Project in Packages). There's essentially a 2-step process: lights written to RenderTexure then lit sprites sample that RenderTexure. This has the advantage that lit sprites don't check themselves against every light (more lights & sprites therefore scale better), but the disadvantage that you don't have positional information about the lights.
     
  5. Sersh

    Sersh

    Joined:
    Oct 25, 2015
    Posts:
    23
    Interseting, but how does normal mapping work in that case, as you'd need the direction of the light for the sprites to display normal mapping properly?
     
  6. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,513
    The light's shader considers that as it renders onto the normals render texture. At least that's my understanding. Haven't looked at that much. See Universal RP > Shaders > 2D > Light 2D Point Volumetric
     
    Last edited: Mar 4, 2021
  7. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    you wont achieve what is happening in this image through out of the box means

    reproducing this effect will require a large amount of trickery with invisible 3d models shaders and normal mapping, this is not something that you can drag 1 light into a scene and it works like this
     
  8. caiotoon

    caiotoon

    Joined:
    Apr 16, 2023
    Posts:
    11
    I believe you want to use normal maps to achieve this.