Camera.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. * @author mikael emtinger / http://gomo.se/
  4. * @author greggman / http://games.greggman.com/
  5. */
  6. THREE.Camera = function ( fov, aspect, near, far, target ) {
  7. THREE.Object3D.call( this );
  8. this.fov = fov || 50;
  9. this.aspect = aspect || 1;
  10. this.near = ( near !== undefined ) ? near : 0.1;
  11. this.far = ( far !== undefined ) ? far : 2000;
  12. this.target = target || new THREE.Object3D();
  13. this.useTarget = true;
  14. this.matrixWorldInverse = new THREE.Matrix4();
  15. this.projectionMatrix = null;
  16. this.updateProjectionMatrix();
  17. };
  18. THREE.Camera.prototype = new THREE.Object3D();
  19. THREE.Camera.prototype.constructor = THREE.Camera;
  20. THREE.Camera.prototype.supr = THREE.Object3D.prototype;
  21. THREE.Camera.prototype.translate = function ( distance, axis ) {
  22. this.matrix.rotateAxis( axis );
  23. axis.multiplyScalar( distance )
  24. this.position.addSelf( axis );
  25. this.target.position.addSelf( axis );
  26. };
  27. THREE.Camera.prototype.updateProjectionMatrix = function () {
  28. if ( this.fullWidth ) {
  29. var aspect = this.fullWidth / this.fullHeight;
  30. var top = Math.tan( this.fov * Math.PI / 360 ) * this.near;
  31. var bottom = -top;
  32. var left = aspect * bottom;
  33. var right = aspect * top;
  34. var width = Math.abs( right - left );
  35. var height = Math.abs( top - bottom );
  36. this.projectionMatrix = THREE.Matrix4.makeFrustum(
  37. left + this.x * width / this.fullWidth,
  38. left + ( this.x + this.width ) * width / this.fullWidth,
  39. top - ( this.y + this.height ) * height / this.fullHeight,
  40. top - this.y * height / this.fullHeight,
  41. this.near,
  42. this.far );
  43. } else {
  44. this.projectionMatrix = THREE.Matrix4.makePerspective( this.fov, this.aspect, this.near, this.far );
  45. }
  46. };
  47. /**
  48. * Sets an offset in a larger frustum. This is useful for multi-window or
  49. * multi-monitor/multi-machine setups.
  50. *
  51. * For example, if you have 3x2 monitors and each monitor is 1920x1080 and
  52. * the monitors are in grid like this
  53. *
  54. * +---+---+---+
  55. * | A | B | C |
  56. * +---+---+---+
  57. * | D | E | F |
  58. * +---+---+---+
  59. *
  60. * then for monitor each monitor you would call it like this
  61. *
  62. * var w = 1920;
  63. * var h = 1080;
  64. * var fullWidth = w * 3;
  65. * var fullHeight = h * 2;
  66. *
  67. * --A--
  68. * camera.setOffset( fullWidth, fullHeight, w * 0, h * 0, w, h );
  69. * --B--
  70. * camera.setOffset( fullWidth, fullHeight, w * 1, h * 0, w, h );
  71. * --C--
  72. * camera.setOffset( fullWidth, fullHeight, w * 2, h * 0, w, h );
  73. * --D--
  74. * camera.setOffset( fullWidth, fullHeight, w * 0, h * 1, w, h );
  75. * --E--
  76. * camera.setOffset( fullWidth, fullHeight, w * 1, h * 1, w, h );
  77. * --F--
  78. * camera.setOffset( fullWidth, fullHeight, w * 2, h * 1, w, h );
  79. *
  80. * Note there is no reason monitors have to be the same size or in a grid.
  81. */
  82. THREE.Camera.prototype.setViewOffset = function( fullWidth, fullHeight, x, y, width, height ) {
  83. this.fullWidth = fullWidth;
  84. this.fullHeight = fullHeight;
  85. this.x = x;
  86. this.y = y;
  87. this.width = width;
  88. this.height = height;
  89. this.updateProjectionMatrix();
  90. };
  91. THREE.Camera.prototype.update = function ( parentMatrixWorld, forceUpdate, camera ) {
  92. if ( this.useTarget ) {
  93. // local
  94. this.matrix.lookAt( this.position, this.target.position, this.up );
  95. this.matrix.setPosition( this.position );
  96. // global
  97. if( parentMatrixWorld ) {
  98. this.matrixWorld.multiply( parentMatrixWorld, this.matrix );
  99. } else {
  100. this.matrixWorld.copy( this.matrix );
  101. }
  102. THREE.Matrix4.makeInvert( this.matrixWorld, this.matrixWorldInverse );
  103. forceUpdate = true;
  104. } else {
  105. this.matrixAutoUpdate && this.updateMatrix();
  106. if ( forceUpdate || this.matrixWorldNeedsUpdate ) {
  107. if ( parentMatrixWorld ) {
  108. this.matrixWorld.multiply( parentMatrixWorld, this.matrix );
  109. } else {
  110. this.matrixWorld.copy( this.matrix );
  111. }
  112. this.matrixWorldNeedsUpdate = false;
  113. forceUpdate = true;
  114. THREE.Matrix4.makeInvert( this.matrixWorld, this.matrixWorldInverse );
  115. }
  116. }
  117. // update children
  118. for ( var i = 0; i < this.children.length; i ++ ) {
  119. this.children[ i ].update( this.matrixWorld, forceUpdate, camera );
  120. }
  121. };
粤ICP备19079148号