Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

view.ts 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * encantar.js
  3. * GPU-accelerated Augmented Reality for the web
  4. * Copyright (C) 2022-2025 Alexandre Martins <alemartf(at)gmail.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. * view.ts
  20. * A view of the 3D world at a moment in time,
  21. * featuring the means to project points into clip space
  22. */
  23. import Speedy from 'speedy-vision';
  24. import { SpeedyMatrix } from 'speedy-vision/types/core/speedy-matrix';
  25. import { CameraModel } from './camera-model';
  26. import { IllegalArgumentError } from '../utils/errors';
  27. import { Nullable } from '../utils/utils';
  28. /** Default distance of the near plane to the optical center of the camera */
  29. const DEFAULT_NEAR = 0.1;
  30. /** Default distance of the far plane to the optical center of the camera */
  31. const DEFAULT_FAR = 10000 * DEFAULT_NEAR;
  32. /**
  33. * A view of the 3D world at a moment in time,
  34. * featuring the means to project points into clip space
  35. */
  36. export interface View
  37. {
  38. /** A 4x4 matrix that projects the viewer space into the clip space, i.e., [-1,1]^3 */
  39. readonly projectionMatrix: SpeedyMatrix;
  40. /** @internal The inverse of the projection matrix */
  41. readonly _projectionMatrixInverse: SpeedyMatrix;
  42. }
  43. /**
  44. * A PerspectiveView is a View defining a symmetric frustum around the z-axis
  45. * (perspective projection)
  46. */
  47. export class PerspectiveView implements View
  48. {
  49. /** Camera model */
  50. private readonly _camera: CameraModel;
  51. /** Distance of the near plane to the optical center of the camera */
  52. private readonly _near: number;
  53. /** Distance of the far plane to the optical center of the camera*/
  54. private readonly _far: number;
  55. /** A 4x4 matrix that projects viewer space into clip space, i.e., [-1,1]^3 */
  56. private readonly _projectionMatrix: SpeedyMatrix;
  57. /** The inverse of the projection matrix, computed lazily */
  58. private _inverseProjection: Nullable<SpeedyMatrix>;
  59. /**
  60. * Constructor
  61. * @param camera camera model
  62. * @param near distance of the near plane
  63. * @param far distance of the far plane
  64. */
  65. constructor(camera: CameraModel, near: number = DEFAULT_NEAR, far: number = DEFAULT_FAR)
  66. {
  67. this._near = +near;
  68. this._far = +far;
  69. if(this._near >= this._far)
  70. throw new IllegalArgumentError(`View expects near < far (found near = ${this._near} and far = ${this._far})`);
  71. else if(this._near <= 0)
  72. throw new IllegalArgumentError(`View expects a positive near (found ${this._near})`);
  73. this._camera = camera;
  74. this._projectionMatrix = camera.computeProjectionMatrix(this._near, this._far);
  75. this._inverseProjection = null;
  76. }
  77. /**
  78. * A 4x4 projection matrix for WebGL
  79. */
  80. get projectionMatrix(): SpeedyMatrix
  81. {
  82. return this._projectionMatrix;
  83. }
  84. /**
  85. * The inverse of the projection matrix
  86. * @internal
  87. */
  88. get _projectionMatrixInverse(): SpeedyMatrix
  89. {
  90. if(this._inverseProjection === null)
  91. this._inverseProjection = Speedy.Matrix(this._projectionMatrix.inverse());
  92. return this._inverseProjection;
  93. }
  94. /**
  95. * Aspect ratio of the frustum
  96. */
  97. get aspect(): number
  98. {
  99. return this._camera.aspectRatio;
  100. }
  101. /**
  102. * Horizontal field-of-view of the frustum, measured in radians
  103. */
  104. get fovx(): number
  105. {
  106. return this._camera.fovx;
  107. }
  108. /**
  109. * Vertical field-of-view of the frustum, measured in radians
  110. */
  111. get fovy(): number
  112. {
  113. return this._camera.fovy;
  114. }
  115. /**
  116. * Distance of the near plane
  117. */
  118. get near(): number
  119. {
  120. return this._near;
  121. }
  122. /**
  123. * Distance of the far plane
  124. */
  125. get far(): number
  126. {
  127. return this._far;
  128. }
  129. }