/*
    VertaLux — shadow map distortion.
    Concentrates shadow map resolution near the player:
    close shadows stay sharp, distant ones remain acceptable.
*/

// SHADOW_DISTORT_STRENGTH: slider in lib/settings.glsl, included BEFORE this file
// by all consumers. Fallback 0.85 applies if a program ever includes distort.glsl
// without settings.glsl. The VLX_SHADOW_DISTORT alias avoids a second
// "#define SHADOW_DISTORT_STRENGTH" (Iris' option scanner would flag the option
// as ambiguous and drop the slider from the menu). Writer (shadow.vsh) and all
// readers (lighting PCF/PCSS, composite1 caustics/AO/glint/godrays, composite RSM)
// must share these TWO functions so the distortion curve stays globally consistent.
#ifdef SHADOW_DISTORT_STRENGTH
    #define VLX_SHADOW_DISTORT SHADOW_DISTORT_STRENGTH
#else
    #define VLX_SHADOW_DISTORT 0.85
#endif

// Same alias mechanism for the Z packing (SHADOW_Z_SCALE, lib/settings.glsl):
// scales the shadow frustum depth range (z*0.2 = 5x range vs 2x for 0.5).
// Fallback 0.5 if distort.glsl is included without settings.glsl.
// WRITER (shadow.vsh) and ALL readers (lighting.glsl PCF/PCSS, composite RSM,
// composite1 caustics/godrays) must go through VLX_SHADOW_ZSCALE.
#ifdef SHADOW_Z_SCALE
    #define VLX_SHADOW_ZSCALE SHADOW_Z_SCALE
#else
    #define VLX_SHADOW_ZSCALE 0.5
#endif

float getDistortFactor(vec2 v) {
    float d = pow(abs(v.x * v.x * v.x) + abs(v.y * v.y * v.y), 1.0 / 3.0);
    return d * VLX_SHADOW_DISTORT + (1.0 - VLX_SHADOW_DISTORT);
}

vec3 distortShadow(vec3 pos, float factor) {
    return vec3(pos.xy / factor, pos.z * VLX_SHADOW_ZSCALE);
}
