#version 330 core
out vec4 FragColor;

// 视口大小（像素，建议传入 framebuffer 尺寸，HiDPI 下要乘 devicePixelRatio）
uniform vec2 uViewport;

// 以像素为单位的网格间距：细网格/粗网格
uniform float uMinorStep;
uniform float uMajorStep;
uniform bool uLightMode;

// 生成抗锯齿网格线（返回 0..1，1 表示在线上）
float gridLine(float stepPx) {
    vec2 coord = gl_FragCoord.xy;
    vec2 q = coord / stepPx;

    // 距离最近网格线的归一化距离，再用 fwidth 做抗锯齿
    vec2 g = abs(fract(q - 0.5) - 0.5) / fwidth(q);
    float line = 1.0 - min(min(g.x, g.y), 1.0);
    return line;
}

void main() {
    vec2 viewport = max(uViewport, vec2(1.0));
    vec2 uv = gl_FragCoord.xy / viewport; // 0..1
    
    vec3 topCol, botCol, minorCol, majorCol;
    float minorStrength, majorStrength;
    float vignettePow, vignetteStrength;

    // 背景渐变：上更亮、下稍灰，常见 3D 软件的“科技感”底色
    topCol = vec3(0.99, 0.99, 1.00);
    botCol = vec3(0.94, 0.95, 0.98);

    minorCol = vec3(0.80, 0.82, 0.87);
    majorCol = vec3(0.70, 0.73, 0.80);

    minorStrength = 0.22;
    majorStrength = 0.35;
    vignettePow = 0.12;
    vignetteStrength = 0.35;
    vec3 col = mix(botCol, topCol, uv.y);

    float minor = gridLine(max(uMinorStep, 1.0));
    float major = gridLine(max(uMajorStep, 1.0));
    col = mix(col, minorCol, minor * minorStrength);
    col = mix(col, majorCol, major * majorStrength);

    // 轻微 vignette（四角略暗），让画面更“聚焦”
    vec2 p = uv * 2.0 - 1.0;
    float v = clamp(1.0 - dot(p, p) * 0.12, 0.0, 1.0);
    col *= mix(1.0, v, 0.35);

    FragColor = vec4(col, 1.0);
}

