Search Unity

Resolved How can I only render world only within given boundaries?

Discussion in 'General Graphics' started by FOXAcemond, Oct 12, 2020.

  1. FOXAcemond

    FOXAcemond

    Joined:
    Jan 23, 2015
    Posts:
    99
    Hello,

    I would like to achieve a similar effect than the one done in "Unrailed!", see video at given timestamp:


    The idea is to divide the space in a cylinder from the space around it.
    To render something in that cylinder and something else outside and to shrink it/expand it dynamically.

    I asked "Unrailed!" dev team, but they don't use Unity so we didn't go into details...

    I didn't find a way to achieve that in Unity yet. Any idea?
     
  2. FOXAcemond

    FOXAcemond

    Joined:
    Jan 23, 2015
    Posts:
    99
    Please note that meshed are "cut" in 3D by the cylinder!
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    MRC-UA likes this.
  4. FOXAcemond

    FOXAcemond

    Joined:
    Jan 23, 2015
    Posts:
    99
    I knew this would come down to writing shaders! Man I really need to learn that sh*t.

    Thanks a lot for the useful link, this is exactly what I was looking for!

    This will actually allow me to start somewhere with the shaders, good stuff.
     
  5. FOXAcemond

    FOXAcemond

    Joined:
    Jan 23, 2015
    Posts:
    99
    So I'm looking more into that today. Does that mean every mesh I'm going to try to cut will have to be set to transparent?
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    Not transparent, no. The
    clip()
    function works on opaque objects, it just skips rendering those pixels.
     
  7. FOXAcemond

    FOXAcemond

    Joined:
    Jan 23, 2015
    Posts:
    99
    I've just read one of your posts in another thread:
    I was wondering why it was so hardcore to start writing lit shaders with my HDRP project...
    So I guess since I'm expected to use shader graphs, this is going to be impossible to implement. Right?
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    Nah, pretty simple to do in shader graph. This is one of the kind of things Shader Graph excels at.

    Position Vector3 property, radius Vector1 property (or a single Vector4 for both). A position node set to world. A sphere mask node. Plug the world position, position property, and radius into the sphere mask, and direct the output into the master alpha. Then use a Vector1 to set the Alpha Threshold to 0.5 and you're done.

    To flip what part of the circle gets hidden, use a one minus node on the sphere mask output. You can use a bool property and a branch to select between the two versions.
     
    Last edited: Oct 14, 2020
  9. FOXAcemond

    FOXAcemond

    Joined:
    Jan 23, 2015
    Posts:
    99
    Thanks I'll try that.

    The world position property has to be a fixed value right? This cannot be a moving gameobject position, right?

    I guess this is the case with any shader anyway.
     
  10. FOXAcemond

    FOXAcemond

    Joined:
    Jan 23, 2015
    Posts:
    99
    No! I can access shader's properties using setFloat in the script!

    OK I'll give it a shot.
     
  11. FOXAcemond

    FOXAcemond

    Joined:
    Jan 23, 2015
    Posts:
    99
    Wrote the base shader you described today. It works in editor and gives a pretty neat result. Thanks!

    Now I need to rethink everything to use that :D