How can I only render world only within given boundaries?

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?

Please note that meshed are “cut” in 3D by the cylinder!

You need to use a custom shader that hides or shows the fragment based on distance from a point.

This article talks about something similar.

1 Like

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.

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?

Not transparent, no. The clip() function works on opaque objects, it just skips rendering those pixels.

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?

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.

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.

No! I can access shader’s properties using setFloat in the script!

OK I’ll give it a shot.

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