PerspectiveCamera.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author greggman / http://games.greggman.com/
  4. * @author zz85 / http://www.lab4games.net/zz85/blog
  5. */
  6. THREE.PerspectiveCamera = function ( fov, aspect, near, far ) {
  7. THREE.Camera.call( this );
  8. this.type = 'PerspectiveCamera';
  9. this.zoom = 1;
  10. this.fov = fov !== undefined ? fov : 50;
  11. this.aspect = aspect !== undefined ? aspect : 1;
  12. this.near = near !== undefined ? near : 0.1;
  13. this.far = far !== undefined ? far : 2000;
  14. this.updateProjectionMatrix();
  15. };
  16. THREE.PerspectiveCamera.prototype = Object.create( THREE.Camera.prototype );
  17. THREE.PerspectiveCamera.prototype.constructor = THREE.PerspectiveCamera;
  18. /**
  19. * Uses Focal Length (in mm) to estimate and set FOV
  20. * 35mm (full-frame) camera is used if frame size is not specified;
  21. * Formula based on http://www.bobatkins.com/photography/technical/field_of_view.html
  22. */
  23. THREE.PerspectiveCamera.prototype.setLens = function ( focalLength, frameHeight ) {
  24. if ( frameHeight === undefined ) frameHeight = 24;
  25. this.fov = 2 * THREE.Math.radToDeg( Math.atan( frameHeight / ( focalLength * 2 ) ) );
  26. this.updateProjectionMatrix();
  27. };
  28. /**
  29. * Sets an offset in a larger frustum. This is useful for multi-window or
  30. * multi-monitor/multi-machine setups.
  31. *
  32. * For example, if you have 3x2 monitors and each monitor is 1920x1080 and
  33. * the monitors are in grid like this
  34. *
  35. * +---+---+---+
  36. * | A | B | C |
  37. * +---+---+---+
  38. * | D | E | F |
  39. * +---+---+---+
  40. *
  41. * then for each monitor you would call it like this
  42. *
  43. * var w = 1920;
  44. * var h = 1080;
  45. * var fullWidth = w * 3;
  46. * var fullHeight = h * 2;
  47. *
  48. * --A--
  49. * camera.setOffset( fullWidth, fullHeight, w * 0, h * 0, w, h );
  50. * --B--
  51. * camera.setOffset( fullWidth, fullHeight, w * 1, h * 0, w, h );
  52. * --C--
  53. * camera.setOffset( fullWidth, fullHeight, w * 2, h * 0, w, h );
  54. * --D--
  55. * camera.setOffset( fullWidth, fullHeight, w * 0, h * 1, w, h );
  56. * --E--
  57. * camera.setOffset( fullWidth, fullHeight, w * 1, h * 1, w, h );
  58. * --F--
  59. * camera.setOffset( fullWidth, fullHeight, w * 2, h * 1, w, h );
  60. *
  61. * Note there is no reason monitors have to be the same size or in a grid.
  62. */
  63. THREE.PerspectiveCamera.prototype.setViewOffset = function ( fullWidth, fullHeight, x, y, width, height ) {
  64. this.fullWidth = fullWidth;
  65. this.fullHeight = fullHeight;
  66. this.x = x;
  67. this.y = y;
  68. this.width = width;
  69. this.height = height;
  70. this.updateProjectionMatrix();
  71. };
  72. THREE.PerspectiveCamera.prototype.updateProjectionMatrix = function () {
  73. var fov = THREE.Math.radToDeg( 2 * Math.atan( Math.tan( THREE.Math.degToRad( this.fov ) * 0.5 ) / this.zoom ) );
  74. if ( this.fullWidth ) {
  75. var aspect = this.fullWidth / this.fullHeight;
  76. var top = Math.tan( THREE.Math.degToRad( fov * 0.5 ) ) * this.near;
  77. var bottom = - top;
  78. var left = aspect * bottom;
  79. var right = aspect * top;
  80. var width = Math.abs( right - left );
  81. var height = Math.abs( top - bottom );
  82. this.projectionMatrix.makeFrustum(
  83. left + this.x * width / this.fullWidth,
  84. left + ( this.x + this.width ) * width / this.fullWidth,
  85. top - ( this.y + this.height ) * height / this.fullHeight,
  86. top - this.y * height / this.fullHeight,
  87. this.near,
  88. this.far
  89. );
  90. } else {
  91. this.projectionMatrix.makePerspective( fov, this.aspect, this.near, this.far );
  92. }
  93. };
  94. THREE.PerspectiveCamera.prototype.clone = function () {
  95. var camera = new THREE.PerspectiveCamera();
  96. THREE.Camera.prototype.clone.call( this, camera );
  97. camera.zoom = this.zoom;
  98. camera.fov = this.fov;
  99. camera.aspect = this.aspect;
  100. camera.near = this.near;
  101. camera.far = this.far;
  102. return camera;
  103. };
  104. THREE.PerspectiveCamera.prototype.toJSON = function ( meta ) {
  105. var data = THREE.Object3D.prototype.toJSON.call( this, meta );
  106. data.object.zoom = this.zoom;
  107. data.object.fov = this.fov;
  108. data.object.aspect = this.aspect;
  109. data.object.near = this.near;
  110. data.object.far = this.far;
  111. return data;
  112. };
粤ICP备19079148号