MapGen: Attempt to speed up generation. Some MapGen.Peak fields can now be accessed directly. Layers are now defined for the entire chunk, rather than per node.

This commit is contained in:
Van
2026-03-15 22:08:13 +03:00
parent a34822ed54
commit 1531abc3e2
7 changed files with 63 additions and 107 deletions

View File

@@ -170,17 +170,17 @@ end
function MapGen:isPointInTriangle(triangle, pointPos)
local e1, e2, e3 = triangle.e1, triangle.e2, triangle.e3
local y1 = e1.vec:cross(pointPos - e1.p1:getPeakPos()).y
local y1 = e1.vec:cross(pointPos - e1.p1.pos).y
if y1 > 0 then
return false, triangle.neighbours[3]
end
local y2 = e2.vec:cross(pointPos - e2.p1:getPeakPos()).y
local y2 = e2.vec:cross(pointPos - e2.p1.pos).y
if y2 > 0 then
return false, triangle.neighbours[1]
end
local y3 = e3.vec:cross(pointPos - e3.p1:getPeakPos()).y
local y3 = e3.vec:cross(pointPos - e3.p1.pos).y
if y3 > 0 then
return false, triangle.neighbours[2]
end
@@ -261,7 +261,7 @@ end
---@param peak MapGen.Peak
---@return string
local function vertexKey(peak)
local pos = peak:getPeakPos()
local pos = peak.pos
return string.format("%.6f,%.6f", pos.x, pos.z)
end
@@ -354,7 +354,7 @@ function MapGen:getTriangulateBoundaryVertices(triangles)
while currentKey and not visited[currentKey] do
visited[currentKey] = true
local pos = vertexData[currentKey]:getPeakPos()
local pos = vertexData[currentKey].pos
table.insert(boundary, {
x = pos.x,
@@ -700,8 +700,8 @@ end
---@return number
local function calcWeight(height, x, y, z)
local pos1 = vector.new(x, y, z)
local pos2 = height.p1:getPeakPos()
local pos3 = height.p2:getPeakPos()
local pos2 = height.p1.pos
local pos3 = height.p2.pos
local v = pos3 - pos2
---@type vector
@@ -737,9 +737,9 @@ local function calcNoise2dValue(category, layer, triangle, x, y, z)
local peak2 = triangle.p2
local peak3 = triangle.p3
local noise1 = layer:getNoiseByColor(category, peak1:getColor())
local noise2 = layer:getNoiseByColor(category, peak2:getColor())
local noise3 = layer:getNoiseByColor(category, peak3:getColor())
local noise1 = layer:getNoiseByColor(category, peak1.color)
local noise2 = layer:getNoiseByColor(category, peak2.color)
local noise3 = layer:getNoiseByColor(category, peak3.color)
local noiseValue = 0.0
@@ -820,64 +820,18 @@ function MapGen:getNoises2dValue(category, layer, x, y, z)
pastCalc.noiseValue = noiseValue
return noiseValue
--[[
if pastCalc.triangle ~= nil then
---@type number?
local noiseValue
local triangles = {
pastCalc.triangle,
pastCalc.triangle.neighbours[1],
pastCalc.triangle.neighbours[2],
pastCalc.triangle.neighbours[3],
}
---@param t MapGen.Triangle
for _, t in ipairs(triangles) do
if pointInTriangle2D(x, z, t) then
noiseValue = calcNoise2dValue(category, layer, t, x, y, z)
pastCalc.x = x
pastCalc.z = z
pastCalc.noiseValue = noiseValue
return noiseValue
end
end
end
if not self:pointInRegion2D(layer, category..'_boundary', {x=x, y=y, z=z}) then
return nil
end
---@param triangle MapGen.Triangle
for _, triangle in ipairs(layer.trianglesList[category]) do
if pointInTriangle2D(x, z, triangle) then
local noiseValue = calcNoise2dValue(category, layer, triangle, x, y, z)
pastCalc.triangle = triangle
pastCalc.x = x
pastCalc.z = z
pastCalc.noiseValue = noiseValue
return noiseValue
end
end--]]
-- If the point does not fall within the triangle, it is impossible to calculate the height.
-- For debugging purposes only, may slow down generation:
--core.log('warning', string.format('The point %s %s %s does not fall within any triangle, calculating the noise value is impossible.', tostring(x), tostring(y), tostring(z)))
end
---Generator handler for specific coordinates.
---@param mapGenerator MapGen
---@param layer MapGen.Layer?
---@param data number[]
---@param index number
---@param x integer
---@param y integer
---@param z integer
---@return number, number, number
local function generatorHandler(mapGenerator, data, index, x, y, z)
local function generatorHandler(mapGenerator, layer, data, index, x, y, z)
local startGetLayerTime = core.get_us_time()
-- If the node belongs to a static layer, then a static node is generated.
local static = mapGenerator:getStaticByHeight(y)
@@ -888,7 +842,6 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
end
-- If generation occurs outside the layers, then a void is generated.
local layer = mapGenerator:getLayerByHeight(y)
if layer == nil then
data[index] = mapGenerator.nodeIDs.air
@@ -972,6 +925,11 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
-- local param2_data = voxelManip:get_param2_data()
local area = VoxelArea:new({MinEdge = eMin, MaxEdge = eMax})
-- We obtain a layer for the entire chunk at once for optimization purposes.
-- Therefore, if the layer boundaries do not coincide with the chunk boundaries,
-- the chunk will be entirely contained within the layer containing its center.
local layer = self:getLayerByHeight((minPos.y+maxPos.y)/2)
local index
-- Initial generation: landscape
for z = minPos.z, maxPos.z do
@@ -980,7 +938,7 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
index = area:index(x, y, z)
if data[index] == core.CONTENT_AIR then
local t1, t2, t3 = generatorHandler(self, data, index, x, y, z)
local t1, t2, t3 = generatorHandler(self, layer, data, index, x, y, z)
table.insert(getLayerTime, t1)
table.insert(NoiseCalcTime, t2)