new dithering

This commit is contained in:
Asuro
2025-06-01 03:17:21 +02:00
parent faa36dd679
commit 6c8bead6ab
9 changed files with 92 additions and 60 deletions

View File

@@ -32,7 +32,7 @@ vec3 desaturate(vec3 color)
float dither(float brightness, vec2 inputUv)
{
float globalScale = 8;
float globalScale = 6;
// constants
float xRes = u_texInfo.z;
@@ -59,10 +59,9 @@ float dither(float brightness, vec2 inputUv)
// Figuring out how many dots we want
float scaleExp = exp2(globalScale);
float spacing = freq.y * scaleExp;
spacing *= dotsPerSide * 0.125;
spacing *= dotsPerSide * (1 / globalScale); // todo: just guessed that globalScale is the right variable here
float brightnessSpacingMultiplier = pow(brightnessCurve * 2 + 0.001, -(1 - 0.5));
// float brightnessSpacingMultiplier = 1;
spacing *= brightnessSpacingMultiplier;
float spacingLog = log2(spacing);
@@ -77,35 +76,58 @@ float dither(float brightness, vec2 inputUv)
float pattern = texture3D(s_ditherSampler, vec3(uv, subLayer)).r;
float contrast = 2 * scaleExp * brightnessSpacingMultiplier * 0.1;
float contrast = 0.5 * scaleExp * brightnessSpacingMultiplier * 0.1;
contrast *= pow(freq.y / freq.x, 1.0);
float baseVal = lerp(0.5, brightness, saturate(1.05 / (1 + contrast)));
float threshold = 1 - brightnessCurve + 0.6;
float threshold = 1 - brightnessCurve;
return saturate((pattern - threshold) * contrast + baseVal);
}
vec4 rgbToCmyk(vec3 rgb)
{
return vec4(1.0, 1.0, 1.0, 1.0);
}
vec3 cmykToRgb(vec4 cmyk)
{
return vec3(1.0, 1.0, 1.0);
}
vec2 rotateUV(vec2 uv, vec2 angle)
{
return uv;
}
void main()
{
// setup
bool isTextured = u_dotColor.x < 0.02;
float testRadius = 30.0;
float testSpeed = 1.0;
vec3 testOffset = vec3(0.0, 0.0, 50.0);
vec3 lightPos = vec3(sin(u_time.x * testSpeed) * testRadius, 5.0, cos(u_time.x * testSpeed) * testRadius);
vec3 texColor = u_dotColor.x > 0.1 ? u_dotColor.xyz : texture2D(s_texColor, v_uv0).xyz;
vec3 baseColor = u_baseColor.xyz;
vec3 texColor = isTextured ? texture2D(s_texColor, v_uv0).xyz : u_dotColor.xyz;
// lighting
// float brightness = calcBrightness(lightPos, v_wpos, v_normal);
float brightness = lerp(0.5, 0.9, calcBrightnessDirectional(vec3(0.5, 0.3, 1.0), v_normal));
// float brightness = 0.5;
// brightness = lerp(0.2, 1.0, sin(u_time.x) * 0.5 + 0.5);
float brightness = lerp(0.2, 0.8, calcBrightnessDirectional(vec3(0.5, 0.3, 1.0), v_normal));
float r = dither(brightness * texColor.r, v_uv0);
float g = dither(brightness * texColor.g, v_uv0);
float b = dither(brightness * texColor.b, v_uv0);
// vec3 finalColor = vec3(r, g, b);
vec3 ditheredColor = dither(brightness, v_uv0);
vec3 finalColor = mix(u_baseColor, texColor, ditheredColor);
gl_FragColor = vec4(finalColor, 1.0);
// dither
float r = dither(texColor.r * brightness, v_uv0);
float g = dither(texColor.g * brightness, v_uv0);
float b = dither(texColor.b * brightness, v_uv0);
vec3 ditheredColor = vec3(r,g,b);
vec4 cmyk = rgbToCmyk(texColor * brightness);
cmyk.x = dither(cmyk.x, rotateUV(v_uv0, float2(0.966, 0.259)));
cmyk.y = dither(cmyk.y, rotateUV(v_uv0, float2(0.259, 0.966)));
cmyk.z = dither(cmyk.z, rotateUV(v_uv0, float2(1.000, 0.000)));
cmyk.w = dither(cmyk.w, rotateUV(v_uv0, float2(0.707, 0.707)));
// vec3 ditheredColor = cmykToRgb(cmyk);
// finalize
vec3 finalColor = mix(baseColor, ditheredColor, ditheredColor);
gl_FragColor = vec4(finalColor, 1.0);
// gl_FragColor = vec4(texColor * brightness, 1.0);
}