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

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:

http://i.imgur.com/eF5mn7x.jpg

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:

http://i.imgur.com/ykyPokv.jpg

The resulting mask is a grayscale alpha channel:

http://i.imgur.com/C72Pq3r.jpg

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

http://i.imgur.com/8gUwtVL.jpg

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!

Looks like your best bet would be to use a RenderTexture combined with Camera.RenderWithShader instead of a GrabPass. The replacement shader used by RenderWithShader could be one which only displays the alpha channels of rendered objects.

The resulting RenderTexture would then be a greyscale mask image (like the one you show above) which you could use in your main shader if you plug it in to a texture slot via script.