Skip to content

Commit

Permalink
fixing again render distance/Chunk Slice/Proc gen bugs...
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin452 committed Feb 28, 2024
1 parent 7ab2f74 commit a5e0d7a
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 294 deletions.
3 changes: 3 additions & 0 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ require("src/world/gen/caves")
require("src/init/!init")
--client
require("src/client/!draw")
require("src/client/blocks/blockrendering")
require("src/client/blocks/tilerendering")

--libs
PROF_CAPTURE = false
_JPROFILER = require("libs/jprofiler/jprof")
Expand Down
127 changes: 127 additions & 0 deletions src/client/blocks/blockrendering.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
function BlockRendering(self, i, j, k, x, y, z, thisTransparency, thisLight, model, scale)
-- top
local get = self.parent:getVoxel(i, j - 1, k)
if CanDrawFace(get, thisTransparency) then
local otx, oty = NumberToCoord(TileTextures(get)[math.min(2, #TileTextures(get))], 16, 16)
otx = otx + 16 * thisLight
local otx2, oty2 = otx + 1, oty + 1
local tx, ty = otx * TileWidth / LightValues, oty * TileHeight
local tx2, ty2 = otx2 * TileWidth / LightValues, oty2 * TileHeight

model[#model + 1] = { x, y, z, tx, ty }
model[#model + 1] = { x + scale, y, z, tx2, ty }
model[#model + 1] = { x, y, z + scale, tx, ty2 }
model[#model + 1] = { x + scale, y, z, tx2, ty }
model[#model + 1] = { x + scale, y, z + scale, tx2, ty2 }
model[#model + 1] = { x, y, z + scale, tx, ty2 }
end

-- bottom
local get = self.parent:getVoxel(i, j + 1, k)
if CanDrawFace(get, thisTransparency) then
local otx, oty = NumberToCoord(TileTextures(get)[math.min(3, #TileTextures(get))], 16, 16)
otx = otx + 16 * math.max(thisLight - 3, 0)
local otx2, oty2 = otx + 1, oty + 1
local tx, ty = otx * TileWidth / LightValues, oty * TileHeight
local tx2, ty2 = otx2 * TileWidth / LightValues, oty2 * TileHeight

model[#model + 1] = { x + scale, y + scale, z, tx2, ty }
model[#model + 1] = { x, y + scale, z, tx, ty }
model[#model + 1] = { x, y + scale, z + scale, tx, ty2 }
model[#model + 1] = { x + scale, y + scale, z + scale, tx2, ty2 }
model[#model + 1] = { x + scale, y + scale, z, tx2, ty }
model[#model + 1] = { x, y + scale, z + scale, tx, ty2 }
end

-- positive x
local get = self.parent:getVoxel(i - 1, j, k)
if i == 1 then
local chunkGet = GetChunk(x - 1, y, z)
if chunkGet ~= nil then
get = chunkGet:getVoxel(ChunkSize, j, k)
end
end
if CanDrawFace(get, thisTransparency) then
local otx, oty = NumberToCoord(TileTextures(get)[1], 16, 16)
otx = otx + 16 * math.max(thisLight - 2, 0)
local otx2, oty2 = otx + 1, oty + 1
local tx, ty = otx * TileWidth / LightValues, oty * TileHeight
local tx2, ty2 = otx2 * TileWidth / LightValues, oty2 * TileHeight

model[#model + 1] = { x, y + scale, z, tx2, ty }
model[#model + 1] = { x, y, z, tx2, ty2 }
model[#model + 1] = { x, y, z + scale, tx, ty2 }
model[#model + 1] = { x, y + scale, z + scale, tx, ty }
model[#model + 1] = { x, y + scale, z, tx2, ty }
model[#model + 1] = { x, y, z + scale, tx, ty2 }
end

-- negative x
local get = self.parent:getVoxel(i + 1, j, k)
if i == ChunkSize then
local chunkGet = GetChunk(x + 1, y, z)
if chunkGet ~= nil then
get = chunkGet:getVoxel(1, j, k)
end
end
if CanDrawFace(get, thisTransparency) then
local otx, oty = NumberToCoord(TileTextures(get)[1], 16, 16)
otx = otx + 16 * math.max(thisLight - 2, 0)
local otx2, oty2 = otx + 1, oty + 1
local tx, ty = otx * TileWidth / LightValues, oty * TileHeight
local tx2, ty2 = otx2 * TileWidth / LightValues, oty2 * TileHeight

model[#model + 1] = { x + scale, y, z, tx, ty2 }
model[#model + 1] = { x + scale, y + scale, z, tx, ty }
model[#model + 1] = { x + scale, y, z + scale, tx2, ty2 }
model[#model + 1] = { x + scale, y + scale, z, tx, ty }
model[#model + 1] = { x + scale, y + scale, z + scale, tx2, ty }
model[#model + 1] = { x + scale, y, z + scale, tx2, ty2 }
end

-- positive z
local get = self.parent:getVoxel(i, j, k - 1)
if k == 1 then
local chunkGet = GetChunk(x, y, z - 1)
if chunkGet ~= nil then
get = chunkGet:getVoxel(i, j, ChunkSize)
end
end
if CanDrawFace(get, thisTransparency) then
local otx, oty = NumberToCoord(TileTextures(get)[1], 16, 16)
otx = otx + 16 * math.max(thisLight - 1, 0)
local otx2, oty2 = otx + 1, oty + 1
local tx, ty = otx * TileWidth / LightValues, oty * TileHeight
local tx2, ty2 = otx2 * TileWidth / LightValues, oty2 * TileHeight

model[#model + 1] = { x, y, z, tx, ty2 }
model[#model + 1] = { x, y + scale, z, tx, ty }
model[#model + 1] = { x + scale, y, z, tx2, ty2 }
model[#model + 1] = { x, y + scale, z, tx, ty }
model[#model + 1] = { x + scale, y + scale, z, tx2, ty }
model[#model + 1] = { x + scale, y, z, tx2, ty2 }
end

-- negative z
local get = self.parent:getVoxel(i, j, k + 1)
if k == ChunkSize then
local chunkGet = GetChunk(x, y, z + 1)
if chunkGet ~= nil then
get = chunkGet:getVoxel(i, j, 1)
end
end
if CanDrawFace(get, thisTransparency) then
local otx, oty = NumberToCoord(TileTextures(get)[1], 16, 16)
otx = otx + 16 * math.max(thisLight - 1, 0)
local otx2, oty2 = otx + 1, oty + 1
local tx, ty = otx * TileWidth / LightValues, oty * TileHeight
local tx2, ty2 = otx2 * TileWidth / LightValues, oty2 * TileHeight

model[#model + 1] = { x, y + scale, z + scale, tx2, ty }
model[#model + 1] = { x, y, z + scale, tx2, ty2 }
model[#model + 1] = { x + scale, y, z + scale, tx, ty2 }
model[#model + 1] = { x + scale, y + scale, z + scale, tx, ty }
model[#model + 1] = { x, y + scale, z + scale, tx2, ty }
model[#model + 1] = { x + scale, y, z + scale, tx, ty2 }
end
end
31 changes: 31 additions & 0 deletions src/client/blocks/tilerendering.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

function TileRendering(self, i, j, k, x, y, z, thisLight, model, scale)
local this = self.parent:getVoxel(i, j, k)
if TileModel(this) == 1 then
local otx, oty = NumberToCoord(TileTextures(this)[1], 16, 16)
otx = otx + 16 * thisLight
local otx2, oty2 = otx + 1, oty + 1
local tx, ty = otx * TileWidth / LightValues, oty * TileHeight
local tx2, ty2 = otx2 * TileWidth / LightValues, oty2 * TileHeight

local diagLong = 0.7071 * scale * 0.5 + 0.5
local diagShort = -0.7071 * scale * 0.5 + 0.5

local vertices = {}

for _, v in ipairs({
{ diagShort, 0, diagShort, tx2, ty2 },
{ diagLong, 0, diagLong, tx, ty2 },
{ diagShort, scale, diagShort, tx2, ty },
{ diagLong, 0, diagLong, tx, ty2 },
{ diagLong, scale, diagLong, tx, ty },
{ diagShort, scale, diagShort, tx2, ty },
}) do
table.insert(vertices, { x + v[1], y + v[2], z + v[3], v[4], v[5] })
end

for _, v in ipairs(vertices) do
model[#model + 1] = v
end
end
end
13 changes: 6 additions & 7 deletions src/utils/mouselogic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ function MouseLogicOnPlay(x, y, b)

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()

for _, chunkSlice in ipairs(chunk.slices) do
-- renderChunkSlice(chunkSlice, ThePlayer.x, ThePlayer.y, ThePlayer.z)
Expand Down Expand Up @@ -89,12 +88,12 @@ function KeyPressed(k)
elseif k == "f1" then
enableTESTBLOCK = not enableTESTBLOCK
elseif k == "f2" then
-- for _, chunk in ipairs(renderChunks) do
-- if chunk.active then
-- chunk:sunlight()
-- chunk:processRequests()
-- end
-- end
-- for _, chunk in ipairs(renderChunks) do
-- if chunk.active then
-- chunk:sunlight()
-- chunk:processRequests()
-- end
-- end
end
end
if gamestate == gamestateGamePausing then
Expand Down
Loading

0 comments on commit a5e0d7a

Please sign in to comment.