FlyControls.js 7.5 KB

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