PointerLockControls.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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( this.domElement );
  97. }
  98. }
  99. connect( element ) {
  100. super.connect( element );
  101. this.domElement.ownerDocument.addEventListener( 'mousemove', this._onMouseMove );
  102. this.domElement.ownerDocument.addEventListener( 'pointerlockchange', this._onPointerlockChange );
  103. this.domElement.ownerDocument.addEventListener( 'pointerlockerror', this._onPointerlockError );
  104. }
  105. disconnect() {
  106. this.domElement.ownerDocument.removeEventListener( 'mousemove', this._onMouseMove );
  107. this.domElement.ownerDocument.removeEventListener( 'pointerlockchange', this._onPointerlockChange );
  108. this.domElement.ownerDocument.removeEventListener( 'pointerlockerror', this._onPointerlockError );
  109. }
  110. dispose() {
  111. this.disconnect();
  112. }
  113. getObject() {
  114. console.warn( 'THREE.PointerLockControls: getObject() has been deprecated. Use controls.object instead.' ); // @deprecated r169
  115. return this.object;
  116. }
  117. /**
  118. * Returns the look direction of the camera.
  119. *
  120. * @param {Vector3} v - The target vector that is used to store the method's result.
  121. * @return {Vector3} The normalized direction vector.
  122. */
  123. getDirection( v ) {
  124. return v.set( 0, 0, - 1 ).applyQuaternion( this.object.quaternion );
  125. }
  126. /**
  127. * Moves the camera forward parallel to the xz-plane. Assumes camera.up is y-up.
  128. *
  129. * @param {number} distance - The signed distance.
  130. */
  131. moveForward( distance ) {
  132. if ( this.enabled === false ) return;
  133. // move forward parallel to the xz-plane
  134. // assumes camera.up is y-up
  135. const camera = this.object;
  136. _vector.setFromMatrixColumn( camera.matrix, 0 );
  137. _vector.crossVectors( camera.up, _vector );
  138. camera.position.addScaledVector( _vector, distance );
  139. }
  140. /**
  141. * Moves the camera sidewards parallel to the xz-plane.
  142. *
  143. * @param {number} distance - The signed distance.
  144. */
  145. moveRight( distance ) {
  146. if ( this.enabled === false ) return;
  147. const camera = this.object;
  148. _vector.setFromMatrixColumn( camera.matrix, 0 );
  149. camera.position.addScaledVector( _vector, distance );
  150. }
  151. /**
  152. * Activates the pointer lock.
  153. *
  154. * @param {boolean} [unadjustedMovement=false] - Disables OS-level adjustment for mouse acceleration, and accesses raw mouse input instead.
  155. * Setting it to true will disable mouse acceleration.
  156. */
  157. lock( unadjustedMovement = false ) {
  158. this.domElement.requestPointerLock( {
  159. unadjustedMovement
  160. } );
  161. }
  162. /**
  163. * Exits the pointer lock.
  164. */
  165. unlock() {
  166. this.domElement.ownerDocument.exitPointerLock();
  167. }
  168. }
  169. // event listeners
  170. function onMouseMove( event ) {
  171. if ( this.enabled === false || this.isLocked === false ) return;
  172. const camera = this.object;
  173. _euler.setFromQuaternion( camera.quaternion );
  174. _euler.y -= event.movementX * 0.002 * this.pointerSpeed;
  175. _euler.x -= event.movementY * 0.002 * this.pointerSpeed;
  176. _euler.x = Math.max( _PI_2 - this.maxPolarAngle, Math.min( _PI_2 - this.minPolarAngle, _euler.x ) );
  177. camera.quaternion.setFromEuler( _euler );
  178. this.dispatchEvent( _changeEvent );
  179. }
  180. function onPointerlockChange() {
  181. if ( this.domElement.ownerDocument.pointerLockElement === this.domElement ) {
  182. this.dispatchEvent( _lockEvent );
  183. this.isLocked = true;
  184. } else {
  185. this.dispatchEvent( _unlockEvent );
  186. this.isLocked = false;
  187. }
  188. }
  189. function onPointerlockError() {
  190. console.error( 'THREE.PointerLockControls: Unable to use Pointer Lock API' );
  191. }
  192. export { PointerLockControls };
粤ICP备19079148号