#version 150

#moj_import <fog.glsl>

uniform sampler2D Sampler0;
uniform sampler2D EndBrineGlowSampler;
uniform vec4 ColorModulator;
uniform float FogStart;
uniform float FogEnd;
uniform sampler2D DepthTex0;
uniform vec2 gBufferProjectionInverse;
uniform vec4 FogColor;
uniform float EndBrineTime;

in float vertexDistance;
in vec4 vertexColor;
in vec2 texCoord0;
in vec4 normal;
in vec2 resonantendEndBrineWorldXZ;
in vec2 resonantendSectionXZ;
flat in float resonantendEndBrineTopFace;
flat in float resonantendEndBrineMarker;

out vec4 fragColor;

const float TWO_PI = 6.28318530718;
const float GLOW_NORMAL_HEIGHT = 0.48;
const float GLOW_CELL_SIZE = 2.00;
const float FULL_COLOR_END = 40.0;
const float LOW_COLOR_START = 56.0;
const float GLOW_FADE_START = 14.0;
const float GLOW_FADE_END = 30.0;
const float FLECK_FADE_START = 20.0;
const float FLECK_FADE_END = 44.0;

float hashValue(vec2 position, float salt) {
    vec3 value = fract(vec3(position.x, position.y, position.x)
        * vec3(0.1031, 0.1030, 0.0973) + salt * 0.000137);
    value += dot(value, value.yzx + 33.33);
    return fract((value.x + value.y) * value.z);
}

float valueNoise(vec2 position, float salt) {
    vec2 cell = floor(position);
    vec2 localPosition = fract(position);
    vec2 smoothPosition = localPosition * localPosition * (3.0 - 2.0 * localPosition);

    float value00 = hashValue(cell, salt);
    float value10 = hashValue(cell + vec2(1.0, 0.0), salt);
    float value01 = hashValue(cell + vec2(0.0, 1.0), salt);
    float value11 = hashValue(cell + vec2(1.0, 1.0), salt);

    float top = mix(value00, value10, smoothPosition.x);
    float bottom = mix(value01, value11, smoothPosition.x);
    return mix(top, bottom, smoothPosition.y);
}

float getLinearDepth(float nonLinearDepth) {
    float depth = nonLinearDepth * 2 - 1;
    return gBufferProjectionInverse.x / (gBufferProjectionInverse.y * depth + 1);
}

float fbm4(vec2 position, float salt) {
    float result = 0.0;
    float amplitude = 0.5;
    vec2 samplePosition = position;
    for (int octave = 0; octave < 4; octave++) {
        result += valueNoise(samplePosition, salt + float(octave) * 53.17) * amplitude;
        samplePosition = samplePosition * 2.03 + vec2(17.13, 9.27);
        amplitude *= 0.5;
    }
    return result / 0.9375;
}

float fbm2(vec2 position, float salt) {
    float result = 0.0;
    float amplitude = 0.5;
    vec2 samplePosition = position;
    for (int octave = 0; octave < 2; octave++) {
        result += valueNoise(samplePosition, salt + float(octave) * 53.17) * amplitude;
        samplePosition = samplePosition * 2.03 + vec2(17.13, 9.27);
        amplitude *= 0.5;
    }
    return result / 0.75;
}

vec2 endBrineFlowSpaceFull(vec2 worldPosition, float phase) {
    vec2 position = worldPosition * 0.100;
    vec2 drift = vec2(cos(phase), sin(phase));

    float firstWarpX = fbm4(position * 0.44 + drift * 0.34 + vec2(4.1, 9.7), 211.0);
    float firstWarpZ = fbm4(position * 0.44 + vec2(-drift.y, drift.x) * 0.34 + vec2(-8.6, 3.2), 307.0);
    vec2 firstWarp = (vec2(firstWarpX, firstWarpZ) - 0.5) * 3.40;
    vec2 firstPosition = position + firstWarp;

    float secondWarpX = fbm4(firstPosition * 0.82 + vec2(12.3, -5.1) + drift * 0.12, 401.0);
    float secondWarpZ = fbm4(firstPosition * 0.82 + vec2(-2.7, 14.9) + vec2(-drift.y, drift.x) * 0.12, 503.0);
    vec2 secondWarp = (vec2(secondWarpX, secondWarpZ) - 0.5) * 1.35;

    return position + firstWarp + secondWarp + drift * 0.17;
}

