Is it possible to create a shader for an object that makes a hole in an other object when passing threw it ?
It’s a bit like a boolean operation like this. But the aim is not to reconstruct the empty part of the mesh like in the example, just occlude/erase it.
Maybe more like this. But here it seems that a texture occlude the object and it should be an object.
How can I do that ? Any help, reference or advice ?
Sure it’s possible, just google “csg gpu” (for constructive solid geometry on GPUs) and you’ll find a couple of papers explaining how to do it. In Unity you probably need the Pro version because these algorithms typically make heavy use of the depth buffer.
For special cases, you can probably also find ways to do it with the free version.
Just to explain exactly my needs, the aim is not to reconstruct some parts but just occlude them like this with an object. In this example we can imagine a sphere occluding the box.
Brutally speaking, you should be able to “paint” alpha value of the material based on mouse position/occluder object distance. People already done that, it would be like a mouse paint on object but refreshes every X frames to reset its alpha value except where the mouse/occluder is.
So I guess that would be more of a scripting issue than a shading one!
My2c
u have to catch the camera position
set the distance to fade between alpha = 1 and alpha= 0
use saturate function so that u have 0 or 1 value
set the final o.Alpha to the value of the previous step
Thanks Martin !
You’re right the right word is “cutaway”, I saw this post but I didn’t had the time to test it.
I tested a simplier shader which consists of playing with the Queue number. But the main problem of this, is that all of those objects are culled and that’s not what I want.
We can imagine a wall and a hole. Behind the first wall another wall.
The first wall’s hole is well culled but the problem is that the wall behind the first wall is not drawn because he is on the same render Queue and I should see it.
Thanks ilya_ca !
I just read this tutorial zhich is very interesting. I thought about a solution but I don’t know if it’s possible. Maybe you can help me.
Is it possible to not render what’s inside a box, maybe using the z buffer ? but render what’s behind. How can I achieve that ? script / shader ?
I don’t care about backface culling. the aim is to hide parts that are in the box. I found a way to achieve that here : http://wiki.unity3d.com/index.php/DepthMask
But the problem is that everything with the same RenderQueue is culled even if it’s not in the box.
In all shaders of all objects that are potentially cut away: compute vertex position in object coordinates of the box (define inverse model matrix as material parameter) and hand it to the fragment shader; in the fragment shader do a discard if the interpolated object coordinates are inside the box (i.e. -0.5 < x < 0.5 or -0.5 < y < 0.5 or -0.5 < z < 0.5).
Yep, but this is done in the “Mesh” shader, no ? This is a problem if I have multiple “Culling box”. For me the culling process needs to be in the “Box” shader.