import { bessel_3 } from "../../../../../core/math/bessel_3.js";
import { clamp } from "../../../../../core/math/clamp.js";

const PI3 = 3.0 * Math.PI;
const BESSEL_3_PI_3 = bessel_3(PI3);
const INV_BESSEL_3_PI_3 = 1 / BESSEL_3_PI_3;

/**
 *
 * Kaiser Bessel window
 * @see https://bgolus.medium.com/sharper-mipmapping-using-shader-based-supersampling-ed7aadb47bec#5bfb:~:text=Kaiser%2Dfiltered%20Mipmaps
 * @see https://www.shadertoy.com/view/wlf3RH
 */
export function kaiser_bessel_window(x) {
    const i = clamp(x * 0.6666666666666666, -1.0, 1.0);

    const t = Math.sqrt(1.0 - i * i);

    return bessel_3(PI3 * t) * INV_BESSEL_3_PI_3;
}

kaiser_bessel_window.support = 1;
