first commit

This commit is contained in:
rafaeldpsilva
2025-12-10 12:32:12 +00:00
commit adbbf6bf50
3442 changed files with 2725681 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,410 @@
import {
Controls,
Matrix4,
Plane,
Raycaster,
Vector2,
Vector3,
MOUSE,
TOUCH
} from 'three';
const _plane = new Plane();
const _pointer = new Vector2();
const _offset = new Vector3();
const _diff = new Vector2();
const _previousPointer = new Vector2();
const _intersection = new Vector3();
const _worldPosition = new Vector3();
const _inverseMatrix = new Matrix4();
const _up = new Vector3();
const _right = new Vector3();
let _selected = null, _hovered = null;
const _intersections = [];
const STATE = {
NONE: - 1,
PAN: 0,
ROTATE: 1
};
class DragControls extends Controls {
constructor( objects, camera, domElement = null ) {
super( camera, domElement );
this.objects = objects;
this.recursive = true;
this.transformGroup = false;
this.rotateSpeed = 1;
this.raycaster = new Raycaster();
// interaction
this.mouseButtons = { LEFT: MOUSE.PAN, MIDDLE: MOUSE.PAN, RIGHT: MOUSE.ROTATE };
this.touches = { ONE: TOUCH.PAN };
// event listeners
this._onPointerMove = onPointerMove.bind( this );
this._onPointerDown = onPointerDown.bind( this );
this._onPointerCancel = onPointerCancel.bind( this );
this._onContextMenu = onContextMenu.bind( this );
//
if ( domElement !== null ) {
this.connect();
}
}
connect() {
this.domElement.addEventListener( 'pointermove', this._onPointerMove );
this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
this.domElement.addEventListener( 'pointerup', this._onPointerCancel );
this.domElement.addEventListener( 'pointerleave', this._onPointerCancel );
this.domElement.addEventListener( 'contextmenu', this._onContextMenu );
this.domElement.style.touchAction = 'none'; // disable touch scroll
}
disconnect() {
this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
this.domElement.removeEventListener( 'pointerup', this._onPointerCancel );
this.domElement.removeEventListener( 'pointerleave', this._onPointerCancel );
this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
this.domElement.style.touchAction = 'auto';
this.domElement.style.cursor = '';
}
dispose() {
this.disconnect();
}
_updatePointer( event ) {
const rect = this.domElement.getBoundingClientRect();
_pointer.x = ( event.clientX - rect.left ) / rect.width * 2 - 1;
_pointer.y = - ( event.clientY - rect.top ) / rect.height * 2 + 1;
}
_updateState( event ) {
// determine action
let action;
if ( event.pointerType === 'touch' ) {
action = this.touches.ONE;
} else {
switch ( event.button ) {
case 0:
action = this.mouseButtons.LEFT;
break;
case 1:
action = this.mouseButtons.MIDDLE;
break;
case 2:
action = this.mouseButtons.RIGHT;
break;
default:
action = null;
}
}
// determine state
switch ( action ) {
case MOUSE.PAN:
case TOUCH.PAN:
this.state = STATE.PAN;
break;
case MOUSE.ROTATE:
case TOUCH.ROTATE:
this.state = STATE.ROTATE;
break;
default:
this.state = STATE.NONE;
}
}
getRaycaster() {
console.warn( 'THREE.DragControls: getRaycaster() has been deprecated. Use controls.raycaster instead.' ); // @deprecated r169
return this.raycaster;
}
setObjects( objects ) {
console.warn( 'THREE.DragControls: setObjects() has been deprecated. Use controls.objects instead.' ); // @deprecated r169
this.objects = objects;
}
getObjects() {
console.warn( 'THREE.DragControls: getObjects() has been deprecated. Use controls.objects instead.' ); // @deprecated r169
return this.objects;
}
activate() {
console.warn( 'THREE.DragControls: activate() has been renamed to connect().' ); // @deprecated r169
this.connect();
}
deactivate() {
console.warn( 'THREE.DragControls: deactivate() has been renamed to disconnect().' ); // @deprecated r169
this.disconnect();
}
set mode( value ) {
console.warn( 'THREE.DragControls: The .mode property has been removed. Define the type of transformation via the .mouseButtons or .touches properties.' ); // @deprecated r169
}
get mode() {
console.warn( 'THREE.DragControls: The .mode property has been removed. Define the type of transformation via the .mouseButtons or .touches properties.' ); // @deprecated r169
}
}
function onPointerMove( event ) {
const camera = this.object;
const domElement = this.domElement;
const raycaster = this.raycaster;
if ( this.enabled === false ) return;
this._updatePointer( event );
raycaster.setFromCamera( _pointer, camera );
if ( _selected ) {
if ( this.state === STATE.PAN ) {
if ( raycaster.ray.intersectPlane( _plane, _intersection ) ) {
_selected.position.copy( _intersection.sub( _offset ).applyMatrix4( _inverseMatrix ) );
}
} else if ( this.state === STATE.ROTATE ) {
_diff.subVectors( _pointer, _previousPointer ).multiplyScalar( this.rotateSpeed );
_selected.rotateOnWorldAxis( _up, _diff.x );
_selected.rotateOnWorldAxis( _right.normalize(), - _diff.y );
}
this.dispatchEvent( { type: 'drag', object: _selected } );
_previousPointer.copy( _pointer );
} else {
// hover support
if ( event.pointerType === 'mouse' || event.pointerType === 'pen' ) {
_intersections.length = 0;
raycaster.setFromCamera( _pointer, camera );
raycaster.intersectObjects( this.objects, this.recursive, _intersections );
if ( _intersections.length > 0 ) {
const object = _intersections[ 0 ].object;
_plane.setFromNormalAndCoplanarPoint( camera.getWorldDirection( _plane.normal ), _worldPosition.setFromMatrixPosition( object.matrixWorld ) );
if ( _hovered !== object && _hovered !== null ) {
this.dispatchEvent( { type: 'hoveroff', object: _hovered } );
domElement.style.cursor = 'auto';
_hovered = null;
}
if ( _hovered !== object ) {
this.dispatchEvent( { type: 'hoveron', object: object } );
domElement.style.cursor = 'pointer';
_hovered = object;
}
} else {
if ( _hovered !== null ) {
this.dispatchEvent( { type: 'hoveroff', object: _hovered } );
domElement.style.cursor = 'auto';
_hovered = null;
}
}
}
}
_previousPointer.copy( _pointer );
}
function onPointerDown( event ) {
const camera = this.object;
const domElement = this.domElement;
const raycaster = this.raycaster;
if ( this.enabled === false ) return;
this._updatePointer( event );
this._updateState( event );
_intersections.length = 0;
raycaster.setFromCamera( _pointer, camera );
raycaster.intersectObjects( this.objects, this.recursive, _intersections );
if ( _intersections.length > 0 ) {
if ( this.transformGroup === true ) {
// look for the outermost group in the object's upper hierarchy
_selected = findGroup( _intersections[ 0 ].object );
} else {
_selected = _intersections[ 0 ].object;
}
_plane.setFromNormalAndCoplanarPoint( camera.getWorldDirection( _plane.normal ), _worldPosition.setFromMatrixPosition( _selected.matrixWorld ) );
if ( raycaster.ray.intersectPlane( _plane, _intersection ) ) {
if ( this.state === STATE.PAN ) {
_inverseMatrix.copy( _selected.parent.matrixWorld ).invert();
_offset.copy( _intersection ).sub( _worldPosition.setFromMatrixPosition( _selected.matrixWorld ) );
} else if ( this.state === STATE.ROTATE ) {
// the controls only support Y+ up
_up.set( 0, 1, 0 ).applyQuaternion( camera.quaternion ).normalize();
_right.set( 1, 0, 0 ).applyQuaternion( camera.quaternion ).normalize();
}
}
domElement.style.cursor = 'move';
this.dispatchEvent( { type: 'dragstart', object: _selected } );
}
_previousPointer.copy( _pointer );
}
function onPointerCancel() {
if ( this.enabled === false ) return;
if ( _selected ) {
this.dispatchEvent( { type: 'dragend', object: _selected } );
_selected = null;
}
this.domElement.style.cursor = _hovered ? 'pointer' : 'auto';
this.state = STATE.NONE;
}
function onContextMenu( event ) {
if ( this.enabled === false ) return;
event.preventDefault();
}
function findGroup( obj, group = null ) {
if ( obj.isGroup ) group = obj;
if ( obj.parent === null ) return group;
return findGroup( obj.parent, group );
}
export { DragControls };

