Search Unity

Creating a hole in an Object using another object in 3D showing interior

Discussion in 'Shaders' started by PylonCrow, Nov 11, 2019.

  1. PylonCrow

    PylonCrow

    Joined:
    Mar 29, 2017
    Posts:
    9
    This is the effect im looking to recreate: upload_2019-11-11_14-37-18.png

    How would one achieve this? The video i pulled this from does this using Ray Marching, however thats reliant on having a DistanceToObject function. If my object is terrain of some sort, this wouldn't work because theres no DistanceToTerrain function you could create, similarly for complex shapes as well.

    I know this is achievable with stencil buffer but that creates a very obvious hole that you'd have to add additional meshs to to look complete, not a solid hole.

    upload_2019-11-11_14-43-5.png

    Any ideas?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    There's no solution to what you're looking to do without using distance fields. Stencils are an inherently 2D thing. You can kind of fake the appearance of volume in some very controlled and contrived setups, but to match the above example is 100% distance fields or nothing. Even ignoring the 2D limitation, Unity's shadows aren't always affected by stencils properly as Unity ignores the render queue order when rendering depth textures like the camera depth texture and shadow maps, so you'll be at the mercy of whatever random order Unity decides to render objects there.

    Now you can generate a 3D texture that holds the SDF of any object you want, mesh or otherwise, to support complex shapes. Unreal makes significant use of these, but there's nothing built into Unity to do this. There are free projects around with implementations though.

    Like this one:
    https://github.com/xraxra/SDFr

    For simple shapes analytical models are far more efficient than an SDF. A sphere for example is just the distance to a point.
     
  3. MarkusKer

    MarkusKer

    Joined:
    Jun 7, 2017
    Posts:
    15
    Hi, from which video did you pull this?