Skip to content

Commit

Permalink
Fix #849
Browse files Browse the repository at this point in the history
  • Loading branch information
gkjohnson committed Dec 22, 2024
1 parent 8e8c4fb commit 3288c9d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
20 changes: 20 additions & 0 deletions PLUGINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ Updates the clip planes and position of the given camera so the globe is encapsu

_extends [EnvironmentControls](#environmentcontrols)_

### .nearMargin

```js
nearMargin = 0.25 : number
```

The margin around the globe to include when positioning the camera near plane as a percentage of the max radius of the tile set ellipsoid. Useful for ensuring visualizations and other models are visible around the globe.

Default is 25%.

### farMargin

```js
farMargin = 0 : number
```

The margin around the globe to include when positioning the camera far plane as a percentage of the max radius of the tile set ellipsoid. Useful for ensuring visualizations and other models are visible around the globe.

Default is 0%.

### .constructor

```js
Expand Down
18 changes: 10 additions & 8 deletions src/three/controls/GlobeControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export class GlobeControls extends EnvironmentControls {
this._dragMode = 0;
this._rotationMode = 0;
this.maxZoom = 0.01;
this.nearMargin = 0.25;
this.farMargin = 0;
this.useFallbackPlane = false;
this.reorientOnDrag = false;

Expand Down Expand Up @@ -200,7 +202,8 @@ export class GlobeControls extends EnvironmentControls {

super.adjustCamera( camera );

const { tilesGroup, ellipsoid } = this;
const { tilesGroup, ellipsoid, nearMargin, farMargin } = this;
const maxRadius = Math.max( ...ellipsoid.radius );
if ( camera.isPerspectiveCamera ) {

// adjust the clip planes
Expand All @@ -211,11 +214,10 @@ export class GlobeControls extends EnvironmentControls {
// update the projection matrix
// interpolate from the 25% radius margin around the globe down to the surface
// so we can avoid z fighting when near value is too far at a high altitude
const largestDistance = Math.max( ...ellipsoid.radius );
const margin = 0.25 * largestDistance;
const alpha = MathUtils.clamp( ( distanceToCenter - largestDistance ) / margin, 0, 1 );
const margin = nearMargin * maxRadius;
const alpha = MathUtils.clamp( ( distanceToCenter - maxRadius ) / margin, 0, 1 );
const minNear = MathUtils.lerp( 1, 1000, alpha );
camera.near = Math.max( minNear, distanceToCenter - largestDistance - margin );
camera.near = Math.max( minNear, distanceToCenter - maxRadius - margin );

// update the far plane to the horizon distance
const invMatrix = _invMatrix.copy( tilesGroup.matrixWorld ).invert();
Expand All @@ -228,7 +230,7 @@ export class GlobeControls extends EnvironmentControls {
const horizonDistance = ellipsoid.calculateHorizonDistance( _latLon.lat, elevation );

// extend the horizon distance by 2.5 to handle cases where geometry extends above the horizon
camera.far = horizonDistance * 2.5 + 0.1;
camera.far = horizonDistance * 2.5 + 0.1 + maxRadius * farMargin;
camera.updateProjectionMatrix();

} else {
Expand All @@ -240,8 +242,8 @@ export class GlobeControls extends EnvironmentControls {
_vec.setFromMatrixPosition( tilesGroup.matrixWorld ).applyMatrix4( _invMatrix );

const distanceToCenter = - _vec.z;
camera.near = distanceToCenter - Math.max( ...ellipsoid.radius ) * 1.1;
camera.far = distanceToCenter + 0.1;
camera.near = distanceToCenter - maxRadius * ( 1 + nearMargin );
camera.far = distanceToCenter + 0.1 + maxRadius * farMargin;

// adjust the position of the ortho camera such that the near value is 0
camera.position.addScaledVector( _forward, camera.near );
Expand Down

0 comments on commit 3288c9d

Please sign in to comment.