From ec3ac51a89ea56b7568e3fb4dd216ae3d6a65297 Mon Sep 17 00:00:00 2001 From: Cametendo Date: Mon, 8 Jun 2026 11:49:44 +0200 Subject: [PATCH] add mc_kuwahara_shader --- mc_kuwahara_shader/final.fsh.settings | 1 + mc_kuwahara_shader/shaders.properties | 3 + mc_kuwahara_shader/shaders/final.fsh | 59 +++++++++++++++++++ mc_kuwahara_shader/shaders/final.vsh | 11 ++++ .../shaders/gbuffers_terrain.fsh | 20 +++++++ .../shaders/gbuffers_terrain.vsh | 21 +++++++ 6 files changed, 115 insertions(+) create mode 100644 mc_kuwahara_shader/final.fsh.settings create mode 100644 mc_kuwahara_shader/shaders.properties create mode 100644 mc_kuwahara_shader/shaders/final.fsh create mode 100644 mc_kuwahara_shader/shaders/final.vsh create mode 100644 mc_kuwahara_shader/shaders/gbuffers_terrain.fsh create mode 100644 mc_kuwahara_shader/shaders/gbuffers_terrain.vsh diff --git a/mc_kuwahara_shader/final.fsh.settings b/mc_kuwahara_shader/final.fsh.settings new file mode 100644 index 0000000..f4b12e6 --- /dev/null +++ b/mc_kuwahara_shader/final.fsh.settings @@ -0,0 +1 @@ +shadowmap=false \ No newline at end of file diff --git a/mc_kuwahara_shader/shaders.properties b/mc_kuwahara_shader/shaders.properties new file mode 100644 index 0000000..cd64bca --- /dev/null +++ b/mc_kuwahara_shader/shaders.properties @@ -0,0 +1,3 @@ +separateAo=true +shadowMapResolution=0 +generateShadowMap=false \ No newline at end of file diff --git a/mc_kuwahara_shader/shaders/final.fsh b/mc_kuwahara_shader/shaders/final.fsh new file mode 100644 index 0000000..c4209fe --- /dev/null +++ b/mc_kuwahara_shader/shaders/final.fsh @@ -0,0 +1,59 @@ +#version 150 + +uniform float viewWidth; +uniform float viewHeight; +uniform sampler2D colortex0; + +in vec2 texCoord; +out vec4 fragColor; + +// Tune these to trade quality vs performance: +// RADIUS 36, STEP 1 → 5476 samples/pixel (original, ~9fps) +// RADIUS 36, STEP 3 → 676 samples/pixel (~8x faster) +// RADIUS 36, STEP 4 → 400 samples/pixel (~14x faster) +// RADIUS 16, STEP 2 → 324 samples/pixel (smaller brush, fast) +#define RADIUS 36 +#define STEP 3 + +vec3 kuwahara_region(vec2 uv, vec2 ts, int x0, int x1, int y0, int y1, out float outVariance) { + vec3 colorSum = vec3(0.0); + float lumaSum = 0.0; + float lumaSqSum = 0.0; + float count = 0.0; + + for (int i = x0; i <= x1; i += STEP) { + for (int j = y0; j <= y1; j += STEP) { + vec3 c = texture(colortex0, uv + vec2(float(i), float(j)) * ts).rgb; + float luma = dot(c, vec3(0.299, 0.587, 0.114)); + colorSum += c; + lumaSum += luma; + lumaSqSum += luma * luma; + count += 1.0; + } + } + + vec3 mean = colorSum / count; + float meanLuma = lumaSum / count; + float variance = (lumaSqSum / count) - (meanLuma * meanLuma); + outVariance = variance * variance; // square for harder selection + return mean; +} + +void main() { + vec2 ts = vec2(1.0 / viewWidth, 1.0 / viewHeight); + + float v_tr, v_tl, v_bl, v_br; + vec3 mean_tr = kuwahara_region(texCoord, ts, 0, RADIUS, 0, RADIUS, v_tr); + vec3 mean_tl = kuwahara_region(texCoord, ts, -RADIUS, 0, 0, RADIUS, v_tl); + vec3 mean_bl = kuwahara_region(texCoord, ts, -RADIUS, 0, -RADIUS, 0, v_bl); + vec3 mean_br = kuwahara_region(texCoord, ts, 0, RADIUS, -RADIUS, 0, v_br); + + vec3 finalColor = mean_tr; + float minVar = v_tr; + + if (v_tl < minVar) { minVar = v_tl; finalColor = mean_tl; } + if (v_bl < minVar) { minVar = v_bl; finalColor = mean_bl; } + if (v_br < minVar) { minVar = v_br; finalColor = mean_br; } + + fragColor = vec4(finalColor, 1.0); +} diff --git a/mc_kuwahara_shader/shaders/final.vsh b/mc_kuwahara_shader/shaders/final.vsh new file mode 100644 index 0000000..54df053 --- /dev/null +++ b/mc_kuwahara_shader/shaders/final.vsh @@ -0,0 +1,11 @@ +#version 150 + +in vec3 vaPosition; +in vec2 vaUV0; + +out vec2 texCoord; + +void main() { + gl_Position = vec4(vaPosition.xy * 2.0 - 1.0, 0.0, 1.0); + texCoord = vaUV0; +} diff --git a/mc_kuwahara_shader/shaders/gbuffers_terrain.fsh b/mc_kuwahara_shader/shaders/gbuffers_terrain.fsh new file mode 100644 index 0000000..5d08a26 --- /dev/null +++ b/mc_kuwahara_shader/shaders/gbuffers_terrain.fsh @@ -0,0 +1,20 @@ +#version 150 + +/* DRAWBUFFERS:0 */ + +uniform sampler2D gtexture; +uniform sampler2D lightmap; + +in vec2 texCoord; +in vec4 tintColor; +in vec2 lightCoord; + +out vec4 fragColor; + +void main() { + vec4 color = texture(gtexture, texCoord); + if (color.a < 0.1) discard; + color *= tintColor; + color.rgb *= texture(lightmap, lightCoord).rgb; + fragColor = color; +} diff --git a/mc_kuwahara_shader/shaders/gbuffers_terrain.vsh b/mc_kuwahara_shader/shaders/gbuffers_terrain.vsh new file mode 100644 index 0000000..bbfd05b --- /dev/null +++ b/mc_kuwahara_shader/shaders/gbuffers_terrain.vsh @@ -0,0 +1,21 @@ +#version 150 + +uniform mat4 modelViewMatrix; +uniform mat4 projectionMatrix; +uniform vec3 chunkOffset; + +in vec3 vaPosition; +in vec4 vaColor; +in vec2 vaUV0; +in ivec2 vaUV2; + +out vec2 texCoord; +out vec4 tintColor; +out vec2 lightCoord; + +void main() { + gl_Position = projectionMatrix * modelViewMatrix * vec4(vaPosition + chunkOffset, 1.0); + texCoord = vaUV0; + tintColor = vaColor; + lightCoord = vaUV2 / 256.0; +}