Skip to content

Commit

Permalink
Merge branch 'transparent'
Browse files Browse the repository at this point in the history
  • Loading branch information
brennop committed Apr 21, 2023
2 parents 056e899 + e39b496 commit e9b80de
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 125 deletions.
15 changes: 14 additions & 1 deletion shaders/camera.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
uniform float time;
uniform bool shouldAnimate;

uniform vec3 lightPos;
uniform Image shadowMap;
Expand All @@ -12,14 +14,25 @@ varying vec4 fragPosShadowSpace;
varying vec3 vertexNormal;
varying vec4 worldPosition;


#ifdef VERTEX
attribute vec3 VertexNormal;

const float TIMESCALE = 1.5;

vec4 position( mat4 transform_projection, vec4 vertexPosition )
{
worldPosition = modelMatrix * vertexPosition;

if (shouldAnimate) {
// use time and sin to animate vertex y
float y = worldPosition.y
+ sin(time * TIMESCALE + worldPosition.x) * 0.1
+ cos(time * TIMESCALE + worldPosition.z) * 0.08;

worldPosition.y = y;
}


vertexNormal = VertexNormal;

fragPosShadowSpace = shadowProjectionMatrix * shadowViewMatrix * worldPosition;
Expand Down
7 changes: 5 additions & 2 deletions src/blocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ blockTypes = {
{ 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
4 changes: 4 additions & 0 deletions src/camera.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,16 @@ function Camera:drawWorld()
end
end
end

self.world:draw()
end

function Camera:drawShadowMap()
love.graphics.setShader(self.shadowShader)

self.shadowShader:send("viewMatrix", self.shadowView)
self.shadowShader:send("projectionMatrix", self.shadowProjection)
-- self.shadowShader:send("time", love.timer.getTime())

love.graphics.setCanvas({ depthstencil = self.shadowMap })
love.graphics.clear(1, 0, 0)
Expand All @@ -157,6 +160,7 @@ function Camera:draw()
self.shader:send("lightPos", { self.light.x, self.light.z, self.light.y })
self.shader:send("viewMatrix", self.view)
self.shader:send("projectionMatrix", self.projection)
self.shader:send("time", love.timer.getTime())

self.shader:send("shadowViewMatrix", self.shadowView)
self.shader:send("shadowProjectionMatrix", self.shadowProjection)
Expand Down
46 changes: 31 additions & 15 deletions src/chunk.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ function Chunk:new(x, y, z, world)
self.loaded = false
self.dirty = true

self.mesh = nil
self.meshes = {
opaque = nil,
transparent = nil
}

self.model = Matrix()
self.model[4] = self.position.x
self.model[8] = self.position.y
self.model[12] = self.position.z

self.channel = self:__tostring()

end

function Chunk:__tostring()
Expand All @@ -56,7 +58,11 @@ function Chunk:load(thread)
self.dirty = false

-- each vertex is currently 36 bytes
self.verticesData = love.data.newByteData(maxVertices * 36)
self.verticeData = {
opaque = love.data.newByteData(maxVertices * 36),
-- we assume transparent vertices are way less than opaque ones
transparent = love.data.newByteData(maxVertices * 36 * 0.5),
}

-- add 1 to the size to include the border
local blocks = {}
Expand All @@ -71,7 +77,7 @@ function Chunk:load(thread)
end
end

thread:start(self.position:table(), blocks, self.channel, blockTypes, self.verticesData)
thread:start(self.position:table(), blocks, self.channel, blockTypes, self.verticeData)
end

function Chunk:getBlock(x, y, z)
Expand Down Expand Up @@ -114,25 +120,35 @@ function Chunk:setBlock(x, y, z, block)
end

function Chunk:update()
local message = love.thread.getChannel(self.channel):pop()
for _, suffix in ipairs { "opaque", "transparent" } do
local message = love.thread.getChannel(self.channel .. suffix):pop()

if message then
local numVertices = message

if message then
local numVertices = message
if numVertices == 0 then return end

if self.mesh then self.mesh:release() end
if self.meshes[suffix] then
self.meshes[suffix]:release()
end

self.mesh = love.graphics.newMesh(format, numVertices, "triangles", "static")
self.mesh:setTexture(tileset)
self.mesh:setVertices(self.verticesData, 1, numVertices)
self.meshes[suffix] = love.graphics.newMesh(format, numVertices, "triangles", "static")
self.meshes[suffix]:setTexture(tileset)
self.meshes[suffix]:setVertices(self.verticeData[suffix], 1, numVertices)

self.verticesData:release()
self.verticeData[suffix]:release()
end
end
end

function Chunk:draw()
if self.mesh then
love.graphics.getShader():send("modelMatrix", self.model)
love.graphics.draw(self.mesh)
-- FIXME: passing model twice wastes memory
if self.meshes.opaque then
table.insert(self.world.opaqueMeshes, { mesh = self.meshes.opaque, model = self.model })
end

if self.meshes.transparent then
table.insert(self.world.transparentMeshes, { mesh = self.meshes.transparent, model = self.model })
end
end

Expand Down
6 changes: 4 additions & 2 deletions src/cube.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ return function(textures, config)
vertices[vertex][3] * 0.5,
u + uvs[uv][1] * step,
v + uvs[uv][2] * step,
normals[normal],
alpha
normals[normal]
}
end
end

cube.alpha = alpha
cube.opaque = alpha == 1

return cube
end
59 changes: 0 additions & 59 deletions src/load_mesh.lua

This file was deleted.

Loading

0 comments on commit e9b80de

Please sign in to comment.