frameCorners static method

void frameCorners(
  1. Camera camera,
  2. Vector3 bottomLeftCorner,
  3. Vector3 bottomRightCorner,
  4. Vector3 topLeftCorner, [
  5. bool estimateViewFrustum = false,
])

Implementation

static void frameCorners(Camera camera,Vector3 bottomLeftCorner,Vector3 bottomRightCorner,Vector3 topLeftCorner, [bool estimateViewFrustum = false] ) {

  final pa = bottomLeftCorner, pb = bottomRightCorner, pc = topLeftCorner;
  final pe = camera.position; // eye position
  final n = camera.near; // distance of near clipping plane
  final f = camera.far; //distance of far clipping plane

  _vr.setFrom( pb ).sub( pa ).normalize();
  _vu.setFrom( pc ).sub( pa ).normalize();
  _vn.cross2( _vr, _vu ).normalize();

  _va.setFrom( pa ).sub( pe ); // from pe to pa
  _vb.setFrom( pb ).sub( pe ); // from pe to pb
  _vc.setFrom( pc ).sub( pe ); // from pe to pc

  final d = - _va.dot( _vn );	// distance from eye to screen
  final l = _vr.dot( _va ) * n / d; // distance to left screen edge
  final r = _vr.dot( _vb ) * n / d; // distance to right screen edge
  final b = _vu.dot( _va ) * n / d; // distance to bottom screen edge
  final t = _vu.dot( _vc ) * n / d; // distance to top screen edge

  // Set the camera rotation to match the focal plane to the corners' plane
  _quat.setFromUnitVectors( _vec.setValues( 0, 1, 0 ), _vu );
  camera.quaternion.setFromUnitVectors( _vec.setValues( 0, 0, 1 ).applyQuaternion( _quat ), _vn ).multiply( _quat );

  // Set the off-axis projection matrix to match the corners
  camera.projectionMatrix.setValues( 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 );
  camera.projectionMatrixInverse.setFrom( camera.projectionMatrix ).invert();

  // FoV estimation to fix frustum culling
  if ( estimateViewFrustum ) {

    // Set fieldOfView to a conservative estimate
    // to make frustum tall/wide enough to encompass it
    camera.fov =
      MathUtils.rad2deg / math.min( 1.0, camera.aspect ) *
      math.atan( ( _vec.setFrom( pb ).sub( pa ).length +
              ( _vec.setFrom( pc ).sub( pa ).length ) ) / _va.length );

  }

}