PointerLockControls.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. ( function () {
  2. const _euler = new THREE.Euler( 0, 0, 0, 'YXZ' );
  3. const _vector = new THREE.Vector3();
  4. const _changeEvent = {
  5. type: 'change'
  6. };
  7. const _lockEvent = {
  8. type: 'lock'
  9. };
  10. const _unlockEvent = {
  11. type: 'unlock'
  12. };
  13. const _PI_2 = Math.PI / 2;
  14. class PointerLockControls extends THREE.EventDispatcher {
  15. constructor( camera, domElement ) {
  16. super();
  17. if ( domElement === undefined ) {
  18. console.warn( 'THREE.PointerLockControls: The second parameter "domElement" is now mandatory.' );
  19. domElement = document.body;
  20. }
  21. this.domElement = domElement;
  22. this.isLocked = false; // Set to constrain the pitch of the camera
  23. // Range is 0 to Math.PI radians
  24. this.minPolarAngle = 0; // radians
  25. this.maxPolarAngle = Math.PI; // radians
  26. const scope = this;
  27. function onMouseMove( event ) {
  28. if ( scope.isLocked === false ) return;
  29. const movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
  30. const 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 - scope.maxPolarAngle, Math.min( _PI_2 - scope.minPolarAngle, _euler.x ) );
  35. camera.quaternion.setFromEuler( _euler );
  36. scope.dispatchEvent( _changeEvent );
  37. }
  38. function onPointerlockChange() {
  39. if ( scope.domElement.ownerDocument.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. scope.domElement.ownerDocument.addEventListener( 'mousemove', onMouseMove );
  52. scope.domElement.ownerDocument.addEventListener( 'pointerlockchange', onPointerlockChange );
  53. scope.domElement.ownerDocument.addEventListener( 'pointerlockerror', onPointerlockError );
  54. };
  55. this.disconnect = function () {
  56. scope.domElement.ownerDocument.removeEventListener( 'mousemove', onMouseMove );
  57. scope.domElement.ownerDocument.removeEventListener( 'pointerlockchange', onPointerlockChange );
  58. scope.domElement.ownerDocument.removeEventListener( 'pointerlockerror', onPointerlockError );
  59. };
  60. this.dispose = function () {
  61. this.disconnect();
  62. };
  63. this.getObject = function () {
  64. // retaining this method for backward compatibility
  65. return camera;
  66. };
  67. this.getDirection = function () {
  68. const direction = new THREE.Vector3( 0, 0, - 1 );
  69. return function ( v ) {
  70. return v.copy( direction ).applyQuaternion( camera.quaternion );
  71. };
  72. }();
  73. this.moveForward = function ( distance ) {
  74. // move forward parallel to the xz-plane
  75. // assumes camera.up is y-up
  76. _vector.setFromMatrixColumn( camera.matrix, 0 );
  77. _vector.crossVectors( camera.up, _vector );
  78. camera.position.addScaledVector( _vector, distance );
  79. };
  80. this.moveRight = function ( distance ) {
  81. _vector.setFromMatrixColumn( camera.matrix, 0 );
  82. camera.position.addScaledVector( _vector, distance );
  83. };
  84. this.lock = function () {
  85. this.domElement.requestPointerLock();
  86. };
  87. this.unlock = function () {
  88. scope.domElement.ownerDocument.exitPointerLock();
  89. };
  90. this.connect();
  91. }
  92. }
  93. THREE.PointerLockControls = PointerLockControls;
  94. } )();