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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

non-additive blending?

Discussion in 'Shaders' started by Programmer-David, Dec 6, 2013.

  1. Programmer-David

    Programmer-David

    Joined:
    Sep 17, 2012
    Posts:
    21
    hey,
    is there a way to achive this with unity shaders : ) ?

    the 2 circle are representing seperated meshes,
    the square represents an object in the background.
    http://s14.directupload.net/images/131206/fbvqtsaq.png

    i need the circles to keep their color where they are overlapping (like on the right side).
     
    Last edited: Dec 6, 2013
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    The blending hardware blends every final fragment output by the shader with the existing background, which is what causes the overlap. It seems you want transparency for the circles at the same time as preventing them from affecting each other. That's going to be difficult. Without them being transparent, you could use the destination alpha channel and a 2-pass shader, in the first pass draw the circles to the dest alpha with a straight `replace`/solid output and not output anything to the rgb channels, then in the second pass only draw solid circles multiplied by the value in the destination alpha channel, something like Blend OneMinusDstAlpha,Zero. But this doesn't account for transparency. Transparency requires both parts of the blend equasion to be available, to do Blend SrcAlpha,OneMinusSrcAlpha so there is no way to do that at the same time as reacting to the destination alpha channel. So it's not possible.

    Another option is to draw your circle first solidly and then draw the rectangle afterwards with some other blend mode that mimics the appearance of transparency.
     
  3. Programmer-David

    Programmer-David

    Joined:
    Sep 17, 2012
    Posts:
    21
    Hey,
    thank you for your very detailed answer, it gave me an excellent insight into the problem.

    this task seems to be a bit over my current skill-level : )
     
  4. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Other option is to draw the circles into a render target without transparency and then draw the render target to the screen with transparency.