4 level posterization (godot 4.3+)

Original shader written by LiathTheCrow but updated to work with godot 4.3+ versions. Differing from the original, the opacity of the selected levels is also taken into account.

Shader code
shader_type spatial;
render_mode unshaded;

uniform sampler2D SCREEN_TEXTURE : hint_screen_texture,filter_linear_mipmap;
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) = .1;
uniform float threshold2 	: hint_range(0.0,1.0) = .4;
uniform float threshold3	: hint_range(0.0,1.0) = .6;


void vertex() {

  POSITION = vec4(VERTEX.rg, 1.0, 1.0);
}

void fragment(){
	vec4 c_texture = texture(SCREEN_TEXTURE, SCREEN_UV).rgba;
	ALBEDO = c_texture.rgb;
	ALPHA = c_texture.a;
	
	float rgb_avg = (ALBEDO.r + ALBEDO.g + ALBEDO.b)/3.0;
	if(rgb_avg < threshold1){
		ALBEDO = level1.rgb;
		ALPHA = level1.a;
		
	}else if(rgb_avg < threshold2){
		ALBEDO = level2.rgb;
		ALPHA = level2.a;

	}else if(rgb_avg < threshold3){
		ALBEDO = level3.rgb;
		ALPHA = level3.a;

	}else{
		ALBEDO = level4.rgb;
		ALPHA = level4.a;
	}
}
Tags
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 (Fragment)

4 level posterization

Fire Shader for Godot Engine 4

Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
MeowKats
MeowKats
3 months ago

Not entirely sure why you’re modifying the vertex, but removing that fixes a problem that it has.