- Home /
Depth mask with alpha / adding alpha to frag grabpass shader
I'm trying to get a depth mask shader to get alpha blending, to no avail.
The problem I'm trying to solve is to be able to fade out multiple objects with different types of renderers with complicated geometry like it were a group on photoshop, i.e. all at the same time, no double rendering of hidden tris, without having to calculate complicated alpha/z buffer or bother with cull masks etc.
Since my scene is very simple, I thought of using a plane that occludes the objects with a depth mask shader, but a normal depth mask shader just won't both be able to draw over an object while at the same time having alpha for queue ordering reasons (or at least, this is the conclusion I've come to).
So my plan has changed to making a grabpass of the background (I'm trying to fade to the skybox for now) and then applying the resulting texture to the plane and applying alpha. I've been trying to do so for the past week. By setting the geometry I need to fade to a cool queue 3000 and making the shader transparency -1, I'm able to make the grabpass before the geometry has rendered. Then I make a Zwrite on pass, enable alpha blending and just project the pixels I grabbed before while adding alpha... but no matter what I do, the end result will be a) always opaque b) instead of fading to transparent, fades to black/white c) always transparent This is how it is right now: almost there, but the alpha won't affect the end result at all.
Here's the shader – what am I getting wrong?
Shader "Custom/Maskv5"
{
Properties
{ _Color ("Color", Color) = (0.5, 0.5, 0.5, 0.5) }
SubShader
{
Tags { "Queue" = "Transparent-1"}
// Grab the screen behind the object into _BackgroundTexture
GrabPass
{
"_BackgroundTexture"
}
// Render the object with the texture generated above and apply alpha
ZWrite On
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag alpha
#include "UnityCG.cginc"
fixed4 _Color;
struct v2f
{
float4 grabPos : TEXCOORD0;
float4 pos : SV_POSITION;
};
v2f vert(appdata_base v) {
v2f o;
// use UnityObjectToClipPos from UnityCG.cginc to calculate
// the clip-space of the vertex
o.pos = UnityObjectToClipPos(v.vertex);
// use ComputeGrabScreenPos function from UnityCG.cginc
// to get the correct texture coordinate
o.grabPos = ComputeGrabScreenPos(o.pos);
return o;
}
sampler2D _BackgroundTexture;
half4 frag(v2f i) : SV_Target
{
half4 bgcolor = tex2Dproj(_BackgroundTexture, i.grabPos);
bgcolor.a = (_Color.a);
return bgcolor;
}
ENDCG
}
}
}