FlyControls.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. if ( this.dragToLook ) {
  79. this.mouseStatus ++;
  80. } else {
  81. switch ( event.button ) {
  82. case 0: this.moveState.forward = 1; break;
  83. case 2: this.moveState.back = 1; break;
  84. }
  85. this.updateMovementVector();
  86. }
  87. };
  88. this.mousemove = function ( event ) {
  89. if ( ! this.dragToLook || this.mouseStatus > 0 ) {
  90. var container = this.getContainerDimensions();
  91. var halfWidth = container.size[ 0 ] / 2;
  92. var halfHeight = container.size[ 1 ] / 2;
  93. this.moveState.yawLeft = - ( ( event.pageX - container.offset[ 0 ] ) - halfWidth ) / halfWidth;
  94. this.moveState.pitchDown = ( ( event.pageY - container.offset[ 1 ] ) - halfHeight ) / halfHeight;
  95. this.updateRotationVector();
  96. }
  97. };
  98. this.mouseup = function ( event ) {
  99. event.preventDefault();
  100. if ( this.dragToLook ) {
  101. this.mouseStatus --;
  102. this.moveState.yawLeft = this.moveState.pitchDown = 0;
  103. } else {
  104. switch ( event.button ) {
  105. case 0: this.moveState.forward = 0; break;
  106. case 2: this.moveState.back = 0; break;
  107. }
  108. this.updateMovementVector();
  109. }
  110. this.updateRotationVector();
  111. };
  112. this.update = function () {
  113. var lastQuaternion = new Quaternion();
  114. var lastPosition = new Vector3();
  115. return function ( delta ) {
  116. var moveMult = delta * scope.movementSpeed;
  117. var rotMult = delta * scope.rollSpeed;
  118. scope.object.translateX( scope.moveVector.x * moveMult );
  119. scope.object.translateY( scope.moveVector.y * moveMult );
  120. scope.object.translateZ( scope.moveVector.z * moveMult );
  121. scope.tmpQuaternion.set( scope.rotationVector.x * rotMult, scope.rotationVector.y * rotMult, scope.rotationVector.z * rotMult, 1 ).normalize();
  122. scope.object.quaternion.multiply( scope.tmpQuaternion );
  123. if (
  124. lastPosition.distanceToSquared( scope.object.position ) > EPS ||
  125. 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS
  126. ) {
  127. scope.dispatchEvent( changeEvent );
  128. lastQuaternion.copy( scope.object.quaternion );
  129. lastPosition.copy( scope.object.position );
  130. }
  131. };
  132. }();
  133. this.updateMovementVector = function () {
  134. var forward = ( this.moveState.forward || ( this.autoForward && ! this.moveState.back ) ) ? 1 : 0;
  135. this.moveVector.x = ( - this.moveState.left + this.moveState.right );
  136. this.moveVector.y = ( - this.moveState.down + this.moveState.up );
  137. this.moveVector.z = ( - forward + this.moveState.back );
  138. //console.log( 'move:', [ this.moveVector.x, this.moveVector.y, this.moveVector.z ] );
  139. };
  140. this.updateRotationVector = function () {
  141. this.rotationVector.x = ( - this.moveState.pitchDown + this.moveState.pitchUp );
  142. this.rotationVector.y = ( - this.moveState.yawRight + this.moveState.yawLeft );
  143. this.rotationVector.z = ( - this.moveState.rollRight + this.moveState.rollLeft );
  144. //console.log( 'rotate:', [ this.rotationVector.x, this.rotationVector.y, this.rotationVector.z ] );
  145. };
  146. this.getContainerDimensions = function () {
  147. if ( this.domElement != document ) {
  148. return {
  149. size: [ this.domElement.offsetWidth, this.domElement.offsetHeight ],
  150. offset: [ this.domElement.offsetLeft, this.domElement.offsetTop ]
  151. };
  152. } else {
  153. return {
  154. size: [ window.innerWidth, window.innerHeight ],
  155. offset: [ 0, 0 ]
  156. };
  157. }
  158. };
  159. function bind( scope, fn ) {
  160. return function () {
  161. fn.apply( scope, arguments );
  162. };
  163. }
  164. function contextmenu( event ) {
  165. event.preventDefault();
  166. }
  167. this.dispose = function () {
  168. this.domElement.removeEventListener( 'contextmenu', contextmenu );
  169. this.domElement.removeEventListener( 'mousedown', _mousedown );
  170. this.domElement.removeEventListener( 'mousemove', _mousemove );
  171. this.domElement.removeEventListener( 'mouseup', _mouseup );
  172. window.removeEventListener( 'keydown', _keydown );
  173. window.removeEventListener( 'keyup', _keyup );
  174. };
  175. var _mousemove = bind( this, this.mousemove );
  176. var _mousedown = bind( this, this.mousedown );
  177. var _mouseup = bind( this, this.mouseup );
  178. var _keydown = bind( this, this.keydown );
  179. var _keyup = bind( this, this.keyup );
  180. this.domElement.addEventListener( 'contextmenu', contextmenu );
  181. this.domElement.addEventListener( 'mousemove', _mousemove );
  182. this.domElement.addEventListener( 'mousedown', _mousedown );
  183. this.domElement.addEventListener( 'mouseup', _mouseup );
  184. window.addEventListener( 'keydown', _keydown );
  185. window.addEventListener( 'keyup', _keyup );
  186. this.updateMovementVector();
  187. this.updateRotationVector();
  188. };
  189. FlyControls.prototype = Object.create( EventDispatcher.prototype );
  190. FlyControls.prototype.constructor = FlyControls;
  191. export { FlyControls };