Is it possible to make a clip plane not affect the skybox?

Example I quickly wrote up in the playground

https://www.babylonjs-playground.com/#UU7RQ#550

I’d like to clip off the cube, not the sky

The only idea I have is to remove the clipPlane before binding the mesh, and them adding it again after rendering it (the skybox) . something like this:

https://www.babylonjs-playground.com/#UU7RQ#551

1 Like

This seems to be working for me in my solution, thanks. I knew there were some lifecycle events I could hook, I just wasn’t sure how they applied. Your reply was a Great help.

Would you know of how clipplanes in general affect performance? And how this solution “may” affect performance. Pure conjecture is welcome.

A clipplane is:

  • fClipDistance = dot(worldPos, vClipPlane); in the vertex shader
  • if (fClipDistance > 0.0) discard; in the fragment shader

So really not something to worry about.

Regarding adding an observer on onBeforeBindObservable / onAfterBindObservable, as long as you don’t do it for tens of thousand meshes (and even then…), it won’t matter either.

2 Likes

One last question, from that it would seem that performance isn’t gained by cliping portions of the screen. If a mesh is part of a scene it will still impact the performance areas in other ways. (most certainly cpu just for processing, but the gpu?)

A more direct question - If a mesh’s material was a complex fragment shader and was impacting performance clipping it off entirely would not improve performance?

Well, if the fragment shader is complex, there is a gain in performance because the discard I showed above is done very early in the fragment shader, so all the code coming after won’t be executed.

So:

Yes, performance would be improved in this case but not as much as if the mesh would have been clipped entirely from the beginning (meaning, not sent to the GPU or entirely clipped after the vertex shader).