I’d like to start off by saying that my shader coding knowledge is pretty limited. I know the basics and I can eventually understand how a shader works, but that’s about it. I’ve made a simpler scene and updated my original question to better explain the problem that I’m having.
Please take a look at the following image:
I have 4 objects in the scene above. The ground (beige), one red cube on the left side that’s above the ground, one red cube on the right side that’s half above and half under the ground and a blue wall in front of the 2 cubes. The red cubes render only if they’re behind the wall so if I was to delete the wall they would become invisible.
What I would like to achieve is to have the red cubes be visible when behind the wall, but not the area that’s under ground. So the right cube should only be rendering the top red part and not the bottom beige part as well.
To gives you a scenario where I would be using something like this, imagine an RTS game where you want units behind walls or other objects to be visible so the cube shader would be like an outline or silhouette shader. Now imagine that you also want these units to hide by going underground. Currently, if they were to go underground but were also behind a wall, they would be visible.
One idea could be to compare the output color and if it’s not red then don’t output anything, but I’m not too sure how to write that so it works :(. If you look at http://www.pulciu.com/screenshot.jpg the right cube which is half above the ground and half under, prints the top part as red and that’s all that I would like to keep.
Holly… I don’t even know what to say, thank you so much man! I’ve been trying literally all that I could find and think of for the past couple of days but because my shader knowledge isn’t that great I just couldn’t get it to work. This is perfect, thank you! thank you! thank you!
My approach is based on testing, if coordinates of cube in world space are located under certain vertical value (in my example: i.worldPos.y < 0.0) , I disable output the result of the current pixel. Of course, you can create various conditions to decide which pixels discards. For example you can use 3D Signed Distance Fields to define “cutoff” shapes.
So, in this example everything with Cube material and located under 0.0 world space position is invisible, so I set ground plane to Y=0.0 to visually compose scene:
This is really cool! I didn’t use anything like this before but now seeing your examples there are definitely many awesome effects that could be achieved with this. Thanks again, really appreciate it!