Pixelate

Pixelate shader. Uses a different factor for X and Y resulting in square pixels independent of texture size and precise pixel size via the Pixel Size param

Shader code
shader_type canvas_item;

uniform int pixelSize = 4;

void fragment()
{
	
	ivec2 size = textureSize(TEXTURE, 0);
	
	int xRes = size.x;
	int yRes = size.y;
	
	float xFactor = float(xRes) / float(pixelSize);
	float yFactor = float(yRes) / float(pixelSize);
	
	float grid_uv_x = round(UV.x * xFactor) / xFactor;
	float grid_uv_y = round(UV.y * yFactor) / yFactor;
	
	vec4 text = texture(TEXTURE, vec2(grid_uv_x, grid_uv_y));
	
	COLOR = text;
}
Tags
Low Poly, pixel, psx
The shader code and all code snippets in this post are under MIT license and can be used freely. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

Related shaders

Palette Filter and Pixelate combined

Pixelate into view (Texture Resolution)

Pixelate into view (Custom Resolution)

guest

6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Rasmus
Rasmus
11 months ago

This is just what I needed for my project. Where do you apply the shader?

Jef - Crab Game
Jef - Crab Game
11 months ago
Reply to  Rasmus

I have no idea either

TrevSaysHi
TrevSaysHi
11 months ago
Reply to  Rasmus

You can get it to work by adding a ViewportContainer with a Viewport as a child node. Set the shader on the ViewportContainer.

In my scene I have ViewportContainer -> Viewport -> Player

Dylan
Dylan
11 months ago

Using this on a 3D scene has really mixed results – sometimes it looks fine, sometimes it just breaks. Can’t quite figure it out, honestly!

SAVC
SAVC
6 months ago

Works flawlessly, but for some reasons all 3D sprites disappear when looked from a certain angle. Apparently said angle depends on the quadmesh position in the world.

Bixlewd
Bixlewd
5 months ago

Only works if the viewport is 1:1 aspect ratio. Otherwise you get distorted (non-square) pixels.