temporal bayer dithering for particles

This commit is contained in:
minjaesong
2024-04-17 16:10:43 +09:00
parent 5e85d560b0
commit 1ddc696e78
2 changed files with 23 additions and 1 deletions

View File

@@ -94,6 +94,7 @@ open class ParticleBase(renderOrder: Actor.RenderOrder, var despawnUponCollision
val oldColour = batch.color.cpy()
if (!flagDespawn) {
batch.shader = IngameRenderer.shaderBayerAlpha
batch.shader.setUniformi("frame", App.GLOBAL_RENDER_TIMER.toInt() % 16)
batch.color = drawColour
drawBodyInGoodPosition(hitbox.startX.toFloat(), hitbox.startY.toFloat()) { x, y ->
drawJob(x, y)

View File

@@ -7,10 +7,29 @@ in vec2 v_texCoords;
uniform sampler2D u_texture; // world texture, has alpha value that is meaningful
uniform float acount = 2.0;
uniform int frame = 0;
out vec4 fragColor;
const int bayer[4 * 4] = int[](0,8,2,10,12,4,14,6,3,11,1,9,15,7,13,5);
const vec2 bayerOffset[4 * 4] = vec2[](
vec2(0,0),
vec2(3,2),
vec2(1,3),
vec2(3,0),
vec2(1,1),
vec2(2,3),
vec2(0,2),
vec2(2,1),
vec2(3,3),
vec2(1,0),
vec2(3,1),
vec2(0,3),
vec2(2,2),
vec2(0,1),
vec2(2,0),
vec2(1,2)
);
const float bayerSize = 4.0;
const float bayerDivider = bayerSize * bayerSize;
@@ -20,7 +39,9 @@ float nearestAlpha(float alpha) {
void main() {
vec4 inColor = texture(u_texture, v_texCoords) * v_color;
vec2 entry = mod(gl_FragCoord.xy, vec2(bayerSize, bayerSize));
vec2 entryOffset = bayerOffset[int(mod(frame, bayerDivider))];
vec2 entry = mod(gl_FragCoord.xy + entryOffset, vec2(bayerSize, bayerSize));
float alpha = nearestAlpha((inColor.a * 16.0 / 15.0) + (bayer[int(entry.y) * int(bayerSize) + int(entry.x)] / bayerDivider - 0.5));