PointerLockControls.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import {
  2. Euler,
  3. EventDispatcher,
  4. Vector3
  5. } from 'three';
  6. const _euler = new Euler( 0, 0, 0, 'YXZ' );
  7. const _vector = new Vector3();
  8. const _changeEvent = { type: 'change' };
  9. const _lockEvent = { type: 'lock' };
  10. const _unlockEvent = { type: 'unlock' };
  11. const _PI_2 = Math.PI / 2;
  12. class PointerLockControls extends EventDispatcher {
  13. constructor( camera, domElement ) {
  14. super();
  15. this.domElement = domElement;
  16. this.isLocked = false;
  17. // Set to constrain the pitch of the camera
  18. // Range is 0 to Math.PI radians
  19. this.minPolarAngle = 0; // radians
  20. this.maxPolarAngle = Math.PI; // radians
  21. this.pointerSpeed = 1.0;
  22. const scope = this;
  23. function onMouseMove( event ) {
  24. if ( scope.isLocked === false ) return;
  25. const movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
  26. const movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
  27. _euler.setFromQuaternion( camera.quaternion );
  28. _euler.y -= movementX * 0.002 * scope.pointerSpeed;
  29. _euler.x -= movementY * 0.002 * scope.pointerSpeed;
  30. _euler.x = Math.max( _PI_2 - scope.maxPolarAngle, Math.min( _PI_2 - scope.minPolarAngle, _euler.x ) );
  31. camera.quaternion.setFromEuler( _euler );
  32. scope.dispatchEvent( _changeEvent );
  33. }
  34. function onPointerlockChange() {
  35. if ( scope.domElement.ownerDocument.pointerLockElement === scope.domElement ) {
  36. scope.dispatchEvent( _lockEvent );
  37. scope.isLocked = true;
  38. } else {
  39. scope.dispatchEvent( _unlockEvent );
  40. scope.isLocked = false;
  41. }
  42. }
  43. function onPointerlockError() {
  44. console.error( 'THREE.PointerLockControls: Unable to use Pointer Lock API' );
  45. }
  46. this.connect = function () {
  47. scope.domElement.ownerDocument.addEventListener( 'mousemove', onMouseMove );
  48. scope.domElement.ownerDocument.addEventListener( 'pointerlockchange', onPointerlockChange );
  49. scope.domElement.ownerDocument.addEventListener( 'pointerlockerror', onPointerlockError );
  50. };
  51. this.disconnect = function () {
  52. scope.domElement.ownerDocument.removeEventListener( 'mousemove', onMouseMove );
  53. scope.domElement.ownerDocument.removeEventListener( 'pointerlockchange', onPointerlockChange );
  54. scope.domElement.ownerDocument.removeEventListener( 'pointerlockerror', onPointerlockError );
  55. };
  56. this.dispose = function () {
  57. this.disconnect();
  58. };
  59. this.getObject = function () { // retaining this method for backward compatibility
  60. return camera;
  61. };
  62. this.getDirection = function () {
  63. const direction = new Vector3( 0, 0, - 1 );
  64. return function ( v ) {
  65. return v.copy( direction ).applyQuaternion( camera.quaternion );
  66. };
  67. }();
  68. this.moveForward = function ( distance ) {
  69. // move forward parallel to the xz-plane
  70. // assumes camera.up is y-up
  71. _vector.setFromMatrixColumn( camera.matrix, 0 );
  72. _vector.crossVectors( camera.up, _vector );
  73. camera.position.addScaledVector( _vector, distance );
  74. };
  75. this.moveRight = function ( distance ) {
  76. _vector.setFromMatrixColumn( camera.matrix, 0 );
  77. camera.position.addScaledVector( _vector, distance );
  78. };
  79. this.lock = function () {
  80. this.domElement.requestPointerLock();
  81. };
  82. this.unlock = function () {
  83. scope.domElement.ownerDocument.exitPointerLock();
  84. };
  85. this.connect();
  86. }
  87. }
  88. export { PointerLockControls };