Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Implementing Day/Night Lighting in 2D

Discussion in 'General Graphics' started by Malecden, May 25, 2022.

  1. Malecden

    Malecden

    Joined:
    Mar 1, 2014
    Posts:
    5
    Hi all,

    I've been looking into implementing Day/Night lighting in a game I'm working on. I have a basic proof of concept but was hoping to get some recommendations for improving simplicity/scalability.

    The way I'm implementing it currently, I have a sub-graph which takes in texture/normal info as well as data about the sun (direction, color, etc...).

    Screenshot 2022-05-25 133305.png

    This gets added to whatever material needs to be lit and works pretty well:
    DayNight.gif

    My concern is mostly with scalability. This requires that I update each material that needs to be lit or implement some sort of messaging for objects with instanced materials.

    I'm very new to shaders and graphics in general, so it's likely I'm missing a simple solution. Some of the other things I've tried are.
    • Using a Freeform light at an arbitrarily large distance with no falloff (This is promising, but wasn't sure how it would look with camera movement)
    • Using a post-processing blit (I could not figure out how to pass normal map data to the shader if that is even possible)
    Any suggestions to improve this would be appreciated. Like I said, I'm pretty new to this, so it's likely I'm just completely unaware of a simple solution here.
     
  2. pixaware_pwedrowski

    pixaware_pwedrowski

    Joined:
    Oct 25, 2018
    Posts:
    116
    You may play with post-processing to make a day-night cycle, I'm pretty sure it will be more convenient to use.

     
  3. Malecden

    Malecden

    Joined:
    Mar 1, 2014
    Posts:
    5
    I had seen that one. It seems pretty close to what I'd want to implement as far as the overall lighting goes. But I'd also like to give a feel for the sun's direction using the sprite normal maps, which it doesn't look like he covers here. Any idea if it's possible to implement something like that?
     
  4. matjumon

    matjumon

    Joined:
    Mar 20, 2019
    Posts:
    15
    You can un-expose your SunDirection variable. That will remove it from the material properties UI, but will allow you to set it as a Global Property using
    Shader.SetGlobalVector
    - that way you only need to update it once instead of updating it on every material. :)
     
    pixaware_pwedrowski and Malecden like this.
  5. Malecden

    Malecden

    Joined:
    Mar 1, 2014
    Posts:
    5
    That was exactly what I was looking for! Thank you so much, that works perfectly.