- Home /
Shader For cut hole and alpha blend
Shaders: Hey guys, just learning shader concept, i am looking for a shader to achieve following Effect:
Say that there is a wall having some texture, if i place a quad in front of the wall, based on the alpha value of the quad texture the wall should be masked,
1)Where there is quad texture alpha = 0 quad has to get the wall's texture in it
2) Where there is quad texture alpha = 1 quad has to mask the wall completely
(Or vise versa )
3) If the alpha value of the quad in-between 0 to 1 based on that it has to blend(transparent) with the wall.
Similar kind of depth mask effect, but it uses mesh shape to cut the object and results in sharp edges , but in my case i want to use alpha value of the texture to mask the wall.
Hope it makes some sense
Could you provide maybe a pictorial example. It's hard to get a grasp on what you are trying to achieve. You could have an alpha blended shader and simply discard certain pixels as you would in a cutout shader, but I don't know if this is what you mean.
Hey Namey5 thanks for your time. This is the effect i want to achieve, Red,Green,Blue box is a quad, based on alpha value of the quad the wall pixels are drawn, and if two quads overlaps then (like red and blue) is it possible to get the highest alpha value of that point and render the pixel ? The quad texture is like this, black is transparent, white is opaque (if needed i can have visa versa texture alpha value)
I believe the stencil buffer may hold what you are looking for.
https://docs.unity3d.com/$$anonymous$$anual/SL-Stencil.html
Having not used it myself, however, I cannot say for sure. I'll try and run some tests tomorrow.
Answer by MakinStuffLookGood · Nov 15, 2016 at 05:33 PM
There are many many ways to achieve this depending on the needs of your project. Here are a few ideas to get you started:
If you only need circles, set uniform vectors on the wall's material that define where in world space the circles are, in your fragment shader, use a fixed radius, and feather the edges exponentially.
If you don't need an arbitrarily high number of cutouts, define the cutout shapes as extra textures, and use a Vector4 to pack in the texture space x/y and width/height of the shape.
For a truly catch all solution, you could render another camera to a rendertexture that captures the cutout shapes and makes that RT available in shader (via Shader.SetTexture). The wall (and anything else that needs the cutout) can get their position in screen space during the vertex shader, and use use their screen space coordinate remapped to the range 0..1 to sample an alpha value from the Cutout rendertexture!
Those examples are roughly sorted in increasing complexity, and also increasing gpu demand. I'd recommend you roll with the minimum solution that will work for your project (though the catch all solution would be a fun experiment/learning exercise)!