Search Unity

  1. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  2. Unity 2017.1 is now released.
    Dismiss Notice
  3. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  4. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice

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:
    378
    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 :

    [​IMG]
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    3,240
  3. StevenGerrard

    StevenGerrard

    Joined:
    Jun 1, 2015
    Posts:
    92
    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:
    378
    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:
    378
    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:
    952
    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:
    378
    How can I implement something like that with shader code ? I'm a noob when it comes to writing scripts for shaders.