Skip to content

Commit 4ed3564

Browse files
authored
Merge pull request #159 from JusGu/main
Fix Dither
2 parents 4040fa5 + 76c5116 commit 4ed3564

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

src/content/Backgrounds/Dither/Dither.jsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,22 @@ const float bayerMatrix8x8[64] = float[64](
114114
);
115115
116116
vec3 dither(vec2 uv, vec3 color) {
117-
int x = int(uv.x * resolution.x) % 8;
118-
int y = int(uv.y * resolution.y) % 8;
117+
vec2 scaledCoord = floor(uv * resolution / pixelSize);
118+
int x = int(mod(scaledCoord.x, 8.0));
119+
int y = int(mod(scaledCoord.y, 8.0));
119120
float threshold = bayerMatrix8x8[y * 8 + x] - 0.25;
120-
color += threshold;
121+
float step = 1.0 / (colorNum - 1.0);
122+
color += threshold * step;
123+
float bias = 0.2;
124+
color = clamp(color - bias, 0.0, 1.0);
121125
return floor(color * (colorNum - 1.0) + 0.5) / (colorNum - 1.0);
122126
}
123127
124128
void mainImage(in vec4 inputColor, in vec2 uv, out vec4 outputColor) {
125129
vec2 normalizedPixelSize = pixelSize / resolution;
126130
vec2 uvPixel = normalizedPixelSize * floor(uv / normalizedPixelSize);
127131
vec4 color = texture2D(inputBuffer, uvPixel);
128-
color.rgb = dither(uvPixel, color.rgb);
132+
color.rgb = dither(uv, color.rgb);
129133
outputColor = color;
130134
}
131135
`;

src/tailwind/Backgrounds/Dither/Dither.jsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,22 @@ const float bayerMatrix8x8[64] = float[64](
112112
);
113113
114114
vec3 dither(vec2 uv, vec3 color) {
115-
int x = int(uv.x * resolution.x) % 8;
116-
int y = int(uv.y * resolution.y) % 8;
115+
vec2 scaledCoord = floor(uv * resolution / pixelSize);
116+
int x = int(mod(scaledCoord.x, 8.0));
117+
int y = int(mod(scaledCoord.y, 8.0));
117118
float threshold = bayerMatrix8x8[y * 8 + x] - 0.25;
118-
color += threshold;
119+
float step = 1.0 / (colorNum - 1.0);
120+
color += threshold * step;
121+
float bias = 0.2;
122+
color = clamp(color - bias, 0.0, 1.0);
119123
return floor(color * (colorNum - 1.0) + 0.5) / (colorNum - 1.0);
120124
}
121125
122126
void mainImage(in vec4 inputColor, in vec2 uv, out vec4 outputColor) {
123127
vec2 normalizedPixelSize = pixelSize / resolution;
124128
vec2 uvPixel = normalizedPixelSize * floor(uv / normalizedPixelSize);
125129
vec4 color = texture2D(inputBuffer, uvPixel);
126-
color.rgb = dither(uvPixel, color.rgb);
130+
color.rgb = dither(uv, color.rgb);
127131
outputColor = color;
128132
}
129133
`;

src/ts-default/Backgrounds/Dither/Dither.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,22 @@ const float bayerMatrix8x8[64] = float[64](
114114
);
115115
116116
vec3 dither(vec2 uv, vec3 color) {
117-
int x = int(uv.x * resolution.x) % 8;
118-
int y = int(uv.y * resolution.y) % 8;
117+
vec2 scaledCoord = floor(uv * resolution / pixelSize);
118+
int x = int(mod(scaledCoord.x, 8.0));
119+
int y = int(mod(scaledCoord.y, 8.0));
119120
float threshold = bayerMatrix8x8[y * 8 + x] - 0.25;
120-
color += threshold;
121+
float step = 1.0 / (colorNum - 1.0);
122+
color += threshold * step;
123+
float bias = 0.2;
124+
color = clamp(color - bias, 0.0, 1.0);
121125
return floor(color * (colorNum - 1.0) + 0.5) / (colorNum - 1.0);
122126
}
123127
124128
void mainImage(in vec4 inputColor, in vec2 uv, out vec4 outputColor) {
125129
vec2 normalizedPixelSize = pixelSize / resolution;
126130
vec2 uvPixel = normalizedPixelSize * floor(uv / normalizedPixelSize);
127131
vec4 color = texture2D(inputBuffer, uvPixel);
128-
color.rgb = dither(uvPixel, color.rgb);
132+
color.rgb = dither(uv, color.rgb);
129133
outputColor = color;
130134
}
131135
`;

src/ts-tailwind/Backgrounds/Dither/Dither.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,22 @@ const float bayerMatrix8x8[64] = float[64](
112112
);
113113
114114
vec3 dither(vec2 uv, vec3 color) {
115-
int x = int(uv.x * resolution.x) % 8;
116-
int y = int(uv.y * resolution.y) % 8;
115+
vec2 scaledCoord = floor(uv * resolution / pixelSize);
116+
int x = int(mod(scaledCoord.x, 8.0));
117+
int y = int(mod(scaledCoord.y, 8.0));
117118
float threshold = bayerMatrix8x8[y * 8 + x] - 0.25;
118-
color += threshold;
119+
float step = 1.0 / (colorNum - 1.0);
120+
color += threshold * step;
121+
float bias = 0.2;
122+
color = clamp(color - bias, 0.0, 1.0);
119123
return floor(color * (colorNum - 1.0) + 0.5) / (colorNum - 1.0);
120124
}
121125
122126
void mainImage(in vec4 inputColor, in vec2 uv, out vec4 outputColor) {
123127
vec2 normalizedPixelSize = pixelSize / resolution;
124128
vec2 uvPixel = normalizedPixelSize * floor(uv / normalizedPixelSize);
125129
vec4 color = texture2D(inputBuffer, uvPixel);
126-
color.rgb = dither(uvPixel, color.rgb);
130+
color.rgb = dither(uv, color.rgb);
127131
outputColor = color;
128132
}
129133
`;

0 commit comments

Comments
 (0)