I’m trying to change OrbitControls target to a new intersection point without altering the current view (same distance and orientation). Even though I compute the offset vector from old target to camera, set new target, reposition camera using the same offset, disable controls/damping temporarily and call update(), the camera still shifts visibly. What internal state or spherical coordinates am I missing that causes this jump?
export class OrbitControl {
private renderer: THREE.WebGLRenderer;
public controls: OrbitControls;
private camera: THREE.PerspectiveCamera;
*constructor*(*camera*: THREE.PerspectiveCamera, *renderer*: THREE.WebGLRenderer) {
this.camera = *camera*;
this.renderer = *renderer*;
this.controls = new OrbitControls(*camera*, *renderer*.domElement);
this.controlConfig();
private controlConfig() {
this.controls.enabled = true;
this.controls.zoomToCursor = true;
this.controls.zoomSpeed = 4;
this.controls.enablePan = true;
}
public focusNodecontrol(intersects: THREE.Intersection[]) {
if (!*intersects*?.length) return;
*const* intersection = *intersects*\[0\];
*const* point = intersection.point.clone();
// 1. Salva stato attuale
*const* oldTarget = this.controls.target.clone();
*const* camera = this.controls.object as THREE.PerspectiveCamera;
*const* oldPos = camera.position.clone();
*const* offset = oldPos.clone().sub(oldTarget);
// 2. Disabilita temporaneamente i controlli e damping
*const* wasEnabled = this.controls.enabled;
*const* hadDamping = this.controls.enableDamping;
this.controls.enabled = false;
this.controls.enableDamping = false;
// 3. Applica nuovo target e posizione
this.controls.target.copy(point);
camera.position.copy(point.clone().add(offset));
// 4. Forza aggiornamento immediato
this.controls.update();
// 5. Ripristina stato precedente
this.controls.enabled = wasEnabled;
this.controls.enableDamping = hadDamping;
// 6. (Opzionale) Svuota eventuali stati residui del mouse
// Simula un evento "mouseup" per evitare che OrbitControls pensi
// che stiamo ancora trascinando.
window.dispatchEvent(new *Event*('mouseup'));
}
}