2
0

FlyControls.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. THREE.FlyControls = function ( object, domElement ) {
  2. if ( domElement === undefined ) {
  3. console.warn( 'THREE.FlyControls: The second parameter "domElement" is now mandatory.' );
  4. domElement = document;
  5. }
  6. this.object = object;
  7. this.domElement = domElement;
  8. if ( domElement ) this.domElement.setAttribute( 'tabindex', - 1 );
  9. // API
  10. this.movementSpeed = 1.0;
  11. this.rollSpeed = 0.005;
  12. this.dragToLook = false;
  13. this.autoForward = false;
  14. // disable default target object behavior
  15. // internals
  16. var scope = this;
  17. var changeEvent = { type: 'change' };
  18. var EPS = 0.000001;
  19. this.tmpQuaternion = new THREE.Quaternion();
  20. this.mouseStatus = 0;
  21. 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 };
  22. this.moveVector = new THREE.Vector3( 0, 0, 0 );
  23. this.rotationVector = new THREE.Vector3( 0, 0, 0 );
  24. this.keydown = function ( event ) {
  25. if ( event.altKey ) {
  26. return;
  27. }
  28. //event.preventDefault();
  29. switch ( event.code ) {
  30. case 'ShiftLeft':
  31. case 'ShiftRight': this.movementSpeedMultiplier = .1; break;
  32. case 'KeyW': this.moveState.forward = 1; break;
  33. case 'KeyS': this.moveState.back = 1; break;
  34. case 'KeyA': this.moveState.left = 1; break;
  35. case 'KeyD': this.moveState.right = 1; break;
  36. case 'KeyR': this.moveState.up = 1; break;
  37. case 'KeyF': this.moveState.down = 1; break;
  38. case 'ArrowUp': this.moveState.pitchUp = 1; break;
  39. case 'ArrowDown': this.moveState.pitchDown = 1; break;
  40. case 'ArrowLeft': this.moveState.yawLeft = 1; break;
  41. case 'ArrowRight': this.moveState.yawRight = 1; break;
  42. case 'KeyQ': this.moveState.rollLeft = 1; break;
  43. case 'KeyE': this.moveState.rollRight = 1; break;
  44. }
  45. this.updateMovementVector();
  46. this.updateRotationVector();
  47. };
  48. this.keyup = function ( event ) {
  49. switch ( event.code ) {
  50. case 'ShiftLeft':
  51. case 'ShiftRight': this.movementSpeedMultiplier = 1; break;
  52. case 'KeyW': this.moveState.forward = 0; break;
  53. case 'KeyS': this.moveState.back = 0; break;
  54. case 'KeyA': this.moveState.left = 0; break;
  55. case 'KeyD': this.moveState.right = 0; break;
  56. case 'KeyR': this.moveState.up = 0; break;
  57. case 'KeyF': this.moveState.down = 0; break;
  58. case 'ArrowUp': this.moveState.pitchUp = 0; break;
  59. case 'ArrowDown': this.moveState.pitchDown = 0; break;
  60. case 'ArrowLeft': this.moveState.yawLeft = 0; break;
  61. case 'ArrowRight': this.moveState.yawRight = 0; break;
  62. case 'KeyQ': this.moveState.rollLeft = 0; break;
  63. case 'KeyE': this.moveState.rollRight = 0; break;
  64. }
  65. this.updateMovementVector();
  66. this.updateRotationVector();
  67. };
  68. this.mousedown = function ( event ) {
  69. if ( this.domElement !== document ) {
  70. this.domElement.focus();
  71. }
  72. event.preventDefault();
  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. if ( this.dragToLook ) {
  96. this.mouseStatus --;
  97. this.moveState.yawLeft = this.moveState.pitchDown = 0;
  98. } else {
  99. switch ( event.button ) {
  100. case 0: this.moveState.forward = 0; break;
  101. case 2: this.moveState.back = 0; break;
  102. }
  103. this.updateMovementVector();
  104. }
  105. this.updateRotationVector();
  106. };
  107. this.update = function () {
  108. var lastQuaternion = new THREE.Quaternion();
  109. var lastPosition = new THREE.Vector3();
  110. return function ( delta ) {
  111. var moveMult = delta * scope.movementSpeed;
  112. var rotMult = delta * scope.rollSpeed;
  113. scope.object.translateX( scope.moveVector.x * moveMult );
  114. scope.object.translateY( scope.moveVector.y * moveMult );
  115. scope.object.translateZ( scope.moveVector.z * moveMult );
  116. scope.tmpQuaternion.set( scope.rotationVector.x * rotMult, scope.rotationVector.y * rotMult, scope.rotationVector.z * rotMult, 1 ).normalize();
  117. scope.object.quaternion.multiply( scope.tmpQuaternion );
  118. if (
  119. lastPosition.distanceToSquared( scope.object.position ) > EPS ||
  120. 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS
  121. ) {
  122. scope.dispatchEvent( changeEvent );
  123. lastQuaternion.copy( scope.object.quaternion );
  124. lastPosition.copy( scope.object.position );
  125. }
  126. };
  127. }();
  128. this.updateMovementVector = function () {
  129. var forward = ( this.moveState.forward || ( this.autoForward && ! this.moveState.back ) ) ? 1 : 0;
  130. this.moveVector.x = ( - this.moveState.left + this.moveState.right );
  131. this.moveVector.y = ( - this.moveState.down + this.moveState.up );
  132. this.moveVector.z = ( - forward + this.moveState.back );
  133. //console.log( 'move:', [ this.moveVector.x, this.moveVector.y, this.moveVector.z ] );
  134. };
  135. this.updateRotationVector = function () {
  136. this.rotationVector.x = ( - this.moveState.pitchDown + this.moveState.pitchUp );
  137. this.rotationVector.y = ( - this.moveState.yawRight + this.moveState.yawLeft );
  138. this.rotationVector.z = ( - this.moveState.rollRight + this.moveState.rollLeft );
  139. //console.log( 'rotate:', [ this.rotationVector.x, this.rotationVector.y, this.rotationVector.z ] );
  140. };
  141. this.getContainerDimensions = function () {
  142. if ( this.domElement != document ) {
  143. return {
  144. size: [ this.domElement.offsetWidth, this.domElement.offsetHeight ],
  145. offset: [ this.domElement.offsetLeft, this.domElement.offsetTop ]
  146. };
  147. } else {
  148. return {
  149. size: [ window.innerWidth, window.innerHeight ],
  150. offset: [ 0, 0 ]
  151. };
  152. }
  153. };
  154. function bind( scope, fn ) {
  155. return function () {
  156. fn.apply( scope, arguments );
  157. };
  158. }
  159. function contextmenu( event ) {
  160. event.preventDefault();
  161. }
  162. this.dispose = function () {
  163. this.domElement.removeEventListener( 'contextmenu', contextmenu );
  164. this.domElement.removeEventListener( 'mousedown', _mousedown );
  165. this.domElement.removeEventListener( 'mousemove', _mousemove );
  166. this.domElement.removeEventListener( 'mouseup', _mouseup );
  167. window.removeEventListener( 'keydown', _keydown );
  168. window.removeEventListener( 'keyup', _keyup );
  169. };
  170. var _mousemove = bind( this, this.mousemove );
  171. var _mousedown = bind( this, this.mousedown );
  172. var _mouseup = bind( this, this.mouseup );
  173. var _keydown = bind( this, this.keydown );
  174. var _keyup = bind( this, this.keyup );
  175. this.domElement.addEventListener( 'contextmenu', contextmenu );
  176. this.domElement.addEventListener( 'mousemove', _mousemove );
  177. this.domElement.addEventListener( 'mousedown', _mousedown );
  178. this.domElement.addEventListener( 'mouseup', _mouseup );
  179. window.addEventListener( 'keydown', _keydown );
  180. window.addEventListener( 'keyup', _keyup );
  181. this.updateMovementVector();
  182. this.updateRotationVector();
  183. };
  184. THREE.FlyControls.prototype = Object.create( THREE.EventDispatcher.prototype );
  185. THREE.FlyControls.prototype.constructor = THREE.FlyControls;