vec2 endBrineFlowSpaceLow(vec2 worldPosition, float phase) {
    vec2 position = worldPosition * 0.100;
    vec2 drift = vec2(cos(phase), sin(phase));

    float firstWarpX = fbm2(position * 0.44 + drift * 0.34 + vec2(4.1, 9.7), 211.0);
    float firstWarpZ = fbm2(position * 0.44 + vec2(-drift.y, drift.x) * 0.34 + vec2(-8.6, 3.2), 307.0);
    vec2 firstWarp = (vec2(firstWarpX, firstWarpZ) - 0.5) * 3.40;
    vec2 firstPosition = position + firstWarp;

    float secondWarpX = fbm2(firstPosition * 0.82 + vec2(12.3, -5.1) + drift * 0.12, 401.0);
    float secondWarpZ = fbm2(firstPosition * 0.82 + vec2(-2.7, 14.9) + vec2(-drift.y, drift.x) * 0.12, 503.0);
    vec2 secondWarp = (vec2(secondWarpX, secondWarpZ) - 0.5) * 1.35;

    return position + firstWarp + secondWarp + drift * 0.17;
}

float ridgeProfile(float value) {
    return 1.0 - abs(value * 2.0 - 1.0);
}

vec2 primaryCurrentSpace(vec2 position) {
    return vec2(position.x * 0.78 + position.y * 0.34,
                position.x * -0.20 + position.y * 1.42);
}

vec2 crossingCurrentSpace(vec2 position) {
    return vec2(position.x * -0.46 + position.y * 1.24,
                position.x * 1.31 + position.y * 0.18);
}

float endBrinePuddleFieldFull(vec2 flowPosition, float phase) {
    float macroField = fbm4(flowPosition * 0.36 + vec2(3.7, -7.2), 701.0);
    vec2 warpedPosition = flowPosition + vec2(macroField - 0.5, 0.5 - macroField) * 1.18;

    vec2 primaryPosition = primaryCurrentSpace(warpedPosition);
    vec2 crossingPosition = crossingCurrentSpace(warpedPosition);

    float primaryNoise = fbm4(primaryPosition * vec2(0.58, 0.32) + vec2(macroField - 0.5, 0.0) * 0.85 + vec2(-9.1, 4.4), 809.0);
    float crossingNoise = fbm4(crossingPosition * vec2(0.52, 0.36) + vec2(0.0, primaryNoise - 0.5) * 0.72 + vec2(14.3, -6.8), 853.0);

    float primaryRidge = ridgeProfile(primaryNoise);
    float crossingRidge = ridgeProfile(crossingNoise);

    float primaryCurrent = 0.5 + 0.5 * sin(primaryPosition.x * 0.86 + primaryPosition.y * 0.17
        + (primaryNoise - 0.5) * 5.1 + (crossingNoise - 0.5) * 1.7 + sin(phase) * 0.29);
    float crossingCurrent = 0.5 + 0.5 * sin(crossingPosition.x * 0.73 - crossingPosition.y * 0.21
        + (crossingNoise - 0.5) * 4.4 - (primaryNoise - 0.5) * 2.2 + cos(phase) * 0.23);

    float breakField = valueNoise(warpedPosition * 1.78 + vec2(17.4, -11.8), 907.0);
    float chippedCurrent = 0.5 + 0.5 * sin(crossingPosition.x * 1.34 - primaryPosition.y * 0.29
        + (breakField - 0.5) * 5.6 + primaryRidge * 1.2);

    float field = macroField * 0.35 + primaryNoise * 0.22 + crossingNoise * 0.13
        + primaryCurrent * 0.14 + crossingCurrent * 0.07 + breakField * 0.06 + chippedCurrent * 0.03;
    field += (primaryRidge - 0.5) * 0.070;
    field -= (crossingRidge - 0.5) * 0.038;
    field = smoothstep(0.17, 0.83, field);
    field += (breakField - 0.5) * (0.035 + primaryRidge * 0.040);
    field += sin(field * TWO_PI * 1.12 + primaryCurrent * 0.85 - crossingCurrent * 0.55) * 0.014;

    return clamp(field, 0.0, 1.0);
}

