/**
 * GLSL shaders for the Liquid orb atmosphere layer.
 *
 * The Liquid variant uses a soft diffuse haze (BackSide) around the sphere
 * with smooth falloff, not a concentrated rim ring.
 */
// ── Shared vertex shader ─────────────────────────────────────────────────────
const glowVertex = /* glsl */ `
varying vec3 vNormal;
varying vec3 vViewDirection;

void main() {
    vNormal = normalize(normalMatrix * normal);
    vec4 worldPos = modelMatrix * vec4(position, 1.0);
    vViewDirection = normalize(cameraPosition - worldPos.xyz);
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`;
// ── Atmosphere (BackSide) — soft diffuse haze, not a rim ring ────────────────
const atmosphereFragment = /* glsl */ `
precision highp float;

uniform float u_time;
uniform vec3  u_color1;
uniform vec3  u_color2;
uniform vec3  u_color3;
uniform float u_intensity;

varying vec3 vNormal;
varying vec3 vViewDirection;

void main() {
    vec3 N = normalize(vNormal);
    vec3 V = normalize(vViewDirection);

    float NdotV = max(dot(N, V), 0.0);

    // Wide, smooth falloff — visible across the whole surface, not just edges.
    // Uses smoothstep to avoid the sharp rim ring that pow(1-NdotV, n) creates.
    float glow = 1.0 - smoothstep(0.0, 0.85, NdotV);
    glow *= glow; // soften further

    // Slow colour rotation
    float a  = u_time * 0.2;
    float ca = cos(a), sa = sin(a);
    vec3 dir = vec3(
        N.x * ca - N.z * sa,
        N.y,
        N.x * sa + N.z * ca
    );

    float vert = dir.y * 0.5 + 0.5;
    vec3  col  = mix(u_color3, u_color1, vert);
    float midW = 1.0 - abs(vert - 0.5) * 2.0;
    col = mix(col, u_color2, midW * 0.5);

    float pulse = 0.92 + sin(u_time * 0.6) * 0.08;
    float alpha = glow * u_intensity * pulse;

    gl_FragColor = vec4(col * alpha, alpha);
}
`;

export { atmosphereFragment, glowVertex };
