Search Unity

Subtractive Boolean Rendering

Discussion in 'Shaders' started by Cactus_on_Fire, Dec 4, 2015.

  1. Cactus_on_Fire

    Cactus_on_Fire

    Joined:
    Aug 12, 2014
    Posts:
    675
    Hello.

    I'm looking for a way to render the meshes where a mesh-defined or geometrically-defined area will "cut out" the mesh when it's rendered so the insides are visible. It doesn't necessarily have to fill the gaps pf the polygons but doing so will be fine as well. Here is an example of what I mean :

     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
  3. StevenGerrard

    StevenGerrard

    Joined:
    Jun 1, 2015
    Posts:
    97
    Here is a step by step progress
    => The sphere material should be "Cull Front" and "Write Depth",
    => "Cull Front" make you only see inside of sphere surface.
    => "Write Depth" make sphere block the cube.
    => The box material should be "Enable Depth Test".
    => "Enable Depth Test" make your cube blocked by sphere.
    => Gaps problem can be solved by something called "soft particle by depth buffer". Try google it.
    => Use stencil also ok however stencil buffer is terrible supported on mobile platform. Take care of it.
     
  4. Cactus_on_Fire

    Cactus_on_Fire

    Joined:
    Aug 12, 2014
    Posts:
    675
    I tried stencils, but they only mask the object and not actually cut off their Zdepths :(


    I'm not sure how I can enable depth test.
     
  5. Cactus_on_Fire

    Cactus_on_Fire

    Joined:
    Aug 12, 2014
    Posts:
    675
    Hey steven. Did you have a solution for closing gaps of meshes when they are cut off ?
     
  6. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    I'd say, pass 1 (after other geometry to have correct contents of the stencil buffer):
    - Render the cube
    - Write 1 to stencil

    Pass 2 (after pass 1):
    - Render the sphere
    - Reverse culling, Cull Front
    - Only if stencil is 1 to only intersect into cube
    - ZTest GEqual

    Stencil is not needed if the sphere should be rendered into everything.
     
  7. Cactus_on_Fire

    Cactus_on_Fire

    Joined:
    Aug 12, 2014
    Posts:
    675
    How can I implement something like that with shader code ? I'm a noob when it comes to writing scripts for shaders.