PerspectiveCamera.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import { Camera } from './Camera';
  2. import { Object3D } from '../core/Object3D';
  3. import { _Math } from '../math/Math';
  4. /**
  5. * @author mrdoob / http://mrdoob.com/
  6. * @author greggman / http://games.greggman.com/
  7. * @author zz85 / http://www.lab4games.net/zz85/blog
  8. * @author tschw
  9. */
  10. function PerspectiveCamera( fov, aspect, near, far ) {
  11. Camera.call( this );
  12. this.type = 'PerspectiveCamera';
  13. this.fov = fov !== undefined ? fov : 50;
  14. this.zoom = 1;
  15. this.near = near !== undefined ? near : 0.1;
  16. this.far = far !== undefined ? far : 2000;
  17. this.focus = 10;
  18. this.aspect = aspect !== undefined ? aspect : 1;
  19. this.view = null;
  20. this.filmGauge = 35; // width of the film (default in millimeters)
  21. this.filmOffset = 0; // horizontal film offset (same unit as gauge)
  22. this.updateProjectionMatrix();
  23. }
  24. PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ), {
  25. constructor: PerspectiveCamera,
  26. isPerspectiveCamera: true,
  27. copy: function ( source ) {
  28. Camera.prototype.copy.call( this, source );
  29. this.fov = source.fov;
  30. this.zoom = source.zoom;
  31. this.near = source.near;
  32. this.far = source.far;
  33. this.focus = source.focus;
  34. this.aspect = source.aspect;
  35. this.view = source.view === null ? null : Object.assign( {}, source.view );
  36. this.filmGauge = source.filmGauge;
  37. this.filmOffset = source.filmOffset;
  38. return this;
  39. },
  40. /**
  41. * Sets the FOV by focal length in respect to the current .filmGauge.
  42. *
  43. * The default film gauge is 35, so that the focal length can be specified for
  44. * a 35mm (full frame) camera.
  45. *
  46. * Values for focal length and film gauge must have the same unit.
  47. */
  48. setFocalLength: function ( focalLength ) {
  49. // see http://www.bobatkins.com/photography/technical/field_of_view.html
  50. var vExtentSlope = 0.5 * this.getFilmHeight() / focalLength;
  51. this.fov = _Math.RAD2DEG * 2 * Math.atan( vExtentSlope );
  52. this.updateProjectionMatrix();
  53. },
  54. /**
  55. * Calculates the focal length from the current .fov and .filmGauge.
  56. */
  57. getFocalLength: function () {
  58. var vExtentSlope = Math.tan( _Math.DEG2RAD * 0.5 * this.fov );
  59. return 0.5 * this.getFilmHeight() / vExtentSlope;
  60. },
  61. getEffectiveFOV: function () {
  62. return _Math.RAD2DEG * 2 * Math.atan(
  63. Math.tan( _Math.DEG2RAD * 0.5 * this.fov ) / this.zoom );
  64. },
  65. getFilmWidth: function () {
  66. // film not completely covered in portrait format (aspect < 1)
  67. return this.filmGauge * Math.min( this.aspect, 1 );
  68. },
  69. getFilmHeight: function () {
  70. // film not completely covered in landscape format (aspect > 1)
  71. return this.filmGauge / Math.max( this.aspect, 1 );
  72. },
  73. /**
  74. * Sets an offset in a larger frustum. This is useful for multi-window or
  75. * multi-monitor/multi-machine setups.
  76. *
  77. * For example, if you have 3x2 monitors and each monitor is 1920x1080 and
  78. * the monitors are in grid like this
  79. *
  80. * +---+---+---+
  81. * | A | B | C |
  82. * +---+---+---+
  83. * | D | E | F |
  84. * +---+---+---+
  85. *
  86. * then for each monitor you would call it like this
  87. *
  88. * var w = 1920;
  89. * var h = 1080;
  90. * var fullWidth = w * 3;
  91. * var fullHeight = h * 2;
  92. *
  93. * --A--
  94. * camera.setOffset( fullWidth, fullHeight, w * 0, h * 0, w, h );
  95. * --B--
  96. * camera.setOffset( fullWidth, fullHeight, w * 1, h * 0, w, h );
  97. * --C--
  98. * camera.setOffset( fullWidth, fullHeight, w * 2, h * 0, w, h );
  99. * --D--
  100. * camera.setOffset( fullWidth, fullHeight, w * 0, h * 1, w, h );
  101. * --E--
  102. * camera.setOffset( fullWidth, fullHeight, w * 1, h * 1, w, h );
  103. * --F--
  104. * camera.setOffset( fullWidth, fullHeight, w * 2, h * 1, w, h );
  105. *
  106. * Note there is no reason monitors have to be the same size or in a grid.
  107. */
  108. setViewOffset: function ( fullWidth, fullHeight, x, y, width, height ) {
  109. this.aspect = fullWidth / fullHeight;
  110. this.view = {
  111. fullWidth: fullWidth,
  112. fullHeight: fullHeight,
  113. offsetX: x,
  114. offsetY: y,
  115. width: width,
  116. height: height
  117. };
  118. this.updateProjectionMatrix();
  119. },
  120. clearViewOffset: function() {
  121. this.view = null;
  122. this.updateProjectionMatrix();
  123. },
  124. updateProjectionMatrix: function () {
  125. var near = this.near,
  126. top = near * Math.tan(
  127. _Math.DEG2RAD * 0.5 * this.fov ) / this.zoom,
  128. height = 2 * top,
  129. width = this.aspect * height,
  130. left = - 0.5 * width,
  131. view = this.view;
  132. if ( view !== null ) {
  133. var fullWidth = view.fullWidth,
  134. fullHeight = view.fullHeight;
  135. left += view.offsetX * width / fullWidth;
  136. top -= view.offsetY * height / fullHeight;
  137. width *= view.width / fullWidth;
  138. height *= view.height / fullHeight;
  139. }
  140. var skew = this.filmOffset;
  141. if ( skew !== 0 ) left += near * skew / this.getFilmWidth();
  142. this.projectionMatrix.makePerspective( left, left + width, top, top - height, near, this.far );
  143. },
  144. toJSON: function ( meta ) {
  145. var data = Object3D.prototype.toJSON.call( this, meta );
  146. data.object.fov = this.fov;
  147. data.object.zoom = this.zoom;
  148. data.object.near = this.near;
  149. data.object.far = this.far;
  150. data.object.focus = this.focus;
  151. data.object.aspect = this.aspect;
  152. if ( this.view !== null ) data.object.view = Object.assign( {}, this.view );
  153. data.object.filmGauge = this.filmGauge;
  154. data.object.filmOffset = this.filmOffset;
  155. return data;
  156. }
  157. } );
  158. export { PerspectiveCamera };
粤ICP备19079148号