FlyControls.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /**
  2. * @author James Baicoianu / http://www.baicoianu.com/
  3. */
  4. import { EventDispatcher, Quaternion, Vector3 } from '../../../build/three.module.js';
  5. var FlyControls = function ( object, domElement ) {
  6. if ( domElement === undefined ) {
  7. console.warn( 'THREE.FlyControls: The second parameter "domElement" is now mandatory.' );
  8. domElement = document;
  9. }
  10. this.object = object;
  11. this.domElement = domElement;
  12. if ( domElement ) this.domElement.setAttribute( 'tabindex', - 1 );
  13. // API
  14. this.movementSpeed = 1.0;
  15. this.rollSpeed = 0.005;
  16. this.dragToLook = false;
  17. this.autoForward = false;
  18. // disable default target object behavior
  19. // internals
  20. var changeEvent = { type: 'change' };
  21. var EPS = 0.000001;
  22. this.tmpQuaternion = new Quaternion();
  23. this.mouseStatus = 0;
  24. 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 };
  25. this.moveVector = new Vector3( 0, 0, 0 );
  26. this.rotationVector = new Vector3( 0, 0, 0 );
  27. this.keydown = function ( event ) {
  28. if ( event.altKey ) {
  29. return;
  30. }
  31. //event.preventDefault();
  32. switch ( event.keyCode ) {
  33. case 16: /* shift */ this.movementSpeedMultiplier = .1; break;
  34. case 87: /*W*/ this.moveState.forward = 1; break;
  35. case 83: /*S*/ this.moveState.back = 1; break;
  36. case 65: /*A*/ this.moveState.left = 1; break;
  37. case 68: /*D*/ this.moveState.right = 1; break;
  38. case 82: /*R*/ this.moveState.up = 1; break;
  39. case 70: /*F*/ this.moveState.down = 1; break;
  40. case 38: /*up*/ this.moveState.pitchUp = 1; break;
  41. case 40: /*down*/ this.moveState.pitchDown = 1; break;
  42. case 37: /*left*/ this.moveState.yawLeft = 1; break;
  43. case 39: /*right*/ this.moveState.yawRight = 1; break;
  44. case 81: /*Q*/ this.moveState.rollLeft = 1; break;
  45. case 69: /*E*/ this.moveState.rollRight = 1; break;
  46. }
  47. this.updateMovementVector();
  48. this.updateRotationVector();
  49. };
  50. this.keyup = function ( event ) {
  51. switch ( event.keyCode ) {
  52. case 16: /* shift */ this.movementSpeedMultiplier = 1; break;
  53. case 87: /*W*/ this.moveState.forward = 0; break;
  54. case 83: /*S*/ this.moveState.back = 0; break;
  55. case 65: /*A*/ this.moveState.left = 0; break;
  56. case 68: /*D*/ this.moveState.right = 0; break;
  57. case 82: /*R*/ this.moveState.up = 0; break;
  58. case 70: /*F*/ this.moveState.down = 0; break;
  59. case 38: /*up*/ this.moveState.pitchUp = 0; break;
  60. case 40: /*down*/ this.moveState.pitchDown = 0; break;
  61. case 37: /*left*/ this.moveState.yawLeft = 0; break;
  62. case 39: /*right*/ this.moveState.yawRight = 0; break;
  63. case 81: /*Q*/ this.moveState.rollLeft = 0; break;
  64. case 69: /*E*/ this.moveState.rollRight = 0; break;
  65. }
  66. this.updateMovementVector();
  67. this.updateRotationVector();
  68. };
  69. this.mousedown = function ( event ) {
  70. if ( this.domElement !== document ) {
  71. this.domElement.focus();
  72. }
  73. event.preventDefault();
  74. event.stopPropagation();
  75. if ( this.dragToLook ) {
  76. this.mouseStatus ++;
  77. } else {
  78. switch ( event.button ) {
  79. case 0: this.moveState.forward = 1; break;
  80. case 2: this.moveState.back = 1; break;
  81. }
  82. this.updateMovementVector();
  83. }
  84. };
  85. this.mousemove = function ( event ) {
  86. if ( ! this.dragToLook || this.mouseStatus > 0 ) {
  87. var container = this.getContainerDimensions();
  88. var halfWidth = container.size[ 0 ] / 2;
  89. var halfHeight = container.size[ 1 ] / 2;
  90. this.moveState.yawLeft = - ( ( event.pageX - container.offset[ 0 ] ) - halfWidth ) / halfWidth;
  91. this.moveState.pitchDown = ( ( event.pageY - container.offset[ 1 ] ) - halfHeight ) / halfHeight;
  92. this.updateRotationVector();
  93. }
  94. };
  95. this.mouseup = function ( event ) {
  96. event.preventDefault();
  97. event.stopPropagation();
  98. if ( this.dragToLook ) {
  99. this.mouseStatus --;
  100. this.moveState.yawLeft = this.moveState.pitchDown = 0;
  101. } else {
  102. switch ( event.button ) {
  103. case 0: this.moveState.forward = 0; break;
  104. case 2: this.moveState.back = 0; break;
  105. }
  106. this.updateMovementVector();
  107. }
  108. this.updateRotationVector();
  109. };
  110. this.update = ( () => {
  111. var lastQuaternion = new Quaternion();
  112. var lastPosition = new Vector3();
  113. return ( delta ) => {
  114. var moveMult = delta * this.movementSpeed;
  115. var rotMult = delta * this.rollSpeed;
  116. this.object.translateX( this.moveVector.x * moveMult );
  117. this.object.translateY( this.moveVector.y * moveMult );
  118. this.object.translateZ( this.moveVector.z * moveMult );
  119. this.tmpQuaternion.set( this.rotationVector.x * rotMult, this.rotationVector.y * rotMult, this.rotationVector.z * rotMult, 1 ).normalize();
  120. this.object.quaternion.multiply( this.tmpQuaternion );
  121. // expose the rotation vector for convenience
  122. this.object.rotation.setFromQuaternion( this.object.quaternion, this.object.rotation.order );
  123. if (
  124. lastPosition.distanceToSquared( this.object.position ) > EPS ||
  125. 8 * ( 1 - lastQuaternion.dot( this.object.quaternion ) ) > EPS
  126. ) {
  127. this.dispatchEvent( changeEvent );
  128. lastQuaternion.copy( this.object.quaternion );
  129. lastPosition.copy( this.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, false );
  169. this.domElement.removeEventListener( 'mousedown', _mousedown, false );
  170. this.domElement.removeEventListener( 'mousemove', _mousemove, false );
  171. this.domElement.removeEventListener( 'mouseup', _mouseup, false );
  172. window.removeEventListener( 'keydown', _keydown, false );
  173. window.removeEventListener( 'keyup', _keyup, false );
  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, false );
  181. this.domElement.addEventListener( 'mousemove', _mousemove, false );
  182. this.domElement.addEventListener( 'mousedown', _mousedown, false );
  183. this.domElement.addEventListener( 'mouseup', _mouseup, false );
  184. window.addEventListener( 'keydown', _keydown, false );
  185. window.addEventListener( 'keyup', _keyup, false );
  186. this.updateMovementVector();
  187. this.updateRotationVector();
  188. };
  189. FlyControls.prototype = Object.create( EventDispatcher.prototype );
  190. FlyControls.prototype.constructor = FlyControls;
  191. export { FlyControls };