FlyControls.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. this.object.useQuaternion = true;
  15. // internals
  16. this.tmpQuaternion = new THREE.Quaternion();
  17. this.mouseStatus = 0;
  18. 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 };
  19. this.moveVector = new THREE.Vector3( 0, 0, 0 );
  20. this.rotationVector = new THREE.Vector3( 0, 0, 0 );
  21. this.lastUpdate = -1;
  22. this.tdiff = 0;
  23. this.handleEvent = function ( event ) {
  24. if ( typeof this[ event.type ] == 'function' ) {
  25. this[ event.type ]( event );
  26. }
  27. };
  28. this.keydown = function( event ) {
  29. if ( event.altKey ) {
  30. return;
  31. }
  32. switch( event.keyCode ) {
  33. case 16: /* shift */ this.movementSpeedMultiplier = .1; break;
  34. case 87: /*W*/ this.moveState.forward = 1; break;
  35. case 83: /*S*/ this.moveState.back = 1; break;
  36. case 65: /*A*/ this.moveState.left = 1; break;
  37. case 68: /*D*/ this.moveState.right = 1; break;
  38. case 82: /*R*/ this.moveState.up = 1; break;
  39. case 70: /*F*/ this.moveState.down = 1; break;
  40. case 38: /*up*/ this.moveState.pitchUp = 1; break;
  41. case 40: /*down*/ this.moveState.pitchDown = 1; break;
  42. case 37: /*left*/ this.moveState.yawLeft = 1; break;
  43. case 39: /*right*/ this.moveState.yawRight = 1; break;
  44. case 81: /*Q*/ this.moveState.rollLeft = 1; break;
  45. case 69: /*E*/ this.moveState.rollRight = 1; break;
  46. }
  47. this.updateMovementVector();
  48. this.updateRotationVector();
  49. };
  50. this.keyup = function( event ) {
  51. switch( event.keyCode ) {
  52. case 16: /* shift */ this.movementSpeedMultiplier = 1; break;
  53. case 87: /*W*/ this.moveState.forward = 0; break;
  54. case 83: /*S*/ this.moveState.back = 0; break;
  55. case 65: /*A*/ this.moveState.left = 0; break;
  56. case 68: /*D*/ this.moveState.right = 0; break;
  57. case 82: /*R*/ this.moveState.up = 0; break;
  58. case 70: /*F*/ this.moveState.down = 0; break;
  59. case 38: /*up*/ this.moveState.pitchUp = 0; break;
  60. case 40: /*down*/ this.moveState.pitchDown = 0; break;
  61. case 37: /*left*/ this.moveState.yawLeft = 0; break;
  62. case 39: /*right*/ this.moveState.yawRight = 0; break;
  63. case 81: /*Q*/ this.moveState.rollLeft = 0; break;
  64. case 69: /*E*/ this.moveState.rollRight = 0; break;
  65. }
  66. this.updateMovementVector();
  67. this.updateRotationVector();
  68. };
  69. this.mousedown = function( event ) {
  70. if ( this.domElement !== document ) {
  71. this.domElement.focus();
  72. }
  73. event.preventDefault();
  74. event.stopPropagation();
  75. if ( this.dragToLook ) {
  76. this.mouseStatus ++;
  77. } else {
  78. switch ( event.button ) {
  79. case 0: this.object.moveForward = true; break;
  80. case 2: this.object.moveBackward = true; break;
  81. }
  82. }
  83. };
  84. this.mousemove = function( event ) {
  85. if ( !this.dragToLook || this.mouseStatus > 0 ) {
  86. var container = this.getContainerDimensions();
  87. var halfWidth = container.size[ 0 ] / 2;
  88. var halfHeight = container.size[ 1 ] / 2;
  89. this.moveState.yawLeft = - ( ( event.pageX - container.offset[ 0 ] ) - halfWidth ) / halfWidth;
  90. this.moveState.pitchDown = ( ( event.pageY - container.offset[ 1 ] ) - halfHeight ) / halfHeight;
  91. this.updateRotationVector();
  92. }
  93. };
  94. this.mouseup = function( event ) {
  95. event.preventDefault();
  96. event.stopPropagation();
  97. if ( this.dragToLook ) {
  98. this.mouseStatus --;
  99. this.moveState.yawLeft = this.moveState.pitchDown = 0;
  100. } else {
  101. switch ( event.button ) {
  102. case 0: this.moveForward = false; break;
  103. case 2: this.moveBackward = false; break;
  104. }
  105. }
  106. this.updateRotationVector();
  107. };
  108. this.update = function( parentMatrixWorld, forceUpdate, camera ) {
  109. var now = new Date().getTime();
  110. if ( this.lastUpdate == -1 ) this.lastUpdate = now;
  111. this.tdiff = ( now - this.lastUpdate ) / 1000;
  112. this.lastUpdate = now;
  113. var moveMult = this.tdiff * this.movementSpeed;
  114. var rotMult = this.tdiff * this.rollSpeed;
  115. this.object.translateX( this.moveVector.x * moveMult );
  116. this.object.translateY( this.moveVector.y * moveMult );
  117. this.object.translateZ( this.moveVector.z * moveMult );
  118. this.tmpQuaternion.set( this.rotationVector.x * rotMult, this.rotationVector.y * rotMult, this.rotationVector.z * rotMult, 1 ).normalize();
  119. this.object.quaternion.multiplySelf( this.tmpQuaternion );
  120. this.object.matrix.setPosition( this.object.position );
  121. this.object.matrix.setRotationFromQuaternion( this.object.quaternion );
  122. this.object.matrixWorldNeedsUpdate = true;
  123. };
  124. this.updateMovementVector = function() {
  125. var forward = ( this.moveState.forward || ( this.autoForward && !this.moveState.back ) ) ? 1 : 0;
  126. this.moveVector.x = ( -this.moveState.left + this.moveState.right );
  127. this.moveVector.y = ( -this.moveState.down + this.moveState.up );
  128. this.moveVector.z = ( -forward + this.moveState.back );
  129. //console.log( 'move:', [ this.moveVector.x, this.moveVector.y, this.moveVector.z ] );
  130. };
  131. this.updateRotationVector = function() {
  132. this.rotationVector.x = ( -this.moveState.pitchDown + this.moveState.pitchUp );
  133. this.rotationVector.y = ( -this.moveState.yawRight + this.moveState.yawLeft );
  134. this.rotationVector.z = ( -this.moveState.rollRight + this.moveState.rollLeft );
  135. //console.log( 'rotate:', [ this.rotationVector.x, this.rotationVector.y, this.rotationVector.z ] );
  136. };
  137. this.getContainerDimensions = function() {
  138. if ( this.domElement != document ) {
  139. return {
  140. size : [ this.domElement.offsetWidth, this.domElement.offsetHeight ],
  141. offset : [ this.domElement.offsetLeft, this.domElement.offsetTop ]
  142. };
  143. } else {
  144. return {
  145. size : [ window.innerWidth, window.innerHeight ],
  146. offset : [ 0, 0 ]
  147. };
  148. }
  149. };
  150. function bind( scope, fn ) {
  151. return function () {
  152. fn.apply( scope, arguments );
  153. };
  154. };
  155. this.domElement.addEventListener( 'mousemove', bind( this, this.mousemove ), false );
  156. this.domElement.addEventListener( 'mousedown', bind( this, this.mousedown ), false );
  157. this.domElement.addEventListener( 'mouseup', bind( this, this.mouseup ), false );
  158. this.domElement.addEventListener( 'keydown', bind( this, this.keydown ), false );
  159. this.domElement.addEventListener( 'keyup', bind( this, this.keyup ), false );
  160. this.updateMovementVector();
  161. this.updateRotationVector();
  162. };