forked from Apancore.Studio/simple-makes-complex
Mapgen: write SuperUltraSigmaMapgen part 5
This commit is contained in:
@@ -1,20 +1,7 @@
|
||||
local mathAbs, mathRound
|
||||
= math.abs, math.round
|
||||
local mathAbs, mathRound, mathMin, mathMax
|
||||
= math.abs, math.round, math.min, math.max
|
||||
|
||||
-- --- MapGen default moises ---
|
||||
---@type ValueNoise
|
||||
local oceanNoise
|
||||
|
||||
---@type NoiseParams
|
||||
local oceanNoiseParams = {
|
||||
offset = -30,
|
||||
scale = 10,
|
||||
spread = vector.new(100, 100, 100),
|
||||
seed = 47,
|
||||
octaves = 8,
|
||||
persistence = 0.4,
|
||||
lacunarity = 2,
|
||||
}
|
||||
-- --- MapGen default noises ---
|
||||
|
||||
---@type ValueNoise
|
||||
local rocksNoise
|
||||
@@ -280,8 +267,8 @@ 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))
|
||||
temp = mathMin(0, mathMax(100, temp))
|
||||
humidity = mathMin(0, mathMax(100, humidity))
|
||||
|
||||
local biome = mapGenerator.biomesDiagram[mathRound(temp)][mathRound(humidity)]
|
||||
|
||||
@@ -296,6 +283,29 @@ local function generateNode(mapGenerator, hight, temp, humidity, data, index, x,
|
||||
end
|
||||
end
|
||||
|
||||
---@param height MapGen.Triangulation.Edge
|
||||
---@param x number
|
||||
---@param y number
|
||||
---@param z number
|
||||
---@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 v = pos3 - pos2
|
||||
---@type vector
|
||||
local w = pos1 - pos2
|
||||
|
||||
-- Formula: (w * v) / (v * v)
|
||||
-- When * the dot product.
|
||||
local t = w:dot(v) / v:dot(v)
|
||||
|
||||
local posP = pos2 + v * t
|
||||
|
||||
return 1 - vector.distance(pos2, posP) / height:length()
|
||||
end
|
||||
|
||||
---@param px number
|
||||
---@param py number
|
||||
---@param x1 number
|
||||
@@ -325,6 +335,72 @@ local function pointInTriangle(px, py, x1, y1, x2, y2, x3, y3)
|
||||
return not (has_positive and has_negative)
|
||||
end
|
||||
|
||||
---Calculates a smoothed 2D noise value based on a preliminary space partitioning.
|
||||
---
|
||||
---Returns `nil` if the point is not included in any triangle.
|
||||
---@param layer MapGen.Layer
|
||||
---@param x number
|
||||
---@param y number
|
||||
---@param z number
|
||||
---@return number?, number?, number?
|
||||
function MapGen:getNoises2dValues(layer, x, y, z)
|
||||
---@param triangle MapGen.Triangle
|
||||
for _, triangle in ipairs(layer.trianglesList) do
|
||||
local peak1 = triangle.p1
|
||||
local peak2 = triangle.p2
|
||||
local peak3 = triangle.p3
|
||||
|
||||
local pos1 = peak1:getPeakPos()
|
||||
local pos2 = peak2:getPeakPos()
|
||||
local pos3 = peak3:getPeakPos()
|
||||
|
||||
-- If the point is in the triangle defined by the `MapGen.Peak`...
|
||||
if pointInTriangle(
|
||||
x, z,
|
||||
pos1.x, pos1.z,
|
||||
pos2.x, pos2.z,
|
||||
pos3.x, pos3.z
|
||||
)
|
||||
then
|
||||
local noiseHeightValue = 0.0
|
||||
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(triangle.h1, x, y, z)
|
||||
noiseHeightValue = noiseHeightValue + peak1:getMultinoise().landscapeNoise:get_2d({x = x, y = z}) * weight
|
||||
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(triangle.h2, x, y, z)
|
||||
noiseHeightValue = noiseHeightValue + peak2:getMultinoise().landscapeNoise:get_2d({x = x, y = z}) * weight
|
||||
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(triangle.h3, x, y, z)
|
||||
noiseHeightValue = noiseHeightValue + peak3:getMultinoise().landscapeNoise:get_2d({x = x, y = z}) * weight
|
||||
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
|
||||
|
||||
return mathRound(noiseHeightValue / totalWeight), mathRound(noiseTempValue / totalWeight), mathRound(noiseHumidityValue / totalWeight)
|
||||
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
|
||||
|
||||
--[[
|
||||
3D noise smoothing is too slow, so it was decided to stop using it.
|
||||
|
||||
---@param P vector
|
||||
---@param A vector
|
||||
---@param B vector
|
||||
@@ -350,95 +426,15 @@ local function pointInTetrahedron(P, A, B, C, 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
|
||||
---@param z number
|
||||
---@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 v = pos3 - pos2
|
||||
---@type vector
|
||||
local w = pos1 - pos2
|
||||
|
||||
-- Formula: (w * v) / (v * v)
|
||||
-- When * the dot product.
|
||||
local t = w:dot(v) / v:dot(v)
|
||||
|
||||
local posP = pos2 + v * t
|
||||
|
||||
return 1 - vector.distance(pos2, posP) / height:length()
|
||||
end
|
||||
|
||||
---Calculates the height of the landscape based
|
||||
---on a pre-triangulated plane stored in a layer.
|
||||
---Calculates a smoothed 3D noise value based on a preliminary space partitioning.
|
||||
---
|
||||
---Returns `nil` if the point is not included in any tetrahedron.
|
||||
---
|
||||
---Returns `nil` if the point is not included in any triangle.
|
||||
---@param layer MapGen.Layer
|
||||
---@param x number
|
||||
---@param y number
|
||||
---@param z number
|
||||
---@return number?
|
||||
function MapGen:getNoises2dValues(layer, x, y, z)
|
||||
---@param triangle MapGen.Triangle
|
||||
for _, triangle in ipairs(layer.trianglesList) do
|
||||
local peak1 = triangle.p1
|
||||
local peak2 = triangle.p2
|
||||
local peak3 = triangle.p3
|
||||
|
||||
local pos1 = peak1:getPeakPos()
|
||||
local pos2 = peak2:getPeakPos()
|
||||
local pos3 = peak3:getPeakPos()
|
||||
|
||||
-- If the point is in the triangle defined by the `MapGen.Peak`...
|
||||
if pointInTriangle(
|
||||
x, z,
|
||||
pos1.x, pos1.z,
|
||||
pos2.x, pos2.z,
|
||||
pos3.x, pos3.z
|
||||
)
|
||||
then
|
||||
local noiseHeightValue = 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(triangle.h1, x, y, z)
|
||||
noiseHeightValue = noiseHeightValue + peak1:getMultinoise().landscapeNoise:get_2d({x = x, y = z}) * weight
|
||||
totalWeight = totalWeight + weight
|
||||
|
||||
weight = calcWeight(triangle.h2, x, y, z)
|
||||
noiseHeightValue = noiseHeightValue + peak2:getMultinoise().landscapeNoise:get_2d({x = x, y = z}) * weight
|
||||
totalWeight = totalWeight + weight
|
||||
|
||||
weight = calcWeight(triangle.h3, x, y, z)
|
||||
noiseHeightValue = noiseHeightValue + peak3:getMultinoise().landscapeNoise:get_2d({x = x, y = z}) * weight
|
||||
totalWeight = totalWeight + weight
|
||||
|
||||
-- The height must be an integer value
|
||||
-- because the world is made up of whole blocks.
|
||||
return mathRound(noiseHeightValue / totalWeight)
|
||||
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
|
||||
|
||||
---
|
||||
---
|
||||
---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
|
||||
@@ -461,45 +457,36 @@ function MapGen:getNoises3dValues(layer, x, y, z)
|
||||
pos4
|
||||
)
|
||||
then
|
||||
local noiseTempValue = 0.0
|
||||
local noiseHumidityValue = 0.0
|
||||
local noiseValue = 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
|
||||
noiseValue = noiseValue + peak1:getMultinoise().someNoise:get_3d({x = x, y = y, z = 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
|
||||
noiseValue = noiseValue + peak1:getMultinoise().someNoise:get_3d({x = x, y = y, z = 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
|
||||
noiseValue = noiseValue + peak1:getMultinoise().someNoise:get_3d({x = x, y = y, z = 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
|
||||
noiseValue = noiseValue + peak1:getMultinoise().someNoise:get_2d({x = x, y = y, z = 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)
|
||||
return mathRound(noiseValue / 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)))
|
||||
-- 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
|
||||
---@param data number[]
|
||||
@@ -508,7 +495,7 @@ end
|
||||
---@param y number
|
||||
---@param z number
|
||||
local function generatorHandler(mapGenerator, data, index, x, y, z)
|
||||
-- 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
|
||||
data[index] = mapGenerator.nodeIDs.air
|
||||
@@ -516,28 +503,18 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
|
||||
return
|
||||
end
|
||||
|
||||
-- The ocean floor height is used as the default value.
|
||||
if oceanNoise == nil then
|
||||
oceanNoise = core.get_value_noise(oceanNoiseParams)
|
||||
end
|
||||
|
||||
-- Calculating landscape height
|
||||
local noiseHeightValue = mapGenerator:getNoises2dValues(layer, x, y, z)
|
||||
local noiseHeightValue, noiseTempValue, noiseHumidityValue = mapGenerator:getNoises2dValues(layer, x, y, z)
|
||||
|
||||
-- If the height of the landscape is zero,
|
||||
-- then the point does not fall within any of the triangles.
|
||||
if noiseHeightValue == nil then
|
||||
noiseHeightValue = 0.0
|
||||
end
|
||||
|
||||
local noiseTempValue, noiseHumidityValue = mapGenerator:getNoises3dValues(layer, x, y, z)
|
||||
|
||||
if noiseTempValue == nil then
|
||||
noiseTempValue = 0.0
|
||||
end
|
||||
|
||||
if noiseHumidityValue == nil then
|
||||
noiseHeightValue = 0.0
|
||||
noiseTempValue = 0.0
|
||||
noiseHumidityValue = 0.0
|
||||
end
|
||||
|
||||
---@diagnostic disable-next-line: param-type-not-match
|
||||
generateNode(mapGenerator, noiseHeightValue, noiseTempValue, noiseHumidityValue, data, index, x, y, z)
|
||||
end
|
||||
|
||||
|
||||
152
mods/CORE/MapGen/src/MapGen/Tetrahedron.lua
Normal file
152
mods/CORE/MapGen/src/MapGen/Tetrahedron.lua
Normal file
@@ -0,0 +1,152 @@
|
||||
local mathAbs = math.abs
|
||||
|
||||
local modInfo = Mod.getInfo()
|
||||
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')
|
||||
|
||||
---@class MapGen.Tetrahedron
|
||||
---@field p1 MapGen.Peak
|
||||
---@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
|
||||
---@field f4 MapGen.Triangle
|
||||
local Tetrahedron = {}
|
||||
|
||||
---@param p1 MapGen.Peak
|
||||
---@param p2 MapGen.Peak
|
||||
---@param p3 MapGen.Peak
|
||||
---@param p4 MapGen.Peak
|
||||
---@return MapGen.Tetrahedron
|
||||
function Tetrahedron:new(p1, p2, p3, p4)
|
||||
local instance = setmetatable({
|
||||
p1 = p1,
|
||||
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),
|
||||
f4 = Triangle:new(p2, p3, p4),
|
||||
}, {
|
||||
__index = self,
|
||||
__tostring = self.toString
|
||||
})
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
---Returns a string describing the object in a readable form.
|
||||
---@return string
|
||||
function Tetrahedron:toString()
|
||||
return ('Tetrahedron: \n %s\n %s\n %s\n %s'):format(
|
||||
tostring(self.p1.getPeakPos()),
|
||||
tostring(self.p2.getPeakPos()),
|
||||
tostring(self.p3.getPeakPos()),
|
||||
tostring(self.p4.getPeakPos())
|
||||
)
|
||||
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()
|
||||
local pos1 = self.p1.getPeakPos()
|
||||
local pos2 = self.p2.getPeakPos()
|
||||
local pos3 = self.p3.getPeakPos()
|
||||
local pos4 = self.p4.getPeakPos()
|
||||
|
||||
local a = pos2 - pos1
|
||||
local b = pos3 - pos1
|
||||
local c = pos4 - pos1
|
||||
|
||||
local a2 = a:dot(a)
|
||||
local b2 = b:dot(b)
|
||||
local c2 = c:dot(c)
|
||||
|
||||
local cross_bc = b:cross(c)
|
||||
local cross_ca = c:cross(a)
|
||||
local cross_ab = a:cross(b)
|
||||
|
||||
local det = a:dot(b:cross(c)) * 2
|
||||
|
||||
if mathAbs(det) < 0 then
|
||||
return (pos1 + pos2 + pos3 + pos4) / 4
|
||||
end
|
||||
|
||||
local center = pos1 + (cross_bc * a2 + cross_ca * b2 + cross_ab * c2) / det
|
||||
|
||||
return center
|
||||
end
|
||||
|
||||
---Returns the radius of the circumscribed sphere.
|
||||
---@return number
|
||||
function Tetrahedron:getCircumSphereRadius()
|
||||
local center = self:getCircumSphereCenter()
|
||||
local pos1 = self.p1.getPeakPos()
|
||||
return (pos1 - center):length()
|
||||
end
|
||||
|
||||
---Checks whether a given point lies within the circumscribed sphere of a tetrahedron.
|
||||
---@param p MapGen.Peak
|
||||
function Tetrahedron:inCircumSphere(p)
|
||||
local pos = p.getPeakPos()
|
||||
local c = self:getCircumSphereCenter()
|
||||
local r = self:getCircumSphereRadius()
|
||||
|
||||
local distance = (pos - c):length()
|
||||
|
||||
return distance <= r
|
||||
end
|
||||
|
||||
return Tetrahedron
|
||||
@@ -1,4 +1,5 @@
|
||||
local mathSqrt = math.sqrt
|
||||
local mathSqrt, mathAbs
|
||||
= math.sqrt, math.abs
|
||||
|
||||
local modInfo = Mod.getInfo()
|
||||
local require = modInfo.require
|
||||
@@ -46,7 +47,7 @@ local function isFlatAngle(p1, p2, p3)
|
||||
|
||||
-- If dot product is -1, vectors are opposite (180° angle)
|
||||
local dot = vec1:dot(vec2)
|
||||
return math.abs(dot + 1.0) < 0
|
||||
return mathAbs(dot + 1.0) < 0
|
||||
end
|
||||
|
||||
-- Class registration --
|
||||
|
||||
@@ -1,22 +1,29 @@
|
||||
-- SMC tringulation lib
|
||||
-- Tringulation lib
|
||||
-- Original code: https://github.com/iskolbin/Triangulation/blob/master/Triangulation.lua
|
||||
-- Date of copying: 07.11.2025
|
||||
-- Commit: e8d26241767cd6af377b5eda0c1e27122b6afe8a
|
||||
|
||||
local assert = assert
|
||||
local mathMax = math.max
|
||||
local tableRemove = table.remove
|
||||
-- Main changes:
|
||||
-- * refactoring code to our standard;
|
||||
-- * splitting code into different files (classes);
|
||||
-- * Adapting the code to the mapgen concept;
|
||||
-- * Adding tetrahedralization;
|
||||
|
||||
local assert, mathMax, tableRemove
|
||||
= assert, math.max, table.remove
|
||||
|
||||
local modInfo = Mod.getInfo()
|
||||
local require = modInfo.require
|
||||
|
||||
---@type MapGen.Triangle
|
||||
local Triangle = require('MapGen.Triangle')
|
||||
---@type MapGen.Tetrahedron
|
||||
local Tetrahedron = require('MapGen.Tetrahedron')
|
||||
---@type MapGen.Triangulation.FakePeak
|
||||
local FakePeak = require('MapGen.Triangulation.FakePeak')
|
||||
|
||||
|
||||
---@class MapGen.Triangulation
|
||||
---@field convexMultiplier number
|
||||
local Triangulation = {
|
||||
convexMultiplier = 1e3,
|
||||
}
|
||||
@@ -136,157 +143,6 @@ function Triangulation.triangulate( vertices )
|
||||
return triangles
|
||||
end
|
||||
|
||||
--- DEV ---
|
||||
|
||||
local modInfo = Mod.getInfo()
|
||||
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')
|
||||
|
||||
-- Класс Тетраэдр
|
||||
---@class MapGen.Tetrahedron
|
||||
---@field p1 MapGen.Peak
|
||||
---@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
|
||||
---@field f4 MapGen.Triangle
|
||||
local Tetrahedron = {}
|
||||
|
||||
---@param p1 MapGen.Peak
|
||||
---@param p2 MapGen.Peak
|
||||
---@param p3 MapGen.Peak
|
||||
---@param p4 MapGen.Peak
|
||||
---@return MapGen.Tetrahedron
|
||||
function Tetrahedron:new(p1, p2, p3, p4)
|
||||
local instance = setmetatable({
|
||||
p1 = p1,
|
||||
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),
|
||||
f4 = Triangle:new(p2, p3, p4),
|
||||
}, {
|
||||
__index = self,
|
||||
__tostring = self.toString
|
||||
})
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
---Returns a string describing the object in a readable form.
|
||||
---@return string
|
||||
function Tetrahedron:toString()
|
||||
return ('Tetrahedron: \n %s\n %s\n %s\n %s'):format(
|
||||
tostring(self.p1.getPeakPos()),
|
||||
tostring(self.p2.getPeakPos()),
|
||||
tostring(self.p3.getPeakPos()),
|
||||
tostring(self.p4.getPeakPos())
|
||||
)
|
||||
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()
|
||||
local pos1 = self.p1.getPeakPos()
|
||||
local pos2 = self.p2.getPeakPos()
|
||||
local pos3 = self.p3.getPeakPos()
|
||||
local pos4 = self.p4.getPeakPos()
|
||||
|
||||
local a = pos2 - pos1
|
||||
local b = pos3 - pos1
|
||||
local c = pos4 - pos1
|
||||
|
||||
local a2 = a:dot(a)
|
||||
local b2 = b:dot(b)
|
||||
local c2 = c:dot(c)
|
||||
|
||||
local cross_bc = b:cross(c)
|
||||
local cross_ca = c:cross(a)
|
||||
local cross_ab = a:cross(b)
|
||||
|
||||
local det = a:dot(b:cross(c)) * 2
|
||||
|
||||
if math.abs(det) < 0 then
|
||||
return (pos1 + pos2 + pos3 + pos4) / 4
|
||||
end
|
||||
|
||||
local center = pos1 + (cross_bc * a2 + cross_ca * b2 + cross_ab * c2) / det
|
||||
|
||||
return center
|
||||
end
|
||||
|
||||
---Returns the radius of the circumscribed sphere.
|
||||
---@return number
|
||||
function Tetrahedron:getCircumSphereRadius()
|
||||
local center = self:getCircumSphereCenter()
|
||||
local pos1 = self.p1.getPeakPos()
|
||||
return (pos1 - center):length()
|
||||
end
|
||||
|
||||
---Checks whether a given point lies within the circumscribed sphere of a tetrahedron.
|
||||
---@param p MapGen.Peak
|
||||
function Tetrahedron:inCircumSphere(p)
|
||||
local pos = p.getPeakPos()
|
||||
local c = self:getCircumSphereCenter()
|
||||
local r = self:getCircumSphereRadius()
|
||||
|
||||
local distance = (pos - c):length()
|
||||
return distance <= r
|
||||
end
|
||||
|
||||
---The Delaunay method divides space into tetrahedra based on a list of their vertices.
|
||||
---
|
||||
---Vertices that do not have a `is3d` group will not participate in the triangulation.
|
||||
@@ -410,5 +266,4 @@ function Triangulation.tetrahedralize(vertices)
|
||||
return tetrahedrons
|
||||
end
|
||||
|
||||
|
||||
return Triangulation
|
||||
@@ -76,8 +76,8 @@ mapGenerator:RegisterPeak("world",
|
||||
vector.new(0,0,0),
|
||||
{
|
||||
landscapeNoise = land1,
|
||||
tempNoise = climat0,
|
||||
humidityNoise = climat0,
|
||||
tempNoise = climat2,
|
||||
humidityNoise = climat2,
|
||||
},
|
||||
{is2d = 1, is3d = 1})
|
||||
|
||||
@@ -85,8 +85,8 @@ mapGenerator:RegisterPeak("world",
|
||||
vector.new(25,0,25),
|
||||
{
|
||||
landscapeNoise = land2,
|
||||
tempNoise = climat1,
|
||||
humidityNoise = climat1,
|
||||
tempNoise = climat2,
|
||||
humidityNoise = climat2,
|
||||
},
|
||||
{is2d = 1, is3d = 1})
|
||||
|
||||
@@ -94,8 +94,8 @@ mapGenerator:RegisterPeak("world",
|
||||
vector.new(-25,0,-25),
|
||||
{
|
||||
landscapeNoise = land2,
|
||||
tempNoise = climat1,
|
||||
humidityNoise = climat1,
|
||||
tempNoise = climat2,
|
||||
humidityNoise = climat2,
|
||||
},
|
||||
{is2d = 1, is3d = 1})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user