float endBrinePuddleFieldLow(vec2 flowPosition, float phase) {
    float macroField = fbm2(flowPosition * 0.36 + vec2(3.7, -7.2), 701.0);
    vec2 warpedPosition = flowPosition + vec2(macroField - 0.5, 0.5 - macroField) * 1.18;

    vec2 primaryPosition = primaryCurrentSpace(warpedPosition);
    vec2 crossingPosition = crossingCurrentSpace(warpedPosition);

    float primaryNoise = fbm2(primaryPosition * vec2(0.58, 0.32) + vec2(macroField - 0.5, 0.0) * 0.85 + vec2(-9.1, 4.4), 809.0);
    float crossingNoise = fbm2(crossingPosition * vec2(0.52, 0.36) + vec2(0.0, primaryNoise - 0.5) * 0.72 + vec2(14.3, -6.8), 853.0);

    float primaryRidge = ridgeProfile(primaryNoise);
    float crossingRidge = ridgeProfile(crossingNoise);

    float primaryCurrent = 0.5 + 0.5 * sin(primaryPosition.x * 0.86 + primaryPosition.y * 0.17
        + (primaryNoise - 0.5) * 5.1 + (crossingNoise - 0.5) * 1.7 + sin(phase) * 0.29);
    float crossingCurrent = 0.5 + 0.5 * sin(crossingPosition.x * 0.73 - crossingPosition.y * 0.21
        + (crossingNoise - 0.5) * 4.4 - (primaryNoise - 0.5) * 2.2 + cos(phase) * 0.23);

    float breakField = valueNoise(warpedPosition * 1.78 + vec2(17.4, -11.8), 907.0);
    float chippedCurrent = 0.5 + 0.5 * sin(crossingPosition.x * 1.34 - primaryPosition.y * 0.29
        + (breakField - 0.5) * 5.6 + primaryRidge * 1.2);

    float field = macroField * 0.35 + primaryNoise * 0.22 + crossingNoise * 0.13
        + primaryCurrent * 0.14 + crossingCurrent * 0.07 + breakField * 0.06 + chippedCurrent * 0.03;
    field += (primaryRidge - 0.5) * 0.060;
    field -= (crossingRidge - 0.5) * 0.032;
    field = smoothstep(0.17, 0.83, field);
    field += (breakField - 0.5) * (0.030 + primaryRidge * 0.034);
    field += sin(field * TWO_PI * 1.12 + primaryCurrent * 0.85 - crossingCurrent * 0.55) * 0.011;

    return clamp(field, 0.0, 1.0);
}

vec3 endBrinePalette(float value) {
    const vec3 DEEP_BLUE = vec3(0.490, 0.700, 0.000);
    const vec3 OCEAN_BLUE = vec3(0.510, 0.720, 0.000);
    const vec3 LUMINOUS_CYAN = vec3(0.530, 0.750, 0.000);
    const vec3 PINK = vec3(0.560, 0.780, 0.010);
    const vec3 LAVENDER = vec3(0.545, 0.760, 0.003);
    const vec3 TURQUOISE = vec3(0.515, 0.730, 0.000);
    const vec3 MINT = vec3(0.570, 0.790, 0.020);
    const vec3 PEARL = vec3(0.600, 0.820, 0.030);
    const vec3 AMBER = vec3(0.545, 0.770, 0.006);
    const vec3 CORAL = vec3(0.525, 0.740, 0.000);
    const vec3 VIOLET = vec3(0.500, 0.710, 0.000);

    float scaledPosition = fract(value) * 11.0;
    int segment = int(floor(scaledPosition));
    float progress = smoothstep(0.0, 1.0, fract(scaledPosition));

    if (segment == 0) return mix(DEEP_BLUE, OCEAN_BLUE, progress);
    if (segment == 1) return mix(OCEAN_BLUE, LUMINOUS_CYAN, progress);
    if (segment == 2) return mix(LUMINOUS_CYAN, PINK, progress);
    if (segment == 3) return mix(PINK, LAVENDER, progress);
    if (segment == 4) return mix(LAVENDER, TURQUOISE, progress);
    if (segment == 5) return mix(TURQUOISE, MINT, progress);
    if (segment == 6) return mix(MINT, PEARL, progress);
    if (segment == 7) return mix(PEARL, AMBER, progress);
    if (segment == 8) return mix(AMBER, CORAL, progress);
    if (segment == 9) return mix(CORAL, VIOLET, progress);
    return mix(VIOLET, DEEP_BLUE, progress);
}

float balancedEndBrinePalettePosition(vec2 flowPosition, float puddleField) {
    float allocationNoise = valueNoise(flowPosition * vec2(0.72, 0.91) + vec2(-23.4, 8.1), 1459.0);
    float crossingAllocation = 0.5 + 0.5 * sin(flowPosition.x * 1.17 - flowPosition.y * 0.83
        + (allocationNoise - 0.5) * 3.6);

    return fract(0.07 + puddleField * 2.30 + allocationNoise * 0.64 + crossingAllocation * 0.28);
}

