Search Unity

Grab Pass alternative? Frag shader that applies alpha values behind it to itself.

Discussion in 'Shaders' started by delogan, May 28, 2013.

  1. delogan

    delogan

    Joined:
    May 11, 2013
    Posts:
    2
    Hi!

    I was wondering if there was a more efficient way to utilize a grab pass? Current model makes it very expensive and adds a draw call for every use. I am not very experienced in shaders, and I just need to implement a simple frag shader that assumes the alpha value of every object behind it (think grab pass grayscale alpha channel):


    Let's say we have a scene like this, with a plane that has our shader+material combo, and a camera that points perpendicular to our plane:


    Now, if we were to add a semi-transparent materials to our objects, we want the plane to see whats behind it (relatively to the Main Camera, of course), and set the total alpha value through a fragment shader to itself, like this:


    The resulting mask is a grayscale alpha channel:


    And the final result should look like this from the Scene view:





    It is a very simple concept, but resulted grab pass shader was clunky and required layering to get the correct alpha (am I doing something wrong?). That is why I need just a simple frag shader that will allow me to modify/recalculate the alpha.

    So how does that data input works in a shader? I always thought of shaders as a simple texture processor, but with grab pass it seems way more than just that. I have several books about shaders, including the cg prog tutorial by nvidia, and none of them explain how does data get in and out in simple terms. Or how the color calculations are made. Or detailed explanation of texture combiners, etc etc.

    Please help or give some advice to a shader beginner! Would GREATLY appreciate your help.
    Long live Unity!
     
  2. BIG-BUG

    BIG-BUG

    Joined:
    Mar 29, 2009
    Posts:
    457
    If you just want to visualize the alpha channel you might get away with simple blending.
    >Docs<

    Try something along the lines of "Blend DstAlpha, Zero"
    The result would not match your scene-view picture as alpha is set to what is rendered behind the plane in camera space. The main camera however should show the expected behaviour.