Simulating a JPEG effect

Simulating JPEG image compression to further optimize GPU performance.

A shader that simplifies the image by breaking it down into blocks—much like JPEG compression at a low-quality setting.

 

Use within a ColorRect.
(cover the entire viewport)

 

(Quality) -  controls the compression force.
(Block Size) - specifies the size of the blocks.
Shader code
shader_type canvas_item;
uniform sampler2D screen_tex : hint_screen_texture, filter_nearest;
uniform int block_size : hint_range(2, 32) = 6;
uniform int color_levels : hint_range(2, 32) = 12;

void fragment() {
	vec2 size = 1.0 / SCREEN_PIXEL_SIZE;
	vec2 center = floor(SCREEN_UV * size / float(block_size)) * float(block_size) + float(block_size) * 0.5;
	vec3 c = texture(screen_tex, center / size).rgb;
	COLOR = vec4(floor(c * float(color_levels)) / float(color_levels), 1.0);
}
Live Preview
Tags
contraction, effect, jpeg, Post processing, simulating
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

Related shaders

Comments 0

No comments yet.