vec3 applyPearlRegion(vec3 color, float palettePosition, float internalDetail) {
    float pearlDistance = abs(palettePosition - 0.6363636);
    pearlDistance = min(pearlDistance, 1.0 - pearlDistance);
    float pearlContour = 1.0 - smoothstep(0.018, 0.060, pearlDistance);
    float pearlStrength = pearlContour * (0.025 + internalDetail * 0.055);

    return mix(color, vec3(0.900, 0.990, 0.550), pearlStrength);
}

vec3 sampleEndBrineColorFull(vec2 worldPosition, float phase) {
    vec2 flowPosition = endBrineFlowSpaceFull(worldPosition, phase);
    float puddleField = endBrinePuddleFieldFull(flowPosition, phase);
    float palettePosition = balancedEndBrinePalettePosition(flowPosition, puddleField);

    vec3 color = endBrinePalette(palettePosition);
    float internalDetail = fbm4(flowPosition * 1.55 + vec2(12.7, -4.3), 1201.0);
    color *= 0.93 + internalDetail * 0.13;

    return applyPearlRegion(color, palettePosition, internalDetail);
}

vec3 sampleEndBrineColorLow(vec2 worldPosition, float phase) {
    vec2 flowPosition = endBrineFlowSpaceLow(worldPosition, phase);
    float puddleField = endBrinePuddleFieldLow(flowPosition, phase);
    float hue = fract(0.095 + puddleField * 1.58);

    vec3 color = endBrinePalette(hue);
    float internalDetail = valueNoise(flowPosition * 1.15 + vec2(12.7, -4.3), 1201.0);
    color *= 0.94 + internalDetail * 0.11;

    return applyPearlRegion(color, hue, internalDetail);
}

vec3 sampleEndBrineColorStable(vec2 worldPosition, float phase, float distanceValue) {
    vec2 flowPosition = endBrineFlowSpaceFull(worldPosition, phase);
    float puddleField = endBrinePuddleFieldFull(flowPosition, phase);
    float hue = fract(0.095 + puddleField * 1.58);

    vec3 color = endBrinePalette(hue);
    float internalDetail = 0.5;
    if (distanceValue < LOW_COLOR_START) {
        float detailedValue = fbm4(flowPosition * 1.55 + vec2(12.7, -4.3), 1201.0);
        float detailWeight = 1.0 - smoothstep(FULL_COLOR_END, LOW_COLOR_START, distanceValue);
        internalDetail = mix(0.5, detailedValue, detailWeight);
    }

    color *= 0.93 + internalDetail * 0.13;
    return applyPearlRegion(color, hue, internalDetail);
}

float squareFleckLayer(vec2 worldPosition, vec2 drift, float cellSize, float existenceThreshold,
                       float minimumHalfSize, float maximumHalfSize, float salt) {
    vec2 movedPosition = worldPosition + drift;
    vec2 cell = floor(movedPosition / cellSize);

    float existence = hashValue(cell, salt);
    float existenceMask = step(existenceThreshold, existence);

    vec2 randomCenter = vec2(hashValue(cell, salt + 17.0), hashValue(cell, salt + 41.0));
    float margin = (maximumHalfSize + 0.012) / cellSize;
    margin = clamp(margin, 0.08, 0.32);
    randomCenter = vec2(margin) + randomCenter * (1.0 - margin * 2.0);
    vec2 center = (cell + randomCenter) * cellSize;

    float halfSize = mix(minimumHalfSize, maximumHalfSize, hashValue(cell, salt + 73.0));
    vec2 difference = abs(movedPosition - center);
    float squareDistance = max(difference.x, difference.y);
    float edgeWidth = max(fwidth(squareDistance) * 0.85, 0.003);
    float mask = 1.0 - smoothstep(halfSize - edgeWidth, halfSize + edgeWidth, squareDistance);
    float opacity = mix(0.050, 0.165, hashValue(cell, salt + 109.0));

    return mask * opacity * existenceMask;
}

float sampleEndBrineFlecks(vec2 worldPosition, float phase) {
    vec2 mediumDrift = vec2(cos(phase * 2.0 + 2.13), sin(phase * 2.0 + 2.13)) * 0.23;
    vec2 tinyDrift = vec2(cos(phase * 3.0 + 4.27), sin(phase * 3.0 + 4.27)) * 0.17;

    float medium = squareFleckLayer(worldPosition, mediumDrift, 0.53, 0.82, 0.020, 0.039, 2101.0);
    float tiny = squareFleckLayer(worldPosition, tinyDrift, 0.30, 0.77, 0.011, 0.024, 2203.0);

    return clamp(medium + tiny, 0.0, 0.23);
}

