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

Question Blending Lights using max components

Discussion in '2D' started by AmmarO74, Apr 5, 2023.

  1. AmmarO74

    AmmarO74

    Joined:
    Apr 23, 2022
    Posts:
    4
    Hello,

    I am trying to blend multiple lights so that the brighter light ignores the softer light.
    For example, if my player is holding a candle in bright daylight, the candlelight should be ignored and the whole area should be lit in normal white light. While if they are holding it at night the light should be lit in a soft orange light. Also, if a blue light and a red light intersect the area should be lit in a purple light.

    I tried using the provided overlap operations but neither seemed to work for me.
    Additive: just adds the two lights together meaning my candlelight would add an orange tint to the world even at high noon.
    Alpha Blend: overwrites the light below it, so the softer light can actually overwrite the bright light. Also, it doesn't seem to work with Ambient light.

    Is there an option for what I want? If not, is there a way to program a new overlap operation?

    Thanks,
     
    DragonCoder likes this.
  2. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    +1 would also be interested in a solution to this
     
  3. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @AmmarO74I think you are talking about two different things here. Your candle example during daylight is clearly a case of stronger lightsource washing out the a weak light source, so that the effect is not visible in that exposure. However in your night description you also add color blending to this exposure issue, which is another totally different thing.

    I assume you are using the URP 2D renderer. I'm not sure if it's possible to write a custom Light Blend Style, I tried to look into this a while ago but didn't have time to find anything conclusive/documentation about this. I did find back then this thread which would lead me to understand it's not yet possible:
    https://forum.unity.com/threads/how-to-modify-lighting-blend-styles.792450/

    But I think you could possibly make your own custom lighting system, render the lighting to a render texture, and then composite it over the camera view. Of course you will lose then many of the tools/features etc. in the renderer.
     
    AmmarO74 likes this.
  4. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    so what is the answer to the first question? how to make the candle light have no effect on areas lit by daylight?
     
  5. AmmarO74

    AmmarO74

    Joined:
    Apr 23, 2022
    Posts:
    4
    Thanks for the reply.

    I do not believe the examples I gave are different things. I used these examples to demonstrate that I want to use the highest component of the overlapping lights (Essentially I want to OR the RGB channels) So to put some numbers to demonstrate how that would fit all the cases I mentioned:
    1- Candle (0.8, 0.8, 0.2) in Daylight (1, 1, 1,) should result in (1,1,1)
    2- Candle (0.8, 0.8, 0.2) in nighttime (0.3, 0.3, 0.3) should result in (0.8, 0.8, 0.3)
    3- Blue Light (0,0,1) and Red light(1,0,0) should result in Purple (1,0,1)

    This should hopefully clarify further the effect I'm looking for.
     
    karderos likes this.
  6. AmmarO74

    AmmarO74

    Joined:
    Apr 23, 2022
    Posts:
    4
    This is the solution I went with eventually. It is not exactly what I want as it only blends lights with the ambient light. However, it does make sure that brighter lights always overwrite softer lights.

    It also feels a little bit hacky, since this kind of thing seems to belong in Shaders and Render Pipeline magic, which unfortunately I am still not very familiar with.

    The code is kinda dirty but I put it here nonetheless if anyone should find it useful.
    To use it add the MixingLightGlobal Component to your ambient light. and add the MixingLight component to your other lights and set their overlap operation to Alpha Blend.
     

    Attached Files:

  7. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    Nice code but Im sure there must be a better way.. How is this done for 3D games? I'm sure theres a way replicate real world conditions out of the box where daylight doesnt get multiplied by the light of a candle?
     
    AmmarO74 likes this.