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;
}
Live Preview
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

Comments 7

  1. Rasmus

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

    1
    1. Jef - Crab Game

      I have no idea either

      -24
    2. TrevSaysHi

      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

      6
  2. Dylan

    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!

    3
  3. 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.

    1
  4. Bixlewd

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

    5
    1. i think it can be fixed using something like this (but havent tested it yet):

       

      uv.x *= SCREEN_PIXEL_SIZE.x / SCREEN_PIXEL_SIZE.y;

      -4