Skip to content

Instantly share code, notes, and snippets.

@scambier
Created September 24, 2023 10:26
Show Gist options
  • Save scambier/22a46f69e3ca36b820d688bbfa814de7 to your computer and use it in GitHub Desktop.
Save scambier/22a46f69e3ca36b820d688bbfa814de7 to your computer and use it in GitHub Desktop.
Bayer dithering in TIC-80
local bayerMatrix = {
0, 8, 2, 10,
12, 4, 14, 6,
3, 11, 1, 9,
15, 7, 13, 5
}
function ditherPixel(x, y, col1, col2, level)
local threshold = bayerMatrix[x % 4 + y % 4 * 4 +1] -- Remove the +1 for 0-based arrays
return (level < threshold) and col1 % 15 or col2 % 15
end
local color1 = 0
local color2 = 1
local level = 0
function TIC()
cls(0)
level = level + 0.25
if level % 15 == 0 then
color1 = color2
color2 = color2 + 1
end
for x=0,15 do
for y=0,15 do
local px = ditherPixel(x, y, color1, color2, level % 15)
pix(x,y,px)
end
end
for x=31,60 do
for y=0,135 do
local px = ditherPixel(x, y, color1, color2, y / (136 / 15))
pix(x,y,px)
end
end
end
@scambier
Copy link
Author

video3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment