float saturate(float x) {
    return clamp(x, 0, 1);
}

float random(vec2 coords) {
    return fract(sin(dot(coords.xy, vec2(12.9898, 78.233))) * 43758.5453);
}

vec2 rotate(vec2 X, float Ang) {
    float s = sin(Ang);
    float c = cos(Ang);
    mat2 RotationMat = mat2(
            c, s,
            -s, c
        );
    return RotationMat * X;
}

float l_depth(float depth, float near, float far) {
    return (near * far) / (depth * (near - far) + far);
}

float l_depth(float depth) {
    return l_depth(depth, near, far);
}

mat3 tbn_normal(vec3 normal) {
    vec3 tangent = normalize(cross(normal, vec3(0.0, 1.0, 1.0)));
    vec3 bitangent = (cross(tangent, normal));
    return mat3(tangent, bitangent, normal);
}

bool solveQuadratic(float a, float b, float c, inout float x1, inout float x2) {
    if (b == 0) {
        // Handle special case where the the two vector ray.dir and V are perpendicular
        // with V = ray.orig - sphere.centre
        if (a == 0) return false;
        x1 = 0;
        x2 = sqrt(-c / a);
        return true;
    }
    float discr = b * b - 4 * a * c;

    if (discr < 0) return false;

    float q = (b < 0.f) ? -0.5f * (b - sqrt(discr)) : -0.5f * (b + sqrt(discr));
    x1 = q / a;
    x2 = c / q;

    return true;
}

bool raySphereIntersect(vec3 orig, vec3 dir, float radius, inout float t0, inout float t1) {
    // They ray dir is normalized so A = 1
    float A = 1;
    float B = 2 * (dir.x * orig.x + dir.y * orig.y + dir.z * orig.z);
    float C = orig.x * orig.x + orig.y * orig.y + orig.z * orig.z - radius * radius;

    if (!solveQuadratic(A, B, C, t0, t1)) return false;

    if (t0 > t1) {
        float aux = t0;
        t0 = t1;
        t1 = aux;
    }

    return true;
}

vec3 intersectRayWithPlane(vec3 rayOrigin, vec3 rayDirection, float PlaneHeight) {
    float denom = rayDirection.y;

    float pointToRay = PlaneHeight - rayOrigin.y;
    float t = pointToRay / denom;

    // Check if the intersection is in the positive direction of the ray
    if (t >= 0.0) {
        return t * rayDirection;
    }
    //return t * rayDirection;

    // No intersection or the intersection is behind the ray origin
    return vec3(0.0);
}

bool intersect_with_cloud_plane(vec3 WorldPos, inout vec3 StartPos, inout vec3 EndPos, vec3 RayDir, const float LOWER_PLANE, const float UPPER_PLANE) {
    if (WorldPos.y < LOWER_PLANE) {
        StartPos = intersectRayWithPlane(WorldPos, RayDir, LOWER_PLANE);
        EndPos = intersectRayWithPlane(WorldPos, RayDir, UPPER_PLANE);
        if (EndPos == vec3(0) || StartPos == vec3(0)) return false;
    }
    else if (WorldPos.y > UPPER_PLANE) {
        StartPos = intersectRayWithPlane(WorldPos, RayDir, UPPER_PLANE);
        EndPos = intersectRayWithPlane(WorldPos, RayDir, LOWER_PLANE);
        if (EndPos == vec3(0) || StartPos == vec3(0)) return false;
    }
    else {
        StartPos = vec3(0);
        if (RayDir.y > 0) {
            EndPos = intersectRayWithPlane(WorldPos, RayDir, UPPER_PLANE);
        }
        else {
            EndPos = intersectRayWithPlane(WorldPos, RayDir, LOWER_PLANE);
        }
    }
    return true;
}

// https://discord.com/channels/237199950235041794/960945132172111952/1200536680931790878
float SmoothF(float x, float alpha) {
    return x > 0.0 ? pow(x / (x + pow(x, -1.0 / alpha)), alpha / (1.0 + alpha)) : x;
}

float SmoothMin(float a, float b, float alpha) {
    return b - 1.0 + SmoothF(a - b + 1.0, alpha);
}

float SmoothMax(float a, float b, float alpha) {
    return b + 1.0 - SmoothF(1.0 - a + b, alpha);
}

// https://github.com/Experience-Monks/glsl-fast-gaussian-blur
vec4 blur5x5(sampler2D image, vec2 uv, vec2 direction) {
    vec4 color = vec4(0.0);
    vec2 off1 = vec2(1.3333333333333333) * direction;
    color += texture2D(image, uv) * 0.29411764705882354;
    color += texture2D(image, uv + (off1 * resolutionInv)) * 0.35294117647058826;
    color += texture2D(image, uv - (off1 * resolutionInv)) * 0.35294117647058826;
    return color;
}

vec4 blur9x9(sampler2D image, vec2 uv, vec2 direction) {
    vec4 color = vec4(0.0);
    vec2 off1 = vec2(1.3846153846) * direction;
    vec2 off2 = vec2(3.2307692308) * direction;
    color += texture2D(image, uv) * 0.2270270270;
    color += texture2D(image, uv + (off1 * resolutionInv)) * 0.3162162162;
    color += texture2D(image, uv - (off1 * resolutionInv)) * 0.3162162162;
    color += texture2D(image, uv + (off2 * resolutionInv)) * 0.0702702703;
    color += texture2D(image, uv - (off2 * resolutionInv)) * 0.0702702703;
    return color;
}

float acosf(float x)
{
    // GPGPU Programming for Games and Science
    float res = -0.156583 * abs(x) + PI / 2.0;
    res *= sqrt(1.0 - abs(x));
    return x >= 0 ? res : PI - res;
}

vec3 rgb_to_xyz(vec3 rgb) {
    const mat3 XYZ_MATRIX = mat3(
            0.5149, 0.3244, 0.1607,
            0.3654, 0.6704, 0.0642,
            0.0248, 0.1248, 0.8504
        );
    return XYZ_MATRIX * rgb;
}

// The MIT License
// Copyright © 2013 Inigo Quilez
// https://www.youtube.com/c/InigoQuilez
// https://iquilezles.org/
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
vec4 textureNice(sampler2D sam, vec2 uv)
{
    float textureResolution = float(textureSize(sam, 0).x);
    uv = uv * textureResolution + 0.5;
    vec2 iuv = floor(uv);
    vec2 fuv = fract(uv);
    uv = iuv + fuv * fuv * (3.0 - 2.0 * fuv);
    uv = (uv - 0.5) / textureResolution;
    return texture(sam, uv);
}

float rescale(float l, float L, float x) {
    return (x - l) / (L - l);
}
vec2 rescale(float l, float L, vec2 x) {
    return (x - l) / (L - l);
}

float min_component(vec2 a) {
    return min(a.x, a.y);
}
float min_component(vec3 a) {
    return min(a.x, min(a.y, a.z));
}
float min_component(vec4 a) {
    return min(a.x, min(a.y, min(a.z, a.w)));
}

float max_component(vec2 a) {
    return max(a.x, a.y);
}
float max_component(vec3 a) {
    return max(a.x, max(a.y, a.z));
}
float max_component(vec4 a) {
    return max(a.x, max(a.y, max(a.z, a.w)));
}

float lerp(float tl, float tr, float bl, float br, vec2 Coords) {
    vec2 f = fract(Coords * resolution);

    float a = mix(tl, tr, f.x);
    float b = mix(bl, br, f.y);
    return mix(a, b, f.y);
}

float pow2(float x) {
    return x * x;
}
float pow4(float x) {
    return pow2(pow2(x));
}
vec2 pow2(vec2 x) {
    return x * x;
}
vec2 pow4(vec2 x) {
    return pow2(pow2(x));
}
vec3 pow2(vec3 x) {
    return x * x;
}
vec3 pow4(vec3 x) {
    return pow2(pow2(x));
}
vec4 pow2(vec4 x) {
    return x * x;
}
vec4 pow4(vec4 x) {
    return pow2(pow2(x));
}
