PointerLockControls.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. */
  53. class PointerLockControls extends Controls {
  54. /**
  55. * Constructs a new controls instance.
  56. *
  57. * @param {Camera} camera - The camera that is managed by the controls.
  58. * @param {?HTMLDOMElement} domElement - The HTML element used for event listeners.
  59. */
  60. constructor( camera, domElement = null ) {
  61. super( camera, domElement );
  62. /**
  63. * Whether the controls are locked or not.
  64. *
  65. * @type {boolean}
  66. * @readonly
  67. * @default false
  68. */
  69. this.isLocked = false;
  70. /**
  71. * Camera pitch, lower limit. Range is '[0, Math.PI]' in radians.
  72. *
  73. * @type {number}
  74. * @default 0
  75. */
  76. this.minPolarAngle = 0;
  77. /**
  78. * Camera pitch, upper limit. Range is '[0, Math.PI]' in radians.
  79. *
  80. * @type {number}
  81. * @default Math.PI
  82. */
  83. this.maxPolarAngle = Math.PI;
  84. /**
  85. * Multiplier for how much the pointer movement influences the camera rotation.
  86. *
  87. * @type {number}
  88. * @default 1
  89. */
  90. this.pointerSpeed = 1.0;
  91. // event listeners
  92. this._onMouseMove = onMouseMove.bind( this );
  93. this._onPointerlockChange = onPointerlockChange.bind( this );
  94. this._onPointerlockError = onPointerlockError.bind( this );
  95. if ( this.domElement !== null ) {
  96. this.connect();
  97. }
  98. }
  99. connect() {
  100. this.domElement.ownerDocument.addEventListener( 'mousemove', this._onMouseMove );
  101. this.domElement.ownerDocument.addEventListener( 'pointerlockchange', this._onPointerlockChange );
  102. this.domElement.ownerDocument.addEventListener( 'pointerlockerror', this._onPointerlockError );
  103. }
  104. disconnect() {
  105. this.domElement.ownerDocument.removeEventListener( 'mousemove', this._onMouseMove );
  106. this.domElement.ownerDocument.removeEventListener( 'pointerlockchange', this._onPointerlockChange );
  107. this.domElement.ownerDocument.removeEventListener( 'pointerlockerror', this._onPointerlockError );
  108. }
  109. dispose() {
  110. this.disconnect();
  111. }
  112. getObject() {
  113. console.warn( 'THREE.PointerLockControls: getObject() has been deprecated. Use controls.object instead.' ); // @deprecated r169
  114. return this.object;
  115. }
  116. /**
  117. * Returns the look direction of the camera.
  118. *
  119. * @param {Vector3} v - The target vector that is used to store the method's result.
  120. * @return {Vector3} The normalized direction vector.
  121. */
  122. getDirection( v ) {
  123. return v.set( 0, 0, - 1 ).applyQuaternion( this.object.quaternion );
  124. }
  125. /**
  126. * Moves the camera forward parallel to the xz-plane. Assumes camera.up is y-up.
  127. *
  128. * @param {number} distance - The signed distance.
  129. */
  130. moveForward( distance ) {
  131. if ( this.enabled === false ) return;
  132. // move forward parallel to the xz-plane
  133. // assumes camera.up is y-up
  134. const camera = this.object;
  135. _vector.setFromMatrixColumn( camera.matrix, 0 );
  136. _vector.crossVectors( camera.up, _vector );
  137. camera.position.addScaledVector( _vector, distance );
  138. }
  139. /**
  140. * Moves the camera sidewards parallel to the xz-plane.
  141. *
  142. * @param {number} distance - The signed distance.
  143. */
  144. moveRight( distance ) {
  145. if ( this.enabled === false ) return;
  146. const camera = this.object;
  147. _vector.setFromMatrixColumn( camera.matrix, 0 );
  148. camera.position.addScaledVector( _vector, distance );
  149. }
  150. /**
  151. * Activates the pointer lock.
  152. */
  153. lock() {
  154. this.domElement.requestPointerLock();
  155. }
  156. /**
  157. * Exits the pointer lock.
  158. */
  159. unlock() {
  160. this.domElement.ownerDocument.exitPointerLock();
  161. }
  162. }
  163. // event listeners
  164. function onMouseMove( event ) {
  165. if ( this.enabled === false || this.isLocked === false ) return;
  166. const camera = this.object;
  167. _euler.setFromQuaternion( camera.quaternion );
  168. _euler.y -= event.movementX * 0.002 * this.pointerSpeed;
  169. _euler.x -= event.movementY * 0.002 * this.pointerSpeed;
  170. _euler.x = Math.max( _PI_2 - this.maxPolarAngle, Math.min( _PI_2 - this.minPolarAngle, _euler.x ) );
  171. camera.quaternion.setFromEuler( _euler );
  172. this.dispatchEvent( _changeEvent );
  173. }
  174. function onPointerlockChange() {
  175. if ( this.domElement.ownerDocument.pointerLockElement === this.domElement ) {
  176. this.dispatchEvent( _lockEvent );
  177. this.isLocked = true;
  178. } else {
  179. this.dispatchEvent( _unlockEvent );
  180. this.isLocked = false;
  181. }
  182. }
  183. function onPointerlockError() {
  184. console.error( 'THREE.PointerLockControls: Unable to use Pointer Lock API' );
  185. }
  186. export { PointerLockControls };
粤ICP备19079148号