I currently working on an open project to create a station indoor map, but having difficulties to limit the pan when using orbitControls,
I tried to limit the control/camera position x/y/z but when i rotate the orbit all those coordinates suddenly changed, i know why this is happened but since i still confused.
It’s best to modify OrbitControls for this use case. So copy the class and define two new variables that you can use to control the minimum and maximum pan movement at the top of the file.
var minPan = new THREE.Vector3( - 2, - 2, - 2 );
var maxPan = new THREE.Vector3( 2, 2, 2 );
In the update() method, add the following line after panOffset was added to the target of OrbitControls.
Panning means to offset the control’s target vector and thus the camera. If you restrict the target vector, you also restrict the camera’s final position.
The problem was that camera position would drift as a consequence of clamping the padding. This is corrected by moving the camera according to the modification done by the clamping.
Good! For performance, you should create minPan and maxPan outside the update function scope, to avoid recreating them on every update. (Alone it has a negligible effect on performance, but as a habit, it will matter.) Also, defining them outside opens for modifying them (depending on how you do it. Update: Not done below.).
Something like:
export function createLimitPan({ camera, controls, THREE }) {
const v = new THREE.Vector3();
const minPan = new THREE.Vector3();
const maxPan = new THREE.Vector3();
return ({
maxX = Infinity,
minX = -Infinity,
maxZ = Infinity,
minZ = -Infinity
}) => {
minPan.set(minX, -Infinity, minZ)
maxPan.set(maxX, Infinity, maxZ)
v.copy(controls.target)
controls.target.clamp(minPan, maxPan)
v.sub(controls.target)
camera.position.sub(v)
}
Hm, why only X,Z? Pan behavior depends on the OrbitControls setting screenSpacePanning. I think it is safer to include Y too.
Hi, this is exactly what i was looking for, thanks !! I just have a little problem, when i move/pan too fast there is a shift between the real position of my object and what i see, and when i decide to move again, the object just teleport to the real location… Do you have any idea where it could come from ?