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:
@@ -170,17 +170,17 @@ end
|
|||||||
function MapGen:isPointInTriangle(triangle, pointPos)
|
function MapGen:isPointInTriangle(triangle, pointPos)
|
||||||
local e1, e2, e3 = triangle.e1, triangle.e2, triangle.e3
|
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
|
if y1 > 0 then
|
||||||
return false, triangle.neighbours[3]
|
return false, triangle.neighbours[3]
|
||||||
end
|
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
|
if y2 > 0 then
|
||||||
return false, triangle.neighbours[1]
|
return false, triangle.neighbours[1]
|
||||||
end
|
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
|
if y3 > 0 then
|
||||||
return false, triangle.neighbours[2]
|
return false, triangle.neighbours[2]
|
||||||
end
|
end
|
||||||
@@ -261,7 +261,7 @@ end
|
|||||||
---@param peak MapGen.Peak
|
---@param peak MapGen.Peak
|
||||||
---@return string
|
---@return string
|
||||||
local function vertexKey(peak)
|
local function vertexKey(peak)
|
||||||
local pos = peak:getPeakPos()
|
local pos = peak.pos
|
||||||
|
|
||||||
return string.format("%.6f,%.6f", pos.x, pos.z)
|
return string.format("%.6f,%.6f", pos.x, pos.z)
|
||||||
end
|
end
|
||||||
@@ -354,7 +354,7 @@ function MapGen:getTriangulateBoundaryVertices(triangles)
|
|||||||
|
|
||||||
while currentKey and not visited[currentKey] do
|
while currentKey and not visited[currentKey] do
|
||||||
visited[currentKey] = true
|
visited[currentKey] = true
|
||||||
local pos = vertexData[currentKey]:getPeakPos()
|
local pos = vertexData[currentKey].pos
|
||||||
|
|
||||||
table.insert(boundary, {
|
table.insert(boundary, {
|
||||||
x = pos.x,
|
x = pos.x,
|
||||||
@@ -700,8 +700,8 @@ end
|
|||||||
---@return number
|
---@return number
|
||||||
local function calcWeight(height, x, y, z)
|
local function calcWeight(height, x, y, z)
|
||||||
local pos1 = vector.new(x, y, z)
|
local pos1 = vector.new(x, y, z)
|
||||||
local pos2 = height.p1:getPeakPos()
|
local pos2 = height.p1.pos
|
||||||
local pos3 = height.p2:getPeakPos()
|
local pos3 = height.p2.pos
|
||||||
|
|
||||||
local v = pos3 - pos2
|
local v = pos3 - pos2
|
||||||
---@type vector
|
---@type vector
|
||||||
@@ -737,9 +737,9 @@ local function calcNoise2dValue(category, layer, triangle, x, y, z)
|
|||||||
local peak2 = triangle.p2
|
local peak2 = triangle.p2
|
||||||
local peak3 = triangle.p3
|
local peak3 = triangle.p3
|
||||||
|
|
||||||
local noise1 = layer:getNoiseByColor(category, peak1:getColor())
|
local noise1 = layer:getNoiseByColor(category, peak1.color)
|
||||||
local noise2 = layer:getNoiseByColor(category, peak2:getColor())
|
local noise2 = layer:getNoiseByColor(category, peak2.color)
|
||||||
local noise3 = layer:getNoiseByColor(category, peak3:getColor())
|
local noise3 = layer:getNoiseByColor(category, peak3.color)
|
||||||
|
|
||||||
local noiseValue = 0.0
|
local noiseValue = 0.0
|
||||||
|
|
||||||
@@ -820,64 +820,18 @@ function MapGen:getNoises2dValue(category, layer, x, y, z)
|
|||||||
pastCalc.noiseValue = noiseValue
|
pastCalc.noiseValue = noiseValue
|
||||||
|
|
||||||
return 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
|
end
|
||||||
|
|
||||||
---Generator handler for specific coordinates.
|
---Generator handler for specific coordinates.
|
||||||
---@param mapGenerator MapGen
|
---@param mapGenerator MapGen
|
||||||
|
---@param layer MapGen.Layer?
|
||||||
---@param data number[]
|
---@param data number[]
|
||||||
---@param index number
|
---@param index number
|
||||||
---@param x integer
|
---@param x integer
|
||||||
---@param y integer
|
---@param y integer
|
||||||
---@param z integer
|
---@param z integer
|
||||||
---@return number, number, number
|
---@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()
|
local startGetLayerTime = core.get_us_time()
|
||||||
-- If the node belongs to a static layer, then a static node is generated.
|
-- If the node belongs to a static layer, then a static node is generated.
|
||||||
local static = mapGenerator:getStaticByHeight(y)
|
local static = mapGenerator:getStaticByHeight(y)
|
||||||
@@ -888,7 +842,6 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- If generation occurs outside the layers, then a void is generated.
|
-- If generation occurs outside the layers, then a void is generated.
|
||||||
local layer = mapGenerator:getLayerByHeight(y)
|
|
||||||
if layer == nil then
|
if layer == nil then
|
||||||
data[index] = mapGenerator.nodeIDs.air
|
data[index] = mapGenerator.nodeIDs.air
|
||||||
|
|
||||||
@@ -972,6 +925,11 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
|
|||||||
-- local param2_data = voxelManip:get_param2_data()
|
-- local param2_data = voxelManip:get_param2_data()
|
||||||
local area = VoxelArea:new({MinEdge = eMin, MaxEdge = eMax})
|
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
|
local index
|
||||||
-- Initial generation: landscape
|
-- Initial generation: landscape
|
||||||
for z = minPos.z, maxPos.z do
|
for z = minPos.z, maxPos.z do
|
||||||
@@ -980,7 +938,7 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
|
|||||||
index = area:index(x, y, z)
|
index = area:index(x, y, z)
|
||||||
|
|
||||||
if data[index] == core.CONTENT_AIR then
|
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(getLayerTime, t1)
|
||||||
table.insert(NoiseCalcTime, t2)
|
table.insert(NoiseCalcTime, t2)
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ function Layer:registerPeak(def, category, scale)
|
|||||||
if peak == nil then
|
if peak == nil then
|
||||||
peak = Peak:new(def)
|
peak = Peak:new(def)
|
||||||
self.peaksByPos[category][positionStr] = peak
|
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.')
|
error('There cannot be two peaks with the same coordinates but different colors.')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ local getInstance = Class.getInstance
|
|||||||
|
|
||||||
---@class MapGen.Peak
|
---@class MapGen.Peak
|
||||||
---@field id number
|
---@field id number
|
||||||
---@field getPeakPos fun(self):vector
|
---@field pos vector
|
||||||
---@field getColor fun(self):ColorString
|
---@field color ColorString
|
||||||
---@field getGroups fun(self):table<string, number>
|
---@field getGroups fun(self):table<string, number>
|
||||||
---@field addGroups fun(self, groups:table<string, number>)
|
---@field addGroups fun(self, groups:table<string, number>)
|
||||||
local Peak = {
|
local Peak = {
|
||||||
@@ -23,7 +23,6 @@ local Peak = {
|
|||||||
---@param def MapGen.PeakDef
|
---@param def MapGen.PeakDef
|
||||||
---@return MapGen.Peak
|
---@return MapGen.Peak
|
||||||
function Peak:new(def)
|
function Peak:new(def)
|
||||||
local _pos = vector.new(def.pos)
|
|
||||||
local _color = def.color
|
local _color = def.color
|
||||||
local _groups
|
local _groups
|
||||||
|
|
||||||
@@ -39,14 +38,8 @@ function Peak:new(def)
|
|||||||
__tostring = self.toString,
|
__tostring = self.toString,
|
||||||
__eq = self.eq
|
__eq = self.eq
|
||||||
})
|
})
|
||||||
|
instance.pos = vector.new(def.pos)
|
||||||
function instance:getPeakPos()
|
instance.color = def.color
|
||||||
return _pos:copy()
|
|
||||||
end
|
|
||||||
|
|
||||||
function instance:getColor()
|
|
||||||
return _color
|
|
||||||
end
|
|
||||||
|
|
||||||
function instance:getGroups()
|
function instance:getGroups()
|
||||||
return table.copy_with_metatables(_groups)
|
return table.copy_with_metatables(_groups)
|
||||||
@@ -66,7 +59,7 @@ end
|
|||||||
---Returns a string describing the object in a readable form.
|
---Returns a string describing the object in a readable form.
|
||||||
---@return string
|
---@return string
|
||||||
function Peak:toString()
|
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)
|
return ('Peak (%s) x: %.2f y: %.2f z: %.2f'):format( self.id, pos.x, pos.y, pos.z)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -74,8 +67,8 @@ end
|
|||||||
---@param other MapGen.Peak
|
---@param other MapGen.Peak
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function Peak:eq(other)
|
function Peak:eq(other)
|
||||||
local pos1 = self:getPeakPos()
|
local pos1 = self.pos
|
||||||
local pos2 = other:getPeakPos()
|
local pos2 = other.pos
|
||||||
|
|
||||||
return pos1.x == pos2.x and pos1.z == pos2.z
|
return pos1.x == pos2.x and pos1.z == pos2.z
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -29,9 +29,9 @@ end
|
|||||||
---@param p3 MapGen.Peak
|
---@param p3 MapGen.Peak
|
||||||
---@return boolean
|
---@return boolean
|
||||||
local function isFlatAngle(p1, p2, p3)
|
local function isFlatAngle(p1, p2, p3)
|
||||||
local pos1 = p1:getPeakPos()
|
local pos1 = p1.pos
|
||||||
local pos2 = p2:getPeakPos()
|
local pos2 = p2.pos
|
||||||
local pos3 = p3:getPeakPos()
|
local pos3 = p3.pos
|
||||||
|
|
||||||
local vec1 = pos1 - pos2 -- Vector from p2 to p1
|
local vec1 = pos1 - pos2 -- Vector from p2 to p1
|
||||||
local vec2 = pos3 - pos2 -- Vector from p2 to p3
|
local vec2 = pos3 - pos2 -- Vector from p2 to p3
|
||||||
@@ -91,7 +91,7 @@ function Triangle:new(id, p1, p2, p3, neighboursIDs)
|
|||||||
---@type ColorString?
|
---@type ColorString?
|
||||||
local color = nil
|
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
|
if c1 == c2 and c2 == c3 and c3 == c1 then
|
||||||
color = c1
|
color = c1
|
||||||
@@ -123,9 +123,9 @@ end
|
|||||||
---@return string
|
---@return string
|
||||||
function Triangle:toString()
|
function Triangle:toString()
|
||||||
return ('Triangle: \n %s\n %s\n %s'):format(
|
return ('Triangle: \n %s\n %s\n %s'):format(
|
||||||
tostring(self.p1.getPeakPos()),
|
tostring(self.p1.pos),
|
||||||
tostring(self.p2.getPeakPos()),
|
tostring(self.p2.pos),
|
||||||
tostring(self.p3.getPeakPos())
|
tostring(self.p3.pos)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -148,9 +148,9 @@ end
|
|||||||
---Returns the coordinates of the triangle's center.
|
---Returns the coordinates of the triangle's center.
|
||||||
---@return vector
|
---@return vector
|
||||||
function Triangle:getCenter()
|
function Triangle:getCenter()
|
||||||
local pos1 = self.p1:getPeakPos()
|
local pos1 = self.p1.pos
|
||||||
local pos2 = self.p2:getPeakPos()
|
local pos2 = self.p2.pos
|
||||||
local pos3 = self.p3:getPeakPos()
|
local pos3 = self.p3.pos
|
||||||
|
|
||||||
return (pos1 + pos2 + pos3) / 3
|
return (pos1 + pos2 + pos3) / 3
|
||||||
end
|
end
|
||||||
@@ -169,9 +169,9 @@ end
|
|||||||
---@param p3 MapGen.Peak
|
---@param p3 MapGen.Peak
|
||||||
---@return MapGen.Triangulation.Edge
|
---@return MapGen.Triangulation.Edge
|
||||||
function Triangle:getHeight(p1, p2, p3)
|
function Triangle:getHeight(p1, p2, p3)
|
||||||
local pos1 = p1:getPeakPos()
|
local pos1 = p1.pos
|
||||||
local pos2 = p2:getPeakPos()
|
local pos2 = p2.pos
|
||||||
local pos3 = p3:getPeakPos()
|
local pos3 = p3.pos
|
||||||
|
|
||||||
local v = pos3 - pos2
|
local v = pos3 - pos2
|
||||||
---@type vector
|
---@type vector
|
||||||
@@ -195,9 +195,9 @@ end
|
|||||||
---@param p3 MapGen.Peak
|
---@param p3 MapGen.Peak
|
||||||
---@return MapGen.Triangulation.Edge
|
---@return MapGen.Triangulation.Edge
|
||||||
function Triangle:getHeight2D(p1, p2, p3)
|
function Triangle:getHeight2D(p1, p2, p3)
|
||||||
local pos1 = p1:getPeakPos()
|
local pos1 = p1.pos
|
||||||
local pos2 = p2:getPeakPos()
|
local pos2 = p2.pos
|
||||||
local pos3 = p3:getPeakPos()
|
local pos3 = p3.pos
|
||||||
|
|
||||||
pos1.y = 0
|
pos1.y = 0
|
||||||
pos2.y = 0
|
pos2.y = 0
|
||||||
@@ -228,9 +228,9 @@ end
|
|||||||
---that is, the calculation is performed on the projection of the triangle XZ.
|
---that is, the calculation is performed on the projection of the triangle XZ.
|
||||||
---@return number, number, number
|
---@return number, number, number
|
||||||
function Triangle:getSidesLength2D()
|
function Triangle:getSidesLength2D()
|
||||||
local pos1 = self.p1:getPeakPos()
|
local pos1 = self.p1.pos
|
||||||
local pos2 = self.p2:getPeakPos()
|
local pos2 = self.p2.pos
|
||||||
local pos3 = self.p3:getPeakPos()
|
local pos3 = self.p3.pos
|
||||||
|
|
||||||
pos1.y = 0
|
pos1.y = 0
|
||||||
pos2.y = 0
|
pos2.y = 0
|
||||||
@@ -245,9 +245,9 @@ end
|
|||||||
---that is, the calculation is performed on the projection of the triangle XZ.
|
---that is, the calculation is performed on the projection of the triangle XZ.
|
||||||
---@return vector
|
---@return vector
|
||||||
function Triangle:getCircumCenter2D()
|
function Triangle:getCircumCenter2D()
|
||||||
local pos1 = self.p1:getPeakPos()
|
local pos1 = self.p1.pos
|
||||||
local pos2 = self.p2:getPeakPos()
|
local pos2 = self.p2.pos
|
||||||
local pos3 = self.p3:getPeakPos()
|
local pos3 = self.p3.pos
|
||||||
|
|
||||||
local D = ( pos1.x * (pos2.z - pos3.z) +
|
local D = ( pos1.x * (pos2.z - pos3.z) +
|
||||||
pos2.x * (pos3.z - pos1.z) +
|
pos2.x * (pos3.z - pos1.z) +
|
||||||
@@ -294,7 +294,7 @@ end
|
|||||||
---@param p MapGen.Peak
|
---@param p MapGen.Peak
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function Triangle:inCircumCircle2D(p)
|
function Triangle:inCircumCircle2D(p)
|
||||||
local pos = p:getPeakPos()
|
local pos = p.pos
|
||||||
|
|
||||||
local c, r = self:getCircumCircle2D()
|
local c, r = self:getCircumCircle2D()
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ function Edge:new( p1, p2 )
|
|||||||
local instance = getInstance(self, {
|
local instance = getInstance(self, {
|
||||||
p1 = p1,
|
p1 = p1,
|
||||||
p2 = p2,
|
p2 = p2,
|
||||||
vec = p2:getPeakPos() - p1:getPeakPos()
|
vec = p2.pos - p1.pos
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
__tostring = self.toString,
|
__tostring = self.toString,
|
||||||
@@ -48,14 +48,14 @@ end
|
|||||||
---Returns the length of an edge.
|
---Returns the length of an edge.
|
||||||
---@return number
|
---@return number
|
||||||
function Edge:length()
|
function Edge:length()
|
||||||
return (self.p1.getPeakPos() - self.p2.getPeakPos()):length()
|
return (self.p1.pos - self.p2.pos):length()
|
||||||
end
|
end
|
||||||
|
|
||||||
---Returns the vector of the point located in the middle of the edge.
|
---Returns the vector of the point located in the middle of the edge.
|
||||||
---@return vector
|
---@return vector
|
||||||
function Edge:getMidPoint()
|
function Edge:getMidPoint()
|
||||||
local pos1 = self.p1:getPeakPos()
|
local pos1 = self.p1.pos
|
||||||
local pos2 = self.p2:getPeakPos()
|
local pos2 = self.p2.pos
|
||||||
|
|
||||||
return (pos1 + pos2 )/2
|
return (pos1 + pos2 )/2
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
-- World generation occurs in a separate thread (MapGen Environment).
|
-- World generation occurs in a separate thread (MapGen Environment).
|
||||||
-- But for the main thread, the mapgenerator object is also initialized.
|
-- 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
|
local mapGeneneratorRequire = Mod.getInfo('smc__core__map_generator').require
|
||||||
|
|
||||||
|
|||||||
@@ -50,6 +50,11 @@ function Profiler:getDeltaTime(units)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---Displays the time difference in the chat using `core.log`.
|
---Displays the time difference in the chat using `core.log`.
|
||||||
function Profiler:logDeltaTime()
|
---@param min number?
|
||||||
core.log(('DeltaTime[%s]: %.3f'):format(self:getDeltaTime()))
|
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user