Prevent camera from zooming through a mesh

Hi all,

Essentially, I’d like to prevent a camera from clipping through an object. I noticed that when a .gltf file is uploaded to the sandbox, the camera enables you to zoom extremely close to the surface without actually going through it. I’d just like to replicate this behavior.

Here is a pg if relevant: Babylon.js Playground

Sorry if this question is worded poorly. I think it might be as I’ve been searching for hours and haven’t found any similar topics. Thanks!!!

The camera in the sandbox uses “framing behavior” to prevent it from getting too close. You can also adjust the radius limit to adjust how close and far away you can get from the object. I created a video on a lot of these mechanics here: https://www.youtube.com/watch?v=VSX8Y026MkQ Hope that helps!

Whoops, forgot to link the documentation before I hit submit: Camera Behaviors | Babylon.js Documentation

2 Likes

Great, thank you for the link and suggestions.

Quick question regarding this- is there a way I could quickly find the minimum radius necessary to avoid clipping with my object?

If you use the framing behavior you can have it set based on the bounds of the object.

  • BABYLON.FramingBehavior.FitFrustumSidesMode : The camera is not allowed to zoom closer to the mesh than the point at which the adjusted bounding sphere touches the frustum sides

That’s one way to do it, otherwise, you can just play around with the lower radius limit setting it to something like 5.

If you have something more dynamic where you may have a variety of objects of similar size you can also create a box that will act as the “bounds” and use the framing behavior to zoom in on the box. Make the box invisible and make sure the imported mesh is in the center of the box. Now when the camera zoom in, it’ll stop when it reaches the bounds of the box.

1 Like

it’s handled automatically by the framing behaviour - here is the pertinent sandbox code:
Babylon.js/renderingZone.tsx at master · BabylonJS/Babylon.js (github.com)

3 Likes

Hi @bflock just checking in, was your question answered? :smiley: