OrthographicCamera.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { Camera } from './Camera.js';
  2. import { Object3D } from '../core/Object3D.js';
  3. function OrthographicCamera( left = - 1, right = 1, top = 1, bottom = - 1, near = 0.1, far = 2000 ) {
  4. Camera.call( this );
  5. this.type = 'OrthographicCamera';
  6. this.zoom = 1;
  7. this.view = null;
  8. this.left = left;
  9. this.right = right;
  10. this.top = top;
  11. this.bottom = bottom;
  12. this.near = near;
  13. this.far = far;
  14. this.updateProjectionMatrix();
  15. }
  16. OrthographicCamera.prototype = Object.assign( Object.create( Camera.prototype ), {
  17. constructor: OrthographicCamera,
  18. isOrthographicCamera: true,
  19. copy: function ( source, recursive ) {
  20. Camera.prototype.copy.call( this, source, recursive );
  21. this.left = source.left;
  22. this.right = source.right;
  23. this.top = source.top;
  24. this.bottom = source.bottom;
  25. this.near = source.near;
  26. this.far = source.far;
  27. this.zoom = source.zoom;
  28. this.view = source.view === null ? null : Object.assign( {}, source.view );
  29. return this;
  30. },
  31. setViewOffset: function ( fullWidth, fullHeight, x, y, width, height ) {
  32. if ( this.view === null ) {
  33. this.view = {
  34. enabled: true,
  35. fullWidth: 1,
  36. fullHeight: 1,
  37. offsetX: 0,
  38. offsetY: 0,
  39. width: 1,
  40. height: 1
  41. };
  42. }
  43. this.view.enabled = true;
  44. this.view.fullWidth = fullWidth;
  45. this.view.fullHeight = fullHeight;
  46. this.view.offsetX = x;
  47. this.view.offsetY = y;
  48. this.view.width = width;
  49. this.view.height = height;
  50. this.updateProjectionMatrix();
  51. },
  52. clearViewOffset: function () {
  53. if ( this.view !== null ) {
  54. this.view.enabled = false;
  55. }
  56. this.updateProjectionMatrix();
  57. },
  58. updateProjectionMatrix: function () {
  59. const dx = ( this.right - this.left ) / ( 2 * this.zoom );
  60. const dy = ( this.top - this.bottom ) / ( 2 * this.zoom );
  61. const cx = ( this.right + this.left ) / 2;
  62. const cy = ( this.top + this.bottom ) / 2;
  63. let left = cx - dx;
  64. let right = cx + dx;
  65. let top = cy + dy;
  66. let bottom = cy - dy;
  67. if ( this.view !== null && this.view.enabled ) {
  68. const scaleW = ( this.right - this.left ) / this.view.fullWidth / this.zoom;
  69. const scaleH = ( this.top - this.bottom ) / this.view.fullHeight / this.zoom;
  70. left += scaleW * this.view.offsetX;
  71. right = left + scaleW * this.view.width;
  72. top -= scaleH * this.view.offsetY;
  73. bottom = top - scaleH * this.view.height;
  74. }
  75. this.projectionMatrix.makeOrthographic( left, right, top, bottom, this.near, this.far );
  76. this.projectionMatrixInverse.copy( this.projectionMatrix ).invert();
  77. },
  78. toJSON: function ( meta ) {
  79. const data = Object3D.prototype.toJSON.call( this, meta );
  80. data.object.zoom = this.zoom;
  81. data.object.left = this.left;
  82. data.object.right = this.right;
  83. data.object.top = this.top;
  84. data.object.bottom = this.bottom;
  85. data.object.near = this.near;
  86. data.object.far = this.far;
  87. if ( this.view !== null ) data.object.view = Object.assign( {}, this.view );
  88. return data;
  89. }
  90. } );
  91. export { OrthographicCamera };
粤ICP备19079148号