Donitzo / godot-color-dither

Multicolored dithering shaders for Godot 4.
Creative Commons Zero v1.0 Universal
33 stars 2 forks source link

Some colors are dithered even though they match the colors in the dither palette. #2

Open sommerper opened 2 weeks ago

sommerper commented 2 weeks ago

Hi again!

I made a little experiment where used a ColorRect and applied the color_dither_post_2d.gdshader to it to get a fullscreen dither.

But I noticed that some colors get dithered even though the match the colors used to generate the dither palette.

My logic tells me that only colors outside of the palette should be dithered - is that correct?

Example: image

Donitzo commented 1 week ago

It's a challenging bug. It does create the correct color mix (same color twice) for the exact color, and for the closest color in 16 discrete RGB steps, but for some reason in the shader that is not the mix chosen. I'll have to continue looking into it.

Color creation:

var color:Color = Color(floor(x / floor(16)) / 15.0, y / 15.0, fmod(x, 16.0) / 15.0)
var mixing_plan:Array = devise_best_mixing_plan.call(color)
for i in dither_color_count:
    dither_palette_image.set_pixel(x, y + i * 16, mixing_plan[i])

Color selection in shader:

float y = (clamp(color.g * 16.0, 0.5, 15.5) + floor(dither_value * color_count) * 16.0) / palette_size.y;
float x = clamp(floor(color.r * 16.0), 0.0, 15.0) / 16.0 + clamp(color.b * 16.0, 0.5, 15.5) / 256.0;
return vec4(texture(dither_palette, vec2(x, y)).rgb, 1.0);
sommerper commented 1 week ago

Ok, thanks for the clarification! At least I know it's not just on my setup. I'll see if I can find some time to have a look at it as well.