4-Level Posterization (Fragment)

Original Spatial Shader by LiathTheCrow

I ended up tweaking Liath’s shader to work using canvas_item, as I wanted to try and use a canvas_item shader + I wanted to use the shader in Godot 4 (Liath wrote their shader in Godot 3).

When adjusting values, be sure to do it in the ColorRect’s Shader Parameters – I tried to change it through the code for around half an hour, but apparently it doesn’t work that way. I’m not entirely sure why, but it doesn’t.

If needed, you can add more params by making another level & threshold, then adding to the elif statements.

Shader code
shader_type canvas_item;
render_mode unshaded; // Credit: LiathTheCrow for original shader code

uniform vec4 level1 : source_color = vec4(0.0, 0.0, 0.0, 1.0);
uniform vec4 level2 : source_color = vec4(0.5, 0.5, 0.5, 1.0);
uniform vec4 level3 : source_color = vec4(1.0, 1.0, 1.0, 1.0);
uniform vec4 level4 : source_color = vec4(1.0, 1.0, 1.0, 1.0);

uniform float threshold1 	: hint_range(0.0,1.0) = .4;
uniform float threshold2 	: hint_range(0.0,1.0) = .52;
uniform float threshold3	: hint_range(0.0,1.0) = .8;
uniform sampler2D SCREEN_TEXTURE : hint_screen_texture, filter_linear_mipmap;

void fragment() {
	COLOR = texture(SCREEN_TEXTURE, SCREEN_UV).rgba;
	float rgb_avg = (COLOR.r + COLOR.g + COLOR.b)/3.0;
	if(rgb_avg < threshold1){
		COLOR = level1.rgba;
	}else if(rgb_avg < threshold2){
		COLOR = level2.rgba;
	}else if(rgb_avg < threshold3){
		COLOR = level3.rgba;
	}else{
		COLOR = level4.rgba;
	}
}
Tags
4-bit, gameboy, Posterisation, posterization
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

4 level posterization

GLES 2.0 2D Fragment Shader Tutorial Series in a Single Godot Project from Beginner to Advanced

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments