Search Unity

Boolean shader

Discussion in 'Shaders' started by MaT227, Dec 6, 2012.

  1. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    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.
    http://unitycoder.com/blog/2012/02/22/x-ray-cutout-shader-with-mouse/

    How can I do that ? Any help, reference or advice ?

    Thanks a lot !
     
  2. ilya_ca

    ilya_ca

    Joined:
    Nov 19, 2011
    Posts:
    274
    Nope, that's not a shader. You should modify the mesh. Try looking at MegaFiers package on the Asset Store, it might have something like this.
     
  3. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    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.
     
  4. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Thanks a lot for your answers !

    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.
     
  5. akabane

    akabane

    Joined:
    Apr 26, 2007
    Posts:
    143
    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
     
  6. cod

    cod

    Joined:
    Nov 26, 2011
    Posts:
    267
    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
     
  7. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    I think "occlusion" is the wrong word then. A better word might be "cutaway".
    If you only require cutaways with spheres, that's not too difficult, see the exercise here:
    http://en.wikibooks.org/wiki/Cg_Programming/Unity/Cutaways#Better_Cutaways

    For arbitrary meshes, you would probably have to apply similar techniques as for CSG.
     
  8. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    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.
     
  9. ilya_ca

    ilya_ca

    Joined:
    Nov 19, 2011
    Posts:
    274
  10. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    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 ?

    $cullingbox.jpg

    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.
     
  11. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    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).
     
  12. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    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.
     
  13. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    Good luck with that. :)
     
  14. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    What about using the renderqueue system ?
     
unityunity