Skip to content

Commit

Permalink
refactoring on blockrendering
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin452 committed Mar 4, 2024
1 parent 253437a commit fc41e63
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 65 deletions.
128 changes: 76 additions & 52 deletions src/client/blocks/blockrendering.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,90 @@ local AIR_TRANSPARENCY = 0
local LEAVES_TRANSPARENCY = 1

local function CanDrawFace(get, thisTransparency)
_JPROFILER.push("CanDrawFace")
local tget = TileTransparency(get)

if tget == AIR_TRANSPARENCY then
_JPROFILER.pop("CanDrawFace")
return false
elseif tget == LEAVES_TRANSPARENCY then
_JPROFILER.pop("CanDrawFace")
return true
else
_JPROFILER.pop("CanDrawFace")
return tget ~= thisTransparency
end
end

local function addFaceToModel(model, x, y, z, otx, oty, thisLight, scale, txModifier, tyModifier)
_JPROFILER.push("addFaceToModel")
local function calculationotxoty(otx, oty)
_JPROFILER.push("calculationotxoty")
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 + txModifier, ty + tyModifier }
model[#model + 1] = { x + scale, y, z, tx2 + txModifier, ty + tyModifier }
model[#model + 1] = { x, y, z + scale, tx + txModifier, ty2 + tyModifier }
model[#model + 1] = { x + scale, y, z, tx2 + txModifier, ty + tyModifier }
model[#model + 1] = { x + scale, y, z + scale, tx2 + txModifier, ty2 + tyModifier }
model[#model + 1] = { x, y, z + scale, tx + txModifier, ty2 + tyModifier }
local tx = otx * TileWidth / LightValues
local ty = oty * TileHeight
local tx2 = otx2 * TileWidth / LightValues
local ty2 = oty2 * TileHeight
_JPROFILER.pop("calculationotxoty")
return tx, ty, tx2, ty2
end

local function addFaceToModel(model, x, y, z, otx, oty, scale)
_JPROFILER.push("addFaceToModel")
local tx, ty, tx2, ty2 = calculationotxoty(otx, oty)
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 }
_JPROFILER.pop("addFaceToModel")
end
local function addFaceToModelPositiveX(model, x, y, z, otx, oty, scale)
_JPROFILER.push("addFaceToModelPositiveX")
local tx, ty, tx2, ty2 = calculationotxoty(otx, oty)
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 }
_JPROFILER.pop("addFaceToModelPositiveX")
end
local function addFaceToModelNegativeX(model, x, y, z, otx, oty, scale)
_JPROFILER.push("addFaceToModelNegativeX")
local tx, ty, tx2, ty2 = calculationotxoty(otx, oty)
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 }
_JPROFILER.pop("addFaceToModelNegativeX")
end

local function addFaceToModelPositiveZ(model, x, y, z, otx, oty, scale)
_JPROFILER.push("addFaceToModelPositiveZ")
local tx, ty, tx2, ty2 = calculationotxoty(otx, oty)
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 }
_JPROFILER.pop("addFaceToModelPositiveZ")
end

local function addFaceToModelNegativeZ(model, x, y, z, otx, oty, scale)
_JPROFILER.push("addFaceToModelNegativeZ")
local tx, ty, tx2, ty2 = calculationotxoty(otx, oty)

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 }
_JPROFILER.pop("addFaceToModelNegativeZ")
end

