Mapgen: write SuperUltraSigmaMapgen part 4

This commit is contained in:
Van
2025-11-14 21:04:55 +03:00
parent f8070fc594
commit 2c2bb2cd72
4 changed files with 282 additions and 145 deletions

View File

@@ -122,12 +122,7 @@ function MapGen:initLayersTrianglesTetrahedronsLists()
---@param layer MapGen.Layer
for _, layer in ipairs(self.layersList) do
layer.trianglesList = Triangulation.triangulate(layer.peaksList)
layer.tetrahedrons = Triangulation.tetrahedralize(layer.peaksList)
print('--debug--')
for _, t in ipairs(layer.tetrahedrons) do
print(tostring(t))
end
print('--debug--')
layer.tetrahedronsList = Triangulation.tetrahedralize(layer.peaksList)
end
end
@@ -285,6 +280,9 @@ end
local function generateNode(mapGenerator, hight, temp, humidity, data, index, x, y, z)
local ids = mapGenerator.nodeIDs
temp = math.min(0, math.max(1, temp))
humidity = math.min(0, math.max(1, humidity))
local biome = mapGenerator.biomesDiagram[mathRound(temp)][mathRound(humidity)]
if y > hight and y > 0 then
@@ -298,15 +296,17 @@ local function generateNode(mapGenerator, hight, temp, humidity, data, index, x,
end
end
---@param px number
---@param py number
---@param x1 number
---@param y1 number
---@param x2 number
---@param y2 number
---@param x3 number
---@param y3 number
---@param px number
---@param py number
---@param x1 number
---@param y1 number
---@param x2 number
---@param y2 number
---@param x3 number
---@param y3 number
local function pointInTriangle(px, py, x1, y1, x2, y2, x3, y3)
-- TODO: переписать с учетом `vector`
-- Function for calculating the vector product
local function crossProduct(ax, ay, bx, by)
return ax * by - ay * bx
@@ -325,6 +325,31 @@ local function pointInTriangle(px, py, x1, y1, x2, y2, x3, y3)
return not (has_positive and has_negative)
end
---@param P vector
---@param A vector
---@param B vector
---@param C vector
local function pointInTetrahedron(P, A, B, C, D)
local function det3(a, b, c)
return a.x * (b.y * c.z - b.z * c.y)
- a.y * (b.x * c.z - b.z * c.x)
+ a.z * (b.x * c.y - b.y * c.x)
end
local v0 = B - A
local v1 = C - A
local v2 = D - A
local v3 = P - A
local d = det3(v0, v1, v2)
local u = det3(v3, v1, v2) / d
local v = det3(v0, v3, v2) / d
local w = det3(v0, v1, v3) / d
return (u >= 0 and v >= 0 and w >= 0 and (u + v + w) <= 1)
end
---@param height MapGen.Triangulation.Edge
---@param x number
---@param y number
@@ -357,7 +382,7 @@ end
---@param y number
---@param z number
---@return number?
function MapGen:getNoiseHeightValue(layer, x, y, z)
function MapGen:getNoises2dValues(layer, x, y, z)
---@param triangle MapGen.Triangle
for _, triangle in ipairs(layer.trianglesList) do
local peak1 = triangle.p1
@@ -403,7 +428,77 @@ function MapGen:getNoiseHeightValue(layer, x, y, z)
-- 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 height of the landscape is impossible.', tostring(x), tostring(y), tostring(z)))
--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
---
---
---Returns `nil` if the point is not included in any tetrahedron.
---@param layer MapGen.Layer
---@param x number
---@param y number
---@param z number
---@return number?, number?
function MapGen:getNoises3dValues(layer, x, y, z)
---@param triangle MapGen.Tetrahedron
for _, tetrahedron in ipairs(layer.tetrahedronsList) do
local peak1 = tetrahedron.p1
local peak2 = tetrahedron.p2
local peak3 = tetrahedron.p3
local peak4 = tetrahedron.p4
local pos1 = peak1:getPeakPos()
local pos2 = peak2:getPeakPos()
local pos3 = peak3:getPeakPos()
local pos4 = peak4:getPeakPos()
-- If the point is in the tetrahedron defined by the `MapGen.Peak`...
if pointInTetrahedron(
vector.new(x, y, z),
pos1,
pos2,
pos3,
pos4
)
then
local noiseTempValue = 0.0
local noiseHumidityValue = 0.0
local totalWeight = 0.0
-- ... we calculate the height for this point using smoothing.
-- We calculate the height value using the smoothing formula:
-- ( peak1 * weight1 + peak2 * weight2 + peak3 * weight3) / totalWeight, when
-- totalWeight = weight1 + weight2 + weight3
local weight = calcWeight(tetrahedron.h1, x, y, z)
noiseTempValue = noiseTempValue + peak1:getMultinoise().tempNoise:get_2d({x = x, y = z}) * weight
noiseHumidityValue = noiseHumidityValue + peak1:getMultinoise().humidityNoise:get_2d({x = x, y = z}) * weight
totalWeight = totalWeight + weight
weight = calcWeight(tetrahedron.h2, x, y, z)
noiseTempValue = noiseTempValue + peak2:getMultinoise().tempNoise:get_2d({x = x, y = z}) * weight
noiseHumidityValue = noiseHumidityValue + peak2:getMultinoise().humidityNoise:get_2d({x = x, y = z}) * weight
totalWeight = totalWeight + weight
weight = calcWeight(tetrahedron.h3, x, y, z)
noiseTempValue = noiseTempValue + peak3:getMultinoise().tempNoise:get_2d({x = x, y = z}) * weight
noiseHumidityValue = noiseHumidityValue + peak3:getMultinoise().humidityNoise:get_2d({x = x, y = z}) * weight
totalWeight = totalWeight + weight
weight = calcWeight(tetrahedron.h4, x, y, z)
noiseTempValue = noiseTempValue + peak4:getMultinoise().tempNoise:get_2d({x = x, y = z}) * weight
noiseHumidityValue = noiseHumidityValue + peak4:getMultinoise().humidityNoise:get_2d({x = x, y = z}) * weight
totalWeight = totalWeight + weight
-- The height must be an integer value
-- because the biome diagram is based on integers
return mathRound(noiseTempValue / totalWeight), mathRound(noiseHumidityValue / totalWeight)
end
end
-- If the point does not fall within the tetrahedron, 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 tetrahedron, calculating the noise value is impossible.', tostring(x), tostring(y), tostring(z)))
end
---@param mapGenerator MapGen
@@ -427,14 +522,21 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
end
-- Calculating landscape height
local noiseHeightValue = mapGenerator:getNoiseHeightValue(layer, x, y, z)
local noiseHeightValue = mapGenerator:getNoises2dValues(layer, x, y, z)
if noiseHeightValue == nil then
noiseHeightValue = 0.0
end
local noiseTempValue = 0.0
local noiseHumidityValue = 0.0
local noiseTempValue, noiseHumidityValue = mapGenerator:getNoises3dValues(layer, x, y, z)
if noiseTempValue == nil then
noiseTempValue = 0.0
end
if noiseHumidityValue == nil then
noiseHumidityValue = 0.0
end
generateNode(mapGenerator, noiseHeightValue, noiseTempValue, noiseHumidityValue, data, index, x, y, z)
end

View File

@@ -1,16 +1,16 @@
local mathSqrt = math.sqrt
---@class MapGen.Layer
---@field name string
---@field minY number
---@field maxY number
---@field peaksList MapGen.Peak[]
---@field trianglesList MapGen.Triangle[]
---@field tetrahedrons MapGen.Tetrahedron[]
---@field name string
---@field minY number
---@field maxY number
---@field peaksList MapGen.Peak[]
---@field trianglesList MapGen.Triangle[]
---@field tetrahedronsList MapGen.Tetrahedron[]
local Layer = {
peaksList = {},
trianglesList = {},
tetrahedrons = {},
tetrahedronsList = {},
}
---@param name string

View File

@@ -29,12 +29,14 @@ local Triangulation = {
---Vertices that do not have a `is2d` group will not participate in the triangulation.
---@param vertices table<integer, MapGen.Peak>
function Triangulation.triangulate( vertices )
vertices = table.copy_with_metatables(vertices)
-- Only peaks with a `is2d` group can be triangulated.
--[[for i, vertex in ipairs(vertices) do
for i, vertex in ipairs(vertices) do
if vertex:getGroups().is2d == nil then
table.remove(vertices, i)
end
end--]]
end
local nvertices = #vertices
@@ -86,7 +88,6 @@ function Triangulation.triangulate( vertices )
---@type MapGen.Triangle[]
local triangles = {Triangle:new( vertices[nvertices + 1], vertices[nvertices + 2], vertices[nvertices + 3] )}
print('uu:', tostring(triangles[1]))
for i = 1, nvertices do
---@type MapGen.Triangulation.Edge[]
local edges = {}
@@ -142,6 +143,8 @@ local require = modInfo.require
---@type MapGen.Triangle
local Triangle = require('MapGen.Triangle')
---@type MapGen.Triangulation.Edge
local Edge = require('MapGen.Triangulation.Edge')
---@type MapGen.Triangulation.FakePeak
local FakePeak = require('MapGen.Triangulation.FakePeak')
@@ -151,6 +154,10 @@ local FakePeak = require('MapGen.Triangulation.FakePeak')
---@field p2 MapGen.Peak
---@field p3 MapGen.Peak
---@field p4 MapGen.Peak
---@field h1 MapGen.Triangulation.Edge
---@field h2 MapGen.Triangulation.Edge
---@field h3 MapGen.Triangulation.Edge
---@field h4 MapGen.Triangulation.Edge
---@field f1 MapGen.Triangle
---@field f2 MapGen.Triangle
---@field f3 MapGen.Triangle
@@ -168,6 +175,10 @@ function Tetrahedron:new(p1, p2, p3, p4)
p2 = p2,
p3 = p3,
p4 = p4,
h1 = self:getHeight(p1, p2, p3, p4),
h2 = self:getHeight(p2, p3, p4, p1),
h3 = self:getHeight(p3, p4, p1, p2),
h4 = self:getHeight(p4, p1, p2, p3),
f1 = Triangle:new(p1, p2, p3),
f2 = Triangle:new(p1, p2, p4),
f3 = Triangle:new(p1, p3, p4),
@@ -191,6 +202,41 @@ function Tetrahedron:toString()
)
end
---@param p1 MapGen.Peak
---@param p2 MapGen.Peak
---@param p3 MapGen.Peak
---@param p4 MapGen.Peak
function Tetrahedron:getHeight(p1, p2, p3, p4)
local pos1 = p1:getPeakPos()
local pos2 = p2:getPeakPos()
local pos3 = p3:getPeakPos()
local pos4 = p4:getPeakPos()
-- Векторы, лежащие в плоскости
local AB = pos3 - pos2
local AC = pos4 - pos2
-- Нормаль к плоскости
local normal = AB:cross(AC)
local normal_length = normal:length()
-- Если плоскость вырождена (точки на одной прямой)
if normal_length < 1e-10 then
return nil -- или другая обработка ошибки
end
-- Вектор от точки на плоскости к нашей точке
local AP = pos1 - pos2
-- Расстояние от точки до плоскости
local distance = AP:dot(normal) / normal_length
-- Проекция точки на плоскость (вектор!)
local projection = pos1 - (normal * (distance / normal_length))
return Edge:new(p1, FakePeak:new(projection))
end
---Returns the center of the circumscribed sphere.
---@return vector
function Tetrahedron:getCircumSphereCenter()
@@ -246,16 +292,14 @@ end
---Vertices that do not have a `is3d` group will not participate in the triangulation.
---@param vertices table<integer, MapGen.Peak>
function Triangulation.tetrahedralize(vertices)
--[[vertices = table.copy(vertices)
vertices = table.copy_with_metatables(vertices)
-- Only peaks with a `is3d` group can be triangulated.
for i, vertex in ipairs(vertices) do
if vertex:getGroups().is3d == nil then
table.remove(vertices, i)
end
end--]]
print(dump(vertices))
end
local nvertices = #vertices
@@ -317,7 +361,6 @@ function Triangulation.tetrahedralize(vertices)
local tetrahedrons = {Tetrahedron:new(p1, p2, p3, p4)}
for i = 1, nvertices do
print(vertices[i]:toString())
---@type MapGen.Triangle[]
local faces = {}
@@ -349,7 +392,6 @@ function Triangulation.tetrahedralize(vertices)
tetrahedrons[#tetrahedrons + 1] = Tetrahedron:new(faces[j].p1, faces[j].p2, faces[j].p3, vertices[i])
end
print('aboba')
end
-- Remove tetrahedra associated with the vertices of a supertetrahedron.