Search Unity

How to blend colors from multiple game objects

Discussion in 'Shaders' started by sundeepk, May 31, 2020.

  1. sundeepk

    sundeepk

    Joined:
    Feb 15, 2017
    Posts:
    10
    Hi, so i'm very new to shaders in unity and wanted to get some insight on what to look at. Basically, I have multiple quads in my 2D game and I want to blend/add the colors on the parts that intersect with each other

    How do I get the colors at runtime of objects that are intersecting to essentially average the colors out? I've been reading about stencil buffers so I wonder if that is required in this case, though I'm not 100% sure how that would work. Any help appreciated!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Sounds like you want to look into something like this:
    https://docs.unity3d.com/Manual/SL-Blend.html

    The specific blend type being employed in the image you posted is additive blending. However to replicate that specific image where the background is white would indeed require some additional work, probably using stencils. The idea would be to render everything using a black background to start with, with all of the objects you want to do blending also writing to the stencil buffer. Then render a large white object (or whatever you want your background to be) so that covers the screen, but which checks the stencil and skips rendering there.

    An alternative would be to render all of your objects you want to use additive blending using two separate sprites. Render your background as you want it first. Then render all of your parts you want to overlap 100% black. Then render all of those parts again using your additive shader.