FlyCamera.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**
  2. * @author James Baicoianu / http://www.baicoianu.com/
  3. * parameters = {
  4. * fov: <float>,
  5. * aspect: <float>,
  6. * near: <float>,
  7. * far: <float>,
  8. * target: <THREE.Object3D>,
  9. * movementSpeed: <float>,
  10. * rollSpeed: <float>,
  11. * noFly: <bool>,
  12. * lookVertical: <bool>,
  13. * autoForward: <bool>,
  14. * heightSpeed: <bool>,
  15. * heightCoef: <float>,
  16. * heightMin: <float>,
  17. * heightMax: <float>,
  18. * domElement: <HTMLElement>,
  19. * }
  20. */
  21. THREE.FlyCamera = function ( parameters ) {
  22. THREE.Camera.call( this, parameters.fov, parameters.aspect, parameters.near, parameters.far, parameters.target );
  23. this.tmpQuaternion = new THREE.Quaternion();
  24. this.movementSpeed = 1.0;
  25. this.rollSpeed = 0.005;
  26. this.dragToLook = false;
  27. this.autoForward = false;
  28. this.domElement = document;
  29. if ( parameters ) {
  30. if ( parameters.movementSpeed !== undefined ) this.movementSpeed = parameters.movementSpeed;
  31. if ( parameters.rollSpeed !== undefined ) this.rollSpeed = parameters.rollSpeed;
  32. if ( parameters.dragToLook !== undefined ) this.dragToLook = parameters.dragToLook;
  33. if ( parameters.autoForward !== undefined ) this.autoForward = parameters.autoForward;
  34. if ( parameters.domElement !== undefined ) this.domElement = parameters.domElement;
  35. }
  36. this.useTarget = false;
  37. this.useQuaternion = true;
  38. this.mouseStatus = 0;
  39. 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 };
  40. this.moveVector = new THREE.Vector3( 0, 0, 0 );
  41. this.rotationVector = new THREE.Vector3( 0, 0, 0 );
  42. this.lastUpdate = -1;
  43. this.tdiff = 0;
  44. this.handleEvent = function ( event ) {
  45. if ( typeof this[ event.type ] == 'function' ) {
  46. this[ event.type ]( event );
  47. }
  48. };
  49. this.keydown = function( event ) {
  50. if ( event.altKey ) {
  51. return;
  52. }
  53. switch( event.keyCode ) {
  54. case 16: /* shift */ this.movementSpeedMultiplier = .1; break;
  55. case 87: /*W*/ this.moveState.forward = 1; break;
  56. case 83: /*S*/ this.moveState.back = 1; break;
  57. case 65: /*A*/ this.moveState.left = 1; break;
  58. case 68: /*D*/ this.moveState.right = 1; break;
  59. case 82: /*R*/ this.moveState.up = 1; break;
  60. case 70: /*F*/ this.moveState.down = 1; break;
  61. case 38: /*up*/ this.moveState.pitchUp = 1; break;
  62. case 40: /*down*/ this.moveState.pitchDown = 1; break;
  63. case 37: /*left*/ this.moveState.yawLeft = 1; break;
  64. case 39: /*right*/ this.moveState.yawRight = 1; break;
  65. case 81: /*Q*/ this.moveState.rollLeft = 1; break;
  66. case 69: /*E*/ this.moveState.rollRight = 1; break;
  67. }
  68. this.updateMovementVector();
  69. this.updateRotationVector();
  70. };
  71. this.keyup = function( event ) {
  72. switch( event.keyCode ) {
  73. case 16: /* shift */ this.movementSpeedMultiplier = 1; break;
  74. case 87: /*W*/ this.moveState.forward = 0; break;
  75. case 83: /*S*/ this.moveState.back = 0; break;
  76. case 65: /*A*/ this.moveState.left = 0; break;
  77. case 68: /*D*/ this.moveState.right = 0; break;
  78. case 82: /*R*/ this.moveState.up = 0; break;
  79. case 70: /*F*/ this.moveState.down = 0; break;
  80. case 38: /*up*/ this.moveState.pitchUp = 0; break;
  81. case 40: /*down*/ this.moveState.pitchDown = 0; break;
  82. case 37: /*left*/ this.moveState.yawLeft = 0; break;
  83. case 39: /*right*/ this.moveState.yawRight = 0; break;
  84. case 81: /*Q*/ this.moveState.rollLeft = 0; break;
  85. case 69: /*E*/ this.moveState.rollRight = 0; break;
  86. }
  87. this.updateMovementVector();
  88. this.updateRotationVector();
  89. };
  90. this.mousedown = function(event) {
  91. event.preventDefault();
  92. event.stopPropagation();
  93. if ( this.dragToLook ) {
  94. this.mouseStatus ++;
  95. } else {
  96. switch ( event.button ) {
  97. case 0: this.moveForward = true; break;
  98. case 2: this.moveBackward = true; break;
  99. }
  100. }
  101. };
  102. this.mousemove = function( event ) {
  103. if ( !this.dragToLook || this.mouseStatus > 0 ) {
  104. var container = this.getContainerDimensions();
  105. var halfWidth = container.size[ 0 ] / 2;
  106. var halfHeight = container.size[ 1 ] / 2;
  107. this.moveState.yawLeft = - ( ( event.clientX - container.offset[ 0 ] ) - halfWidth ) / halfWidth;
  108. this.moveState.pitchDown = ( ( event.clientY - container.offset[ 1 ] ) - halfHeight ) / halfHeight;
  109. this.updateRotationVector();
  110. }
  111. };
  112. this.mouseup = function( event ) {
  113. event.preventDefault();
  114. event.stopPropagation();
  115. if ( this.dragToLook ) {
  116. this.mouseStatus --;
  117. //this.mouseX = this.mouseY = 0;
  118. this.moveState.yawLeft = this.moveState.pitchDown = 0;
  119. } else {
  120. switch ( event.button ) {
  121. case 0: this.moveForward = false; break;
  122. case 2: this.moveBackward = false; break;
  123. }
  124. }
  125. this.updateRotationVector();
  126. };
  127. this.update = function( parentMatrixWorld, forceUpdate, camera ) {
  128. var now = new Date().getTime();
  129. if ( this.lastUpdate == -1 ) this.lastUpdate = now;
  130. this.tdiff = ( now - this.lastUpdate ) / 1000;
  131. this.lastUpdate = now;
  132. //this.position.addSelf( this.moveVector.multiplyScalar( tdiff ) );
  133. var moveMult = this.tdiff * this.movementSpeed;
  134. var rotMult = this.tdiff * this.rollSpeed;
  135. this.translateX( this.moveVector.x * moveMult );
  136. this.translateY( this.moveVector.y * moveMult );
  137. this.translateZ( this.moveVector.z * moveMult );
  138. this.tmpQuaternion.set( this.rotationVector.x * rotMult, this.rotationVector.y * rotMult, this.rotationVector.z * rotMult, 1 ).normalize();
  139. this.quaternion.multiplySelf( this.tmpQuaternion );
  140. this.matrix.setPosition( this.position );
  141. this.matrix.setRotationFromQuaternion( this.quaternion );
  142. this.matrixWorldNeedsUpdate = true;
  143. this.supr.update.call( this );
  144. };
  145. this.updateMovementVector = function() {
  146. var forward = ( this.moveState.forward || ( this.autoForward && !this.moveState.back ) ) ? 1 : 0;
  147. this.moveVector.x = ( -this.moveState.left + this.moveState.right );
  148. this.moveVector.y = ( -this.moveState.down + this.moveState.up );
  149. this.moveVector.z = ( -forward + this.moveState.back );
  150. //console.log( 'move:', [ this.moveVector.x, this.moveVector.y, this.moveVector.z ] );
  151. };
  152. this.updateRotationVector = function() {
  153. this.rotationVector.x = ( -this.moveState.pitchDown + this.moveState.pitchUp );
  154. this.rotationVector.y = ( -this.moveState.yawRight + this.moveState.yawLeft );
  155. this.rotationVector.z = ( -this.moveState.rollRight + this.moveState.rollLeft );
  156. //console.log( 'rotate:', [ this.rotationVector.x, this.rotationVector.y, this.rotationVector.z ] );
  157. };
  158. this.getContainerDimensions = function() {
  159. if ( this.domElement != document ) {
  160. return {
  161. size : [ this.domElement.offsetWidth, this.domElement.offsetHeight ],
  162. offset : [ this.domElement.offsetLeft, this.domElement.offsetTop ]
  163. };
  164. } else {
  165. return {
  166. size : [ window.innerWidth, window.innerHeight ],
  167. offset : [ 0, 0 ]
  168. };
  169. }
  170. };
  171. function bind( scope, fn ) {
  172. return function () {
  173. fn.apply( scope, arguments );
  174. };
  175. };
  176. this.domElement.addEventListener( 'mousemove', bind( this, this.mousemove ), false );
  177. this.domElement.addEventListener( 'mousedown', bind( this, this.mousedown ), false );
  178. this.domElement.addEventListener( 'mouseup', bind( this, this.mouseup ), false );
  179. window.addEventListener( 'keydown', bind( this, this.keydown ), false );
  180. window.addEventListener( 'keyup', bind( this, this.keyup ), false );
  181. this.updateMovementVector();
  182. this.updateRotationVector();
  183. };
  184. THREE.FlyCamera.prototype = new THREE.Camera();
  185. THREE.FlyCamera.prototype.constructor = THREE.FlyCamera;
  186. THREE.FlyCamera.prototype.supr = THREE.Camera.prototype;
粤ICP备19079148号