View File

@@ -0,0 +1,337 @@
import {
Controls,
MathUtils,
Spherical,
Vector3
} from 'three';
const _lookDirection = new Vector3();
const _spherical = new Spherical();
const _target = new Vector3();
const _targetPosition = new Vector3();
class FirstPersonControls extends Controls {
constructor( object, domElement = null ) {
super( object, domElement );
// API
this.movementSpeed = 1.0;
this.lookSpeed = 0.005;
this.lookVertical = true;
this.autoForward = false;
this.activeLook = true;
this.heightSpeed = false;
this.heightCoef = 1.0;
this.heightMin = 0.0;
this.heightMax = 1.0;
this.constrainVertical = false;
this.verticalMin = 0;
this.verticalMax = Math.PI;
this.mouseDragOn = false;
// internals
this._autoSpeedFactor = 0.0;
this._pointerX = 0;
this._pointerY = 0;
this._moveForward = false;
this._moveBackward = false;
this._moveLeft = false;
this._moveRight = false;
this._viewHalfX = 0;
this._viewHalfY = 0;
this._lat = 0;
this._lon = 0;
// event listeners
this._onPointerMove = onPointerMove.bind( this );
this._onPointerDown = onPointerDown.bind( this );
this._onPointerUp = onPointerUp.bind( this );
this._onContextMenu = onContextMenu.bind( this );
this._onKeyDown = onKeyDown.bind( this );
this._onKeyUp = onKeyUp.bind( this );
//
if ( domElement !== null ) {
this.connect();
this.handleResize();
}
this._setOrientation();
}
connect() {
window.addEventListener( 'keydown', this._onKeyDown );
window.addEventListener( 'keyup', this._onKeyUp );
this.domElement.addEventListener( 'pointermove', this._onPointerMove );
this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
this.domElement.addEventListener( 'pointerup', this._onPointerUp );
this.domElement.addEventListener( 'contextmenu', this._onContextMenu );
}
disconnect() {
window.removeEventListener( 'keydown', this._onKeyDown );
window.removeEventListener( 'keyup', this._onKeyUp );
this.domElement.removeEventListener( 'pointerdown', this._onPointerMove );
this.domElement.removeEventListener( 'pointermove', this._onPointerDown );
this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
}
dispose() {
this.disconnect();
}
handleResize() {
if ( this.domElement === document ) {
this._viewHalfX = window.innerWidth / 2;
this._viewHalfY = window.innerHeight / 2;
} else {
this._viewHalfX = this.domElement.offsetWidth / 2;
this._viewHalfY = this.domElement.offsetHeight / 2;
}
}
lookAt( x, y, z ) {
if ( x.isVector3 ) {
_target.copy( x );
} else {
_target.set( x, y, z );
}
this.object.lookAt( _target );
this._setOrientation();
return this;
}
update( delta ) {
if ( this.enabled === false ) return;
if ( this.heightSpeed ) {
const y = MathUtils.clamp( this.object.position.y, this.heightMin, this.heightMax );
const heightDelta = y - this.heightMin;
this._autoSpeedFactor = delta * ( heightDelta * this.heightCoef );
} else {
this._autoSpeedFactor = 0.0;
}
const actualMoveSpeed = delta * this.movementSpeed;
if ( this._moveForward || ( this.autoForward && ! this._moveBackward ) ) this.object.translateZ( - ( actualMoveSpeed + this._autoSpeedFactor ) );
if ( this._moveBackward ) this.object.translateZ( actualMoveSpeed );
if ( this._moveLeft ) this.object.translateX( - actualMoveSpeed );
if ( this._moveRight ) this.object.translateX( actualMoveSpeed );
if ( this._moveUp ) this.object.translateY( actualMoveSpeed );
if ( this._moveDown ) this.object.translateY( - actualMoveSpeed );
let actualLookSpeed = delta * this.lookSpeed;
if ( ! this.activeLook ) {
actualLookSpeed = 0;
}
let verticalLookRatio = 1;
if ( this.constrainVertical ) {
verticalLookRatio = Math.PI / ( this.verticalMax - this.verticalMin );
}
this._lon -= this._pointerX * actualLookSpeed;
if ( this.lookVertical ) this._lat -= this._pointerY * actualLookSpeed * verticalLookRatio;
this._lat = Math.max( - 85, Math.min( 85, this._lat ) );
let phi = MathUtils.degToRad( 90 - this._lat );
const theta = MathUtils.degToRad( this._lon );
if ( this.constrainVertical ) {
phi = MathUtils.mapLinear( phi, 0, Math.PI, this.verticalMin, this.verticalMax );
}
const position = this.object.position;
_targetPosition.setFromSphericalCoords( 1, phi, theta ).add( position );
this.object.lookAt( _targetPosition );
}
_setOrientation() {
const quaternion = this.object.quaternion;
_lookDirection.set( 0, 0, - 1 ).applyQuaternion( quaternion );
_spherical.setFromVector3( _lookDirection );
this._lat = 90 - MathUtils.radToDeg( _spherical.phi );
this._lon = MathUtils.radToDeg( _spherical.theta );
}
}
function onPointerDown( event ) {
if ( this.domElement !== document ) {
this.domElement.focus();
}
if ( this.activeLook ) {
switch ( event.button ) {
case 0: this._moveForward = true; break;
case 2: this._moveBackward = true; break;
}
}
this.mouseDragOn = true;
}
function onPointerUp( event ) {
if ( this.activeLook ) {
switch ( event.button ) {
case 0: this._moveForward = false; break;
case 2: this._moveBackward = false; break;
}
}
this.mouseDragOn = false;
}
function onPointerMove( event ) {
if ( this.domElement === document ) {
this._pointerX = event.pageX - this._viewHalfX;
this._pointerY = event.pageY - this._viewHalfY;
} else {
this._pointerX = event.pageX - this.domElement.offsetLeft - this._viewHalfX;
this._pointerY = event.pageY - this.domElement.offsetTop - this._viewHalfY;
}
}
function onKeyDown( event ) {
switch ( event.code ) {
case 'ArrowUp':
case 'KeyW': this._moveForward = true; break;
case 'ArrowLeft':
case 'KeyA': this._moveLeft = true; break;
case 'ArrowDown':
case 'KeyS': this._moveBackward = true; break;
case 'ArrowRight':
case 'KeyD': this._moveRight = true; break;
case 'KeyR': this._moveUp = true; break;
case 'KeyF': this._moveDown = true; break;
}
}
function onKeyUp( event ) {
switch ( event.code ) {
case 'ArrowUp':
case 'KeyW': this._moveForward = false; break;
case 'ArrowLeft':
case 'KeyA': this._moveLeft = false; break;
case 'ArrowDown':
case 'KeyS': this._moveBackward = false; break;
case 'ArrowRight':
case 'KeyD': this._moveRight = false; break;
case 'KeyR': this._moveUp = false; break;
case 'KeyF': this._moveDown = false; break;
}
}
function onContextMenu( event ) {
if ( this.enabled === false ) return;
event.preventDefault();
}
export { FirstPersonControls };

View File

@@ -0,0 +1,332 @@
import {
Controls,
Quaternion,
Vector3
} from 'three';
const _changeEvent = { type: 'change' };
const _EPS = 0.000001;
const _tmpQuaternion = new Quaternion();
class FlyControls extends Controls {
constructor( object, domElement = null ) {
super( object, domElement );
this.movementSpeed = 1.0;
this.rollSpeed = 0.005;
this.dragToLook = false;
this.autoForward = false;
// internals
this._moveState = { up: 0, down: 0, left: 0, right: 0, forward: 0, back: 0, pitchUp: 0, pitchDown: 0, yawLeft: 0, yawRight: 0, rollLeft: 0, rollRight: 0 };
this._moveVector = new Vector3( 0, 0, 0 );
this._rotationVector = new Vector3( 0, 0, 0 );
this._lastQuaternion = new Quaternion();
this._lastPosition = new Vector3();
this._status = 0;
// event listeners
this._onKeyDown = onKeyDown.bind( this );
this._onKeyUp = onKeyUp.bind( this );
this._onPointerMove = onPointerMove.bind( this );
this._onPointerDown = onPointerDown.bind( this );
this._onPointerUp = onPointerUp.bind( this );
this._onPointerCancel = onPointerCancel.bind( this );
this._onContextMenu = onContextMenu.bind( this );
//
if ( domElement !== null ) {
this.connect();
}
}
connect() {
window.addEventListener( 'keydown', this._onKeyDown );
window.addEventListener( 'keyup', this._onKeyUp );
this.domElement.addEventListener( 'pointermove', this._onPointerMove );
this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
this.domElement.addEventListener( 'pointerup', this._onPointerUp );
this.domElement.addEventListener( 'pointercancel', this._onPointerCancel );
this.domElement.addEventListener( 'contextmenu', this._onContextMenu );
}
disconnect() {
window.removeEventListener( 'keydown', this._onKeyDown );
window.removeEventListener( 'keyup', this._onKeyUp );
this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
this.domElement.removeEventListener( 'pointercancel', this._onPointerCancel );
this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
}
dispose() {
this.disconnect();
}
update( delta ) {
if ( this.enabled === false ) return;
const object = this.object;
const moveMult = delta * this.movementSpeed;
const rotMult = delta * this.rollSpeed;
object.translateX( this._moveVector.x * moveMult );
object.translateY( this._moveVector.y * moveMult );
object.translateZ( this._moveVector.z * moveMult );
_tmpQuaternion.set( this._rotationVector.x * rotMult, this._rotationVector.y * rotMult, this._rotationVector.z * rotMult, 1 ).normalize();
object.quaternion.multiply( _tmpQuaternion );
if (
this._lastPosition.distanceToSquared( object.position ) > _EPS ||
8 * ( 1 - this._lastQuaternion.dot( object.quaternion ) ) > _EPS
) {
this.dispatchEvent( _changeEvent );
this._lastQuaternion.copy( object.quaternion );
this._lastPosition.copy( object.position );
}
}
// private
_updateMovementVector() {
const forward = ( this._moveState.forward || ( this.autoForward && ! this._moveState.back ) ) ? 1 : 0;
this._moveVector.x = ( - this._moveState.left + this._moveState.right );
this._moveVector.y = ( - this._moveState.down + this._moveState.up );
this._moveVector.z = ( - forward + this._moveState.back );
//console.log( 'move:', [ this._moveVector.x, this._moveVector.y, this._moveVector.z ] );
}
_updateRotationVector() {
this._rotationVector.x = ( - this._moveState.pitchDown + this._moveState.pitchUp );
this._rotationVector.y = ( - this._moveState.yawRight + this._moveState.yawLeft );
this._rotationVector.z = ( - this._moveState.rollRight + this._moveState.rollLeft );
//console.log( 'rotate:', [ this._rotationVector.x, this._rotationVector.y, this._rotationVector.z ] );
}
_getContainerDimensions() {
if ( this.domElement != document ) {
return {
size: [ this.domElement.offsetWidth, this.domElement.offsetHeight ],
offset: [ this.domElement.offsetLeft, this.domElement.offsetTop ]
};
} else {
return {
size: [ window.innerWidth, window.innerHeight ],
offset: [ 0, 0 ]
};
}
}
}
function onKeyDown( event ) {
if ( event.altKey || this.enabled === false ) {
return;
}
switch ( event.code ) {
case 'ShiftLeft':
case 'ShiftRight': this.movementSpeedMultiplier = .1; break;
case 'KeyW': this._moveState.forward = 1; break;
case 'KeyS': this._moveState.back = 1; break;
case 'KeyA': this._moveState.left = 1; break;
case 'KeyD': this._moveState.right = 1; break;
case 'KeyR': this._moveState.up = 1; break;
case 'KeyF': this._moveState.down = 1; break;
case 'ArrowUp': this._moveState.pitchUp = 1; break;
case 'ArrowDown': this._moveState.pitchDown = 1; break;
case 'ArrowLeft': this._moveState.yawLeft = 1; break;
case 'ArrowRight': this._moveState.yawRight = 1; break;
case 'KeyQ': this._moveState.rollLeft = 1; break;
case 'KeyE': this._moveState.rollRight = 1; break;
}
this._updateMovementVector();
this._updateRotationVector();
}
function onKeyUp( event ) {
if ( this.enabled === false ) return;
switch ( event.code ) {
case 'ShiftLeft':
case 'ShiftRight': this.movementSpeedMultiplier = 1; break;
case 'KeyW': this._moveState.forward = 0; break;
case 'KeyS': this._moveState.back = 0; break;
case 'KeyA': this._moveState.left = 0; break;
case 'KeyD': this._moveState.right = 0; break;
case 'KeyR': this._moveState.up = 0; break;
case 'KeyF': this._moveState.down = 0; break;
case 'ArrowUp': this._moveState.pitchUp = 0; break;
case 'ArrowDown': this._moveState.pitchDown = 0; break;
case 'ArrowLeft': this._moveState.yawLeft = 0; break;
case 'ArrowRight': this._moveState.yawRight = 0; break;
case 'KeyQ': this._moveState.rollLeft = 0; break;
case 'KeyE': this._moveState.rollRight = 0; break;
}
this._updateMovementVector();
this._updateRotationVector();
}
function onPointerDown( event ) {
if ( this.enabled === false ) return;
if ( this.dragToLook ) {
this._status ++;
} else {
switch ( event.button ) {
case 0: this._moveState.forward = 1; break;
case 2: this._moveState.back = 1; break;
}
this._updateMovementVector();
}
}
function onPointerMove( event ) {
if ( this.enabled === false ) return;
if ( ! this.dragToLook || this._status > 0 ) {
const container = this._getContainerDimensions();
const halfWidth = container.size[ 0 ] / 2;
const halfHeight = container.size[ 1 ] / 2;
this._moveState.yawLeft = - ( ( event.pageX - container.offset[ 0 ] ) - halfWidth ) / halfWidth;
this._moveState.pitchDown = ( ( event.pageY - container.offset[ 1 ] ) - halfHeight ) / halfHeight;
this._updateRotationVector();
}
}
function onPointerUp( event ) {
if ( this.enabled === false ) return;
if ( this.dragToLook ) {
this._status --;
this._moveState.yawLeft = this._moveState.pitchDown = 0;
} else {
switch ( event.button ) {
case 0: this._moveState.forward = 0; break;
case 2: this._moveState.back = 0; break;
}
this._updateMovementVector();
}
this._updateRotationVector();
}
function onPointerCancel() {
if ( this.enabled === false ) return;
if ( this.dragToLook ) {
this._status = 0;
this._moveState.yawLeft = this._moveState.pitchDown = 0;
} else {
this._moveState.forward = 0;
this._moveState.back = 0;
this._updateMovementVector();
}
this._updateRotationVector();
}
function onContextMenu( event ) {
if ( this.enabled === false ) return;
event.preventDefault();
}
export { FlyControls };

View File

@@ -0,0 +1,28 @@
import { MOUSE, TOUCH } from 'three';
import { OrbitControls } from './OrbitControls.js';
// MapControls performs orbiting, dollying (zooming), and panning.
// Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
//
// Orbit - right mouse, or left mouse + ctrl/meta/shiftKey / touch: two-finger rotate
// Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
// Pan - left mouse, or arrow keys / touch: one-finger move
class MapControls extends OrbitControls {
constructor( object, domElement ) {
super( object, domElement );
this.screenSpacePanning = false; // pan orthogonal to world-space direction camera.up
this.mouseButtons = { LEFT: MOUSE.PAN, MIDDLE: MOUSE.DOLLY, RIGHT: MOUSE.ROTATE };
this.touches = { ONE: TOUCH.PAN, TWO: TOUCH.DOLLY_ROTATE };
}
}
export { MapControls };

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,171 @@
import {
Controls,
Euler,
Vector3
} from 'three';
const _euler = new Euler( 0, 0, 0, 'YXZ' );
const _vector = new Vector3();
const _changeEvent = { type: 'change' };
const _lockEvent = { type: 'lock' };
const _unlockEvent = { type: 'unlock' };
const _PI_2 = Math.PI / 2;
class PointerLockControls extends Controls {
constructor( camera, domElement = null ) {
super( camera, domElement );
this.isLocked = false;
// Set to constrain the pitch of the camera
// Range is 0 to Math.PI radians
this.minPolarAngle = 0; // radians
this.maxPolarAngle = Math.PI; // radians
this.pointerSpeed = 1.0;
// event listeners
this._onMouseMove = onMouseMove.bind( this );
this._onPointerlockChange = onPointerlockChange.bind( this );
this._onPointerlockError = onPointerlockError.bind( this );
if ( this.domElement !== null ) {
this.connect();
}
}
connect() {
this.domElement.ownerDocument.addEventListener( 'mousemove', this._onMouseMove );
this.domElement.ownerDocument.addEventListener( 'pointerlockchange', this._onPointerlockChange );
this.domElement.ownerDocument.addEventListener( 'pointerlockerror', this._onPointerlockError );
}
disconnect() {
this.domElement.ownerDocument.removeEventListener( 'mousemove', this._onMouseMove );
this.domElement.ownerDocument.removeEventListener( 'pointerlockchange', this._onPointerlockChange );
this.domElement.ownerDocument.removeEventListener( 'pointerlockerror', this._onPointerlockError );
}
dispose() {
this.disconnect();
}
getObject() {
console.warn( 'THREE.PointerLockControls: getObject() has been deprecated. Use controls.object instead.' ); // @deprecated r169
return this.object;
}
getDirection( v ) {
return v.set( 0, 0, - 1 ).applyQuaternion( this.object.quaternion );
}
moveForward( distance ) {
if ( this.enabled === false ) return;
// move forward parallel to the xz-plane
// assumes camera.up is y-up
const camera = this.object;
_vector.setFromMatrixColumn( camera.matrix, 0 );
_vector.crossVectors( camera.up, _vector );
camera.position.addScaledVector( _vector, distance );
}
moveRight( distance ) {
if ( this.enabled === false ) return;
const camera = this.object;
_vector.setFromMatrixColumn( camera.matrix, 0 );
camera.position.addScaledVector( _vector, distance );
}
lock() {
this.domElement.requestPointerLock();
}
unlock() {
this.domElement.ownerDocument.exitPointerLock();
}
}
// event listeners
function onMouseMove( event ) {
if ( this.enabled === false || this.isLocked === false ) return;
const movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
const movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
const camera = this.object;
_euler.setFromQuaternion( camera.quaternion );
_euler.y -= movementX * 0.002 * this.pointerSpeed;
_euler.x -= movementY * 0.002 * this.pointerSpeed;
_euler.x = Math.max( _PI_2 - this.maxPolarAngle, Math.min( _PI_2 - this.minPolarAngle, _euler.x ) );
camera.quaternion.setFromEuler( _euler );
this.dispatchEvent( _changeEvent );
}
function onPointerlockChange() {
if ( this.domElement.ownerDocument.pointerLockElement === this.domElement ) {
this.dispatchEvent( _lockEvent );
this.isLocked = true;
} else {
this.dispatchEvent( _unlockEvent );
this.isLocked = false;
}
}
function onPointerlockError() {
console.error( 'THREE.PointerLockControls: Unable to use Pointer Lock API' );
}
export { PointerLockControls };

