3D Pixelation via material

with this shader you can set personal level of pixelation for each object

INSTRUCTION:

-just drop this shader to next pass in resource

 

my first shader and yes im gonna fix that crops

Shader code
shader_type spatial;
render_mode unshaded;

uniform sampler2D SCREEN_TEXTURE : hint_screen_texture;
uniform int pixel_size = 4;

void vertex() {
	float nw = (1.0*float(pixel_size));
	if (NORMAL.x == 1.0 || NORMAL.y == 1.0 || NORMAL.z == 1.0 || NORMAL.x == -1.0 || NORMAL.y == -1.0 || NORMAL.z == -1.0){
		VERTEX *= 1.0+0.01*nw;
	}
	if ((NORMAL.x != 1.0 && NORMAL.y != 0.0 && NORMAL.z != 0.0) && (NORMAL.y != 1.0 && NORMAL.x != 0.0 && NORMAL.z != 0.0) && (NORMAL.z != 1.0 && NORMAL.x != 0.0 && NORMAL.y != 0.0) && (NORMAL.x != -1.0 && NORMAL.y != 0.0 && NORMAL.z != 0.0) && (NORMAL.y != -1.0 && NORMAL.x != 0.0 && NORMAL.z != 0.0) && (NORMAL.z != -1.0 && NORMAL.x != 0.0 && NORMAL.y != 0.0)){
		VERTEX += NORMAL*0.01*nw;
	}
}

void fragment() {
	float x = float(int(FRAGCOORD.x) % pixel_size);
	float y = float(int(FRAGCOORD.y) % pixel_size);
	x = FRAGCOORD.x + floor(float(pixel_size) / 2.0) - x;
	y = FRAGCOORD.y + floor(float(pixel_size) / 2.0) - y;
	vec4 oldtext = texture(SCREEN_TEXTURE,SCREEN_UV*1.0);
	vec4 pixelatedtext = texture(SCREEN_TEXTURE, vec2(x, y) / (VIEWPORT_SIZE));
	if (pixelatedtext.a==0.0 && oldtext.a==0.0){
		discard;
	}
	ALBEDO = pixelatedtext.rgb;
}
Tags
3d, pixelation
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 LopkaUna

Clean pixel perfect outline via material

Related shaders

Selective post-processing via material ID masking on a second visibility layer

Clean pixel perfect outline via material

The simplest outline shader (via material)

Subscribe
Notify of
guest

5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
DigitalUnity
DigitalUnity
1 year ago

This is a GREAT shader! I was looking for something that would let me do exactly what this does and it’s on the front page just waiting for me to stumble upon it! Easy to use, works great, and very good at what it does! Thank you so much for making this!!!

aaa
aaa
11 months ago

This is great! It does however seem to affect everything else that is around it, this is what happens to a background object if I place it on a sphere, it also gets pixelated https://postimg.cc/RNXW3w4c do you know any way to fix it?

B en
B en
6 months ago
Reply to  LopkaUna

Couldn’t it be done with some sort of depth test?