float gbf(in vec3 eyePlayerPos, in float far)
{
    float distXZ = length((vec3(eyePlayerPos.x, 0.0, eyePlayerPos.z)).xyz);
    float distY = length((vec3(0.0, eyePlayerPos.y, 0.0)).xyz);
    float eyeDist = max(distXZ, distY);
#ifdef DISTANT_HORIZONS
    float borderFogFactor = smoothstep(0.0, dhRenderDistance, eyeDist);
    borderFogFactor *= borderFogFactor * borderFogFactor;
    return borderFogFactor;
#else

    float borderFogFactor = smoothstep(far * 0.5, far, eyeDist);
    borderFogFactor *= borderFogFactor * borderFogFactor;

    return borderFogFactor;

#endif
}
void applySkyBlend2(inout vec4 toblend, vec3 sky, vec3 p3, float far)
{
#ifdef SKYBLEND

    float blend_factor = gbf(p3, far);

    toblend = mix(toblend, vec4(sky, 0.0), blend_factor * blend_factor);

#endif
}
const bool colortex7MipmapEnabled = true;

vec3 waterNormal(vec2 coord, vec3 normFragpos)
{
    // Normalize coordinates within a tile
    coord = mod(coord * (1.0 / 128.0), 1.0);

    // Determine normal strength based on camera position
    float baseStrength = 5.0;

    // Calculate Level of Detail (LOD) based on view angle
    float viewDot = dot(normFragpos, normal0);
    float lodFactor = pow(1.0 + viewDot, 4.0);
    float lod = clamp(lodFactor, 0.0, 1.0);
    // Adjust offset and strength based on LOD to reduce detail at distance
    float deltaPos = mix(0.002, 0.0, lod);
    float strength = mix(baseStrength, 0.0, lod);

    // Define sampling offsets dynamically
    vec2 offsets[4] = vec2[](
        vec2(deltaPos, 0.0),
        vec2(-deltaPos, 0.0),
        vec2(0.0, deltaPos),
        vec2(0.0, -deltaPos));

    // Sample heights from the texture
    float heights[4];
    for (int i = 0; i < 4; i++)
    {
        heights[i] = textureLod(colortex7, coord + offsets[i], 32).x;
    }

    // Compute normal components with distance-based scaling to reduce shimmering
    float dx = (heights[1] - heights[0]) * strength * (1.0 - lod);
    float dy = (heights[3] - heights[2]) * strength * (1.0 - lod);

    // Construct and normalize the normal vector
    vec3 normal = normalize(vec3(dx, dy, 0.5));

    // Apply a bias to bring normals to [0,1] range if needed
    normal = normal * 0.5 + 0.5;

    return normal;
}
