FlyControls.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. var scope = this;
  18. var changeEvent = { type: "change" };
  19. var EPS = 0.000001;
  20. this.tmpQuaternion = new THREE.Quaternion();
  21. this.mouseStatus = 0;
  22. 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 };
  23. this.moveVector = new THREE.Vector3( 0, 0, 0 );
  24. this.rotationVector = new THREE.Vector3( 0, 0, 0 );
  25. this.keydown = function ( event ) {
  26. if ( event.altKey ) {
  27. return;
  28. }
  29. //event.preventDefault();
  30. switch ( event.keyCode ) {
  31. case 16: /* shift */ this.movementSpeedMultiplier = .1; break;
  32. case 87: /*W*/ this.moveState.forward = 1; break;
  33. case 83: /*S*/ this.moveState.back = 1; break;
  34. case 65: /*A*/ this.moveState.left = 1; break;
  35. case 68: /*D*/ this.moveState.right = 1; break;
  36. case 82: /*R*/ this.moveState.up = 1; break;
  37. case 70: /*F*/ this.moveState.down = 1; break;
  38. case 38: /*up*/ this.moveState.pitchUp = 1; break;
  39. case 40: /*down*/ this.moveState.pitchDown = 1; break;
  40. case 37: /*left*/ this.moveState.yawLeft = 1; break;
  41. case 39: /*right*/ this.moveState.yawRight = 1; break;
  42. case 81: /*Q*/ this.moveState.rollLeft = 1; break;
  43. case 69: /*E*/ this.moveState.rollRight = 1; break;
  44. }
  45. this.updateMovementVector();
  46. this.updateRotationVector();
  47. };
  48. this.keyup = function ( event ) {
  49. switch ( event.keyCode ) {
  50. case 16: /* shift */ this.movementSpeedMultiplier = 1; break;
  51. case 87: /*W*/ this.moveState.forward = 0; break;
  52. case 83: /*S*/ this.moveState.back = 0; break;
  53. case 65: /*A*/ this.moveState.left = 0; break;
  54. case 68: /*D*/ this.moveState.right = 0; break;
  55. case 82: /*R*/ this.moveState.up = 0; break;
  56. case 70: /*F*/ this.moveState.down = 0; break;
  57. case 38: /*up*/ this.moveState.pitchUp = 0; break;
  58. case 40: /*down*/ this.moveState.pitchDown = 0; break;
  59. case 37: /*left*/ this.moveState.yawLeft = 0; break;
  60. case 39: /*right*/ this.moveState.yawRight = 0; break;
  61. case 81: /*Q*/ this.moveState.rollLeft = 0; break;
  62. case 69: /*E*/ this.moveState.rollRight = 0; break;
  63. }
  64. this.updateMovementVector();
  65. this.updateRotationVector();
  66. };
  67. this.mousedown = function ( event ) {
  68. if ( this.domElement !== document ) {
  69. this.domElement.focus();
  70. }
  71. event.preventDefault();
  72. event.stopPropagation();
  73. if ( this.dragToLook ) {
  74. this.mouseStatus ++;
  75. } else {
  76. switch ( event.button ) {
  77. case 0: this.moveState.forward = 1; break;
  78. case 2: this.moveState.back = 1; break;
  79. }
  80. this.updateMovementVector();
  81. }
  82. };
  83. this.mousemove = function ( event ) {
  84. if ( ! this.dragToLook || this.mouseStatus > 0 ) {
  85. var container = this.getContainerDimensions();
  86. var halfWidth = container.size[ 0 ] / 2;
  87. var halfHeight = container.size[ 1 ] / 2;
  88. this.moveState.yawLeft = - ( ( event.pageX - container.offset[ 0 ] ) - halfWidth ) / halfWidth;
  89. this.moveState.pitchDown = ( ( event.pageY - container.offset[ 1 ] ) - halfHeight ) / halfHeight;
  90. this.updateRotationVector();
  91. }
  92. };
  93. this.mouseup = function ( event ) {
  94. event.preventDefault();
  95. event.stopPropagation();
  96. if ( this.dragToLook ) {
  97. this.mouseStatus --;
  98. this.moveState.yawLeft = this.moveState.pitchDown = 0;
  99. } else {
  100. switch ( event.button ) {
  101. case 0: this.moveState.forward = 0; break;
  102. case 2: this.moveState.back = 0; break;
  103. }
  104. this.updateMovementVector();
  105. }
  106. this.updateRotationVector();
  107. };
  108. this.update = function () {
  109. var lastQuaternion = new THREE.Quaternion();
  110. var lastPosition = new THREE.Vector3();
  111. return function ( delta ) {
  112. var moveMult = delta * scope.movementSpeed;
  113. var rotMult = delta * scope.rollSpeed;
  114. scope.object.translateX( scope.moveVector.x * moveMult );
  115. scope.object.translateY( scope.moveVector.y * moveMult );
  116. scope.object.translateZ( scope.moveVector.z * moveMult );
  117. scope.tmpQuaternion.set( scope.rotationVector.x * rotMult, scope.rotationVector.y * rotMult, scope.rotationVector.z * rotMult, 1 ).normalize();
  118. scope.object.quaternion.multiply( scope.tmpQuaternion );
  119. if (
  120. lastPosition.distanceToSquared( scope.object.position ) > EPS ||
  121. 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS
  122. ) {
  123. scope.dispatchEvent( changeEvent );
  124. lastQuaternion.copy( scope.object.quaternion );
  125. lastPosition.copy( scope.object.position );
  126. }
  127. };
  128. }();
  129. this.updateMovementVector = function () {
  130. var forward = ( this.moveState.forward || ( this.autoForward && ! this.moveState.back ) ) ? 1 : 0;
  131. this.moveVector.x = ( - this.moveState.left + this.moveState.right );
  132. this.moveVector.y = ( - this.moveState.down + this.moveState.up );
  133. this.moveVector.z = ( - forward + this.moveState.back );
  134. //console.log( 'move:', [ this.moveVector.x, this.moveVector.y, this.moveVector.z ] );
  135. };
  136. this.updateRotationVector = function () {
  137. this.rotationVector.x = ( - this.moveState.pitchDown + this.moveState.pitchUp );
  138. this.rotationVector.y = ( - this.moveState.yawRight + this.moveState.yawLeft );
  139. this.rotationVector.z = ( - this.moveState.rollRight + this.moveState.rollLeft );
  140. //console.log( 'rotate:', [ this.rotationVector.x, this.rotationVector.y, this.rotationVector.z ] );
  141. };
  142. this.getContainerDimensions = function () {
  143. if ( this.domElement != document ) {
  144. return {
  145. size: [ this.domElement.offsetWidth, this.domElement.offsetHeight ],
  146. offset: [ this.domElement.offsetLeft, this.domElement.offsetTop ]
  147. };
  148. } else {
  149. return {
  150. size: [ window.innerWidth, window.innerHeight ],
  151. offset: [ 0, 0 ]
  152. };
  153. }
  154. };
  155. function bind( scope, fn ) {
  156. return function () {
  157. fn.apply( scope, arguments );
  158. };
  159. }
  160. function contextmenu( event ) {
  161. event.preventDefault();
  162. }
  163. this.dispose = function () {
  164. this.domElement.removeEventListener( 'contextmenu', contextmenu, false );
  165. this.domElement.removeEventListener( 'mousedown', _mousedown, false );
  166. this.domElement.removeEventListener( 'mousemove', _mousemove, false );
  167. this.domElement.removeEventListener( 'mouseup', _mouseup, false );
  168. window.removeEventListener( 'keydown', _keydown, false );
  169. window.removeEventListener( 'keyup', _keyup, false );
  170. };
  171. var _mousemove = bind( this, this.mousemove );
  172. var _mousedown = bind( this, this.mousedown );
  173. var _mouseup = bind( this, this.mouseup );
  174. var _keydown = bind( this, this.keydown );
  175. var _keyup = bind( this, this.keyup );
  176. this.domElement.addEventListener( 'contextmenu', contextmenu, false );
  177. this.domElement.addEventListener( 'mousemove', _mousemove, false );
  178. this.domElement.addEventListener( 'mousedown', _mousedown, false );
  179. this.domElement.addEventListener( 'mouseup', _mouseup, false );
  180. window.addEventListener( 'keydown', _keydown, false );
  181. window.addEventListener( 'keyup', _keyup, false );
  182. this.updateMovementVector();
  183. this.updateRotationVector();
  184. };
  185. THREE.FlyControls.prototype = Object.create( THREE.EventDispatcher.prototype );
  186. THREE.FlyControls.prototype.constructor = THREE.FlyControls;