import { ViewerCamera } from '../camera/ViewerCamera';
/**
 * Tracks distance of objects relative a camera.
 *
 * Call {@link trackDistanceToCamera} for each visible mesh, then read
 * {@link minDistanceToCamera} to get the result.
 * Call {@link endFrame} before each frame.
 */
export class DistanceToCamera {
    _distanceDivisor = 128.0;
    _maxNearPlaneDistance = 4.0;
    _minDistanceToCamera = Number.MAX_VALUE;
    /**
     * Starts tracking distances for a new frame.
     */
    beginFrame() {
        this._minDistanceToCamera = Number.MAX_VALUE;
    }
    /**
     * Tracks a mesh's distance to the camera.
     *
     * Updates the internal minimum distance if this mesh is closer than
     * previously tracked meshes.
     *
     * @param options - The tracking options
     * @returns The distance from the mesh to the camera
     */
    trackDistanceToCamera(options) {
        const { mesh, camera } = options;
        const meshDistanceToCamera = mesh.getDistanceToCamera(camera);
        this._minDistanceToCamera = Math.min(this._minDistanceToCamera, meshDistanceToCamera);
        return meshDistanceToCamera;
    }
    /**
     * The calculated minimum distance from the closest tracked mesh to the camera,
     * divided by the distance divisor and clamped between {@link ViewerCamera.cameraMinZ}
     * and the max near plane distance. Resets when {@link beginFrame} is called.
     */
    get minDistanceToCamera() {
        if (this._minDistanceToCamera === Number.MAX_VALUE) {
            return ViewerCamera.cameraMinZ;
        }
        const divided = this._minDistanceToCamera / this._distanceDivisor;
        return Math.min(Math.max(ViewerCamera.cameraMinZ, divided), this._maxNearPlaneDistance);
    }
}
//# sourceMappingURL=DistanceToCamera.js.map