PointerLockControls.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import {
  2. Controls,
  3. Euler,
  4. Vector3
  5. } from 'three';
  6. const _euler = new Euler( 0, 0, 0, 'YXZ' );
  7. const _vector = new Vector3();
  8. /**
  9. * Fires when the user moves the mouse.
  10. *
  11. * @event PointerLockControls#change
  12. * @type {Object}
  13. */
  14. const _changeEvent = { type: 'change' };
  15. /**
  16. * Fires when the pointer lock status is "locked" (in other words: the mouse is captured).
  17. *
  18. * @event PointerLockControls#lock
  19. * @type {Object}
  20. */
  21. const _lockEvent = { type: 'lock' };
  22. /**
  23. * Fires when the pointer lock status is "unlocked" (in other words: the mouse is not captured anymore).
  24. *
  25. * @event PointerLockControls#unlock
  26. * @type {Object}
  27. */
  28. const _unlockEvent = { type: 'unlock' };
  29. const _PI_2 = Math.PI / 2;
  30. /**
  31. * The implementation of this class is based on the [Pointer Lock API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Pointer_Lock_API}.
  32. * `PointerLockControls` is a perfect choice for first person 3D games.
  33. *
  34. * ```js
  35. * const controls = new PointerLockControls( camera, document.body );
  36. *
  37. * // add event listener to show/hide a UI (e.g. the game's menu)
  38. * controls.addEventListener( 'lock', function () {
  39. *
  40. * menu.style.display = 'none';
  41. *
  42. * } );
  43. *
  44. * controls.addEventListener( 'unlock', function () {
  45. *
  46. * menu.style.display = 'block';
  47. *
  48. * } );
  49. * ```
  50. *
  51. * @augments Controls
  52. * @three_import import { PointerLockControls } from 'three/addons/controls/PointerLockControls.js';
  53. */
  54. class PointerLockControls extends Controls {
  55. /**
  56. * Constructs a new controls instance.
  57. *
  58. * @param {Camera} camera - The camera that is managed by the controls.
  59. * @param {?HTMLDOMElement} domElement - The HTML element used for event listeners.
  60. */
  61. constructor( camera, domElement = null ) {
  62. super( camera, domElement );
  63. /**
  64. * Whether the controls are locked or not.
  65. *
  66. * @type {boolean}
  67. * @readonly
  68. * @default false
  69. */
  70. this.isLocked = false;
  71. /**
  72. * Camera pitch, lower limit. Range is '[0, Math.PI]' in radians.
  73. *
  74. * @type {number}
  75. * @default 0
  76. */
  77. this.minPolarAngle = 0;
  78. /**
  79. * Camera pitch, upper limit. Range is '[0, Math.PI]' in radians.
  80. *
  81. * @type {number}
  82. * @default Math.PI
  83. */
  84. this.maxPolarAngle = Math.PI;
  85. /**
  86. * Multiplier for how much the pointer movement influences the camera rotation.
  87. *
  88. * @type {number}
  89. * @default 1
  90. */
  91. this.pointerSpeed = 1.0;
  92. // event listeners
  93. this._onMouseMove = onMouseMove.bind( this );
  94. this._onPointerlockChange = onPointerlockChange.bind( this );
  95. this._onPointerlockError = onPointerlockError.bind( this );
  96. if ( this.domElement !== null ) {
  97. this.connect( this.domElement );
  98. }
  99. }
  100. connect( element ) {
  101. super.connect( element );
  102. this.domElement.ownerDocument.addEventListener( 'mousemove', this._onMouseMove );
  103. this.domElement.ownerDocument.addEventListener( 'pointerlockchange', this._onPointerlockChange );
  104. this.domElement.ownerDocument.addEventListener( 'pointerlockerror', this._onPointerlockError );
  105. }
  106. disconnect() {
  107. this.domElement.ownerDocument.removeEventListener( 'mousemove', this._onMouseMove );
  108. this.domElement.ownerDocument.removeEventListener( 'pointerlockchange', this._onPointerlockChange );
  109. this.domElement.ownerDocument.removeEventListener( 'pointerlockerror', this._onPointerlockError );
  110. }
  111. dispose() {
  112. this.disconnect();
  113. }
  114. getObject() {
  115. console.warn( 'THREE.PointerLockControls: getObject() has been deprecated. Use controls.object instead.' ); // @deprecated r169
  116. return this.object;
  117. }
  118. /**
  119. * Returns the look direction of the camera.
  120. *
  121. * @param {Vector3} v - The target vector that is used to store the method's result.
  122. * @return {Vector3} The normalized direction vector.
  123. */
  124. getDirection( v ) {
  125. return v.set( 0, 0, - 1 ).applyQuaternion( this.object.quaternion );
  126. }
  127. /**
  128. * Moves the camera forward parallel to the xz-plane. Assumes camera.up is y-up.
  129. *
  130. * @param {number} distance - The signed distance.
  131. */
  132. moveForward( distance ) {
  133. if ( this.enabled === false ) return;
  134. // move forward parallel to the xz-plane
  135. // assumes camera.up is y-up
  136. const camera = this.object;
  137. _vector.setFromMatrixColumn( camera.matrix, 0 );
  138. _vector.crossVectors( camera.up, _vector );
  139. camera.position.addScaledVector( _vector, distance );
  140. }
  141. /**
  142. * Moves the camera sidewards parallel to the xz-plane.
  143. *
  144. * @param {number} distance - The signed distance.
  145. */
  146. moveRight( distance ) {
  147. if ( this.enabled === false ) return;
  148. const camera = this.object;
  149. _vector.setFromMatrixColumn( camera.matrix, 0 );
  150. camera.position.addScaledVector( _vector, distance );
  151. }
  152. /**
  153. * Activates the pointer lock.
  154. *
  155. * @param {boolean} [unadjustedMovement=false] - Disables OS-level adjustment for mouse acceleration, and accesses raw mouse input instead.
  156. * Setting it to true will disable mouse acceleration.
  157. */
  158. lock( unadjustedMovement = false ) {
  159. this.domElement.requestPointerLock( {
  160. unadjustedMovement
  161. } );
  162. }
  163. /**
  164. * Exits the pointer lock.
  165. */
  166. unlock() {
  167. this.domElement.ownerDocument.exitPointerLock();
  168. }
  169. }
  170. // event listeners
  171. function onMouseMove( event ) {
  172. if ( this.enabled === false || this.isLocked === false ) return;
  173. const camera = this.object;
  174. _euler.setFromQuaternion( camera.quaternion );
  175. _euler.y -= event.movementX * 0.002 * this.pointerSpeed;
  176. _euler.x -= event.movementY * 0.002 * this.pointerSpeed;
  177. _euler.x = Math.max( _PI_2 - this.maxPolarAngle, Math.min( _PI_2 - this.minPolarAngle, _euler.x ) );
  178. camera.quaternion.setFromEuler( _euler );
  179. this.dispatchEvent( _changeEvent );
  180. }
  181. function onPointerlockChange() {
  182. if ( this.domElement.ownerDocument.pointerLockElement === this.domElement ) {
  183. this.dispatchEvent( _lockEvent );
  184. this.isLocked = true;
  185. } else {
  186. this.dispatchEvent( _unlockEvent );
  187. this.isLocked = false;
  188. }
  189. }
  190. function onPointerlockError() {
  191. console.error( 'THREE.PointerLockControls: Unable to use Pointer Lock API' );
  192. }
  193. export { PointerLockControls };
粤ICP备19079148号