Do not let camera go through objects on Zoom, how?

Given a model, how can I make sure that on zoom or rotation camera never goes inside the model and stays certain distance out of it? I was checking controls examples but cant figure out how to go about it.

To clarify: model is a large uneven 2D terrain with mountains, valleys and so on, so if a zoom target is behind a mountain- camera goes through the mountain. If I rotate with controls, I can go inside the mountain as well. Setting controls min distance won’t help in this case.

1 Like

One way to do it,
Using the raycaster, create a ray from the controls target to the camera position. If there is an intersect between, then reposition the camera at the intersect point. That way it is never behind the object that was in front of it.
This is a demo. https://sbcode.net/threejs/raycaster2/
Zoom out, then rotate past one of the objects that is close to the crosshair. The crosshair is always in view.

1 Like