FlyControls.js 7.7 KB

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