shader_type canvas_item;

uniform vec4 bg_color: source_color;
uniform float zoom;

const vec4 grid_gray = vec4(0.8, 0.8, 0.8, 1.0);
const vec4 grid_white = vec4(1.0, 1.0, 1.0, 1.0);

varying vec2 pixel_pos;


void vertex() {
	pixel_pos = (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0)).xy;
}

void fragment() {
	float grid_size = pow(2.0, ceil(log2((16.0 / zoom))));
    
    vec2 grid_coord = floor(pixel_pos / grid_size);
    bool is_dark_cell = mod(grid_coord.x + grid_coord.y, 2.0) == 0.0;
    COLOR = is_dark_cell ? grid_gray : grid_white;
    if (bg_color.a > 0.0) {
        COLOR = mix(COLOR, bg_color, bg_color.a);
    }
}
