Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question 2D Sprite Shader light intensity based on y position.

Discussion in '2D Experimental Preview' started by SpudGums, May 19, 2020.

  1. SpudGums

    SpudGums

    Joined:
    Mar 10, 2015
    Posts:
    10
    How would I get the 2D point light positions in a shader using the Universal Render Pipeline in Unity?
    I'm trying to simulate a 3D effect by reducing light intensity from lights that are above a sprite in the Y position.
    I duplicated the Sprite-Lit-Default shader and easily managed to change the output by doing a lerp between the sprite's default color and black.

    I Just changed
    o.color = v.color;
    to
    o.color = lerp(v.color, black, lightYPos);

    The problem is that I can't really get the Y pos from the light sources, and I only know it works because I manually set the lightYPos from 0 to 1 for testing.

    Anyone knows how I can manage to get the point lights positions from a shader?

    The effect I'm trying to achieve is like this one:



    This is from a Gamastra article where the Graveyard Keeper developers share how they did some of the effects in their game.

    They say that "There is a shader that calculates the distance between a light source and a sprite on the vertical axis. If it’s positive (the light source is in front of the sprite), we illuminate the sprite like we always do, but if it’s negative (the sprite is blocking the light source), then the intensity of lighting is fading depending on the distance at a very quick rate."

    But they don't share any info about the shader itself.

    Would love some help on how I can retrieve point lights positions in the URP 2D Sprite Lit Shader...
     
  2. RafaelGaju

    RafaelGaju

    Joined:
    Apr 8, 2020
    Posts:
    6
    "What I ended up doing was just publishing the light's position to a scriptable object and reading that from a script that's on each tree object. Then I have all the positional data I need, so I pass (transform.position.y - lightPos.y) into a sigmoid function and then pass that value to a copy of the 2D/Sprite-Lit-Default shader"

    This is what someone on Reddit did to get a similar effect

    here is the post:
    https://www.reddit.com/r/Unity2D/comments/m1kqfn/finally_figured_out_a_shitty_way_to_kind_of_get/