CameraUtils.js 2.9 KB

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