CameraUtils.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import {
  2. MathUtils,
  3. Quaternion,
  4. Vector3
  5. } from '../../../build/three.module.js';
  6. const _va = /*@__PURE__*/ new Vector3(), // from pe to pa
  7. _vb = /*@__PURE__*/ new Vector3(), // from pe to pb
  8. _vc = /*@__PURE__*/ new Vector3(), // from pe to pc
  9. _vr = /*@__PURE__*/ new Vector3(), // right axis of screen
  10. _vu = /*@__PURE__*/ new Vector3(), // up axis of screen
  11. _vn = /*@__PURE__*/ new Vector3(), // normal vector of screen
  12. _vec = /*@__PURE__*/ new Vector3(), // temporary vector
  13. _quat = /*@__PURE__*/ new Quaternion(); // temporary quaternion
  14. class CameraUtils {
  15. /** Set a PerspectiveCamera's projectionMatrix and quaternion
  16. * to exactly frame the corners of an arbitrary rectangle.
  17. * NOTE: This function ignores the standard parameters;
  18. * do not call updateProjectionMatrix() after this!
  19. * @param {Vector3} bottomLeftCorner
  20. * @param {Vector3} bottomRightCorner
  21. * @param {Vector3} topLeftCorner
  22. * @param {boolean} estimateViewFrustum */
  23. static frameCorners( camera, bottomLeftCorner, bottomRightCorner, topLeftCorner, estimateViewFrustum = false ) {
  24. const pa = bottomLeftCorner, pb = bottomRightCorner, pc = topLeftCorner;
  25. const pe = camera.position; // eye position
  26. const n = camera.near; // distance of near clipping plane
  27. const f = camera.far; //distance of far clipping plane
  28. _vr.copy( pb ).sub( pa ).normalize();
  29. _vu.copy( pc ).sub( pa ).normalize();
  30. _vn.crossVectors( _vr, _vu ).normalize();
  31. _va.copy( pa ).sub( pe ); // from pe to pa
  32. _vb.copy( pb ).sub( pe ); // from pe to pb
  33. _vc.copy( pc ).sub( pe ); // from pe to pc
  34. const d = - _va.dot( _vn ); // distance from eye to screen
  35. const l = _vr.dot( _va ) * n / d; // distance to left screen edge
  36. const r = _vr.dot( _vb ) * n / d; // distance to right screen edge
  37. const b = _vu.dot( _va ) * n / d; // distance to bottom screen edge
  38. const t = _vu.dot( _vc ) * n / d; // distance to top screen edge
  39. // Set the camera rotation to match the focal plane to the corners' plane
  40. _quat.setFromUnitVectors( _vec.set( 0, 1, 0 ), _vu );
  41. camera.quaternion.setFromUnitVectors( _vec.set( 0, 0, 1 ).applyQuaternion( _quat ), _vn ).multiply( _quat );
  42. // Set the off-axis projection matrix to match the corners
  43. camera.projectionMatrix.set( 2.0 * n / ( r - l ), 0.0,
  44. ( r + l ) / ( r - l ), 0.0, 0.0,
  45. 2.0 * n / ( t - b ),
  46. ( t + b ) / ( t - b ), 0.0, 0.0, 0.0,
  47. ( f + n ) / ( n - f ),
  48. 2.0 * f * n / ( n - f ), 0.0, 0.0, - 1.0, 0.0 );
  49. camera.projectionMatrixInverse.copy( camera.projectionMatrix ).invert();
  50. // FoV estimation to fix frustum culling
  51. if ( estimateViewFrustum ) {
  52. // Set fieldOfView to a conservative estimate
  53. // to make frustum tall/wide enough to encompass it
  54. camera.fov =
  55. MathUtils.RAD2DEG / Math.min( 1.0, camera.aspect ) *
  56. Math.atan( ( _vec.copy( pb ).sub( pa ).length() +
  57. ( _vec.copy( pc ).sub( pa ).length() ) ) / _va.length() );
  58. }
  59. }
  60. }
  61. export { CameraUtils };