Pixel Shader Script Help

Godot Version

4.2.1

Question

Hey y’all, I’m wanting to make an ASCII shader, but can’t seem to get past getting the shader script to basically pixelate the screen texture so that I can replace the bigger pixelated chunks into the proper text character.

I’ve tried following some tutorials and googling the math so that I can understand what’s going on in the code in order to adjust stuff properly in the future, but can’t seem to get past this. My biggest point of confusion is that if I only get the floor of ONE of the UV coordinates and not the other, then I get color bands along that axis, which makes sense for what I want, but once I get the floor of BOTH, it instead just takes the color of the pixel at (0,0) and makes the entire screen that color and nothing else.

I’m relatively new to scripting shaders so I can’t tell if it’s something very simple and obvious, so any help / explanation for this behavior is greatly appreciated!

shader_type spatial;
render_mode unshaded;
uniform sampler2D SCREEN_TEXTURE : hint_screen_texture, repeat_disable, filter_linear;

void vertex() {
	// Called for every vertex the material is visible on.
	POSITION = vec4(VERTEX, 1.0);
}

void fragment() {
	float uv_x = (SCREEN_UV.x / 8.) * 8.;
	float uv_y = floor(SCREEN_UV.y / 8.) * 8.;
	vec2 uv = vec2(uv_x, uv_y);
	
	vec3 color = texture(SCREEN_TEXTURE, uv).rgb;
	
	// Called for every pixel the material is visible on.
	ALBEDO = color;
}


Shader Output With Only Y UV Floored


Shader Output With Both UVs Floored

I believe it’s because UV coordinates are in the range [0, 1]. So when you use floor it returns 0.

1 Like