Skip to content

Instantly share code, notes, and snippets.

@QueenOfSquiggles
Last active April 1, 2022 02:28
Show Gist options
  • Save QueenOfSquiggles/a45f0c9c6a43e290b2137e58dce4597f to your computer and use it in GitHub Desktop.
Save QueenOfSquiggles/a45f0c9c6a43e290b2137e58dce4597f to your computer and use it in GitHub Desktop.
Godot Mosaic Shader
shader_type canvas_item;
uniform float num_colors = 16.0;
uniform float pixelate_amount = 10.0; // this is what affects the size of the mosaic
uniform vec2 screen_size = vec2(1024.0, 600.0); // use your screen size
const float bayer_matrix[] = {
0.0, 8.0, 2.0, 10.0,
12.0, 4.0, 14.0, 6.0,
3.0, 11.0, 1.0, 9.0,
15.0, 7.0, 13.0, 5.0
};
const float bayer_matrix_width = 4.0;
const float bayer_matrix_order = 16.0;
vec3 posterize(vec3 input, float gamma, float numColours){
vec3 c = pow(input, vec3(gamma));
c = c * numColours;
c = floor(c);
c = c / num_colors;
c = pow(c, vec3(1.0 / gamma));
return c;
}
float luma(vec3 c){
return (c.r * 0.3) + (c.g * 0.3) + (c.b * 0.3);
}
vec3 dither(vec3 input, vec2 screen_pos){
vec2 pixel = mod(floor(screen_pos), bayer_matrix_width);
int index = int(pixel.x + pixel.y * bayer_matrix_width);
float threshold = (bayer_matrix[index] / bayer_matrix_order) - 0.5;
float light = luma(input);
if (light < threshold) {
return vec3(0.0);
}
return input;
}
void fragment() {
vec2 factor = screen_size / pixelate_amount;
vec2 pixel_coords = round(SCREEN_UV * factor) / factor;
vec3 sample = texture(SCREEN_TEXTURE, pixel_coords).rgb;
sample = posterize(sample, 0.9, num_colors);// optional
sample = dither(sample, pixel_coords); // optional
COLOR = vec4(sample, 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment