/* Oxygen Atmosphere - water_voxy.glsl
Water reflection and refraction related functions (voxy).
*/

#include "/lib/oxygen_atmosphere_water_surface.glsl"

#if SUN_REFLECTION == 1
    #if !defined NETHER && !defined THE_END
        float sun_reflection(vec3 fragpos, vec2 lmcoord) {
        vec3 astroLightPos = worldTime > 12900 ? moonPosition : sunPosition;
        float astroAlignment =
            max(dot(normalize(fragpos), normalize(astroLightPos)), 0.0);

        return smoothstep(0.995, 1.0, astroAlignment) *
            clamp(lmcoord.y, 0.0, 1.0) *
            (1.0 - rainStrength) * 3.0;
        }

    #endif
#endif

vec3 refraction_voxy(
    vec3 fragpos,
    vec3 color,
    vec3 refraction,
    float dayMixerV,
    float nightMixerV,
    float dayMomentV,
    out float waterColumnDepth
) {
    vec2 pos = gl_FragCoord.xy * vec2(pixelSizeX, pixelSizeY);

    #if REFRACTION == 1
        pos = pos + refraction.xy * (0.075 / (1.0 + length(fragpos) * 0.4));
    #endif

    const float vxNear = 16.0;  // Must be 16.0, but 8.0 for fast non-linearity compensation in water-earth distance calculation
    const float vxFar  = 48000.0; // 16 * 3000  Documented farplane

    float water_absortion;
    waterColumnDepth = 0.0;

    if (isEyeInWater == 0) {

        float water_distance =
            2.0 * vxNear * vxFar / (vxFar + vxNear - (2.0 * gl_FragCoord.z - 1.0) * (vxFar - vxNear));

        float earth_depth = texture(vxDepthTexTrans, pos.xy).r;
        float earth_distance =
            2.0 * vxNear * vxFar / (vxFar + vxNear - (2.0 * earth_depth - 1.0) * (vxFar - vxNear));

        waterColumnDepth = max(earth_distance - water_distance, 0.0);
        float timeExtinction = dayBlendFloatVoxy(
            1.12,
            0.88,
            1.25,
            dayMixerV,
            nightMixerV,
            dayMomentV
        );
        float absorptionDensity =
            (0.045 + WATER_ABSORPTION * 0.20) *
            timeExtinction *
            (1.0 + rainStrength * 0.12);
        water_absortion = clamp(
            1.0 - exp2(-waterColumnDepth * absorptionDensity),
            0.0,
            1.0
        );

    } else {
        water_absortion = 0.0;
    }

    return mix(texture(gaux1, pos.xy).rgb, color, water_absortion);
}

vec3 get_normals_voxy(vec3 bump, vec3 fragpos, vec3 tangent, vec3 binormal, vec3 waterNormal) {
    float NdotE = abs(dot(waterNormal, normalize(fragpos)));

    bump *= vec3(NdotE) + vec3(0.0, 0.0, 1.0 - NdotE);

    mat3 tbn_matrix = mat3(
        tangent.x, binormal.x, waterNormal.x,
        tangent.y, binormal.y, waterNormal.y,
        tangent.z, binormal.z, waterNormal.z
    );

    return normalize(bump * tbn_matrix);
}

vec4 reflection_calc_voxy(vec3 fragpos, vec3 normal, vec3 reflected) {
    vec3 pos = camera_to_screen_voxy(fragpos + reflected * 768.0);

    float border =
        clamp((1.0 - (max(0.0, abs(pos.y - 0.5)) * 2.0)) * 50.0, 0.0, 1.0);

    border = clamp(border - pow(pos.y, 10.0), 0.0, 1.0);

    pos.x = abs(pos.x);
    if (pos.x > 1.0) {
        pos.x = 1.0 - (pos.x - 1.0);
    }

    return vec4(texture(gaux1, pos.xy).rgb, border);
}

vec3 water_shader_voxy(
    vec3 fragpos,
    vec3 normal,
    vec3 color,
    vec3 sky_reflect,
    vec3 reflected,
    float fresnel,
    float visibleSky,
    vec3 lightColor,
    vec2 lmcoord
) {
    vec4 reflection = vec4(0.0);
    float infinite = 1.0;

    #if REFLECTION == 1
        reflection =
            reflection_calc_voxy(fragpos, normal, reflected);
    #endif

    reflection.rgb = mix(
        sky_reflect * visibleSky,
        reflection.rgb,
        reflection.a
    );

    #ifdef VANILLA_WATER
        fresnel *= 0.8;
    #endif

    #if SUN_REFLECTION == 1
        #ifndef NETHER
            #ifndef THE_END
                return mix(color, reflection.rgb, fresnel * REFLEX_INDEX) +
                    vec3(sun_reflection(reflect(normalize(fragpos), normal), lmcoord)) * lightColor * infinite * visibleSky;
            #else
                return mix(color, reflection.rgb, fresnel * REFLEX_INDEX);
            #endif
        #else
            return mix(color, reflection.rgb, fresnel * REFLEX_INDEX);
        #endif
    #else
        return mix(color, reflection.rgb, fresnel * REFLEX_INDEX);
    #endif
}

vec4 cristal_reflection_calc_voxy(vec3 fragpos, vec3 normal) {
    vec3 reflectedVector = reflect(normalize(fragpos), normal) * 768.0;
    vec3 pos = camera_to_screen_voxy(fragpos + reflectedVector);

    float border_x = max(-fourthPow(abs(2.0 * pos.x - 1.0)) + 1.0, 0.0);
    float border_y = max(-fourthPow(abs(2.0 * pos.y - 1.0)) + 1.0, 0.0);
    float border = min(border_x, border_y);

    return vec4(texture(gaux1, pos.xy, 0.0).rgb, border);
}

vec4 cristal_shader_voxy(
    vec3 fragpos,
    vec3 normal,
    vec4 color,
    vec3 skyReflectionColor,
    float fresnel,
    float visibleSky,
    vec3 lightColor,
    vec2 lmcoord
) {
    vec4 reflection = vec4(0.0);
    float infinite = 0.0;

    #if REFLECTION == 1
        reflection = cristal_reflection_calc_voxy(fragpos, normal);
    #endif

    skyReflectionColor = mix(color.rgb, skyReflectionColor, visibleSky * visibleSky);

    reflection.rgb = mix(
        skyReflectionColor,
        reflection.rgb,
        reflection.a
    );

    color.rgb = mix(color.rgb, skyReflectionColor, fresnel);
    color.rgb = mix(color.rgb, reflection.rgb, fresnel);

    color.a = mix(color.a, 1.0, fresnel * .9);

    #if SUN_REFLECTION == 1
        #ifndef NETHER
        #ifndef THE_END
            return color + vec4(
                mix(
                    vec3(sun_reflection(reflect(normalize(fragpos), normal), lmcoord) * lightColor * infinite * visibleSky),
                    vec3(0.0),
                    reflection.a
                ),
                0.0
            );
        #else
            return color;
        #endif
        #else
            return color;
        #endif
    #else
        return color;
    #endif
}
