Clip some objects nearer to camera, others farther away

Hey there,

so I need some of the objects in my scene to clip at a specific distance to the camera, while others could basically reach much closer to the camera.

(A) First thing I found was to Frustum culling and preventing it by setting

GetComponent<MeshFilter>().sharedMesh.bounds = // something large

but this isn’t really what I need, my object is not being culled completely, it just has parts that reach inside the nearClipping plane of the camera.

(B) the objects that should clip at nearClipping = 1 are more at the lower edge of the screen. The objects that should not be clipped are more higher up. So I thought about setting my “camera.nearClipPlane = 0” and have a kind of occlusion cube at the bottom of the camera, that occluded objects that are closer than 1 from the camera. The problem here is that all depth buffer based occlusion shaders I know, occlude everything that is behind the object from the camera perspective. What I would need is to only occlude what is nearer to the camera.

(C) third approach I found was to create two layers for the objects, with two camera at exactly the same position, but different camera.nearClipPlane values. But my scene already has a bunch of layers used for other things and that would make everything more complex. Also I’m afraid that rendering on two camera would have a performance impact.

How would you approach this? Is there any obvious trick I am missing?

You can set additional clip and culling planes dynamically per object if you use a custom shader.
All you have to do is to set the SV_ClipDistance and SV_CullDistance semantics in the vertex shader appropriately.

1 Like

Yeah, I think I will have to do that. Do you happen to know a good sample/template shader that works just like URP/Lit ? One that has albedo_smoothness + metalness+normal already working, so I don’t have to set it up from scratch again? Or is everyone just rebuilding that shader by themselves when they start a slightly modified one?

You can download the built-in shaders and modify them: Download Archive

1 Like

Thank you, I managed to create a transition effect using a shader that makes vertices transparent when crossing a certain position. I also added some effect into the emission channel, to make the transition more impactful:

1 Like