256 colour pixelation
This shader is designed to create a retro-gaming look by limiting colours and pixelating a canvas item’s appearance.
By default a range of 256 colours may be output by this shader. That is to say black, white, and 254 others. The Red, Green and Blue channels are individually stepped by the cube-root of 255, resulting in an even distribution of available colours.
The pixelation effect divides the texture’s x and y dimensions individually, both defaulting to 32 divisions. Setting this on a per-object basis allows the apparent resolution of each part to be matched.
Shader code
shader_type canvas_item;
uniform int resX = 32;
uniform int resY = 32;
// 0.100392156862 is the cube root of 255
uniform vec3 rgb255 = vec3(0.100392156862, 0.100392156862, 0.100392156862);
void fragment() {
float uvX = UV.x - mod(UV.x * float(resX), 1) / float(resX);
float uvY = UV.y - mod(UV.y * float(resY), 1) / float(resY);
vec2 grid_uv = vec2(uvX, uvY);
vec4 col = texture(TEXTURE, grid_uv);
if(col.r < 1.0 && col.g < 1.0 && col.b < 1.0) {
vec3 remainder = mod(col.rgb, rgb255);
col.rgb = col.rgb - remainder;
}
COLOR = col;
}
my texture appearing as plain white, how to use this?
For Godot 4, add this line to the top of the file:
And change line 14 to:
Hello, the cube root of 255 is not 0.100392156862. every single thing I have tried is telling me this number is random. I’ve tried 255^⅓, I’ve tried the reciprocal, I’ve tried dividing it by 255 I’ve tried going the other way I do not understand?????
thank you for the shader it works very well I’m only confused not upset
I couldn’t quite figure it out either. My guess is that there’s a reason why 0.1003… was chosen specifically, due to some color mapping theory perhaps, but I am not totally sure. You can actually get closer to 256 colors if you use 0.125 which is 1/8. 8^3 = 256.