FlyControls.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.handleEvent = function ( event ) {
  22. if ( typeof this[ event.type ] == 'function' ) {
  23. this[ event.type ]( event );
  24. }
  25. };
  26. this.keydown = function( event ) {
  27. if ( event.altKey ) {
  28. return;
  29. }
  30. //event.preventDefault();
  31. switch ( event.keyCode ) {
  32. case 16: /* shift */ this.movementSpeedMultiplier = .1; break;
  33. case 87: /*W*/ this.moveState.forward = 1; break;
  34. case 83: /*S*/ this.moveState.back = 1; break;
  35. case 65: /*A*/ this.moveState.left = 1; break;
  36. case 68: /*D*/ this.moveState.right = 1; break;
  37. case 82: /*R*/ this.moveState.up = 1; break;
  38. case 70: /*F*/ this.moveState.down = 1; break;
  39. case 38: /*up*/ this.moveState.pitchUp = 1; break;
  40. case 40: /*down*/ this.moveState.pitchDown = 1; break;
  41. case 37: /*left*/ this.moveState.yawLeft = 1; break;
  42. case 39: /*right*/ this.moveState.yawRight = 1; break;
  43. case 81: /*Q*/ this.moveState.rollLeft = 1; break;
  44. case 69: /*E*/ this.moveState.rollRight = 1; break;
  45. }
  46. this.updateMovementVector();
  47. this.updateRotationVector();
  48. };
  49. this.keyup = function( event ) {
  50. switch( event.keyCode ) {
  51. case 16: /* shift */ this.movementSpeedMultiplier = 1; break;
  52. case 87: /*W*/ this.moveState.forward = 0; break;
  53. case 83: /*S*/ this.moveState.back = 0; break;
  54. case 65: /*A*/ this.moveState.left = 0; break;
  55. case 68: /*D*/ this.moveState.right = 0; break;
  56. case 82: /*R*/ this.moveState.up = 0; break;
  57. case 70: /*F*/ this.moveState.down = 0; break;
  58. case 38: /*up*/ this.moveState.pitchUp = 0; break;
  59. case 40: /*down*/ this.moveState.pitchDown = 0; break;
  60. case 37: /*left*/ this.moveState.yawLeft = 0; break;
  61. case 39: /*right*/ this.moveState.yawRight = 0; break;
  62. case 81: /*Q*/ this.moveState.rollLeft = 0; break;
  63. case 69: /*E*/ 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. event.stopPropagation();
  74. if ( this.dragToLook ) {
  75. this.mouseStatus ++;
  76. } else {
  77. switch ( event.button ) {
  78. case 0: this.object.moveForward = true; break;
  79. case 2: this.object.moveBackward = true; break;
  80. }
  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.moveForward = false; break;
  102. case 2: this.moveBackward = false; break;
  103. }
  104. }
  105. this.updateRotationVector();
  106. };
  107. this.update = function( delta ) {
  108. var moveMult = delta * this.movementSpeed;
  109. var rotMult = delta * this.rollSpeed;
  110. this.object.translateX( this.moveVector.x * moveMult );
  111. this.object.translateY( this.moveVector.y * moveMult );
  112. this.object.translateZ( this.moveVector.z * moveMult );
  113. this.tmpQuaternion.set( this.rotationVector.x * rotMult, this.rotationVector.y * rotMult, this.rotationVector.z * rotMult, 1 ).normalize();
  114. this.object.quaternion.multiply( this.tmpQuaternion );
  115. // expose the rotation vector for convenience
  116. this.object.rotation.setEulerFromQuaternion( this.object.quaternion, this.object.eulerOrder );
  117. };
  118. this.updateMovementVector = function() {
  119. var forward = ( this.moveState.forward || ( this.autoForward && !this.moveState.back ) ) ? 1 : 0;
  120. this.moveVector.x = ( -this.moveState.left + this.moveState.right );
  121. this.moveVector.y = ( -this.moveState.down + this.moveState.up );
  122. this.moveVector.z = ( -forward + this.moveState.back );
  123. //console.log( 'move:', [ this.moveVector.x, this.moveVector.y, this.moveVector.z ] );
  124. };
  125. this.updateRotationVector = function() {
  126. this.rotationVector.x = ( -this.moveState.pitchDown + this.moveState.pitchUp );
  127. this.rotationVector.y = ( -this.moveState.yawRight + this.moveState.yawLeft );
  128. this.rotationVector.z = ( -this.moveState.rollRight + this.moveState.rollLeft );
  129. //console.log( 'rotate:', [ this.rotationVector.x, this.rotationVector.y, this.rotationVector.z ] );
  130. };
  131. this.getContainerDimensions = function() {
  132. if ( this.domElement != document ) {
  133. return {
  134. size : [ this.domElement.offsetWidth, this.domElement.offsetHeight ],
  135. offset : [ this.domElement.offsetLeft, this.domElement.offsetTop ]
  136. };
  137. } else {
  138. return {
  139. size : [ window.innerWidth, window.innerHeight ],
  140. offset : [ 0, 0 ]
  141. };
  142. }
  143. };
  144. function bind( scope, fn ) {
  145. return function () {
  146. fn.apply( scope, arguments );
  147. };
  148. };
  149. this.domElement.addEventListener( 'mousemove', bind( this, this.mousemove ), false );
  150. this.domElement.addEventListener( 'mousedown', bind( this, this.mousedown ), false );
  151. this.domElement.addEventListener( 'mouseup', bind( this, this.mouseup ), false );
  152. this.domElement.addEventListener( 'keydown', bind( this, this.keydown ), false );
  153. this.domElement.addEventListener( 'keyup', bind( this, this.keyup ), false );
  154. this.updateMovementVector();
  155. this.updateRotationVector();
  156. };