PointerLockControls.js 5.8 KB

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