PointerLockControls.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. console.warn( "THREE.PointerLockControls: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. THREE.PointerLockControls = function ( camera, domElement ) {
  3. if ( domElement === undefined ) {
  4. console.warn( 'THREE.PointerLockControls: The second parameter "domElement" is now mandatory.' );
  5. domElement = document.body;
  6. }
  7. this.domElement = domElement;
  8. this.isLocked = false;
  9. // Set to constrain the pitch of the camera
  10. // Range is 0 to Math.PI radians
  11. this.minPolarAngle = 0; // radians
  12. this.maxPolarAngle = Math.PI; // radians
  13. //
  14. // internals
  15. //
  16. var scope = this;
  17. var changeEvent = { type: 'change' };
  18. var lockEvent = { type: 'lock' };
  19. var unlockEvent = { type: 'unlock' };
  20. var euler = new THREE.Euler( 0, 0, 0, 'YXZ' );
  21. var PI_2 = Math.PI / 2;
  22. var vec = new THREE.Vector3();
  23. function onMouseMove( event ) {
  24. if ( scope.isLocked === false ) return;
  25. var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
  26. var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
  27. euler.setFromQuaternion( camera.quaternion );
  28. euler.y -= movementX * 0.002;
  29. euler.x -= movementY * 0.002;
  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, false );
  48. scope.domElement.ownerDocument.addEventListener( 'pointerlockchange', onPointerlockChange, false );
  49. scope.domElement.ownerDocument.addEventListener( 'pointerlockerror', onPointerlockError, false );
  50. };
  51. this.disconnect = function () {
  52. scope.domElement.ownerDocument.removeEventListener( 'mousemove', onMouseMove, false );
  53. scope.domElement.ownerDocument.removeEventListener( 'pointerlockchange', onPointerlockChange, false );
  54. scope.domElement.ownerDocument.removeEventListener( 'pointerlockerror', onPointerlockError, false );
  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. var direction = new THREE.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. vec.setFromMatrixColumn( camera.matrix, 0 );
  72. vec.crossVectors( camera.up, vec );
  73. camera.position.addScaledVector( vec, distance );
  74. };
  75. this.moveRight = function ( distance ) {
  76. vec.setFromMatrixColumn( camera.matrix, 0 );
  77. camera.position.addScaledVector( vec, 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. THREE.PointerLockControls.prototype = Object.create( THREE.EventDispatcher.prototype );
  88. THREE.PointerLockControls.prototype.constructor = THREE.PointerLockControls;