function BlockRendering(self, i, j, k, x, y, z, thisTransparency, thisLight, model, scale)
_JPROFILER.push("BlockRendering")
Expand All @@ -36,13 +96,13 @@ function BlockRendering(self, i, j, k, x, y, z, thisTransparency, thisLight, mod
if CanDrawFace(getTop, thisTransparency) then
local otx, oty = NumberToCoord(TileTextures(getTop)[math.min(2, #TileTextures(getTop))], 16, 16)
otx = otx + 16 * thisLight
addFaceToModel(model, x, y, z, otx, oty, thisLight, scale, 0, 0)
addFaceToModel(model, x, y, z, otx, oty, scale)
end

if CanDrawFace(getBottom, thisTransparency) then
local otx, oty = NumberToCoord(TileTextures(getBottom)[math.min(3, #TileTextures(getBottom))], 16, 16)
otx = otx + 16 * math.max(thisLight - 3, 0)
addFaceToModel(model, x, y + scale, z, otx, oty, thisLight, scale, 0, 0)
addFaceToModel(model, x, y + scale, z, otx, oty, scale)
end

-- positive x
Expand All @@ -56,16 +116,7 @@ function BlockRendering(self, i, j, k, x, y, z, thisTransparency, thisLight, mod
if CanDrawFace(getPositiveX, thisTransparency) then
local otx, oty = NumberToCoord(TileTextures(getPositiveX)[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 }
addFaceToModelPositiveX(model, x, y, z, otx, oty, scale)
end

-- negative x
Expand All @@ -79,16 +130,7 @@ function BlockRendering(self, i, j, k, x, y, z, thisTransparency, thisLight, mod
if CanDrawFace(getNegativeX, thisTransparency) then
local otx, oty = NumberToCoord(TileTextures(getNegativeX)[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 }
addFaceToModelNegativeX(model, x, y, z, otx, oty, scale)
end

-- positive z
Expand All @@ -102,16 +144,7 @@ function BlockRendering(self, i, j, k, x, y, z, thisTransparency, thisLight, mod
if CanDrawFace(getPositiveZ, thisTransparency) then
local otx, oty = NumberToCoord(TileTextures(getPositiveZ)[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 }
addFaceToModelPositiveZ(model, x, y, z, otx, oty, scale)
end

-- negative z
Expand All @@ -125,16 +158,7 @@ function BlockRendering(self, i, j, k, x, y, z, thisTransparency, thisLight, mod
if CanDrawFace(getNegativeZ, thisTransparency) then
local otx, oty = NumberToCoord(TileTextures(getNegativeZ)[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 }
addFaceToModelNegativeZ(model, x, y, z, otx, oty, scale)
end
_JPROFILER.pop("BlockRendering")
end
4 changes: 2 additions & 2 deletions src/init/!init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ function InitializeGame()
InitializeShaders()
_JPROFILER.pop("InitializeShaders")
_JPROFILER.push("InitializeTileCanevas")
InitializeTileCanevas()
InitializeHUDTileCanvas()
_JPROFILER.pop("InitializeTileCanevas")
_JPROFILER.push("InitalizeLightningCanevas")
InitalizeLightningCanevas()
InitializeGameTileCanvas()
_JPROFILER.pop("InitalizeLightningCanevas")
_JPROFILER.push("initWorldGenerationVariables")
initWorldGenerationVariables()
Expand Down
22 changes: 11 additions & 11 deletions src/init/canvasinit.lua
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
function InitializeTileCanevas()
function InitializeHUDTileCanvas()
TileCanvas = {}
local tileSize = 16
for i = 1, 16 do
local xx = (i - 1) * tileSize
for j = 1, 16 do
local yy = (j - 1) * tileSize
local index = (j - 1) * 16 + i
TileCanvas[index] = love.graphics.newCanvas(tileSize, tileSize)
TileCanvas[index] = lovegraphics.newCanvas(tileSize, tileSize)
local this = TileCanvas[index]
love.graphics.setCanvas(this)
love.graphics.draw(TileTexture, -xx, -yy)
lovegraphics.setCanvas(this)
lovegraphics.draw(TileTexture, -xx, -yy)
end
end
end

function InitalizeLightningCanevas()
function InitializeGameTileCanvas()
-- create lighting value textures on LightingTexture canvas
LightValues = 16
local width, height = TileTexture:getWidth(), TileTexture:getHeight()
LightingTexture = love.graphics.newCanvas(width * LightValues, height)
local mult = 1
love.graphics.setCanvas(LightingTexture)
love.graphics.clear(1, 1, 1, 0)
lovegraphics.setCanvas(LightingTexture)
lovegraphics.clear(1, 1, 1, 0)
for i = LightValues, 1, -1 do
local xx = (i - 1) * width
love.graphics.setColor(mult, mult, mult)
love.graphics.draw(TileTexture, xx, 0)
lovegraphics.setColor(mult, mult, mult)
lovegraphics.draw(TileTexture, xx, 0)
mult = mult * 0.8
end
love.graphics.setColor(1, 1, 1)
love.graphics.setCanvas()
lovegraphics.setColor(1, 1, 1)
lovegraphics.setCanvas()
end

0 comments on commit fc41e63

Please sign in to comment.