FlyControls.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. console.warn( "THREE.FlyControls: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. THREE.FlyControls = function ( object, domElement ) {
  3. if ( domElement === undefined ) {
  4. console.warn( 'THREE.FlyControls: The second parameter "domElement" is now mandatory.' );
  5. domElement = document;
  6. }
  7. this.object = object;
  8. this.domElement = domElement;
  9. if ( domElement ) this.domElement.setAttribute( 'tabindex', - 1 );
  10. // API
  11. this.movementSpeed = 1.0;
  12. this.rollSpeed = 0.005;
  13. this.dragToLook = false;
  14. this.autoForward = false;
  15. // disable default target object behavior
  16. // internals
  17. this.tmpQuaternion = new THREE.Quaternion();
  18. this.mouseStatus = 0;
  19. 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 };
  20. this.moveVector = new THREE.Vector3( 0, 0, 0 );
  21. this.rotationVector = new THREE.Vector3( 0, 0, 0 );
  22. this.keydown = function ( event ) {
  23. if ( event.altKey ) {
  24. return;
  25. }
  26. //event.preventDefault();
  27. switch ( event.keyCode ) {
  28. case 16: /* shift */ this.movementSpeedMultiplier = .1; break;
  29. case 87: /*W*/ this.moveState.forward = 1; break;
  30. case 83: /*S*/ this.moveState.back = 1; break;
  31. case 65: /*A*/ this.moveState.left = 1; break;
  32. case 68: /*D*/ this.moveState.right = 1; break;
  33. case 82: /*R*/ this.moveState.up = 1; break;
  34. case 70: /*F*/ this.moveState.down = 1; break;
  35. case 38: /*up*/ this.moveState.pitchUp = 1; break;
  36. case 40: /*down*/ this.moveState.pitchDown = 1; break;
  37. case 37: /*left*/ this.moveState.yawLeft = 1; break;
  38. case 39: /*right*/ this.moveState.yawRight = 1; break;
  39. case 81: /*Q*/ this.moveState.rollLeft = 1; break;
  40. case 69: /*E*/ this.moveState.rollRight = 1; break;
  41. }
  42. this.updateMovementVector();
  43. this.updateRotationVector();
  44. };
  45. this.keyup = function ( event ) {
  46. switch ( event.keyCode ) {
  47. case 16: /* shift */ this.movementSpeedMultiplier = 1; break;
  48. case 87: /*W*/ this.moveState.forward = 0; break;
  49. case 83: /*S*/ this.moveState.back = 0; break;
  50. case 65: /*A*/ this.moveState.left = 0; break;
  51. case 68: /*D*/ this.moveState.right = 0; break;
  52. case 82: /*R*/ this.moveState.up = 0; break;
  53. case 70: /*F*/ this.moveState.down = 0; break;
  54. case 38: /*up*/ this.moveState.pitchUp = 0; break;
  55. case 40: /*down*/ this.moveState.pitchDown = 0; break;
  56. case 37: /*left*/ this.moveState.yawLeft = 0; break;
  57. case 39: /*right*/ this.moveState.yawRight = 0; break;
  58. case 81: /*Q*/ this.moveState.rollLeft = 0; break;
  59. case 69: /*E*/ this.moveState.rollRight = 0; break;
  60. }
  61. this.updateMovementVector();
  62. this.updateRotationVector();
  63. };
  64. this.mousedown = function ( event ) {
  65. if ( this.domElement !== document ) {
  66. this.domElement.focus();
  67. }
  68. event.preventDefault();
  69. event.stopPropagation();
  70. if ( this.dragToLook ) {
  71. this.mouseStatus ++;
  72. } else {
  73. switch ( event.button ) {
  74. case 0: this.moveState.forward = 1; break;
  75. case 2: this.moveState.back = 1; break;
  76. }
  77. this.updateMovementVector();
  78. }
  79. };
  80. this.mousemove = function ( event ) {
  81. if ( ! this.dragToLook || this.mouseStatus > 0 ) {
  82. var container = this.getContainerDimensions();
  83. var halfWidth = container.size[ 0 ] / 2;
  84. var halfHeight = container.size[ 1 ] / 2;
  85. this.moveState.yawLeft = - ( ( event.pageX - container.offset[ 0 ] ) - halfWidth ) / halfWidth;
  86. this.moveState.pitchDown = ( ( event.pageY - container.offset[ 1 ] ) - halfHeight ) / halfHeight;
  87. this.updateRotationVector();
  88. }
  89. };
  90. this.mouseup = function ( event ) {
  91. event.preventDefault();
  92. event.stopPropagation();
  93. if ( this.dragToLook ) {
  94. this.mouseStatus --;
  95. this.moveState.yawLeft = this.moveState.pitchDown = 0;
  96. } else {
  97. switch ( event.button ) {
  98. case 0: this.moveState.forward = 0; break;
  99. case 2: this.moveState.back = 0; break;
  100. }
  101. this.updateMovementVector();
  102. }
  103. this.updateRotationVector();
  104. };
  105. this.update = function ( delta ) {
  106. var moveMult = delta * this.movementSpeed;
  107. var rotMult = delta * this.rollSpeed;
  108. this.object.translateX( this.moveVector.x * moveMult );
  109. this.object.translateY( this.moveVector.y * moveMult );
  110. this.object.translateZ( this.moveVector.z * moveMult );
  111. this.tmpQuaternion.set( this.rotationVector.x * rotMult, this.rotationVector.y * rotMult, this.rotationVector.z * rotMult, 1 ).normalize();
  112. this.object.quaternion.multiply( this.tmpQuaternion );
  113. // expose the rotation vector for convenience
  114. this.object.rotation.setFromQuaternion( this.object.quaternion, this.object.rotation.order );
  115. };
  116. this.updateMovementVector = function () {
  117. var forward = ( this.moveState.forward || ( this.autoForward && ! this.moveState.back ) ) ? 1 : 0;
  118. this.moveVector.x = ( - this.moveState.left + this.moveState.right );
  119. this.moveVector.y = ( - this.moveState.down + this.moveState.up );
  120. this.moveVector.z = ( - forward + this.moveState.back );
  121. //console.log( 'move:', [ this.moveVector.x, this.moveVector.y, this.moveVector.z ] );
  122. };
  123. this.updateRotationVector = function () {
  124. this.rotationVector.x = ( - this.moveState.pitchDown + this.moveState.pitchUp );
  125. this.rotationVector.y = ( - this.moveState.yawRight + this.moveState.yawLeft );
  126. this.rotationVector.z = ( - this.moveState.rollRight + this.moveState.rollLeft );
  127. //console.log( 'rotate:', [ this.rotationVector.x, this.rotationVector.y, this.rotationVector.z ] );
  128. };
  129. this.getContainerDimensions = function () {
  130. if ( this.domElement != document ) {
  131. return {
  132. size: [ this.domElement.offsetWidth, this.domElement.offsetHeight ],
  133. offset: [ this.domElement.offsetLeft, this.domElement.offsetTop ]
  134. };
  135. } else {
  136. return {
  137. size: [ window.innerWidth, window.innerHeight ],
  138. offset: [ 0, 0 ]
  139. };
  140. }
  141. };
  142. function bind( scope, fn ) {
  143. return function () {
  144. fn.apply( scope, arguments );
  145. };
  146. }
  147. function contextmenu( event ) {
  148. event.preventDefault();
  149. }
  150. this.dispose = function () {
  151. this.domElement.removeEventListener( 'contextmenu', contextmenu, false );
  152. this.domElement.removeEventListener( 'mousedown', _mousedown, false );
  153. this.domElement.removeEventListener( 'mousemove', _mousemove, false );
  154. this.domElement.removeEventListener( 'mouseup', _mouseup, false );
  155. window.removeEventListener( 'keydown', _keydown, false );
  156. window.removeEventListener( 'keyup', _keyup, false );
  157. };
  158. var _mousemove = bind( this, this.mousemove );
  159. var _mousedown = bind( this, this.mousedown );
  160. var _mouseup = bind( this, this.mouseup );
  161. var _keydown = bind( this, this.keydown );
  162. var _keyup = bind( this, this.keyup );
  163. this.domElement.addEventListener( 'contextmenu', contextmenu, false );
  164. this.domElement.addEventListener( 'mousemove', _mousemove, false );
  165. this.domElement.addEventListener( 'mousedown', _mousedown, false );
  166. this.domElement.addEventListener( 'mouseup', _mouseup, false );
  167. window.addEventListener( 'keydown', _keydown, false );
  168. window.addEventListener( 'keyup', _keyup, false );
  169. this.updateMovementVector();
  170. this.updateRotationVector();
  171. };