Skip to content

Commit

Permalink
Fix ortho zoom (#855)
Browse files Browse the repository at this point in the history
  • Loading branch information
gkjohnson authored Dec 7, 2024
1 parent 8a5db8e commit b812541
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions src/three/controls/EnvironmentControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,17 +809,39 @@ export class EnvironmentControls extends EventDispatcher {
this._updateZoomDirection();

// zoom straight into the globe if we haven't hit anything
if ( this.zoomPointSet || this._updateZoomPoint() ) {
const zoomIntoPoint = this.zoomPointSet || this._updateZoomPoint();

// get the mouse position before zoom
_mouseBefore.unproject( camera );

// zoom the camera
const normalizedDelta = Math.pow( 0.95, Math.abs( scale * 0.05 ) );
let scaleFactor = scale > 0 ? 1 / Math.abs( normalizedDelta ) : normalizedDelta;
scaleFactor *= zoomSpeed;

if ( scaleFactor > 1 ) {

if ( maxZoom < camera.zoom * scaleFactor ) {

// get the mouse position before zoom
_mouseBefore.unproject( camera );
scaleFactor = 1;

// zoom the camera
const normalizedDelta = Math.pow( 0.95, Math.abs( scale * 0.05 ) );
const scaleFactor = scale > 0 ? 1 / Math.abs( normalizedDelta ) : normalizedDelta;
}

} else {

camera.zoom = Math.max( minZoom, Math.min( maxZoom, camera.zoom * scaleFactor * zoomSpeed ) );
camera.updateProjectionMatrix();
if ( minZoom > camera.zoom * scaleFactor ) {

scaleFactor = 1;

}

}

camera.zoom *= scaleFactor;
camera.updateProjectionMatrix();

// adjust the surface point to be in the same position if the globe is hovered over
if ( zoomIntoPoint ) {

// get the mouse position after zoom
mouseToCoords( _pointer.x, _pointer.y, domElement, _mouseAfter );
Expand All @@ -829,15 +851,11 @@ export class EnvironmentControls extends EventDispatcher {
camera.position.sub( _mouseAfter ).add( _mouseBefore );
camera.updateMatrixWorld();

} else {

const normalizedDelta = Math.pow( 0.95, Math.abs( scale * 0.05 ) );
const scaleFactor = scale > 0 ? 1 / Math.abs( normalizedDelta ) : normalizedDelta;
camera.zoom = Math.max( minZoom, Math.min( maxZoom, camera.zoom * scaleFactor * zoomSpeed ) );
camera.updateProjectionMatrix();

}

// TODO: the user can currently zoom into the sky and hide the globe.
// Consider forcing the camera to zoom into the closest horizon point

} else {

// initialize the zoom direction
Expand Down

0 comments on commit b812541

Please sign in to comment.