"Overlap" effect?

I haven’t tried my hand at shaders yet. Could a shader theoretically do the following?

  • It looks like a plain old glossy opaque object normally.

  • BUT when another object of the SAME shader passes behind it, you see a new color. LIke “seeing through to” the other object, only what you see is a color change.

Example:

Two solid yellow squares, one near, one far, move across the screen until their corners overlap. The area of the overlap makes the front square turn blue just in that area. So you are “seeing through” to the other yellow square’s shape, and that shape manifests as blue instead of any kind of real transparency. Meanwhile, both squares are opaque to the background (including other objects/shaders), with the “see through” effect ONLY affecting each other. (See attached mockup.)

Would a shader (or pair of shaders, or tags/layers) be able to do that?

It’s not anything important, just an exercise trying to re-create a 3D logo effect that I did with masks in Flash.

14772--499--$overlap_382.jpg

Right now I can’t think of an easy way. The easiest would be using stencil buffer of the video card, but stencil buffer access is not exposed in ShaderLab at the moment. I’ll file a note to myself to somehow expose stencil buffer in the future.

Other than that, you could create a screen-sized render texture, and draw the shapes with additive blending into it. Then write a shader that reads the rendertexture and checks for some color sum (e.g. the shapes each add 1; and the shader checks if sum is 2, then it outputs another color). This would require Unity Pro though.

Interesting–not worth it for just playing around like I am, but it’s interesting to think about how it COULD be done :slight_smile: