FlyControls.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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.handleEvent = function ( event ) {
  21. if ( typeof this[ event.type ] == 'function' ) {
  22. this[ event.type ]( event );
  23. }
  24. };
  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( delta ) {
  109. var moveMult = delta * this.movementSpeed;
  110. var rotMult = delta * this.rollSpeed;
  111. this.object.translateX( this.moveVector.x * moveMult );
  112. this.object.translateY( this.moveVector.y * moveMult );
  113. this.object.translateZ( this.moveVector.z * moveMult );
  114. this.tmpQuaternion.set( this.rotationVector.x * rotMult, this.rotationVector.y * rotMult, this.rotationVector.z * rotMult, 1 ).normalize();
  115. this.object.quaternion.multiply( this.tmpQuaternion );
  116. // expose the rotation vector for convenience
  117. this.object.rotation.setFromQuaternion( this.object.quaternion, this.object.rotation.order );
  118. };
  119. this.updateMovementVector = function() {
  120. var forward = ( this.moveState.forward || ( this.autoForward && ! this.moveState.back ) ) ? 1 : 0;
  121. this.moveVector.x = ( - this.moveState.left + this.moveState.right );
  122. this.moveVector.y = ( - this.moveState.down + this.moveState.up );
  123. this.moveVector.z = ( - forward + this.moveState.back );
  124. //console.log( 'move:', [ this.moveVector.x, this.moveVector.y, this.moveVector.z ] );
  125. };
  126. this.updateRotationVector = function() {
  127. this.rotationVector.x = ( - this.moveState.pitchDown + this.moveState.pitchUp );
  128. this.rotationVector.y = ( - this.moveState.yawRight + this.moveState.yawLeft );
  129. this.rotationVector.z = ( - this.moveState.rollRight + this.moveState.rollLeft );
  130. //console.log( 'rotate:', [ this.rotationVector.x, this.rotationVector.y, this.rotationVector.z ] );
  131. };
  132. this.getContainerDimensions = function() {
  133. if ( this.domElement != document ) {
  134. return {
  135. size : [ this.domElement.offsetWidth, this.domElement.offsetHeight ],
  136. offset : [ this.domElement.offsetLeft, this.domElement.offsetTop ]
  137. };
  138. } else {
  139. return {
  140. size : [ window.innerWidth, window.innerHeight ],
  141. offset : [ 0, 0 ]
  142. };
  143. }
  144. };
  145. function bind( scope, fn ) {
  146. return function () {
  147. fn.apply( scope, arguments );
  148. };
  149. }
  150. function contextmenu( event ) {
  151. event.preventDefault();
  152. }
  153. this.dispose = function() {
  154. this.domElement.removeEventListener( 'contextmenu', contextmenu, false );
  155. this.domElement.removeEventListener( 'mousedown', _mousedown, false );
  156. this.domElement.removeEventListener( 'mousemove', _mousemove, false );
  157. this.domElement.removeEventListener( 'mouseup', _mouseup, false );
  158. window.removeEventListener( 'keydown', _keydown, false );
  159. window.removeEventListener( 'keyup', _keyup, false );
  160. };
  161. var _mousemove = bind( this, this.mousemove );
  162. var _mousedown = bind( this, this.mousedown );
  163. var _mouseup = bind( this, this.mouseup );
  164. var _keydown = bind( this, this.keydown );
  165. var _keyup = bind( this, this.keyup );
  166. this.domElement.addEventListener( 'contextmenu', contextmenu, false );
  167. this.domElement.addEventListener( 'mousemove', _mousemove, false );
  168. this.domElement.addEventListener( 'mousedown', _mousedown, false );
  169. this.domElement.addEventListener( 'mouseup', _mouseup, false );
  170. window.addEventListener( 'keydown', _keydown, false );
  171. window.addEventListener( 'keyup', _keyup, false );
  172. this.updateMovementVector();
  173. this.updateRotationVector();
  174. };