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)

View File

@@ -124,7 +124,7 @@ function Layer:registerPeak(def, category, scale)
if peak == nil then
peak = Peak:new(def)
self.peaksByPos[category][positionStr] = peak
elseif peak.getColor() ~= def.color then
elseif peak.color ~= def.color then
error('There cannot be two peaks with the same coordinates but different colors.')
end

View File

@@ -2,8 +2,8 @@ local getInstance = Class.getInstance
---@class MapGen.Peak
---@field id number
---@field getPeakPos fun(self):vector
---@field getColor fun(self):ColorString
---@field pos vector
---@field color ColorString
---@field getGroups fun(self):table<string, number>
---@field addGroups fun(self, groups:table<string, number>)
local Peak = {
@@ -23,7 +23,6 @@ local Peak = {
---@param def MapGen.PeakDef
---@return MapGen.Peak
function Peak:new(def)
local _pos = vector.new(def.pos)
local _color = def.color
local _groups
@@ -39,14 +38,8 @@ function Peak:new(def)
__tostring = self.toString,
__eq = self.eq
})
function instance:getPeakPos()
return _pos:copy()
end
function instance:getColor()
return _color
end
instance.pos = vector.new(def.pos)
instance.color = def.color
function instance:getGroups()
return table.copy_with_metatables(_groups)
@@ -66,7 +59,7 @@ end
---Returns a string describing the object in a readable form.
---@return string
function Peak:toString()
local pos = self:getPeakPos()
local pos = self.pos
return ('Peak (%s) x: %.2f y: %.2f z: %.2f'):format( self.id, pos.x, pos.y, pos.z)
end
@@ -74,8 +67,8 @@ end
---@param other MapGen.Peak
---@return boolean
function Peak:eq(other)
local pos1 = self:getPeakPos()
local pos2 = other:getPeakPos()
local pos1 = self.pos
local pos2 = other.pos
return pos1.x == pos2.x and pos1.z == pos2.z
end

View File

@@ -29,9 +29,9 @@ end
---@param p3 MapGen.Peak
---@return boolean
local function isFlatAngle(p1, p2, p3)
local pos1 = p1:getPeakPos()
local pos2 = p2:getPeakPos()
local pos3 = p3:getPeakPos()
local pos1 = p1.pos
local pos2 = p2.pos
local pos3 = p3.pos
local vec1 = pos1 - pos2 -- Vector from p2 to p1
local vec2 = pos3 - pos2 -- Vector from p2 to p3
@@ -91,7 +91,7 @@ function Triangle:new(id, p1, p2, p3, neighboursIDs)
---@type ColorString?
local color = nil
local c1, c2, c3 = p1.getColor(), p2.getColor(), p3.getColor()
local c1, c2, c3 = p1.color, p2.color, p3.color
if c1 == c2 and c2 == c3 and c3 == c1 then
color = c1
@@ -123,9 +123,9 @@ end
---@return string
function Triangle:toString()
return ('Triangle: \n %s\n %s\n %s'):format(
tostring(self.p1.getPeakPos()),
tostring(self.p2.getPeakPos()),
tostring(self.p3.getPeakPos())
tostring(self.p1.pos),
tostring(self.p2.pos),
tostring(self.p3.pos)
)
end
@@ -148,9 +148,9 @@ end
---Returns the coordinates of the triangle's center.
---@return vector
function Triangle:getCenter()
local pos1 = self.p1:getPeakPos()
local pos2 = self.p2:getPeakPos()
local pos3 = self.p3:getPeakPos()
local pos1 = self.p1.pos
local pos2 = self.p2.pos
local pos3 = self.p3.pos
return (pos1 + pos2 + pos3) / 3
end
@@ -169,9 +169,9 @@ end
---@param p3 MapGen.Peak
---@return MapGen.Triangulation.Edge
function Triangle:getHeight(p1, p2, p3)
local pos1 = p1:getPeakPos()
local pos2 = p2:getPeakPos()
local pos3 = p3:getPeakPos()
local pos1 = p1.pos
local pos2 = p2.pos
local pos3 = p3.pos
local v = pos3 - pos2
---@type vector
@@ -195,9 +195,9 @@ end
---@param p3 MapGen.Peak
---@return MapGen.Triangulation.Edge
function Triangle:getHeight2D(p1, p2, p3)
local pos1 = p1:getPeakPos()
local pos2 = p2:getPeakPos()
local pos3 = p3:getPeakPos()
local pos1 = p1.pos
local pos2 = p2.pos
local pos3 = p3.pos
pos1.y = 0
pos2.y = 0
@@ -228,9 +228,9 @@ end
---that is, the calculation is performed on the projection of the triangle XZ.
---@return number, number, number
function Triangle:getSidesLength2D()
local pos1 = self.p1:getPeakPos()
local pos2 = self.p2:getPeakPos()
local pos3 = self.p3:getPeakPos()
local pos1 = self.p1.pos
local pos2 = self.p2.pos
local pos3 = self.p3.pos
pos1.y = 0
pos2.y = 0
@@ -245,9 +245,9 @@ end
---that is, the calculation is performed on the projection of the triangle XZ.
---@return vector
function Triangle:getCircumCenter2D()
local pos1 = self.p1:getPeakPos()
local pos2 = self.p2:getPeakPos()
local pos3 = self.p3:getPeakPos()
local pos1 = self.p1.pos
local pos2 = self.p2.pos
local pos3 = self.p3.pos
local D = ( pos1.x * (pos2.z - pos3.z) +
pos2.x * (pos3.z - pos1.z) +
@@ -294,7 +294,7 @@ end
---@param p MapGen.Peak
---@return boolean
function Triangle:inCircumCircle2D(p)
local pos = p:getPeakPos()
local pos = p.pos
local c, r = self:getCircumCircle2D()

View File

@@ -12,9 +12,9 @@ local Edge = {}
function Edge:new( p1, p2 )
---@type MapGen.Triangulation.Edge
local instance = getInstance(self, {
p1 = p1,
p2 = p2,
vec = p2:getPeakPos() - p1:getPeakPos()
p1 = p1,
p2 = p2,
vec = p2.pos - p1.pos
},
{
__tostring = self.toString,
@@ -48,14 +48,14 @@ end
---Returns the length of an edge.
---@return number
function Edge:length()
return (self.p1.getPeakPos() - self.p2.getPeakPos()):length()
return (self.p1.pos - self.p2.pos):length()
end
---Returns the vector of the point located in the middle of the edge.
---@return vector
function Edge:getMidPoint()
local pos1 = self.p1:getPeakPos()
local pos2 = self.p2:getPeakPos()
local pos1 = self.p1.pos
local pos2 = self.p2.pos
return (pos1 + pos2 )/2
end

View File

@@ -1,7 +1,7 @@
-- World generation occurs in a separate thread (MapGen Environment).
-- But for the main thread, the mapgenerator object is also initialized.
core.set_mapgen_setting('chunksize', '{x=2,y=10,z=2}', true)
--core.set_mapgen_setting('chunksize', '{x=2,y=10,z=2}', true)
local mapGeneneratorRequire = Mod.getInfo('smc__core__map_generator').require

View File

@@ -50,6 +50,11 @@ function Profiler:getDeltaTime(units)
end
---Displays the time difference in the chat using `core.log`.
function Profiler:logDeltaTime()
core.log(('DeltaTime[%s]: %.3f'):format(self:getDeltaTime()))
---@param min number?
function Profiler:logDeltaTime(min)
local delta = self:getDeltaTime()
if delta >= (min or 0) then
core.log(('DeltaTime[%s]: %.3f'):format(self.name, delta))
end
end