Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. We are looking for your feedback about Templates! Tell us about your experiences by taking our survey.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

(Multiply) Blending isn't being cleared through cameras

Discussion in 'Shaders' started by shellyalon, Jun 16, 2018.

  1. shellyalon

    shellyalon

    Joined:
    Mar 26, 2013
    Posts:
    16
    Okay, I had a hard time giving this thread a title. But I can show you pretty easily where I struggle.
    I'm making a game, where I need objects to overlap and mix colors. For example Blue + Yellow = Green. I'd love to do this by code and define the colors myself, but I haven't found a good way yet.

    Right now, I want to use the blending modes and color correct everything in post-processing. My problem now is, that I can't have a colored background because the objects blend into the background. So I tried the logical way to use 2 cameras: but this didn't help.
    I put the two cubes on one layer and set the second camera's culling mask to only this layer. I tried "depth only" and "don't clear" but nothing works. The cubes always blend into the background and are much darker.

    With a white background: Bildschirmfoto 2018-06-17 um 00.25.52.png

    With a red background: Bildschirmfoto 2018-06-17 um 00.26.01.png

    What I actually want: Bildschirmfoto 2018-06-17 um 00.40.48.png

    Config: Bildschirmfoto 2018-06-17 um 00.23.34.png

    Any chance that it's possible? The only solution I see is to render the cubes in white in the background, but I don't know how to do that efficiently. Any ideas?
    Also, if you have an idea how to blend colors in a more flexibel way, so I'll be able to configure it by hand (rather than by math), that would be awesome. (so I could do something like Blue + Yellow = Red or something).

    Thanks!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    9,782
    Each camera is just going to blend on top of the previous one if you’re not clearing.

    What you probably want is to use stencils. In the basic case of yellow and blue makes green, you could have the camera clear white, render the two cubes with the multiply color shader, but also have them write to the stencil buffer some non zero value. You then render a full screen red object that only renders where the stencil is still zero.

    https://docs.unity3d.com/Manual/SL-Stencil.html

    If you want more control you could use stencils still using multiple passes to find where objects overlap.
     
    shellyalon likes this.
  3. shellyalon

    shellyalon

    Joined:
    Mar 26, 2013
    Posts:
    16
    Hi, that was a great advice! Solved exactly what I was struggling with. Thank you, bgolus!
    Bildschirmfoto 2018-06-17 um 12.59.39.png

    I'm still not sure how to get more control using stencils. I'd love to have a solution, that allows me to define colors by hand. But it's a bit more complicated than two cubes: I have lines that are overlapping. They can overlap themselves, too and each line can have differently colored segments.
    My guess would be, that I'd have to use a GrabPass? But would that apply to a line that overlaps itself? Here's a mockup of what I actually try to do. Maybe you've got an idea. And.. maybe it's something for another thread.
    Bildschirmfoto 2018-06-17 um 13.09.22.png
     
  4. shellyalon

    shellyalon

    Joined:
    Mar 26, 2013
    Posts:
    16
    Okay, I do have a problem now. It works great with one camera, but with two camera I am still having the same problem. The stencils don't work and the cubes are being blended into the darker background. And with one camera I can't use color correction to just tweek the colors of the cubes/lines. :/
    Am I doing something wrong?
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    9,782
    For the logic you’re looking to do you’ll likely have to do much of it in c#, or at least keep track of where important overlaps are and only do additional stencil work there.

    For the simple case of a line overlapping another and showing a different color where they overlap:
    Draw blue line with stencil writing to Ref 1.
    Draw orange line with stencil test against Ref 0, Comp Equal.
    Draw same line but green and stencil test against Ref 1, Comp Equal.

    The result is the orange will only draw where the blue line has not, and the green only where the blue line has drawn.
     
    Last edited: Jun 18, 2018
unityunity