FlyControls.js 6.8 KB

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