FlyControls.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import {
  2. EventDispatcher,
  3. Quaternion,
  4. Vector3
  5. } from '../../../build/three.module.js';
  6. const _changeEvent = { type: 'change' };
  7. class FlyControls extends EventDispatcher {
  8. constructor( object, domElement ) {
  9. super();
  10. if ( domElement === undefined ) {
  11. console.warn( 'THREE.FlyControls: The second parameter "domElement" is now mandatory.' );
  12. domElement = document;
  13. }
  14. this.object = object;
  15. this.domElement = domElement;
  16. // API
  17. this.movementSpeed = 1.0;
  18. this.rollSpeed = 0.005;
  19. this.dragToLook = false;
  20. this.autoForward = false;
  21. // disable default target object behavior
  22. // internals
  23. const scope = this;
  24. const EPS = 0.000001;
  25. const lastQuaternion = new Quaternion();
  26. const lastPosition = new Vector3();
  27. this.tmpQuaternion = new Quaternion();
  28. this.mouseStatus = 0;
  29. this.moveState = { up: 0, down: 0, left: 0, right: 0, forward: 0, back: 0, pitchUp: 0, pitchDown: 0, yawLeft: 0, yawRight: 0, rollLeft: 0, rollRight: 0 };
  30. this.moveVector = new Vector3( 0, 0, 0 );
  31. this.rotationVector = new Vector3( 0, 0, 0 );
  32. this.keydown = function ( event ) {
  33. if ( event.altKey ) {
  34. return;
  35. }
  36. switch ( event.code ) {
  37. case 'ShiftLeft':
  38. case 'ShiftRight': this.movementSpeedMultiplier = .1; break;
  39. case 'KeyW': this.moveState.forward = 1; break;
  40. case 'KeyS': this.moveState.back = 1; break;
  41. case 'KeyA': this.moveState.left = 1; break;
  42. case 'KeyD': this.moveState.right = 1; break;
  43. case 'KeyR': this.moveState.up = 1; break;
  44. case 'KeyF': this.moveState.down = 1; break;
  45. case 'ArrowUp': this.moveState.pitchUp = 1; break;
  46. case 'ArrowDown': this.moveState.pitchDown = 1; break;
  47. case 'ArrowLeft': this.moveState.yawLeft = 1; break;
  48. case 'ArrowRight': this.moveState.yawRight = 1; break;
  49. case 'KeyQ': this.moveState.rollLeft = 1; break;
  50. case 'KeyE': this.moveState.rollRight = 1; break;
  51. }
  52. this.updateMovementVector();
  53. this.updateRotationVector();
  54. };
  55. this.keyup = function ( event ) {
  56. switch ( event.code ) {
  57. case 'ShiftLeft':
  58. case 'ShiftRight': this.movementSpeedMultiplier = 1; break;
  59. case 'KeyW': this.moveState.forward = 0; break;
  60. case 'KeyS': this.moveState.back = 0; break;
  61. case 'KeyA': this.moveState.left = 0; break;
  62. case 'KeyD': this.moveState.right = 0; break;
  63. case 'KeyR': this.moveState.up = 0; break;
  64. case 'KeyF': this.moveState.down = 0; break;
  65. case 'ArrowUp': this.moveState.pitchUp = 0; break;
  66. case 'ArrowDown': this.moveState.pitchDown = 0; break;
  67. case 'ArrowLeft': this.moveState.yawLeft = 0; break;
  68. case 'ArrowRight': this.moveState.yawRight = 0; break;
  69. case 'KeyQ': this.moveState.rollLeft = 0; break;
  70. case 'KeyE': this.moveState.rollRight = 0; break;
  71. }
  72. this.updateMovementVector();
  73. this.updateRotationVector();
  74. };
  75. this.mousedown = function ( event ) {
  76. if ( this.dragToLook ) {
  77. this.mouseStatus ++;
  78. } else {
  79. switch ( event.button ) {
  80. case 0: this.moveState.forward = 1; break;
  81. case 2: this.moveState.back = 1; break;
  82. }
  83. this.updateMovementVector();
  84. }
  85. };
  86. this.mousemove = function ( event ) {
  87. if ( ! this.dragToLook || this.mouseStatus > 0 ) {
  88. const container = this.getContainerDimensions();
  89. const halfWidth = container.size[ 0 ] / 2;
  90. const halfHeight = container.size[ 1 ] / 2;
  91. this.moveState.yawLeft = - ( ( event.pageX - container.offset[ 0 ] ) - halfWidth ) / halfWidth;
  92. this.moveState.pitchDown = ( ( event.pageY - container.offset[ 1 ] ) - halfHeight ) / halfHeight;
  93. this.updateRotationVector();
  94. }
  95. };
  96. this.mouseup = function ( event ) {
  97. if ( this.dragToLook ) {
  98. this.mouseStatus --;
  99. this.moveState.yawLeft = this.moveState.pitchDown = 0;
  100. } else {
  101. switch ( event.button ) {
  102. case 0: this.moveState.forward = 0; break;
  103. case 2: this.moveState.back = 0; break;
  104. }
  105. this.updateMovementVector();
  106. }
  107. this.updateRotationVector();
  108. };
  109. this.update = function ( delta ) {
  110. const moveMult = delta * scope.movementSpeed;
  111. const rotMult = delta * scope.rollSpeed;
  112. scope.object.translateX( scope.moveVector.x * moveMult );
  113. scope.object.translateY( scope.moveVector.y * moveMult );
  114. scope.object.translateZ( scope.moveVector.z * moveMult );
  115. scope.tmpQuaternion.set( scope.rotationVector.x * rotMult, scope.rotationVector.y * rotMult, scope.rotationVector.z * rotMult, 1 ).normalize();
  116. scope.object.quaternion.multiply( scope.tmpQuaternion );
  117. if (
  118. lastPosition.distanceToSquared( scope.object.position ) > EPS ||
  119. 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS
  120. ) {
  121. scope.dispatchEvent( _changeEvent );
  122. lastQuaternion.copy( scope.object.quaternion );
  123. lastPosition.copy( scope.object.position );
  124. }
  125. };
  126. this.updateMovementVector = function () {
  127. const forward = ( this.moveState.forward || ( this.autoForward && ! this.moveState.back ) ) ? 1 : 0;
  128. this.moveVector.x = ( - this.moveState.left + this.moveState.right );
  129. this.moveVector.y = ( - this.moveState.down + this.moveState.up );
  130. this.moveVector.z = ( - forward + this.moveState.back );
  131. //console.log( 'move:', [ this.moveVector.x, this.moveVector.y, this.moveVector.z ] );
  132. };
  133. this.updateRotationVector = function () {
  134. this.rotationVector.x = ( - this.moveState.pitchDown + this.moveState.pitchUp );
  135. this.rotationVector.y = ( - this.moveState.yawRight + this.moveState.yawLeft );
  136. this.rotationVector.z = ( - this.moveState.rollRight + this.moveState.rollLeft );
  137. //console.log( 'rotate:', [ this.rotationVector.x, this.rotationVector.y, this.rotationVector.z ] );
  138. };
  139. this.getContainerDimensions = function () {
  140. if ( this.domElement != document ) {
  141. return {
  142. size: [ this.domElement.offsetWidth, this.domElement.offsetHeight ],
  143. offset: [ this.domElement.offsetLeft, this.domElement.offsetTop ]
  144. };
  145. } else {
  146. return {
  147. size: [ window.innerWidth, window.innerHeight ],
  148. offset: [ 0, 0 ]
  149. };
  150. }
  151. };
  152. this.dispose = function () {
  153. this.domElement.removeEventListener( 'contextmenu', contextmenu );
  154. this.domElement.removeEventListener( 'mousedown', _mousedown );
  155. this.domElement.removeEventListener( 'mousemove', _mousemove );
  156. this.domElement.removeEventListener( 'mouseup', _mouseup );
  157. window.removeEventListener( 'keydown', _keydown );
  158. window.removeEventListener( 'keyup', _keyup );
  159. };
  160. const _mousemove = this.mousemove.bind( this );
  161. const _mousedown = this.mousedown.bind( this );
  162. const _mouseup = this.mouseup.bind( this );
  163. const _keydown = this.keydown.bind( this );
  164. const _keyup = this.keyup.bind( this );
  165. this.domElement.addEventListener( 'contextmenu', contextmenu );
  166. this.domElement.addEventListener( 'mousemove', _mousemove );
  167. this.domElement.addEventListener( 'mousedown', _mousedown );
  168. this.domElement.addEventListener( 'mouseup', _mouseup );
  169. window.addEventListener( 'keydown', _keydown );
  170. window.addEventListener( 'keyup', _keyup );
  171. this.updateMovementVector();
  172. this.updateRotationVector();
  173. }
  174. }
  175. function contextmenu( event ) {
  176. event.preventDefault();
  177. }
  178. export { FlyControls };