PointerLockControls.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author Mugen87 / https://github.com/Mugen87
  4. */
  5. import {
  6. Euler,
  7. EventDispatcher,
  8. Vector3
  9. } from "../../../build/three.module.js";
  10. var PointerLockControls = function ( camera, domElement ) {
  11. if ( domElement === undefined ) {
  12. console.warn( 'THREE.PointerLockControls: The second parameter "domElement" is now mandatory.' );
  13. domElement = document.body;
  14. }
  15. this.domElement = domElement;
  16. this.isLocked = false;
  17. //
  18. // internals
  19. //
  20. var scope = this;
  21. var changeEvent = { type: 'change' };
  22. var lockEvent = { type: 'lock' };
  23. var unlockEvent = { type: 'unlock' };
  24. var euler = new Euler( 0, 0, 0, 'YXZ' );
  25. var PI_2 = Math.PI / 2;
  26. var vec = new Vector3();
  27. function onMouseMove( event ) {
  28. if ( scope.isLocked === false ) return;
  29. var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
  30. var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
  31. euler.setFromQuaternion( camera.quaternion );
  32. euler.y -= movementX * 0.002;
  33. euler.x -= movementY * 0.002;
  34. euler.x = Math.max( - PI_2, Math.min( PI_2, euler.x ) );
  35. camera.quaternion.setFromEuler( euler );
  36. scope.dispatchEvent( changeEvent );
  37. }
  38. function onPointerlockChange() {
  39. if ( document.pointerLockElement === scope.domElement ) {
  40. scope.dispatchEvent( lockEvent );
  41. scope.isLocked = true;
  42. } else {
  43. scope.dispatchEvent( unlockEvent );
  44. scope.isLocked = false;
  45. }
  46. }
  47. function onPointerlockError() {
  48. console.error( 'THREE.PointerLockControls: Unable to use Pointer Lock API' );
  49. }
  50. this.connect = function () {
  51. document.addEventListener( 'mousemove', onMouseMove, false );
  52. document.addEventListener( 'pointerlockchange', onPointerlockChange, false );
  53. document.addEventListener( 'pointerlockerror', onPointerlockError, false );
  54. };
  55. this.disconnect = function () {
  56. document.removeEventListener( 'mousemove', onMouseMove, false );
  57. document.removeEventListener( 'pointerlockchange', onPointerlockChange, false );
  58. document.removeEventListener( 'pointerlockerror', onPointerlockError, false );
  59. };
  60. this.dispose = function () {
  61. this.disconnect();
  62. };
  63. this.getObject = function () { // retaining this method for backward compatibility
  64. return camera;
  65. };
  66. this.getDirection = function () {
  67. var direction = new Vector3( 0, 0, - 1 );
  68. return function ( v ) {
  69. return v.copy( direction ).applyQuaternion( camera.quaternion );
  70. };
  71. }();
  72. this.moveForward = function ( distance ) {
  73. // move forward parallel to the xz-plane
  74. // assumes camera.up is y-up
  75. vec.setFromMatrixColumn( camera.matrix, 0 );
  76. vec.crossVectors( camera.up, vec );
  77. camera.position.addScaledVector( vec, distance );
  78. };
  79. this.moveRight = function ( distance ) {
  80. vec.setFromMatrixColumn( camera.matrix, 0 );
  81. camera.position.addScaledVector( vec, distance );
  82. };
  83. this.lock = function () {
  84. this.domElement.requestPointerLock();
  85. };
  86. this.unlock = function () {
  87. document.exitPointerLock();
  88. };
  89. this.connect();
  90. };
  91. PointerLockControls.prototype = Object.create( EventDispatcher.prototype );
  92. PointerLockControls.prototype.constructor = PointerLockControls;
  93. export { PointerLockControls };