How to render only the part of object inside the cube

first, sorry about my poor english…

i have lots of invisible 3D objects in the space, and i have a transparent cube.

i can move this cube, and I want 3D objects appear visible when it enters inside the cube.
If only half of the object enters the cube, then only half of the object should be visible.

I tried to do this with the shader(depth mask, stencil), but it doesn’t work because it also show objects which are behind the cube.(not inside the cube)
Also, visible part keep changing if i rotate the camera.

if an object is across over the cube’s plane, it has to be shown as it is cut.

i appreciate any replies, please help me. thank you

That’s actually quite a complex problem. You’ll need to clip the objects against 6 clipping planes for the 6 sides of the cube. To get you started, here’s someone who’s made a shader that does it with ONE clipping plane: