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.

viewer.ts 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * encantar.js
  3. * GPU-accelerated Augmented Reality for the web
  4. * Copyright (C) 2022-2024 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 viewer represents a virtual camera in 3D world space
  21. */
  22. import Speedy from 'speedy-vision';
  23. import { CameraModel } from './camera-model';
  24. import { Pose } from './pose';
  25. import { ViewerPose } from './viewer-pose';
  26. import { View, PerspectiveView } from './view';
  27. import { Transform } from './transform';
  28. import { Vector2 } from './vector2';
  29. import { Vector3 } from './vector3';
  30. import { Ray } from './ray';
  31. /**
  32. * A viewer represents a virtual camera in 3D world space
  33. */
  34. export class Viewer
  35. {
  36. /** the pose of the viewer in 3D world space */
  37. private readonly _pose: ViewerPose;
  38. /** the views of this viewer (1 for monoscopic rendering; 2 for stereoscopic) */
  39. private readonly _views: View[];
  40. /**
  41. * Constructor
  42. * @param camera camera model
  43. */
  44. constructor(camera: CameraModel)
  45. {
  46. this._pose = new ViewerPose(camera);
  47. this._views = [ new PerspectiveView(camera) ];
  48. }
  49. /**
  50. * The pose of this viewer
  51. */
  52. get pose(): ViewerPose
  53. {
  54. return this._pose;
  55. }
  56. /**
  57. * The Transform of this viewer
  58. * A shortcut to pose.transform
  59. */
  60. get transform(): Transform
  61. {
  62. return this._pose.transform;
  63. }
  64. /**
  65. * The view of this viewer (only for monoscopic rendering)
  66. */
  67. get view(): View
  68. {
  69. /*
  70. if(this._views.length > 1)
  71. throw new IllegalOperationError('Use viewer.views for stereoscopic rendering');
  72. */
  73. return this._views[0];
  74. }
  75. /**
  76. * The views of this viewer
  77. */
  78. /*
  79. get views(): View[]
  80. {
  81. return this._views.concat([]);
  82. }
  83. */
  84. /**
  85. * Convert a pose from world space to viewer space
  86. * @param pose a pose in world space
  87. * @returns a pose in viewer space
  88. */
  89. convertToViewerSpace(pose: Pose): Pose
  90. {
  91. const modelMatrix = pose.transform.matrix;
  92. const viewMatrix = this._pose.viewMatrix;
  93. const modelViewMatrix = Speedy.Matrix(viewMatrix.times(modelMatrix));
  94. const transform = new Transform(modelViewMatrix);
  95. return new Pose(transform);
  96. }
  97. /**
  98. * Cast a ray from a point in the image space associated with this Viewer
  99. * @param position a point in image space, given in normalized units [-1,1]x[-1,1]
  100. * @returns a ray in world space that corresponds to the given point
  101. */
  102. raycast(position: Vector2): Ray
  103. {
  104. const projectionMatrixInverse = this.view._projectionMatrixInverse;
  105. const viewMatrixInverse = this._pose.transform.matrix;
  106. const pointInClipSpace = Speedy.Matrix(4, 1, [
  107. // Normalized Device Coordinates (NDC)
  108. position.x,
  109. position.y,
  110. 0, // (*)
  111. 1 // homogeneous coordinates
  112. ]);
  113. const pointInViewSpace = projectionMatrixInverse.times(pointInClipSpace);
  114. const pointInWorldSpace = viewMatrixInverse.times(pointInViewSpace);
  115. const p = Speedy.Matrix(pointInWorldSpace).read();
  116. /*
  117. (*) since we're just interested in the direction, any z coordinate in
  118. clip space [-1,1] will give us a suitable point p in world space.
  119. */
  120. const origin = this._pose.transform.position;
  121. const direction = new Vector3(p[0] / p[3], p[1] / p[3], p[2] / p[3])
  122. ._subtract(origin)._normalize();
  123. return new Ray(origin, direction);
  124. }
  125. }