Skip to content

Commit

Permalink
Merge branch 'dither'
Browse files Browse the repository at this point in the history
  • Loading branch information
brennop committed Oct 16, 2023
2 parents 9c5251f + 6f72324 commit b2e5e56
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 28 deletions.
2 changes: 1 addition & 1 deletion main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function love.draw()
local w, h = love.graphics.getDimensions()
love.graphics.circle("fill", w / 2, h / 2, 2)

debug("FPS:", love.timer.getFPS())
-- debug("FPS:", love.timer.getFPS())

for i, v in ipairs(_debug) do
love.graphics.print(v, 10, 10 + (i - 1) * 20)
Expand Down
15 changes: 14 additions & 1 deletion shaders/camera.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ float calculateShadow(vec4 fragPosLightSpace, float lightDot)
return shadow;
}


uniform Image bayer;

vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords)
{
vec3 lightDir = normalize(lightPos - worldPosition.xyz);
Expand All @@ -81,6 +84,16 @@ vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords)
vec4 light = vec4(ambient + diffuse * shadow, 1.0);

vec4 texturecolor = Texel(tex, texture_coords);
return texturecolor * color * light;
vec4 f = texturecolor * color * light;
float alpha = f.a;

vec2 ditherIndex = vec2(mod(screen_coords.x, 8.0), mod(screen_coords.y, 8.0));
float dither = Texel(bayer, ditherIndex / 8.0).r;

if (dither > alpha)
discard;

f.a = 1.0;
return f;
}
#endif
16 changes: 5 additions & 11 deletions src/blocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,17 @@ blockTypes = {
cube({ { 3, 31 }, { 3, 31 }, { 3, 31 }, { 3, 31 }, { 3, 31 }, { 3, 31 } }),
-- 4: water
cube({ { 2, 9 }, { 2, 9 }, { 2, 9 }, { 2, 9 }, { 2, 9 }, { 2, 9 } }, {
vertices = {
{ -1, -1, 1 },
{ 1, -1, 1 },
{ -1, 0.75, 1 },
{ 1, 0.75, 1 },
{ -1, 0.75, -1 },
{ 1, 0.75, -1 },
{ -1, -1, -1 },
{ 1, -1, -1 },
}
alpha = 0.5,
}),
-- 5: sand
cube({ { 3, 7 }, { 3, 7 }, { 3, 7 }, { 3, 7 }, { 3, 7 }, { 3, 7 } }),
-- 6: wood
cube({ { 14, 13 }, { 15, 13 }, { 14, 13 }, { 15, 13 }, { 14, 13 }, { 14, 13 } }),
-- 7: leaves
cube({ { 1, 17 }, { 1, 17 }, { 1, 17 }, { 1, 17 }, { 1, 17 }, { 1, 17 } }, { }),
cube({
{ 1, 17 }, { 1, 17 }, { 1, 17 }, { 1, 17 }, { 1, 17 }, { 1, 17 } }, {
alpha = 0.95,
}),
}

return blockTypes
32 changes: 20 additions & 12 deletions src/camera.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ local cos, sin = math.cos, math.sin
local w, h = 400, 300
local shadowMapResolution = 2048

local bayer = {
{0, 32, 8, 40, 2, 34, 10, 42},
{48, 16, 56, 24, 50, 18, 58, 26},
{12, 44, 4, 36, 14, 46, 6, 38},
{60, 28, 52, 20, 62, 30, 54, 22},
{3, 35, 11, 43, 1, 33, 9, 41},
{51, 19, 59, 27, 49, 17, 57, 25},
{15, 47, 7, 39, 13, 45, 5, 37},
{63, 31, 55, 23, 61, 29, 53, 21},
}

function Camera:new(world)
self.position = Vector(0, CHUNK_HEIGHT + 2, 0)
self.world = world
Expand Down Expand Up @@ -43,18 +54,19 @@ function Camera:new(world)
local k = 80
self.shadowProjection:ortho(-k, k, -k, k, 1, 100)

self.debugShadowMapShader = love.graphics.newShader([[
vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords)
{
float v = Texel(tex, texture_coords).r;
return vec4(v, v, v, 1.0);
}
]])

self:updateDirection(0,0)
self:updateProjection(self.fov)
self:updateView()

local bayerData = love.image.newImageData(8, 8, "r8")
for y = 1, 8 do
for x = 1, 8 do
local value = bayer[y][x] / 64
bayerData:setPixel(x - 1, y - 1, value, value, value, 1)
end
end
self.bayer = love.graphics.newImage(bayerData)
self.shader:send("bayer", self.bayer)
end

function Camera:updateProjection(_fov)
Expand Down Expand Up @@ -165,10 +177,6 @@ function Camera:draw()
self:drawWorld()

love.graphics.setShader()

-- love.graphics.setShader(self.debugShadowMapShader)
-- love.graphics.draw(self.shadowMap, 0, 0, 0, 0.25, 0.25)
-- love.graphics.setShader()
end

function Camera:hit()
Expand Down
15 changes: 12 additions & 3 deletions src/mesh.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local CHUNK_SIZE = 8
local CHUNK_HEIGHT = 32

local blockTypes = require "src.blocks"

local shading = {
0.3, 0.4, 0.6, 1,
}
Expand All @@ -15,13 +17,20 @@ function sign(value)
end
end

local translucent = {
[4] = true,
[7] = true,
}

function setVertex(index, i, mesh, x, y, z, value, getBlock, outVertex)
local block = getBlock(x, y, z)

if (value == 0 or (value == 4 and block ~= 4)) and mesh then
local vertex = mesh[index*6+i]
local vx, vy, vz, u, v, normal, alpha = unpack(vertex)
if not mesh then return false end

local vertex = mesh[index*6+i]
local vx, vy, vz, u, v, normal, alpha = unpack(vertex)

if (value == 0 or (translucent[value] and block ~= value)) and mesh then
local dx, dy, dz = sign(vx), sign(vy), sign(vz)
local nx, ny, nz = normal[1], normal[2], normal[3]
local side1, side2, corner, m
Expand Down

0 comments on commit b2e5e56

Please sign in to comment.