feat: initialize project with core dependencies and game entry point
This commit is contained in:
3536
node_modules/three/examples/jsm/controls/ArcballControls.js
generated
vendored
Normal file
3536
node_modules/three/examples/jsm/controls/ArcballControls.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
452
node_modules/three/examples/jsm/controls/DragControls.js
generated
vendored
Normal file
452
node_modules/three/examples/jsm/controls/DragControls.js
generated
vendored
Normal file
@@ -0,0 +1,452 @@
|
||||
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
|
||||
};
|
||||
|
||||
/**
|
||||
* This class can be used to provide a drag'n'drop interaction.
|
||||
*
|
||||
* ```js
|
||||
* const controls = new DragControls( objects, camera, renderer.domElement );
|
||||
*
|
||||
* // add event listener to highlight dragged objects
|
||||
* controls.addEventListener( 'dragstart', function ( event ) {
|
||||
*
|
||||
* event.object.material.emissive.set( 0xaaaaaa );
|
||||
*
|
||||
* } );
|
||||
*
|
||||
* controls.addEventListener( 'dragend', function ( event ) {
|
||||
*
|
||||
* event.object.material.emissive.set( 0x000000 );
|
||||
*
|
||||
* } );
|
||||
* ```
|
||||
*
|
||||
* @augments Controls
|
||||
* @three_import import { DragControls } from 'three/addons/controls/DragControls.js';
|
||||
*/
|
||||
class DragControls extends Controls {
|
||||
|
||||
/**
|
||||
* Constructs a new controls instance.
|
||||
*
|
||||
* @param {Array<Object3D>} objects - An array of draggable 3D objects.
|
||||
* @param {Camera} camera - The camera of the rendered scene.
|
||||
* @param {?HTMLElement} [domElement=null] - The HTML DOM element used for event listeners.
|
||||
*/
|
||||
constructor( objects, camera, domElement = null ) {
|
||||
|
||||
super( camera, domElement );
|
||||
|
||||
/**
|
||||
* An array of draggable 3D objects.
|
||||
*
|
||||
* @type {Array<Object3D>}
|
||||
*/
|
||||
this.objects = objects;
|
||||
|
||||
/**
|
||||
* Whether children of draggable objects can be dragged independently from their parent.
|
||||
*
|
||||
* @type {boolean}
|
||||
* @default true
|
||||
*/
|
||||
this.recursive = true;
|
||||
|
||||
/**
|
||||
* This option only works if the `objects` array contains a single draggable group object.
|
||||
* If set to `true`, the controls does not transform individual objects but the entire group.
|
||||
*
|
||||
* @type {boolean}
|
||||
* @default false
|
||||
*/
|
||||
this.transformGroup = false;
|
||||
|
||||
/**
|
||||
* The speed at which the object will rotate when dragged in `rotate` mode.
|
||||
* The higher the number the faster the rotation.
|
||||
*
|
||||
* @type {number}
|
||||
* @default 1
|
||||
*/
|
||||
this.rotateSpeed = 1;
|
||||
|
||||
/**
|
||||
* The raycaster used for detecting 3D objects.
|
||||
*
|
||||
* @type {Raycaster}
|
||||
*/
|
||||
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( domElement );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
connect( element ) {
|
||||
|
||||
super.connect( element );
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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 ) );
|
||||
this.dispatchEvent( { type: 'drag', object: _selected } );
|
||||
|
||||
}
|
||||
|
||||
} 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 ) );
|
||||
domElement.style.cursor = 'move';
|
||||
this.dispatchEvent( { type: 'dragstart', object: _selected } );
|
||||
|
||||
} 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 );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires when the user drags a 3D object.
|
||||
*
|
||||
* @event DragControls#drag
|
||||
* @type {Object}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Fires when the user has finished dragging a 3D object.
|
||||
*
|
||||
* @event DragControls#dragend
|
||||
* @type {Object}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Fires when the pointer is moved onto a 3D object, or onto one of its children.
|
||||
*
|
||||
* @event DragControls#hoveron
|
||||
* @type {Object}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Fires when the pointer is moved out of a 3D object.
|
||||
*
|
||||
* @event DragControls#hoveroff
|
||||
* @type {Object}
|
||||
*/
|
||||
|
||||
export { DragControls };
|
||||
447
node_modules/three/examples/jsm/controls/FirstPersonControls.js
generated
vendored
Normal file
447
node_modules/three/examples/jsm/controls/FirstPersonControls.js
generated
vendored
Normal file
@@ -0,0 +1,447 @@
|
||||
import {
|
||||
Controls,
|
||||
MathUtils,
|
||||
Spherical,
|
||||
Vector3
|
||||
} from 'three';
|
||||
|
||||
const _lookDirection = new Vector3();
|
||||
const _spherical = new Spherical();
|
||||
const _target = new Vector3();
|
||||
const _targetPosition = new Vector3();
|
||||
|
||||
/**
|
||||
* This class is an alternative implementation of {@link FlyControls}.
|
||||
*
|
||||
* @augments Controls
|
||||
* @three_import import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js';
|
||||
*/
|
||||
class FirstPersonControls extends Controls {
|
||||
|
||||
/**
|
||||
* Constructs a new controls instance.
|
||||
*
|
||||
* @param {Object3D} object - The object that is managed by the controls.
|
||||
* @param {?HTMLElement} domElement - The HTML element used for event listeners.
|
||||
*/
|
||||
constructor( object, domElement = null ) {
|
||||
|
||||
super( object, domElement );
|
||||
|
||||
/**
|
||||
* The movement speed.
|
||||
*
|
||||
* @type {number}
|
||||
* @default 1
|
||||
*/
|
||||
this.movementSpeed = 1.0;
|
||||
|
||||
/**
|
||||
* The look around speed.
|
||||
*
|
||||
* @type {number}
|
||||
* @default 0.005
|
||||
*/
|
||||
this.lookSpeed = 0.005;
|
||||
|
||||
/**
|
||||
* Whether it's possible to vertically look around or not.
|
||||
*
|
||||
* @type {boolean}
|
||||
* @default true
|
||||
*/
|
||||
this.lookVertical = true;
|
||||
|
||||
/**
|
||||
* Whether the camera is automatically moved forward or not.
|
||||
*
|
||||
* @type {boolean}
|
||||
* @default false
|
||||
*/
|
||||
this.autoForward = false;
|
||||
|
||||
/**
|
||||
* Whether it's possible to look around or not.
|
||||
*
|
||||
* @type {boolean}
|
||||
* @default true
|
||||
*/
|
||||
this.activeLook = true;
|
||||
|
||||
/**
|
||||
* Whether or not the camera's height influences the forward movement speed.
|
||||
* Use the properties `heightCoef`, `heightMin` and `heightMax` for configuration.
|
||||
*
|
||||
* @type {boolean}
|
||||
* @default false
|
||||
*/
|
||||
this.heightSpeed = false;
|
||||
|
||||
/**
|
||||
* Determines how much faster the camera moves when it's y-component is near `heightMax`.
|
||||
*
|
||||
* @type {number}
|
||||
* @default 1
|
||||
*/
|
||||
this.heightCoef = 1.0;
|
||||
|
||||
/**
|
||||
* Lower camera height limit used for movement speed adjustment.
|
||||
*
|
||||
* @type {number}
|
||||
* @default 0
|
||||
*/
|
||||
this.heightMin = 0.0;
|
||||
|
||||
/**
|
||||
* Upper camera height limit used for movement speed adjustment.
|
||||
*
|
||||
* @type {number}
|
||||
* @default 1
|
||||
*/
|
||||
this.heightMax = 1.0;
|
||||
|
||||
/**
|
||||
* Whether or not looking around is vertically constrained by `verticalMin` and `verticalMax`.
|
||||
*
|
||||
* @type {boolean}
|
||||
* @default false
|
||||
*/
|
||||
this.constrainVertical = false;
|
||||
|
||||
/**
|
||||
* How far you can vertically look around, lower limit. Range is `0` to `Math.PI` in radians.
|
||||
*
|
||||
* @type {number}
|
||||
* @default 0
|
||||
*/
|
||||
this.verticalMin = 0;
|
||||
|
||||
/**
|
||||
* How far you can vertically look around, upper limit. Range is `0` to `Math.PI` in radians.
|
||||
*
|
||||
* @type {number}
|
||||
* @default 0
|
||||
*/
|
||||
this.verticalMax = Math.PI;
|
||||
|
||||
/**
|
||||
* Whether the mouse is pressed down or not.
|
||||
*
|
||||
* @type {boolean}
|
||||
* @readonly
|
||||
* @default false
|
||||
*/
|
||||
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( domElement );
|
||||
|
||||
this.handleResize();
|
||||
|
||||
}
|
||||
|
||||
this._setOrientation();
|
||||
|
||||
}
|
||||
|
||||
connect( element ) {
|
||||
|
||||
super.connect( element );
|
||||
|
||||
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( 'pointermove', this._onPointerMove );
|
||||
this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
|
||||
this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
|
||||
this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
|
||||
|
||||
}
|
||||
|
||||
dispose() {
|
||||
|
||||
this.disconnect();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Must be called if the application window is resized.
|
||||
*/
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates the camera towards the defined target position.
|
||||
*
|
||||
* @param {number|Vector3} x - The x coordinate of the target position or alternatively a vector representing the target position.
|
||||
* @param {number} y - The y coordinate of the target position.
|
||||
* @param {number} z - The z coordinate of the target position.
|
||||
* @return {FirstPersonControls} A reference to this controls.
|
||||
*/
|
||||
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 };
|
||||
380
node_modules/three/examples/jsm/controls/FlyControls.js
generated
vendored
Normal file
380
node_modules/three/examples/jsm/controls/FlyControls.js
generated
vendored
Normal file
@@ -0,0 +1,380 @@
|
||||
import {
|
||||
Controls,
|
||||
Quaternion,
|
||||
Vector3
|
||||
} from 'three';
|
||||
|
||||
/**
|
||||
* Fires when the camera has been transformed by the controls.
|
||||
*
|
||||
* @event FlyControls#change
|
||||
* @type {Object}
|
||||
*/
|
||||
const _changeEvent = { type: 'change' };
|
||||
|
||||
const _EPS = 0.000001;
|
||||
const _tmpQuaternion = new Quaternion();
|
||||
|
||||
/**
|
||||
* This class enables a navigation similar to fly modes in DCC tools like Blender.
|
||||
* You can arbitrarily transform the camera in 3D space without any limitations
|
||||
* (e.g. focus on a specific target).
|
||||
*
|
||||
* @augments Controls
|
||||
* @three_import import { FlyControls } from 'three/addons/controls/FlyControls.js';
|
||||
*/
|
||||
class FlyControls extends Controls {
|
||||
|
||||
/**
|
||||
* Constructs a new controls instance.
|
||||
*
|
||||
* @param {Object3D} object - The object that is managed by the controls.
|
||||
* @param {?HTMLElement} domElement - The HTML element used for event listeners.
|
||||
*/
|
||||
constructor( object, domElement = null ) {
|
||||
|
||||
super( object, domElement );
|
||||
|
||||
/**
|
||||
* The movement speed.
|
||||
*
|
||||
* @type {number}
|
||||
* @default 1
|
||||
*/
|
||||
this.movementSpeed = 1.0;
|
||||
|
||||
/**
|
||||
* The rotation speed.
|
||||
*
|
||||
* @type {number}
|
||||
* @default 0.005
|
||||
*/
|
||||
this.rollSpeed = 0.005;
|
||||
|
||||
/**
|
||||
* If set to `true`, you can only look around by performing a drag interaction.
|
||||
*
|
||||
* @type {boolean}
|
||||
* @default false
|
||||
*/
|
||||
this.dragToLook = false;
|
||||
|
||||
/**
|
||||
* If set to `true`, the camera automatically moves forward (and does not stop) when initially translated.
|
||||
*
|
||||
* @type {boolean}
|
||||
* @default 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( domElement );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
connect( element ) {
|
||||
|
||||
super.connect( element );
|
||||
|
||||
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 };
|
||||
116
node_modules/three/examples/jsm/controls/MapControls.js
generated
vendored
Normal file
116
node_modules/three/examples/jsm/controls/MapControls.js
generated
vendored
Normal file
@@ -0,0 +1,116 @@
|
||||
import { MOUSE, TOUCH, Plane, Raycaster, Vector2, Vector3 } from 'three';
|
||||
|
||||
import { OrbitControls } from './OrbitControls.js';
|
||||
|
||||
const _plane = new Plane();
|
||||
const _raycaster = new Raycaster();
|
||||
const _mouse = new Vector2();
|
||||
const _panCurrent = new Vector3();
|
||||
|
||||
/**
|
||||
* This class is intended for transforming a camera over a map from bird's eye perspective.
|
||||
* The class shares its implementation with {@link OrbitControls} but uses a specific preset
|
||||
* for mouse/touch interaction and disables screen space panning 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.
|
||||
*
|
||||
* @augments OrbitControls
|
||||
* @three_import import { MapControls } from 'three/addons/controls/MapControls.js';
|
||||
*/
|
||||
class MapControls extends OrbitControls {
|
||||
|
||||
constructor( object, domElement ) {
|
||||
|
||||
super( object, domElement );
|
||||
|
||||
/**
|
||||
* Overwritten and set to `false` to pan orthogonal to world-space direction `camera.up`.
|
||||
*
|
||||
* @type {boolean}
|
||||
* @default false
|
||||
*/
|
||||
this.screenSpacePanning = false;
|
||||
|
||||
/**
|
||||
* This object contains references to the mouse actions used by the controls.
|
||||
*
|
||||
* ```js
|
||||
* controls.mouseButtons = {
|
||||
* LEFT: THREE.MOUSE.PAN,
|
||||
* MIDDLE: THREE.MOUSE.DOLLY,
|
||||
* RIGHT: THREE.MOUSE.ROTATE
|
||||
* }
|
||||
* ```
|
||||
* @type {Object}
|
||||
*/
|
||||
this.mouseButtons = { LEFT: MOUSE.PAN, MIDDLE: MOUSE.DOLLY, RIGHT: MOUSE.ROTATE };
|
||||
|
||||
/**
|
||||
* This object contains references to the touch actions used by the controls.
|
||||
*
|
||||
* ```js
|
||||
* controls.mouseButtons = {
|
||||
* ONE: THREE.TOUCH.PAN,
|
||||
* TWO: THREE.TOUCH.DOLLY_ROTATE
|
||||
* }
|
||||
* ```
|
||||
* @type {Object}
|
||||
*/
|
||||
this.touches = { ONE: TOUCH.PAN, TWO: TOUCH.DOLLY_ROTATE };
|
||||
|
||||
this._panWorldStart = new Vector3();
|
||||
|
||||
}
|
||||
|
||||
_handleMouseDownPan( event ) {
|
||||
|
||||
super._handleMouseDownPan( event );
|
||||
|
||||
this._panOffset.set( 0, 0, 0 );
|
||||
|
||||
if ( this.screenSpacePanning === true ) return;
|
||||
|
||||
_plane.setFromNormalAndCoplanarPoint( this.object.up, this.target );
|
||||
|
||||
const element = this.domElement;
|
||||
const rect = element.getBoundingClientRect();
|
||||
_mouse.x = ( ( event.clientX - rect.left ) / rect.width ) * 2 - 1;
|
||||
_mouse.y = - ( ( event.clientY - rect.top ) / rect.height ) * 2 + 1;
|
||||
|
||||
_raycaster.setFromCamera( _mouse, this.object );
|
||||
_raycaster.ray.intersectPlane( _plane, this._panWorldStart );
|
||||
|
||||
}
|
||||
|
||||
_handleMouseMovePan( event ) {
|
||||
|
||||
if ( this.screenSpacePanning === true ) {
|
||||
|
||||
super._handleMouseMovePan( event );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
const element = this.domElement;
|
||||
const rect = element.getBoundingClientRect();
|
||||
_mouse.x = ( ( event.clientX - rect.left ) / rect.width ) * 2 - 1;
|
||||
_mouse.y = - ( ( event.clientY - rect.top ) / rect.height ) * 2 + 1;
|
||||
|
||||
_raycaster.setFromCamera( _mouse, this.object );
|
||||
|
||||
if ( _raycaster.ray.intersectPlane( _plane, _panCurrent ) ) {
|
||||
|
||||
_panCurrent.sub( this._panWorldStart );
|
||||
this._panOffset.copy( _panCurrent ).negate();
|
||||
|
||||
this.update();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export { MapControls };
|
||||
1860
node_modules/three/examples/jsm/controls/OrbitControls.js
generated
vendored
Normal file
1860
node_modules/three/examples/jsm/controls/OrbitControls.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
264
node_modules/three/examples/jsm/controls/PointerLockControls.js
generated
vendored
Normal file
264
node_modules/three/examples/jsm/controls/PointerLockControls.js
generated
vendored
Normal file
@@ -0,0 +1,264 @@
|
||||
import {
|
||||
Controls,
|
||||
Euler,
|
||||
Vector3
|
||||
} from 'three';
|
||||
|
||||
const _euler = new Euler( 0, 0, 0, 'YXZ' );
|
||||
const _vector = new Vector3();
|
||||
|
||||
/**
|
||||
* Fires when the user moves the mouse.
|
||||
*
|
||||
* @event PointerLockControls#change
|
||||
* @type {Object}
|
||||
*/
|
||||
const _changeEvent = { type: 'change' };
|
||||
|
||||
/**
|
||||
* Fires when the pointer lock status is "locked" (in other words: the mouse is captured).
|
||||
*
|
||||
* @event PointerLockControls#lock
|
||||
* @type {Object}
|
||||
*/
|
||||
const _lockEvent = { type: 'lock' };
|
||||
|
||||
/**
|
||||
* Fires when the pointer lock status is "unlocked" (in other words: the mouse is not captured anymore).
|
||||
*
|
||||
* @event PointerLockControls#unlock
|
||||
* @type {Object}
|
||||
*/
|
||||
const _unlockEvent = { type: 'unlock' };
|
||||
|
||||
const _MOUSE_SENSITIVITY = 0.002;
|
||||
const _PI_2 = Math.PI / 2;
|
||||
|
||||
/**
|
||||
* The implementation of this class is based on the [Pointer Lock API](https://developer.mozilla.org/en-US/docs/Web/API/Pointer_Lock_API).
|
||||
* `PointerLockControls` is a perfect choice for first person 3D games.
|
||||
*
|
||||
* ```js
|
||||
* const controls = new PointerLockControls( camera, document.body );
|
||||
*
|
||||
* // add event listener to show/hide a UI (e.g. the game's menu)
|
||||
* controls.addEventListener( 'lock', function () {
|
||||
*
|
||||
* menu.style.display = 'none';
|
||||
*
|
||||
* } );
|
||||
*
|
||||
* controls.addEventListener( 'unlock', function () {
|
||||
*
|
||||
* menu.style.display = 'block';
|
||||
*
|
||||
* } );
|
||||
* ```
|
||||
*
|
||||
* @augments Controls
|
||||
* @three_import import { PointerLockControls } from 'three/addons/controls/PointerLockControls.js';
|
||||
*/
|
||||
class PointerLockControls extends Controls {
|
||||
|
||||
/**
|
||||
* Constructs a new controls instance.
|
||||
*
|
||||
* @param {Camera} camera - The camera that is managed by the controls.
|
||||
* @param {?HTMLElement} domElement - The HTML element used for event listeners.
|
||||
*/
|
||||
constructor( camera, domElement = null ) {
|
||||
|
||||
super( camera, domElement );
|
||||
|
||||
/**
|
||||
* Whether the controls are locked or not.
|
||||
*
|
||||
* @type {boolean}
|
||||
* @readonly
|
||||
* @default false
|
||||
*/
|
||||
this.isLocked = false;
|
||||
|
||||
/**
|
||||
* Camera pitch, lower limit. Range is '[0, Math.PI]' in radians.
|
||||
*
|
||||
* @type {number}
|
||||
* @default 0
|
||||
*/
|
||||
this.minPolarAngle = 0;
|
||||
|
||||
/**
|
||||
* Camera pitch, upper limit. Range is '[0, Math.PI]' in radians.
|
||||
*
|
||||
* @type {number}
|
||||
* @default Math.PI
|
||||
*/
|
||||
this.maxPolarAngle = Math.PI;
|
||||
|
||||
/**
|
||||
* Multiplier for how much the pointer movement influences the camera rotation.
|
||||
*
|
||||
* @type {number}
|
||||
* @default 1
|
||||
*/
|
||||
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( this.domElement );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
connect( element ) {
|
||||
|
||||
super.connect( element );
|
||||
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the look direction of the camera.
|
||||
*
|
||||
* @param {Vector3} v - The target vector that is used to store the method's result.
|
||||
* @return {Vector3} The normalized direction vector.
|
||||
*/
|
||||
getDirection( v ) {
|
||||
|
||||
return v.set( 0, 0, - 1 ).applyQuaternion( this.object.quaternion );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the camera forward parallel to the xz-plane. Assumes camera.up is y-up.
|
||||
*
|
||||
* @param {number} distance - The signed distance.
|
||||
*/
|
||||
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 );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the camera sidewards parallel to the xz-plane.
|
||||
*
|
||||
* @param {number} distance - The signed distance.
|
||||
*/
|
||||
moveRight( distance ) {
|
||||
|
||||
if ( this.enabled === false ) return;
|
||||
|
||||
const camera = this.object;
|
||||
|
||||
_vector.setFromMatrixColumn( camera.matrix, 0 );
|
||||
|
||||
camera.position.addScaledVector( _vector, distance );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Activates the pointer lock.
|
||||
*
|
||||
* @param {boolean} [unadjustedMovement=false] - Disables OS-level adjustment for mouse acceleration, and accesses raw mouse input instead.
|
||||
* Setting it to true will disable mouse acceleration.
|
||||
*/
|
||||
lock( unadjustedMovement = false ) {
|
||||
|
||||
this.domElement.requestPointerLock( {
|
||||
unadjustedMovement
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Exits the pointer lock.
|
||||
*/
|
||||
unlock() {
|
||||
|
||||
this.domElement.ownerDocument.exitPointerLock();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// event listeners
|
||||
|
||||
function onMouseMove( event ) {
|
||||
|
||||
if ( this.enabled === false || this.isLocked === false ) return;
|
||||
|
||||
const camera = this.object;
|
||||
_euler.setFromQuaternion( camera.quaternion );
|
||||
|
||||
_euler.y -= event.movementX * _MOUSE_SENSITIVITY * this.pointerSpeed;
|
||||
_euler.x -= event.movementY * _MOUSE_SENSITIVITY * 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 };
|
||||
1001
node_modules/three/examples/jsm/controls/TrackballControls.js
generated
vendored
Normal file
1001
node_modules/three/examples/jsm/controls/TrackballControls.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1918
node_modules/three/examples/jsm/controls/TransformControls.js
generated
vendored
Normal file
1918
node_modules/three/examples/jsm/controls/TransformControls.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user