View File

@@ -0,0 +1,849 @@
import {
Controls,
MathUtils,
MOUSE,
Quaternion,
Vector2,
Vector3
} from 'three';
const _changeEvent = { type: 'change' };
const _startEvent = { type: 'start' };
const _endEvent = { type: 'end' };
const _EPS = 0.000001;
const _STATE = { NONE: - 1, ROTATE: 0, ZOOM: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_ZOOM_PAN: 4 };
const _v2 = new Vector2();
const _mouseChange = new Vector2();
const _objectUp = new Vector3();
const _pan = new Vector3();
const _axis = new Vector3();
const _quaternion = new Quaternion();
const _eyeDirection = new Vector3();
const _objectUpDirection = new Vector3();
const _objectSidewaysDirection = new Vector3();
const _moveDirection = new Vector3();
class TrackballControls extends Controls {
constructor( object, domElement = null ) {
super( object, domElement );
// API
this.enabled = true;
this.screen = { left: 0, top: 0, width: 0, height: 0 };
this.rotateSpeed = 1.0;
this.zoomSpeed = 1.2;
this.panSpeed = 0.3;
this.noRotate = false;
this.noZoom = false;
this.noPan = false;
this.staticMoving = false;
this.dynamicDampingFactor = 0.2;
this.minDistance = 0;
this.maxDistance = Infinity;
this.minZoom = 0;
this.maxZoom = Infinity;
this.keys = [ 'KeyA' /*A*/, 'KeyS' /*S*/, 'KeyD' /*D*/ ];
this.mouseButtons = { LEFT: MOUSE.ROTATE, MIDDLE: MOUSE.DOLLY, RIGHT: MOUSE.PAN };
this.state = _STATE.NONE;
this.keyState = _STATE.NONE;
this.target = new Vector3();
// internals
this._lastPosition = new Vector3();
this._lastZoom = 1;
this._touchZoomDistanceStart = 0;
this._touchZoomDistanceEnd = 0;
this._lastAngle = 0;
this._eye = new Vector3();
this._movePrev = new Vector2();
this._moveCurr = new Vector2();
this._lastAxis = new Vector3();
this._zoomStart = new Vector2();
this._zoomEnd = new Vector2();
this._panStart = new Vector2();
this._panEnd = new Vector2();
this._pointers = [];
this._pointerPositions = {};
// event listeners
this._onPointerMove = onPointerMove.bind( this );
this._onPointerDown = onPointerDown.bind( this );
this._onPointerUp = onPointerUp.bind( this );
this._onPointerCancel = onPointerCancel.bind( this );
this._onContextMenu = onContextMenu.bind( this );
this._onMouseWheel = onMouseWheel.bind( this );
this._onKeyDown = onKeyDown.bind( this );
this._onKeyUp = onKeyUp.bind( this );
this._onTouchStart = onTouchStart.bind( this );
this._onTouchMove = onTouchMove.bind( this );
this._onTouchEnd = onTouchEnd.bind( this );
this._onMouseDown = onMouseDown.bind( this );
this._onMouseMove = onMouseMove.bind( this );
this._onMouseUp = onMouseUp.bind( this );
// for reset
this._target0 = this.target.clone();
this._position0 = this.object.position.clone();
this._up0 = this.object.up.clone();
this._zoom0 = this.object.zoom;
if ( domElement !== null ) {
this.connect();
this.handleResize();
}
// force an update at start
this.update();
}
connect() {
window.addEventListener( 'keydown', this._onKeyDown );
window.addEventListener( 'keyup', this._onKeyUp );
this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
this.domElement.addEventListener( 'pointercancel', this._onPointerCancel );
this.domElement.addEventListener( 'wheel', this._onMouseWheel, { passive: false } );
this.domElement.addEventListener( 'contextmenu', this._onContextMenu );
this.domElement.style.touchAction = 'none'; // disable touch scroll
}
disconnect() {
window.removeEventListener( 'keydown', this._onKeyDown );
window.removeEventListener( 'keyup', this._onKeyUp );
this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
this.domElement.removeEventListener( 'pointercancel', this._onPointerCancel );
this.domElement.removeEventListener( 'wheel', this._onMouseWheel );
this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
this.domElement.style.touchAction = 'auto'; // disable touch scroll
}
dispose() {
this.disconnect();
}
handleResize() {
const box = this.domElement.getBoundingClientRect();
// adjustments come from similar code in the jquery offset() function
const d = this.domElement.ownerDocument.documentElement;
this.screen.left = box.left + window.pageXOffset - d.clientLeft;
this.screen.top = box.top + window.pageYOffset - d.clientTop;
this.screen.width = box.width;
this.screen.height = box.height;
}
update() {
this._eye.subVectors( this.object.position, this.target );
if ( ! this.noRotate ) {
this._rotateCamera();
}
if ( ! this.noZoom ) {
this._zoomCamera();
}
if ( ! this.noPan ) {
this._panCamera();
}
this.object.position.addVectors( this.target, this._eye );
if ( this.object.isPerspectiveCamera ) {
this._checkDistances();
this.object.lookAt( this.target );
if ( this._lastPosition.distanceToSquared( this.object.position ) > _EPS ) {
this.dispatchEvent( _changeEvent );
this._lastPosition.copy( this.object.position );
}
} else if ( this.object.isOrthographicCamera ) {
this.object.lookAt( this.target );
if ( this._lastPosition.distanceToSquared( this.object.position ) > _EPS || this._lastZoom !== this.object.zoom ) {
this.dispatchEvent( _changeEvent );
this._lastPosition.copy( this.object.position );
this._lastZoom = this.object.zoom;
}
} else {
console.warn( 'THREE.TrackballControls: Unsupported camera type.' );
}
}
reset() {
this.state = _STATE.NONE;
this.keyState = _STATE.NONE;
this.target.copy( this._target0 );
this.object.position.copy( this._position0 );
this.object.up.copy( this._up0 );
this.object.zoom = this._zoom0;
this.object.updateProjectionMatrix();
this._eye.subVectors( this.object.position, this.target );
this.object.lookAt( this.target );
this.dispatchEvent( _changeEvent );
this._lastPosition.copy( this.object.position );
this._lastZoom = this.object.zoom;
}
_panCamera() {
_mouseChange.copy( this._panEnd ).sub( this._panStart );
if ( _mouseChange.lengthSq() ) {
if ( this.object.isOrthographicCamera ) {
const scale_x = ( this.object.right - this.object.left ) / this.object.zoom / this.domElement.clientWidth;
const scale_y = ( this.object.top - this.object.bottom ) / this.object.zoom / this.domElement.clientWidth;
_mouseChange.x *= scale_x;
_mouseChange.y *= scale_y;
}
_mouseChange.multiplyScalar( this._eye.length() * this.panSpeed );
_pan.copy( this._eye ).cross( this.object.up ).setLength( _mouseChange.x );
_pan.add( _objectUp.copy( this.object.up ).setLength( _mouseChange.y ) );
this.object.position.add( _pan );
this.target.add( _pan );
if ( this.staticMoving ) {
this._panStart.copy( this._panEnd );
} else {
this._panStart.add( _mouseChange.subVectors( this._panEnd, this._panStart ).multiplyScalar( this.dynamicDampingFactor ) );
}
}
}
_rotateCamera() {
_moveDirection.set( this._moveCurr.x - this._movePrev.x, this._moveCurr.y - this._movePrev.y, 0 );
let angle = _moveDirection.length();
if ( angle ) {
this._eye.copy( this.object.position ).sub( this.target );
_eyeDirection.copy( this._eye ).normalize();
_objectUpDirection.copy( this.object.up ).normalize();
_objectSidewaysDirection.crossVectors( _objectUpDirection, _eyeDirection ).normalize();
_objectUpDirection.setLength( this._moveCurr.y - this._movePrev.y );
_objectSidewaysDirection.setLength( this._moveCurr.x - this._movePrev.x );
_moveDirection.copy( _objectUpDirection.add( _objectSidewaysDirection ) );
_axis.crossVectors( _moveDirection, this._eye ).normalize();
angle *= this.rotateSpeed;
_quaternion.setFromAxisAngle( _axis, angle );
this._eye.applyQuaternion( _quaternion );
this.object.up.applyQuaternion( _quaternion );
this._lastAxis.copy( _axis );
this._lastAngle = angle;
} else if ( ! this.staticMoving && this._lastAngle ) {
this._lastAngle *= Math.sqrt( 1.0 - this.dynamicDampingFactor );
this._eye.copy( this.object.position ).sub( this.target );
_quaternion.setFromAxisAngle( this._lastAxis, this._lastAngle );
this._eye.applyQuaternion( _quaternion );
this.object.up.applyQuaternion( _quaternion );
}
this._movePrev.copy( this._moveCurr );
}
_zoomCamera() {
let factor;
if ( this.state === _STATE.TOUCH_ZOOM_PAN ) {
factor = this._touchZoomDistanceStart / this._touchZoomDistanceEnd;
this._touchZoomDistanceStart = this._touchZoomDistanceEnd;
if ( this.object.isPerspectiveCamera ) {
this._eye.multiplyScalar( factor );
} else if ( this.object.isOrthographicCamera ) {
this.object.zoom = MathUtils.clamp( this.object.zoom / factor, this.minZoom, this.maxZoom );
if ( this._lastZoom !== this.object.zoom ) {
this.object.updateProjectionMatrix();
}
} else {
console.warn( 'THREE.TrackballControls: Unsupported camera type' );
}
} else {
factor = 1.0 + ( this._zoomEnd.y - this._zoomStart.y ) * this.zoomSpeed;
if ( factor !== 1.0 && factor > 0.0 ) {
if ( this.object.isPerspectiveCamera ) {
this._eye.multiplyScalar( factor );
} else if ( this.object.isOrthographicCamera ) {
this.object.zoom = MathUtils.clamp( this.object.zoom / factor, this.minZoom, this.maxZoom );
if ( this._lastZoom !== this.object.zoom ) {
this.object.updateProjectionMatrix();
}
} else {
console.warn( 'THREE.TrackballControls: Unsupported camera type' );
}
}
if ( this.staticMoving ) {
this._zoomStart.copy( this._zoomEnd );
} else {
this._zoomStart.y += ( this._zoomEnd.y - this._zoomStart.y ) * this.dynamicDampingFactor;
}
}
}
_getMouseOnScreen( pageX, pageY ) {
_v2.set(
( pageX - this.screen.left ) / this.screen.width,
( pageY - this.screen.top ) / this.screen.height
);
return _v2;
}
_getMouseOnCircle( pageX, pageY ) {
_v2.set(
( ( pageX - this.screen.width * 0.5 - this.screen.left ) / ( this.screen.width * 0.5 ) ),
( ( this.screen.height + 2 * ( this.screen.top - pageY ) ) / this.screen.width ) // screen.width intentional
);
return _v2;
}
_addPointer( event ) {
this._pointers.push( event );
}
_removePointer( event ) {
delete this._pointerPositions[ event.pointerId ];
for ( let i = 0; i < this._pointers.length; i ++ ) {
if ( this._pointers[ i ].pointerId == event.pointerId ) {
this._pointers.splice( i, 1 );
return;
}
}
}
_trackPointer( event ) {
let position = this._pointerPositions[ event.pointerId ];
if ( position === undefined ) {
position = new Vector2();
this._pointerPositions[ event.pointerId ] = position;
}
position.set( event.pageX, event.pageY );
}
_getSecondPointerPosition( event ) {
const pointer = ( event.pointerId === this._pointers[ 0 ].pointerId ) ? this._pointers[ 1 ] : this._pointers[ 0 ];
return this._pointerPositions[ pointer.pointerId ];
}
_checkDistances() {
if ( ! this.noZoom || ! this.noPan ) {
if ( this._eye.lengthSq() > this.maxDistance * this.maxDistance ) {
this.object.position.addVectors( this.target, this._eye.setLength( this.maxDistance ) );
this._zoomStart.copy( this._zoomEnd );
}
if ( this._eye.lengthSq() < this.minDistance * this.minDistance ) {
this.object.position.addVectors( this.target, this._eye.setLength( this.minDistance ) );
this._zoomStart.copy( this._zoomEnd );
}
}
}
}
function onPointerDown( event ) {
if ( this.enabled === false ) return;
if ( this._pointers.length === 0 ) {
this.domElement.setPointerCapture( event.pointerId );
this.domElement.addEventListener( 'pointermove', this._onPointerMove );
this.domElement.addEventListener( 'pointerup', this._onPointerUp );
}
//
this._addPointer( event );
if ( event.pointerType === 'touch' ) {
this._onTouchStart( event );
} else {
this._onMouseDown( event );
}
}
function onPointerMove( event ) {
if ( this.enabled === false ) return;
if ( event.pointerType === 'touch' ) {
this._onTouchMove( event );
} else {
this._onMouseMove( event );
}
}
function onPointerUp( event ) {
if ( this.enabled === false ) return;
if ( event.pointerType === 'touch' ) {
this._onTouchEnd( event );
} else {
this._onMouseUp();
}
//
this._removePointer( event );
if ( this._pointers.length === 0 ) {
this.domElement.releasePointerCapture( event.pointerId );
this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
}
}
function onPointerCancel( event ) {
this._removePointer( event );
}
function onKeyUp() {
if ( this.enabled === false ) return;
this.keyState = _STATE.NONE;
window.addEventListener( 'keydown', this._onKeyDown );
}
function onKeyDown( event ) {
if ( this.enabled === false ) return;
window.removeEventListener( 'keydown', this._onKeyDown );
if ( this.keyState !== _STATE.NONE ) {
return;
} else if ( event.code === this.keys[ _STATE.ROTATE ] && ! this.noRotate ) {
this.keyState = _STATE.ROTATE;
} else if ( event.code === this.keys[ _STATE.ZOOM ] && ! this.noZoom ) {
this.keyState = _STATE.ZOOM;
} else if ( event.code === this.keys[ _STATE.PAN ] && ! this.noPan ) {
this.keyState = _STATE.PAN;
}
}
function onMouseDown( event ) {
let mouseAction;
switch ( event.button ) {
case 0:
mouseAction = this.mouseButtons.LEFT;
break;
case 1:
mouseAction = this.mouseButtons.MIDDLE;
break;
case 2:
mouseAction = this.mouseButtons.RIGHT;
break;
default:
mouseAction = - 1;
}
switch ( mouseAction ) {
case MOUSE.DOLLY:
this.state = _STATE.ZOOM;
break;
case MOUSE.ROTATE:
this.state = _STATE.ROTATE;
break;
case MOUSE.PAN:
this.state = _STATE.PAN;
break;
default:
this.state = _STATE.NONE;
}
const state = ( this.keyState !== _STATE.NONE ) ? this.keyState : this.state;
if ( state === _STATE.ROTATE && ! this.noRotate ) {
this._moveCurr.copy( this._getMouseOnCircle( event.pageX, event.pageY ) );
this._movePrev.copy( this._moveCurr );
} else if ( state === _STATE.ZOOM && ! this.noZoom ) {
this._zoomStart.copy( this._getMouseOnScreen( event.pageX, event.pageY ) );
this._zoomEnd.copy( this._zoomStart );
} else if ( state === _STATE.PAN && ! this.noPan ) {
this._panStart.copy( this._getMouseOnScreen( event.pageX, event.pageY ) );
this._panEnd.copy( this._panStart );
}
this.dispatchEvent( _startEvent );
}
function onMouseMove( event ) {
const state = ( this.keyState !== _STATE.NONE ) ? this.keyState : this.state;
if ( state === _STATE.ROTATE && ! this.noRotate ) {
this._movePrev.copy( this._moveCurr );
this._moveCurr.copy( this._getMouseOnCircle( event.pageX, event.pageY ) );
} else if ( state === _STATE.ZOOM && ! this.noZoom ) {
this._zoomEnd.copy( this._getMouseOnScreen( event.pageX, event.pageY ) );
} else if ( state === _STATE.PAN && ! this.noPan ) {
this._panEnd.copy( this._getMouseOnScreen( event.pageX, event.pageY ) );
}
}
function onMouseUp() {
this.state = _STATE.NONE;
this.dispatchEvent( _endEvent );
}
function onMouseWheel( event ) {
if ( this.enabled === false ) return;
if ( this.noZoom === true ) return;
event.preventDefault();
switch ( event.deltaMode ) {
case 2:
// Zoom in pages
this._zoomStart.y -= event.deltaY * 0.025;
break;
case 1:
// Zoom in lines
this._zoomStart.y -= event.deltaY * 0.01;
break;
default:
// undefined, 0, assume pixels
this._zoomStart.y -= event.deltaY * 0.00025;
break;
}
this.dispatchEvent( _startEvent );
this.dispatchEvent( _endEvent );
}
function onContextMenu( event ) {
if ( this.enabled === false ) return;
event.preventDefault();
}
function onTouchStart( event ) {
this._trackPointer( event );
switch ( this._pointers.length ) {
case 1:
this.state = _STATE.TOUCH_ROTATE;
this._moveCurr.copy( this._getMouseOnCircle( this._pointers[ 0 ].pageX, this._pointers[ 0 ].pageY ) );
this._movePrev.copy( this._moveCurr );
break;
default: // 2 or more
this.state = _STATE.TOUCH_ZOOM_PAN;
const dx = this._pointers[ 0 ].pageX - this._pointers[ 1 ].pageX;
const dy = this._pointers[ 0 ].pageY - this._pointers[ 1 ].pageY;
this._touchZoomDistanceEnd = this._touchZoomDistanceStart = Math.sqrt( dx * dx + dy * dy );
const x = ( this._pointers[ 0 ].pageX + this._pointers[ 1 ].pageX ) / 2;
const y = ( this._pointers[ 0 ].pageY + this._pointers[ 1 ].pageY ) / 2;
this._panStart.copy( this._getMouseOnScreen( x, y ) );
this._panEnd.copy( this._panStart );
break;
}
this.dispatchEvent( _startEvent );
}
function onTouchMove( event ) {
this._trackPointer( event );
switch ( this._pointers.length ) {
case 1:
this._movePrev.copy( this._moveCurr );
this._moveCurr.copy( this._getMouseOnCircle( event.pageX, event.pageY ) );
break;
default: // 2 or more
const position = this._getSecondPointerPosition( event );
const dx = event.pageX - position.x;
const dy = event.pageY - position.y;
this._touchZoomDistanceEnd = Math.sqrt( dx * dx + dy * dy );
const x = ( event.pageX + position.x ) / 2;
const y = ( event.pageY + position.y ) / 2;
this._panEnd.copy( this._getMouseOnScreen( x, y ) );
break;
}
}
function onTouchEnd( event ) {
switch ( this._pointers.length ) {
case 0:
this.state = _STATE.NONE;
break;
case 1:
this.state = _STATE.TOUCH_ROTATE;
this._moveCurr.copy( this._getMouseOnCircle( event.pageX, event.pageY ) );
this._movePrev.copy( this._moveCurr );
break;
case 2:
this.state = _STATE.TOUCH_ZOOM_PAN;
for ( let i = 0; i < this._pointers.length; i ++ ) {
if ( this._pointers[ i ].pointerId !== event.pointerId ) {
const position = this._pointerPositions[ this._pointers[ i ].pointerId ];
this._moveCurr.copy( this._getMouseOnCircle( position.x, position.y ) );
this._movePrev.copy( this._moveCurr );
break;
}
}
break;
}
this.dispatchEvent( _endEvent );
}
export { TrackballControls };

File diff suppressed because it is too large Load Diff