Kuwahara Shader
This is a converted shadertoy shader from https://www.shadertoy.com/view/MsXSz4
The Kuwahara filter can be used as an Sprite or post-processing effect. Just add it to an Sprite2D or TextureRect node and add an input texture. The offset can also be used to tint the canvas.
Shader code
shader_type canvas_item;
uniform int radius = 5;
uniform vec3 offset=vec3(0.0);
void fragment()
{
vec2 src_size = vec2 ( SCREEN_PIXEL_SIZE.x, SCREEN_PIXEL_SIZE.y);
vec2 uv = UV.xy;
float n = float((radius + 1) * (radius + 1));
vec3 m0 = offset; vec3 m1 = offset; vec3 m2 = offset; vec3 m3 = offset;
vec3 s0 = offset; vec3 s1 = offset; vec3 s2 = offset; vec3 s3 = offset;
vec3 c;
for (int j = -radius; j <= 0; ++j) {
for (int i = -radius; i <= 0; ++i) {
c = texture(TEXTURE, uv + vec2(float(i),float(j)) * src_size).rgb;
m0 += c;
s0 += c * c;
}
}
for (int j = -radius; j <= 0; ++j) {
for (int i = 0; i <= radius; ++i) {
c = texture(TEXTURE, uv + vec2(float(i),float(j)) * src_size).rgb;
m1 += c;
s1 += c * c;
}
}
for (int j = 0; j <= radius; ++j) {
for (int i = 0; i <= radius; ++i) {
c = texture(TEXTURE, uv + vec2(float(i),float(j)) * src_size).rgb;
m2 += c;
s2 += c * c;
}
}
for (int j = 0; j <= radius; ++j) {
for (int i = -radius; i <= 0; ++i) {
c = texture(TEXTURE, uv + vec2(float(i),float(j)) * src_size).rgb;
m3 += c;
s3 += c * c;
}
}
float min_sigma2 = 1e+2;
m0 /= n;
s0 = abs(s0 / n - m0 * m0);
float sigma2 = s0.r + s0.g + s0.b;
if (sigma2 < min_sigma2) {
min_sigma2 = sigma2;
COLOR = vec4(m0, 1.0);
}
m1 /= n;
s1 = abs(s1 / n - m1 * m1);
sigma2 = s1.r + s1.g + s1.b;
if (sigma2 < min_sigma2) {
min_sigma2 = sigma2;
COLOR = vec4(m1, 1.0);
}
m2 /= n;
s2 = abs(s2 / n - m2 * m2);
sigma2 = s2.r + s2.g + s2.b;
if (sigma2 < min_sigma2) {
min_sigma2 = sigma2;
COLOR = vec4(m2, 1.0);
}
m3 /= n;
s3 = abs(s3 / n - m3 * m3);
sigma2 = s3.r + s3.g + s3.b;
if (sigma2 < min_sigma2) {
min_sigma2 = sigma2;
COLOR = vec4(m3, 1.0);
}
}
this work on 3D projects ? i can’t manage to work. can you give an example ?
it does. you need to to render your 3D game to an texturerect and apply the shader.
Use this in the shader material in color rect
vGodot4.1.3
What do you mean with add an input texture?
If u are using Godot 4 then use this code instead.
This doesn’t work in Godot 4, so I made some changes.
CanvasLayer >> ColorRect >> Material>>ShaderMaterial>>Paste this Code
Hey, this doesn’t seem to work, I just get a blank image instead of the intended effect…