From f1bcea144d8b4495fab00c8fe6e4ea472f0e51a4 Mon Sep 17 00:00:00 2001 From: quentin452 <42176772+quentin452@users.noreply.github.com> Date: Tue, 27 Feb 2024 12:04:36 +0100 Subject: [PATCH] RenderDistance/Procedurallychunkgen PART 3 (bugged/unfinished) --- src/client/drawhud.lua | 2 +- src/utils/mouselogic.lua | 36 +++++++++++++++++++++++++++--------- src/world/chunk.lua | 17 ++++++++--------- src/world/updatelogic.lua | 19 +++++++++++++------ 4 files changed, 49 insertions(+), 25 deletions(-) diff --git a/src/client/drawhud.lua b/src/client/drawhud.lua index 16722eb65..a32f2d74c 100644 --- a/src/client/drawhud.lua +++ b/src/client/drawhud.lua @@ -13,7 +13,7 @@ function DrawCanevas() end function DrawF3() - love.graphics.setColor(0, 0, 0) + love.graphics.setColor(1, 1, 1) love.graphics.print( "x: " .. math.floor(ThePlayer.x + 0.5) diff --git a/src/utils/mouselogic.lua b/src/utils/mouselogic.lua index ddf1c70d1..12e6ce281 100644 --- a/src/utils/mouselogic.lua +++ b/src/utils/mouselogic.lua @@ -19,22 +19,40 @@ function MouseLogicOnPlay(x, y, b) value = PlayerInventory.items[PlayerInventory.hotbarSelect] or 0 end - local cx, cy, cz = pos and pos.x, pos and pos.y, pos and pos.z local chunk = pos and pos.chunk - -- Check if the block is not placed at Y 128 - if chunk and ThePlayer and ThePlayer.cursorpos and ThePlayer.cursorHit and cy and cy < 128 then - chunk:setVoxel(cx, cy, cz, value, true) + if chunk and ThePlayer and ThePlayer.cursorpos and ThePlayer.cursorHit and pos.y and pos.y < 128 then + chunk:setVoxel(pos.x, pos.y, pos.z, value, true) LightingUpdate() - -- chunk:updateModel(cx, cy, cz) - -- LuaCraftPrintLoggingNormal("---") - -- LuaCraftPrintLoggingNormal(cx, cy, cz) - -- LuaCraftPrintLoggingNormal(cx % ChunkSize, cy % SliceHeight, cz % ChunkSize) - elseif pos.x and pos.z and pos.y >= WorldHeight and ThePlayer.cursorpos and ThePlayer.cursorHit == true then + + for _, chunkSlice in ipairs(chunk.slices) do + renderChunkSlice(chunkSlice, ThePlayer.x, ThePlayer.y, ThePlayer.z) + UpdateNeighboringChunks(chunk, pos.y) + end + elseif pos and pos.x and pos.z and pos.y >= WorldHeight and ThePlayer.cursorpos and ThePlayer.cursorHit == true then hudMessage = "you cannot place blocks at Y = " .. WorldHeight .. " or more" hudTimeLeft = 3 end end +function UpdateNeighboringChunks(chunk, y) + local neighborOffsets = { + { -1, 0 }, + { 1, 0 }, + { 0, -1 }, + { 0, 1 }, + } + + for _, offset in ipairs(neighborOffsets) do + local neighborChunk = GetChunkRaw(chunk.x + offset[1], chunk.z + offset[2]) + if neighborChunk then + local sliceIndex = math.floor(y / SliceHeight) + 1 + local neighborSlice = neighborChunk.slices[sliceIndex] + if neighborSlice then + neighborSlice.alreadyrendered = false + end + end + end +end function KeyPressed(k) -- simplified hotbar number press code, thanks nico-abram! diff --git a/src/world/chunk.lua b/src/world/chunk.lua index 3ebf59995..627a4316b 100644 --- a/src/world/chunk.lua +++ b/src/world/chunk.lua @@ -301,7 +301,6 @@ function NewChunk(x, z) for i = 1, WorldHeight / SliceHeight do sliceUpdates[i] = { false, false, false, false, false } end - -- find which slices need to be updated for i = 1, #self.changes do local index = math.floor((self.changes[i][2] - 1) / SliceHeight) + 1 @@ -349,9 +348,9 @@ function NewChunk(x, z) if sliceUpdates[i][2] then local chunk = GetChunkRaw(self.x - 1, self.z) - if chunk ~= nil then + if chunk then local neighborSlice = chunk.slices[i] - if neighborSlice ~= nil then + if neighborSlice then neighborSlice:updateModel() end end @@ -359,9 +358,9 @@ function NewChunk(x, z) if sliceUpdates[i][3] then local chunk = GetChunkRaw(self.x + 1, self.z) - if chunk ~= nil then + if chunk then local neighborSlice = chunk.slices[i] - if neighborSlice ~= nil then + if neighborSlice then neighborSlice:updateModel() end end @@ -369,9 +368,9 @@ function NewChunk(x, z) if sliceUpdates[i][4] or sliceUpdates[i][5] then local chunk = GetChunkRaw(self.x, self.z - 1) - if chunk ~= nil then + if chunk then local neighborSlice = chunk.slices[i] - if neighborSlice ~= nil then + if neighborSlice then neighborSlice:updateModel() end end @@ -379,9 +378,9 @@ function NewChunk(x, z) if sliceUpdates[i][4] or sliceUpdates[i][5] then local chunk = GetChunkRaw(self.x, self.z + 1) - if chunk ~= nil then + if chunk then local neighborSlice = chunk.slices[i] - if neighborSlice ~= nil then + if neighborSlice then neighborSlice:updateModel() end end diff --git a/src/world/updatelogic.lua b/src/world/updatelogic.lua index 0461e0716..752083761 100644 --- a/src/world/updatelogic.lua +++ b/src/world/updatelogic.lua @@ -24,11 +24,19 @@ function UpdateGame(dt) local renderChunks = getRenderChunks(playerX, playerY, playerZ) for _, chunk in ipairs(renderChunks) do - if chunk.isInitialized and chunk.active and #chunk.changes > 0 then - chunk:updateModel() + if chunk.isInitialized and chunk.active then + -- Only update the model if there are changes + if #chunk.changes > 0 then + chunk:updateModel() + end + for _, chunkSlice in ipairs(chunk.slices) do if chunkSlice.active then - renderChunkSlice(chunkSlice, playerX, playerY, playerZ) + if not chunkSlice.alreadyrendered then + renderChunkSlice(chunkSlice, playerX, playerY, playerZ) + end + else + chunkSlice.alreadyrendered = false end end end @@ -38,7 +46,6 @@ function UpdateGame(dt) -- update all things in ThingList update queue updateThingList(dt) - -- update 3D scene with dt only if PhysicsStep is true if PhysicsStep then Scene:update() @@ -99,7 +106,6 @@ function getRenderChunks(playerX, playerY, playerZ) return renderChunks end - function renderChunkSlice(chunkSlice, playerX, playerY, playerZ) local model = {} @@ -114,7 +120,7 @@ function renderChunkSlice(chunkSlice, playerX, playerY, playerZ) (chunkSlice.x - 1) * ChunkSize + i - 1, 1 * j * scale, (chunkSlice.z - 1) * ChunkSize + k - 1 if thisTransparency < 3 then - --LuaCraftPrintLoggingNormal("Rendering voxel:", i, j, k, "at position:", x, y, z) + -- LuaCraftPrintLoggingNormal("Rendering voxel:", i, j, k, "at position:", x, y, z) TileRendering(chunkSlice, i, j, k, x, y, z, thisLight, model, scale) BlockRendering(chunkSlice, i, j, k, x, y, z, thisTransparency, thisLight, model, scale) end @@ -123,6 +129,7 @@ function renderChunkSlice(chunkSlice, playerX, playerY, playerZ) end chunkSlice.model:setVerts(model) + chunkSlice.alreadyrendered = true end function updateThingList(dt)