Hi Everyone,
I’m somewhat new to shaders but I’ve been working with them pretty heavily over the last couple weeks so things are starting to make sense for me. This one has me stumped though.
I want to create a z-mask shader that uses an alpha blend.
- The object that uses this shader would have a black and white image applied to it.
-
- Whatever is white would mask the objects behind the one with the shader.
-
- Whatever is black would just be invisible and you would see through it like regular transparency.
-
- Any shade of grey would be a blend between the two.
The closest I’ve been able to get is a shader that does 1. and 3. but black renders as black rather than transparent.
This is the shader I’ve got so far. I’ve got a feeling I’m pretty close but after staring at it for a couple days I can’t figure out what to do next.
Shader "Custom/cameraMask" {
Properties {
_Mask ("Culling Mask", 2D) = "white" {}
}
SubShader {
Tags {"Queue" = "Background"}
Lighting Off
ZWrite On
ZTest Always
Blend SrcAlpha OneMinusSrcAlpha
Pass {
SetTexture [_Mask] {combine texture}
}
}
}
Try swapping the tag line
Shader "Custom/Test" {
Properties {
_Mask ("Culling Mask", 2D) = "white" {}
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
Lighting Off
ZWrite On
ZTest Always
Blend SrcAlpha OneMinusSrcAlpha
Pass {
SetTexture [_Mask] {combine texture}
}
}
}
Hmm… that doesn’t seem to be working for me.
The black is now opaque, which is proper blended transparency. Nothing is masked out though.
Is that how it works for you?
Are there any news on this? I have gotten to the same point as OtsegoDoom (I am also fairly new to shaders), but I have everything black creating a zmask and everything white getting rendered. The results are this:
The first image shows the shader running in Play mode, and the second shows it in the Editor window. Basically, I would like it as it is now, but instead of the white getting rendered, I would like anything white on the texture to be transparent.
Also, OtsegoDoom I think the reason twiesner’s shader is getting rendered opaque is due to the rendering queue being set to Transparent. It needs to be rendered before standard geometry for it to mask.
You can’t blend between the two using zmasking - the depth buffer is either written to or it is not.
Farfarer, I’m not sure I follow. I understand that that the depth buffer is either written to or it isn’t, but all I want is for the white to be transparent rather than white. At the moment, the main texture is a circular gradient going from white to black and set to “Alpha from Grayscale”. As you can see from the image, the effect is achievable, since the shader gets written to the depth buffer, but only the opaque part of it actually gets rendered (from what I understand, the rest does not get rendered, hence the masking effect). I just need a shader that renders the white part actually transparent, but the problem is with the above shader everything with a low alpha does not get rendered.
I hope I am making sense and I apologize if I don’t or it seems I don’t know what I am talking about, I am also quite new to shaders.