Mosaic/Pixelization Effect Shader

This is a mosaic effect shader.

2D version mosaic effect is on here

Shader code
/*
	Mosaic shader for 3D spatial by Yui Kinomoto @arlez80
	3Dモデル用モザイクシェーダー by あるる(きのもと 結衣)

	size = tile size

	MIT License
*/
shader_type spatial;
render_mode unshaded;

uniform float size = 20.0;

void fragment( )
{
	vec2 p = floor( FRAGCOORD.xy / size ) * size;
	vec2 quat_x = vec2( size / 4.0, 0 );
	vec2 quat_y = vec2( 0, quat_x.x );
	ALBEDO = (
		texelFetch( SCREEN_TEXTURE, ivec2( p ), 0 ).xyz
	+	texelFetch( SCREEN_TEXTURE, ivec2( p + quat_x ), 0 ).xyz
	+	texelFetch( SCREEN_TEXTURE, ivec2( p + quat_x * 2.0 ), 0 ).xyz
	+	texelFetch( SCREEN_TEXTURE, ivec2( p + quat_x * 3.0 ), 0 ).xyz
	+	texelFetch( SCREEN_TEXTURE, ivec2( p + quat_y ), 0 ).xyz
	+	texelFetch( SCREEN_TEXTURE, ivec2( p + quat_y + quat_x ), 0 ).xyz
	+	texelFetch( SCREEN_TEXTURE, ivec2( p + quat_y + quat_x * 2.0 ), 0 ).xyz
	+	texelFetch( SCREEN_TEXTURE, ivec2( p + quat_y + quat_x * 3.0 ), 0 ).xyz
	+	texelFetch( SCREEN_TEXTURE, ivec2( p + quat_y * 2.0 ), 0 ).xyz
	+	texelFetch( SCREEN_TEXTURE, ivec2( p + quat_y * 2.0 + quat_x ), 0 ).xyz
	+	texelFetch( SCREEN_TEXTURE, ivec2( p + quat_y * 2.0 + quat_x * 2.0 ), 0 ).xyz
	+	texelFetch( SCREEN_TEXTURE, ivec2( p + quat_y * 2.0 + quat_x * 3.0 ), 0 ).xyz
	+	texelFetch( SCREEN_TEXTURE, ivec2( p + quat_y * 3.0 ), 0 ).xyz
	+	texelFetch( SCREEN_TEXTURE, ivec2( p + quat_y * 3.0 + quat_x ), 0 ).xyz
	+	texelFetch( SCREEN_TEXTURE, ivec2( p + quat_y * 3.0 + quat_x * 2.0 ), 0 ).xyz
	+	texelFetch( SCREEN_TEXTURE, ivec2( p + quat_y * 3.0 + quat_x * 3.0 ), 0 ).xyz
	) / 16.0;
}
Tags
mosaic, pixelization, Post Effect
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.

More from arlez80

Procedural Grain Wood Shader

Procedural Brick Shader

Glass Square Effect Shader

Related shaders

Spatial Pixelization/Mosaic

Hex Pixelization Shader

Scratch mosaic effect

Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
zpecc
zpecc
5 days ago

Had to add this line to make it work with Godot 4:

uniform sampler2D SCREEN_TEXTURE : hint_screen_texture, filter_linear_mipmap;

But other than that it works great!