FlyControls.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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.mouseStatus = 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. console.log("keydown:",keydown)
  33. switch ( event.code ) {
  34. case 'ShiftLeft':
  35. case 'ShiftRight': this.movementSpeedMultiplier = .1; break;
  36. case 'KeyW': this.moveState.forward = 1; break;
  37. case 'KeyS': this.moveState.back = 1; break;
  38. case 'KeyA': this.moveState.left = 1; break;
  39. case 'KeyD': this.moveState.right = 1; break;
  40. case 'KeyR': this.moveState.up = 1; break;
  41. case 'KeyF': this.moveState.down = 1; break;
  42. case 'ArrowUp': this.moveState.pitchUp = 1; break;
  43. case 'ArrowDown': this.moveState.pitchDown = 1; break;
  44. case 'ArrowLeft': this.moveState.yawLeft = 1; break;
  45. case 'ArrowRight': this.moveState.yawRight = 1; break;
  46. case 'KeyQ': this.moveState.rollLeft = 1; break;
  47. case 'KeyE': this.moveState.rollRight = 1; break;
  48. }
  49. this.updateMovementVector();
  50. this.updateRotationVector();
  51. };
  52. this.keyup = function ( event ) {
  53. console.log("keyup:",keyup)
  54. switch ( event.code ) {
  55. case 'ShiftLeft':
  56. case 'ShiftRight': this.movementSpeedMultiplier = 1; break;
  57. case 'KeyW': this.moveState.forward = 0; break;
  58. case 'KeyS': this.moveState.back = 0; break;
  59. case 'KeyA': this.moveState.left = 0; break;
  60. case 'KeyD': this.moveState.right = 0; break;
  61. case 'KeyR': this.moveState.up = 0; break;
  62. case 'KeyF': this.moveState.down = 0; break;
  63. case 'ArrowUp': this.moveState.pitchUp = 0; break;
  64. case 'ArrowDown': this.moveState.pitchDown = 0; break;
  65. case 'ArrowLeft': this.moveState.yawLeft = 0; break;
  66. case 'ArrowRight': this.moveState.yawRight = 0; break;
  67. case 'KeyQ': this.moveState.rollLeft = 0; break;
  68. case 'KeyE': this.moveState.rollRight = 0; break;
  69. }
  70. this.updateMovementVector();
  71. this.updateRotationVector();
  72. };
  73. this.mousedown = function ( event ) {
  74. if ( this.dragToLook ) {
  75. this.mouseStatus ++;
  76. } else {
  77. switch ( event.button ) {
  78. case 0: this.moveState.forward = 1; break;
  79. case 2: this.moveState.back = 1; break;
  80. }
  81. this.updateMovementVector();
  82. }
  83. };
  84. this.mousemove = function ( event ) {
  85. if ( ! this.dragToLook || this.mouseStatus > 0 ) {
  86. const container = this.getContainerDimensions();
  87. const halfWidth = container.size[ 0 ] / 2;
  88. const halfHeight = container.size[ 1 ] / 2;
  89. this.moveState.yawLeft = - ( ( event.pageX - container.offset[ 0 ] ) - halfWidth ) / halfWidth;
  90. this.moveState.pitchDown = ( ( event.pageY - container.offset[ 1 ] ) - halfHeight ) / halfHeight;
  91. this.updateRotationVector();
  92. }
  93. };
  94. this.mouseup = function ( event ) {
  95. if ( this.dragToLook ) {
  96. this.mouseStatus --;
  97. this.moveState.yawLeft = this.moveState.pitchDown = 0;
  98. } else {
  99. switch ( event.button ) {
  100. case 0: this.moveState.forward = 0; break;
  101. case 2: this.moveState.back = 0; break;
  102. }
  103. this.updateMovementVector();
  104. }
  105. this.updateRotationVector();
  106. };
  107. this.update = function ( delta ) {
  108. const moveMult = delta * scope.movementSpeed;
  109. const rotMult = delta * scope.rollSpeed;
  110. scope.object.translateX( scope.moveVector.x * moveMult );
  111. scope.object.translateY( scope.moveVector.y * moveMult );
  112. scope.object.translateZ( scope.moveVector.z * moveMult );
  113. scope.tmpQuaternion.set( scope.rotationVector.x * rotMult, scope.rotationVector.y * rotMult, scope.rotationVector.z * rotMult, 1 ).normalize();
  114. scope.object.quaternion.multiply( scope.tmpQuaternion );
  115. if (
  116. lastPosition.distanceToSquared( scope.object.position ) > EPS ||
  117. 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS
  118. ) {
  119. scope.dispatchEvent( _changeEvent );
  120. lastQuaternion.copy( scope.object.quaternion );
  121. lastPosition.copy( scope.object.position );
  122. }
  123. };
  124. this.updateMovementVector = function () {
  125. const forward = ( this.moveState.forward || ( this.autoForward && ! this.moveState.back ) ) ? 1 : 0;
  126. this.moveVector.x = ( - this.moveState.left + this.moveState.right );
  127. this.moveVector.y = ( - this.moveState.down + this.moveState.up );
  128. this.moveVector.z = ( - forward + this.moveState.back );
  129. //console.log( 'move:', [ this.moveVector.x, this.moveVector.y, this.moveVector.z ] );
  130. };
  131. this.updateRotationVector = function () {
  132. this.rotationVector.x = ( - this.moveState.pitchDown + this.moveState.pitchUp );
  133. this.rotationVector.y = ( - this.moveState.yawRight + this.moveState.yawLeft );
  134. this.rotationVector.z = ( - this.moveState.rollRight + this.moveState.rollLeft );
  135. //console.log( 'rotate:', [ this.rotationVector.x, this.rotationVector.y, this.rotationVector.z ] );
  136. };
  137. this.getContainerDimensions = function () {
  138. if ( this.domElement != document ) {
  139. return {
  140. size: [ this.domElement.offsetWidth, this.domElement.offsetHeight ],
  141. offset: [ this.domElement.offsetLeft, this.domElement.offsetTop ]
  142. };
  143. } else {
  144. return {
  145. size: [ window.innerWidth, window.innerHeight ],
  146. offset: [ 0, 0 ]
  147. };
  148. }
  149. };
  150. this.dispose = function () {
  151. this.domElement.removeEventListener( 'contextmenu', contextmenu );
  152. this.domElement.removeEventListener( 'mousedown', _mousedown );
  153. this.domElement.removeEventListener( 'mousemove', _mousemove );
  154. this.domElement.removeEventListener( 'mouseup', _mouseup );
  155. window.removeEventListener( 'keydown', _keydown );
  156. window.removeEventListener( 'keyup', _keyup );
  157. };
  158. const _mousemove = this.mousemove.bind( this );
  159. const _mousedown = this.mousedown.bind( this );
  160. const _mouseup = this.mouseup.bind( this );
  161. const _keydown = this.keydown.bind( this );
  162. const _keyup = this.keyup.bind( this );
  163. this.domElement.addEventListener( 'contextmenu', contextmenu );
  164. this.domElement.addEventListener( 'mousemove', _mousemove );
  165. this.domElement.addEventListener( 'mousedown', _mousedown );
  166. this.domElement.addEventListener( 'mouseup', _mouseup );
  167. window.addEventListener( 'keydown', _keydown );
  168. window.addEventListener( 'keyup', _keyup );
  169. this.updateMovementVector();
  170. this.updateRotationVector();
  171. }
  172. }
  173. function contextmenu( event ) {
  174. event.preventDefault();
  175. }
  176. export { FlyControls };