FlyControls.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * @author James Baicoianu / http://www.baicoianu.com/
  3. */
  4. import {
  5. Quaternion,
  6. Vector3
  7. } from "../../../build/three.module.js";
  8. var FlyControls = function ( object, domElement ) {
  9. if ( domElement === undefined ) {
  10. console.warn( 'THREE.FlyControls: The second parameter "domElement" is now mandatory.' );
  11. domElement = document;
  12. }
  13. this.object = object;
  14. this.domElement = domElement;
  15. if ( domElement ) this.domElement.setAttribute( 'tabindex', - 1 );
  16. // API
  17. this.movementSpeed = 1.0;
  18. this.rollSpeed = 0.005;
  19. this.dragToLook = false;
  20. this.autoForward = false;
  21. // disable default target object behavior
  22. // internals
  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. //event.preventDefault();
  33. switch ( event.keyCode ) {
  34. case 16: /* shift */ this.movementSpeedMultiplier = .1; break;
  35. case 87: /*W*/ this.moveState.forward = 1; break;
  36. case 83: /*S*/ this.moveState.back = 1; break;
  37. case 65: /*A*/ this.moveState.left = 1; break;
  38. case 68: /*D*/ this.moveState.right = 1; break;
  39. case 82: /*R*/ this.moveState.up = 1; break;
  40. case 70: /*F*/ this.moveState.down = 1; break;
  41. case 38: /*up*/ this.moveState.pitchUp = 1; break;
  42. case 40: /*down*/ this.moveState.pitchDown = 1; break;
  43. case 37: /*left*/ this.moveState.yawLeft = 1; break;
  44. case 39: /*right*/ this.moveState.yawRight = 1; break;
  45. case 81: /*Q*/ this.moveState.rollLeft = 1; break;
  46. case 69: /*E*/ this.moveState.rollRight = 1; break;
  47. }
  48. this.updateMovementVector();
  49. this.updateRotationVector();
  50. };
  51. this.keyup = function ( event ) {
  52. switch ( event.keyCode ) {
  53. case 16: /* shift */ this.movementSpeedMultiplier = 1; break;
  54. case 87: /*W*/ this.moveState.forward = 0; break;
  55. case 83: /*S*/ this.moveState.back = 0; break;
  56. case 65: /*A*/ this.moveState.left = 0; break;
  57. case 68: /*D*/ this.moveState.right = 0; break;
  58. case 82: /*R*/ this.moveState.up = 0; break;
  59. case 70: /*F*/ this.moveState.down = 0; break;
  60. case 38: /*up*/ this.moveState.pitchUp = 0; break;
  61. case 40: /*down*/ this.moveState.pitchDown = 0; break;
  62. case 37: /*left*/ this.moveState.yawLeft = 0; break;
  63. case 39: /*right*/ this.moveState.yawRight = 0; break;
  64. case 81: /*Q*/ this.moveState.rollLeft = 0; break;
  65. case 69: /*E*/ this.moveState.rollRight = 0; break;
  66. }
  67. this.updateMovementVector();
  68. this.updateRotationVector();
  69. };
  70. this.mousedown = function ( event ) {
  71. if ( this.domElement !== document ) {
  72. this.domElement.focus();
  73. }
  74. event.preventDefault();
  75. event.stopPropagation();
  76. if ( this.dragToLook ) {
  77. this.mouseStatus ++;
  78. } else {
  79. switch ( event.button ) {
  80. case 0: this.moveState.forward = 1; break;
  81. case 2: this.moveState.back = 1; break;
  82. }
  83. this.updateMovementVector();
  84. }
  85. };
  86. this.mousemove = function ( event ) {
  87. if ( ! this.dragToLook || this.mouseStatus > 0 ) {
  88. var container = this.getContainerDimensions();
  89. var halfWidth = container.size[ 0 ] / 2;
  90. var halfHeight = container.size[ 1 ] / 2;
  91. this.moveState.yawLeft = - ( ( event.pageX - container.offset[ 0 ] ) - halfWidth ) / halfWidth;
  92. this.moveState.pitchDown = ( ( event.pageY - container.offset[ 1 ] ) - halfHeight ) / halfHeight;
  93. this.updateRotationVector();
  94. }
  95. };
  96. this.mouseup = function ( event ) {
  97. event.preventDefault();
  98. event.stopPropagation();
  99. if ( this.dragToLook ) {
  100. this.mouseStatus --;
  101. this.moveState.yawLeft = this.moveState.pitchDown = 0;
  102. } else {
  103. switch ( event.button ) {
  104. case 0: this.moveState.forward = 0; break;
  105. case 2: this.moveState.back = 0; break;
  106. }
  107. this.updateMovementVector();
  108. }
  109. this.updateRotationVector();
  110. };
  111. this.update = function ( delta ) {
  112. var moveMult = delta * this.movementSpeed;
  113. var rotMult = delta * this.rollSpeed;
  114. this.object.translateX( this.moveVector.x * moveMult );
  115. this.object.translateY( this.moveVector.y * moveMult );
  116. this.object.translateZ( this.moveVector.z * moveMult );
  117. this.tmpQuaternion.set( this.rotationVector.x * rotMult, this.rotationVector.y * rotMult, this.rotationVector.z * rotMult, 1 ).normalize();
  118. this.object.quaternion.multiply( this.tmpQuaternion );
  119. // expose the rotation vector for convenience
  120. this.object.rotation.setFromQuaternion( this.object.quaternion, this.object.rotation.order );
  121. };
  122. this.updateMovementVector = function () {
  123. var 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. function bind( scope, fn ) {
  149. return function () {
  150. fn.apply( scope, arguments );
  151. };
  152. }
  153. function contextmenu( event ) {
  154. event.preventDefault();
  155. }
  156. this.dispose = function () {
  157. this.domElement.removeEventListener( 'contextmenu', contextmenu, false );
  158. this.domElement.removeEventListener( 'mousedown', _mousedown, false );
  159. this.domElement.removeEventListener( 'mousemove', _mousemove, false );
  160. this.domElement.removeEventListener( 'mouseup', _mouseup, false );
  161. window.removeEventListener( 'keydown', _keydown, false );
  162. window.removeEventListener( 'keyup', _keyup, false );
  163. };
  164. var _mousemove = bind( this, this.mousemove );
  165. var _mousedown = bind( this, this.mousedown );
  166. var _mouseup = bind( this, this.mouseup );
  167. var _keydown = bind( this, this.keydown );
  168. var _keyup = bind( this, this.keyup );
  169. this.domElement.addEventListener( 'contextmenu', contextmenu, false );
  170. this.domElement.addEventListener( 'mousemove', _mousemove, false );
  171. this.domElement.addEventListener( 'mousedown', _mousedown, false );
  172. this.domElement.addEventListener( 'mouseup', _mouseup, false );
  173. window.addEventListener( 'keydown', _keydown, false );
  174. window.addEventListener( 'keyup', _keyup, false );
  175. this.updateMovementVector();
  176. this.updateRotationVector();
  177. };
  178. export { FlyControls };