@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
23 lines (17 loc) • 781 B
JavaScript
import { assert } from "../../../core/assert.js";
/**
* Compute camera distance offset in order to fit a given length (height) into view completely
* @param {number} length Height of the focus area in world space
* @param {number} fov Field of View in radians
* @returns {number}
*/
export function camera_compute_distance_to_fit_length(length, fov) {
// @see https://docs.unity3d.com/Manual/FrustumSizeAtDistance.html
assert.isNumber(length, 'length');
assert.greaterThanOrEqual(length, 0, 'length < 0');
assert.isNumber(fov, 'fov');
assert.greaterThan(fov, 0, 'fov <= 0');
assert.lessThan(fov, Math.PI, 'fov >= pi');
const signed_distance = (length * 0.5) / Math.tan(fov * 0.5);
return Math.abs(signed_distance);
}