Skip to content

Commit

Permalink
EnvironmentControls: add plane fallback (#516)
Browse files Browse the repository at this point in the history
* Use environment controls in fade example

* Update raycasting

* Add fallback plane

* Add comment
  • Loading branch information
gkjohnson authored Mar 25, 2024
1 parent f08f0bc commit 8a30ed0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
19 changes: 7 additions & 12 deletions example/fadingTiles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import {
FadeTilesRenderer,
} from './src/FadeTilesRenderer.js';
import {
Scene,
DirectionalLight,
Expand All @@ -9,7 +6,8 @@ import {
PerspectiveCamera,
Group,
} from 'three';
import { FlyOrbitControls } from './src/controls/FlyOrbitControls.js';
import { FadeTilesRenderer, } from './src/FadeTilesRenderer.js';
import { EnvironmentControls } from '../src/index.js';
import { GUI } from 'three/examples/jsm/libs/lil-gui.module.min.js';

let camera, controls, scene, renderer;
Expand Down Expand Up @@ -43,17 +41,13 @@ function init() {
document.body.appendChild( renderer.domElement );
renderer.domElement.tabIndex = 1;

camera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 4000 );
camera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.25, 4000 );
camera.position.set( 20, 10, 20 );

// controls
controls = new FlyOrbitControls( camera, renderer.domElement );
controls.screenSpacePanning = false;
controls.minDistance = 1;
controls.maxDistance = 2000;
controls.maxPolarAngle = Math.PI / 2;
controls.baseSpeed = 0.1;
controls.fastSpeed = 0.2;
controls = new EnvironmentControls( scene, camera, renderer.domElement );
controls.minZoomDistance = 2;
controls.cameraRadius = 1;

// lights
const dirLight = new DirectionalLight( 0xffffff );
Expand Down Expand Up @@ -123,6 +117,7 @@ function render() {

requestAnimationFrame( render );

controls.update();
camera.updateMatrixWorld();

groundTiles.errorTarget = params.errorTarget;
Expand Down
48 changes: 39 additions & 9 deletions src/three/controls/EnvironmentControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ export class EnvironmentControls extends EventDispatcher {

this.up = new Vector3( 0, 1, 0 );

this.fallbackPlane = new Plane( new Vector3( 0, 1, 0 ), 0 );
this.useFallbackPlane = true;

this._detachCallback = null;
this._upInitialized = false;

Expand Down Expand Up @@ -180,7 +183,6 @@ export class EnvironmentControls extends EventDispatcher {
camera,
raycaster,
domElement,
scene,
up,
pivotMesh,
pointerTracker,
Expand Down Expand Up @@ -215,7 +217,7 @@ export class EnvironmentControls extends EventDispatcher {

// find the hit point
raycaster.setFromCamera( _pointer, camera );
const hit = raycaster.intersectObject( scene )[ 0 ] || null;
const hit = this._raycast( raycaster );
if ( hit ) {

// if two fingers, right click, or shift click are being used then we trigger
Expand Down Expand Up @@ -636,10 +638,9 @@ export class EnvironmentControls extends EventDispatcher {
const finalZoomDirection = _vec.copy( zoomDirection );

// always update the zoom target point in case the tiles are changing
let dist = Infinity;
if ( this._updateZoomPoint() ) {

dist = zoomPoint.distanceTo( camera.position );
const dist = zoomPoint.distanceTo( camera.position );

// scale the distance based on how far there is to move
if ( scale < 0 ) {
Expand All @@ -665,7 +666,7 @@ export class EnvironmentControls extends EventDispatcher {
const hit = this._getPointBelowCamera();
if ( hit ) {

dist = hit.distance;
const dist = hit.distance;
finalZoomDirection.set( 0, 0, - 1 ).transformDirection( camera.matrixWorld );
camera.position.addScaledVector( finalZoomDirection, scale * dist * 0.01 );
camera.updateMatrixWorld();
Expand All @@ -684,7 +685,6 @@ export class EnvironmentControls extends EventDispatcher {
zoomDirectionSet,
zoomDirection,
raycaster,
scene,
zoomPoint,
} = this;

Expand All @@ -697,7 +697,7 @@ export class EnvironmentControls extends EventDispatcher {
raycaster.ray.origin.copy( camera.position );
raycaster.ray.direction.copy( zoomDirection );

const hit = raycaster.intersectObject( scene )[ 0 ] || null;
const hit = this._raycast( raycaster );
if ( hit ) {

zoomPoint.copy( hit.point );
Expand All @@ -713,11 +713,11 @@ export class EnvironmentControls extends EventDispatcher {
// returns the point below the camera
_getPointBelowCamera() {

const { camera, raycaster, scene, up } = this;
const { camera, raycaster, up } = this;
raycaster.ray.direction.copy( up ).multiplyScalar( - 1 );
raycaster.ray.origin.copy( camera.position ).addScaledVector( up, 1e5 );

const hit = raycaster.intersectObject( scene )[ 0 ] || null;
const hit = this._raycast( raycaster );
if ( hit ) {

hit.distance -= 1e5;
Expand Down Expand Up @@ -907,4 +907,34 @@ export class EnvironmentControls extends EventDispatcher {

}

_raycast( raycaster ) {

const { scene, useFallbackPlane, fallbackPlane } = this;
const result = raycaster.intersectObject( scene )[ 0 ] || null;
if ( result ) {

return result;

} else if ( useFallbackPlane ) {

// if we don't hit any geometry then try to intersect the fallback
// plane so the camera can still be manipulated
const plane = fallbackPlane;
if ( raycaster.ray.intersectPlane( plane, _vec ) ) {

const planeHit = {
point: _vec.clone(),
distance: raycaster.ray.origin.distanceTo( _vec ),
};

return planeHit;

}

}

return null;

}

}
1 change: 1 addition & 0 deletions src/three/controls/GlobeControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class GlobeControls extends EnvironmentControls {
this._tilesRenderer = null;
this._dragMode = 0;
this._rotationMode = 0;
this.useFallbackPlane = false;

this.setTilesRenderer( tilesRenderer );

Expand Down

0 comments on commit 8a30ed0

Please sign in to comment.