FlyControls.js 6.9 KB

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