Essentially I’m using the alpha channel as either fully opaque or full transparent and I would like if two fully opaque pixels are to be drawn on top of each other they become transparent. This quick image of two overlapping rectangles should illustrate what I mean. I feel like this should be really simple, but I’ve been getting nowhere with it. Would majorly appreciate any help, and if you can work it in to the shader I’m using right now I would be beyond grateful.
To give you the background I’m using this along with SetRendeQueue from http://wiki.unity3d.com/index.php?title=DepthMask to create a depth mask which takes alpha from a texture (so the depth mask only appears in certain places).
You can see the slightly smudgy text ‘Chara’ which uses the alpha testing (and the big circles underneath are also masking letting the colourful background come through). Essentially I want to write chara atop the circles and have that text not be cut out (amongst other possible uses of placing masks on top of each other). It’s getting complicated now but it would be enormously beneficial for my game to get this working (which is so very nearly done now).
I also looked into Ztesting so if there is alternative way to achieve the same effect (masking the mask or blocking parts of the mask without blocking what it’s masking) I’m all ears. I think inverting alpha would be the simplest and most flexible way of doing this however.
Enormous thanks for any help. Let me know if you need any more information on anything.
My thoughts: Adobe Flash would be perfect for this, because you can do a similar trick with overlapping vector graphic paths.
I guess you could draw the entire scene to a RenderTexture (using shader replacement), with a shader that writes a 1 when the number of rendered surfaces is odd, and a 0 if the number of surfaces is even*. Then use that data to determine if an object should be rendered during regular rendering. But there are simpler ways to draw text with a specific color, if you don’t need its transparency to depend on what has been rendered in the scene.
Perhaps someone comes up with a better idea if you describe why and how you want to use this effect.
[edit] no wait, I’m too used to incremental shaders with renderTextures, accessing previous data, and I don’t think Grabpass actually uses the native color buffer, so changing output based on that might be expensive (I don’t know, try it ;]) To find out if its even or odd, you could do some kind of fancy alpha blending.
Here’s a video of the project from a couple months back when I was last working on it, hopefully should give you an idea of the masking technique. You can see how I’ve adapted the 3d text shader to mask as well. Now what I want to do next is, essentially, masking the mask so it masks the masked object less. I want to do this precisely because I want to put text atop the mask and make it readable against the masked object (because it doesn’t mask it). Of course I could use alpha textures that have the text I want to write missing, but that would be time consuming and less flexible than a coded solution.
The alpha flipping shader doesn’t need to go x layers deep (though that would be cool), only has to flip once.
I think I just need some help in manually alpha blending which I’ve never done. How can you access the dst pixel’s alpha value? Then based on this ( the texture of the current object), I would either flip it or leave it. I assume I will probably have to use a fragment/vertex shader? Even a fragment/vertex shader version of the above would be a very handy start.
With a comma in set texture the bit before describes how to combine color and after just the alpha. So here, it’s just taking alpha straight from the texture (this doesn’t have much impact on anything, can easily change it).
Probably I missed something, but as far as I see it, a possible solution strongly depends on your setup. Are you talking about blending two textures or blending two objects?
If you have just 2 textures to blend in a XOR manner, you could have them set as material properties and have them XOR blended in a simple fragment shader. Something like combinedAlpha = abs(firstAlpha - secondAlpha) should perfectly work, even for semi-transparency.