vec3 applyEndBrineFlecks(vec3 baseColor, vec2 worldPosition, float phase, float detailFade) {
    float opacity = sampleEndBrineFlecks(worldPosition, phase) * detailFade;
    vec3 fleckColor = mix(baseColor, vec3(0.85, 0.99, 0.60), 0.55);
    return mix(baseColor, fleckColor, opacity);
}

float sampleSingleGlow(vec2 worldPosition, vec2 glowCell, float glowClock) {
    float existence = hashValue(glowCell, 3001.0);
    if (existence < 0.25) return 0.0;

    float timeOffset = hashValue(glowCell, 3109.0);
    float cyclePosition = fract(glowClock + timeOffset);
    const float ACTIVE_PORTION = 0.78;
    if (cyclePosition >= ACTIVE_PORTION) return 0.0;

    float life = cyclePosition / ACTIVE_PORTION;
    float lifetimeFade = smoothstep(0.00, 0.15, life) * (1.0 - smoothstep(0.68, 1.00, life));

    vec2 anchorRandom = vec2(hashValue(glowCell, 3203.0), hashValue(glowCell, 3301.0));
    anchorRandom = 0.17 + anchorRandom * 0.66;
    vec2 anchor = (glowCell + anchorRandom) * GLOW_CELL_SIZE;

    float directionAngle = hashValue(glowCell, 3407.0) * TWO_PI;
    vec2 travelDirection = vec2(cos(directionAngle), sin(directionAngle));
    vec2 travelSide = vec2(travelDirection.y, -travelDirection.x);

    float travelDistance = mix(1.25, 2.20, hashValue(glowCell, 3501.0));
    float swayAmount = mix(0.006, 0.040, hashValue(glowCell, 3607.0));
    float swayPhase = hashValue(glowCell, 3701.0) * TWO_PI;

    vec2 glowPosition = anchor + travelDirection * (life - 0.5) * travelDistance
        + travelSide * sin(life * TWO_PI + swayPhase) * swayAmount;

    float sizeFactor = mix(0.78, 1.00, hashValue(glowCell, 3803.0));
    float glowHeight = GLOW_NORMAL_HEIGHT * sizeFactor;
    float glowWidth = glowHeight * (5.0 / 6.0);

    vec2 spriteUp = travelDirection;
    vec2 spriteRight = vec2(spriteUp.y, -spriteUp.x);
    vec2 difference = worldPosition - glowPosition;
    vec2 localPosition = vec2(dot(difference, spriteRight), dot(difference, spriteUp));
    vec2 glowUv = localPosition / vec2(glowWidth, glowHeight) + 0.5;
    if (glowUv.x < 0.0 || glowUv.x > 1.0 || glowUv.y < 0.0 || glowUv.y > 1.0) return 0.0;

    glowUv.y = 1.0 - glowUv.y;
    vec4 glowTexel = textureLod(EndBrineGlowSampler, glowUv, 0.0);
    float opacity = mix(0.15, 0.27, hashValue(glowCell, 3907.0));

    return glowTexel.a * lifetimeFade * opacity;
}

float sampleEndBrineGlow(vec2 worldPosition) {
    float glowClock = EndBrineTime * (220.0 / 1200.0);
    vec2 centerCell = floor(worldPosition / GLOW_CELL_SIZE);
    float accumulatedOpacity = 0.0;

    for (int offsetX = -1; offsetX <= 1; offsetX++) {
        for (int offsetZ = -1; offsetZ <= 1; offsetZ++) {
            vec2 glowCell = centerCell + vec2(float(offsetX), float(offsetZ));
            float glowOpacity = sampleSingleGlow(worldPosition, glowCell, glowClock);
            accumulatedOpacity = 1.0 - (1.0 - accumulatedOpacity) * (1.0 - glowOpacity);
        }
    }

    return clamp(accumulatedOpacity, 0.0, 0.36);
}

vec3 applyEndBrineGlow(vec3 baseColor, vec2 worldPosition, float detailFade) {
    float opacity = sampleEndBrineGlow(worldPosition) * detailFade;
    vec3 glowColor = mix(baseColor, vec3(0.85, 1.00, 0.62), 0.76);
    return mix(baseColor, glowColor, opacity);
}

