Post-Processing, Grain PP effect and Palette Color

A post-processing w/ grain pp effect and color palette like Buckshot.
Made for Godot 3.5 (With adjustments it is possible to apply in version 4).

 

Palette: Use a “GradientTexture2D” if you want.

Shader code
shader_type canvas_item;

uniform float pixel = 1.0;
uniform sampler2D pallete;

const float bit = 6.0;
const mat4 bayer = mat4(
	vec4(1.0,  9.0,  3.0, 11.0),
    vec4(13.0,  5.0, 15.0,  7.0),
    vec4(4.0, 12.0,  2.0, 10.0),
    vec4(16.0,  8.0, 14.0,  6.0));

float getbayer(int x, int y) {
	if (x == 0) {
		if (y == 0) return bayer[0][0];
		if (y == 1) return bayer[0][1];
		if (y == 2) return bayer[0][2];
		if (y == 3) return bayer[0][3];
	}
	else if (x == 1) {
		if (y == 0) return bayer[1][0];
		if (y == 1) return bayer[1][1];
		if (y == 2) return bayer[1][2];
		if (y == 3) return bayer[1][3];
	}
	else if (x == 2) {
		if (y == 0) return bayer[2][0];
		if (y == 1) return bayer[2][1];
		if (y == 2) return bayer[2][2];
		if (y == 3) return bayer[2][3];
	}
	else if (x == 3) {
		if (y == 0) return bayer[3][0];
		if (y == 1) return bayer[3][1];
		if (y == 2) return bayer[3][2];
		if (y == 3) return bayer[3][3];
	}
}


void fragment() {
	vec4 original = texture(TEXTURE, UV);
	vec4 _color = texture(SCREEN_TEXTURE, floor(FRAGCOORD.xy / pixel) / floor((1.0 / SCREEN_PIXEL_SIZE) / pixel));
	vec4 color = _color * texture(pallete, vec2(_color.r, 1.0));
	
	
	float b = getbayer(int(FRAGCOORD.x) % 4, int(FRAGCOORD.y) % 4);// * 1.0) / 1.0;
	vec2 uv = FRAGCOORD.xy / SCREEN_PIXEL_SIZE;
	vec4 col_noise = color;
	vec3 noise = vec3(fract(sin(dot(FRAGCOORD.xy / SCREEN_PIXEL_SIZE, vec2(12.9898, 78.233))) * 43758.5453));
	noise *= 0.1;
	noise.xy *= (b / 16.0) * 1.5;
	col_noise.rgb += noise; //noise effect
	
	vec4 post = col_noise * floor(col_noise * 4.0) / 4.0; //noise + post-effect
	
	
	
	COLOR *= post;
	
	
}
Tags
Grain, Post processing
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.

More from qjpg

Accumulated light (3D)

Related shaders

Extensible Color Palette (Gradient Maps) Now with Palette Blending!

Arbitrary Color Reduction and Palette Ordered Dithering

Earthbound-like battle background shader w/scroll effect and palette cycling

Subscribe
Notify of
guest

5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Cryobolt Interactive
Cryobolt Interactive
7 months ago

dude, you can just write

float getbayer(int x, int y)
{
return bayer[x][y];
}

anusb
anusb
6 months ago

unfortunately i can’t seem to make it work with godot 4.2 🙁
does anyone managed to do it ?

Last edited 6 months ago by anusb
Zalby
Zalby
2 months ago

Similarly to what @anusb already said I can compile it in Godot 4.2.1 but I cannot make it work.

Premise: the game I am trying this with is dark (few and not intense light sources) and I am an absolute beginner in shaders.

This is what I have done…

in the player’s 1st person view, create a ColorRect leaving the color “white” as defaultin “Material” I have set “ShaderMaterial”in the Shader Editor I have pasted your code but I have added, as suggested by the editor, this line of code in the definitions sectionuniform sampler2D SCREEN_TEXTURE: hint_screen_texture, filter_linear_mipmap;
in the Shaders Parameters, Pixel is 1 and Pallete is the default black-to-white “GradientTexture2D”Result: it compiles and run but it’s all black, I cannot see anything.
Variant: if I clear the Pallete section and I leave it blank it’s still extremely dark and unplayable, but I can sort of see the sky outline, so it might be an improvement.

I feel I might be missing something, but please let me know if you have any suggestions.

Last edited 2 months ago by Zalby
vyntrix
2 months ago
Reply to  Zalby

I’m trying to get it to work If i find it out I’ll let you know.