I have this question for a long time. Let’s say you have a plane and you want to display the pixels under the plane in black, and the ones above the plane in white. How I can know in the fragment shader at the time I calculate the color of the pixel, that its position is above or under the geometry?
If someone found a solution to a similar problem I will be glad to heart about it.
Or it can be simple as, if the pixel is located in the 3d space <0 I display it in black and in white otherwise.
This is the sort of thing that the depth buffer is good for. You render your plane first, with ZWrite On. Then you do your other geometry in two passes: one with ZTest LEqual, and the other with ZTest Greater. The former will clip geometry that goes behind the rendered plane, and the latter will clip it when it goes in front or at the same depth as the plane.
If you don’t actually want to draw the plane itself, but just write its depth, you can use a shader with ColorMask 0 to prevent it from drawing to the frame buffer at all.
If you only need to know if a ‘pixel’ (fragment, actually) is above or below a simple plane in the y direction, you can pass that value as a simple shader property, and compare it with the worldPos.
Daniel Brauer@: Interesting idea, but I need to have the Y coordinates, it is about water. I want to darker under the water, and maybe shade under the water depending on the Camera/Geometrie orientation… not something easy to do!
rc-1290@: I see what you mean, but if I pass the Y threshold from C# or JS, how do I know in the Fragment shader that the current pixel is above or under. I do not have the x,y,z position of the pixel as if it was a raytracer for example… I only have to x/y screen coordinates, the normal, or the vertex x,y,z…
If I want to have a water separation between the top and the bottom of the water, it has to be pixel by pixel. Now if I use a grid with animated waves, it gets even more complex…
So I would like to find a way to estimate the Y coordinate for each pixel I draw… it is maybe impossible without Ray Tracing?
For a moving water surface you’ll need to pass more than one value, so the technique I was talking about won’t work, because the height values are different everywhere.
I briefly used the built-in shadow casting functionality in my watersimulation. It had a pretty interesting effect, but it was a bit odd because I didn’t add any underwater lighting, so the effect was too dark, and looked odd at the maximum range of the shadow. Improving the water surface graphics themselves had a bigger impact on the ‘prettiness’.