vec3 sampleSideColorFull(vec2 worldPosition, float phase) {
    vec2 sidePosition = worldPosition * 0.115;
    vec2 sideDrift = vec2(cos(phase), sin(phase)) * 0.27;

    float sideMacro = fbm4(sidePosition * 0.52 + sideDrift + vec2(3.2, 8.8), 1801.0);
    float sideLobe = fbm4(sidePosition * 0.88 + vec2(sideMacro - 0.5, 0.5 - sideMacro) * 1.25 + vec2(-5.1, 11.2), 1907.0);
    float sideField = smoothstep(0.12, 0.88, sideMacro * 0.72 + sideLobe * 0.28);
    float sidePalettePosition = balancedEndBrinePalettePosition(sidePosition, sideField);

    return endBrinePalette(sidePalettePosition) * 0.76;
}

vec3 sampleSideColorLow(vec2 worldPosition, float phase) {
    vec2 sidePosition = worldPosition * 0.115;
    vec2 sideDrift = vec2(cos(phase), sin(phase)) * 0.27;

    float sideMacro = fbm2(sidePosition * 0.52 + sideDrift + vec2(3.2, 8.8), 1801.0);
    float sideLobe = fbm2(sidePosition * 0.88 + vec2(sideMacro - 0.5, 0.5 - sideMacro) * 1.25 + vec2(-5.1, 11.2), 1907.0);
    float sideField = smoothstep(0.12, 0.88, sideMacro * 0.72 + sideLobe * 0.28);
    float sidePalettePosition = balancedEndBrinePalettePosition(sidePosition, sideField);

    return endBrinePalette(sidePalettePosition) * 0.76;
}

vec3 sampleSideColorLod(vec2 worldPosition, float phase, float distanceValue) {
    if (distanceValue <= FULL_COLOR_END) return sampleSideColorFull(worldPosition, phase);
    if (distanceValue >= LOW_COLOR_START) return sampleSideColorLow(worldPosition, phase);

    float transition = smoothstep(FULL_COLOR_END, LOW_COLOR_START, distanceValue);
    return mix(sampleSideColorFull(worldPosition, phase),
               sampleSideColorLow(worldPosition, phase), transition);
}

void main() {
    if (resonantendEndBrineMarker < 0.5) {
        vec4 sampledTexture = texture(Sampler0, texCoord0);
        vec4 vanillaColor = sampledTexture * vertexColor * ColorModulator;
        fragColor = linear_fog(vanillaColor, vertexDistance, FogStart, FogEnd, FogColor);
        return;
    }

    float phase = EndBrineTime * (TWO_PI / 50.0);
    vec3 color;
    float alpha = 0.90;

    if (resonantendEndBrineTopFace > 0.5) {
        alpha = 0.86;
        color = sampleEndBrineColorStable(resonantendEndBrineWorldXZ, phase, vertexDistance);

        float glowFade = 1.0 - smoothstep(GLOW_FADE_START, GLOW_FADE_END, vertexDistance);
        if (glowFade > 0.001) {
            color = applyEndBrineGlow(color, resonantendEndBrineWorldXZ, glowFade);
        }

        float fleckFade = 1.0 - smoothstep(FLECK_FADE_START, FLECK_FADE_END, vertexDistance);
        if (fleckFade > 0.001) {
            color = applyEndBrineFlecks(color, resonantendEndBrineWorldXZ, phase, fleckFade);
        }
    } else {
        alpha = 0.90;
        color = sampleSideColorFull(resonantendSectionXZ, phase);
    }

    /*
    step 1: get screen texture coordinate (gl_fragcore.xy)
    step 2: sample the depth of terrain begind the fluid (texture2D) where we use the depthbuffer
    step 3: get the linear terrain depth and the linear fluid depth (use the helper method you got from step2 and gl fragcore.z
    step 4: calculate the fluid depth get the difference between the linear terrain depth and the linear fluid depth and you absolute it
    step 5: design the actual effect mathematically (max depth, effect scale and all that)
    step 6: set the current pixel in the buffer to the fluid colour that we have
    */

    float viewDot = clamp(abs(normal.z), 0.0, 1.0);
    viewDot = viewDot * viewDot;
    alpha = mix(alpha, alpha - 0.45, viewDot);
    alpha = clamp(alpha, 0.15, 0.90);

    vec4 endBrineColor = vec4(clamp(color, vec3(0.0), vec3(1.0)), alpha);
    fragColor = linear_fog(endBrineColor, vertexDistance, FogStart, FogEnd, FogColor);
}