Mapgen: write SuperUltraSigmaMapgen part 5.2
This commit is contained in:
@@ -19,6 +19,24 @@ local rocksNoiseParams ={
|
|||||||
|
|
||||||
-- --- End default noises ---
|
-- --- End default noises ---
|
||||||
|
|
||||||
|
|
||||||
|
local pastNoise2DCalc = {
|
||||||
|
---@type MapGen.Triangle?
|
||||||
|
triangle = nil,
|
||||||
|
---@type number?
|
||||||
|
x = nil,
|
||||||
|
---@type number?
|
||||||
|
y = nil,
|
||||||
|
noiseValues = {
|
||||||
|
---@type number?
|
||||||
|
Height = nil,
|
||||||
|
---@type number?
|
||||||
|
Humidity = nil,
|
||||||
|
---@type number?
|
||||||
|
Temp = nil,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
local modInfo = Mod.getInfo()
|
local modInfo = Mod.getInfo()
|
||||||
local require = modInfo.require
|
local require = modInfo.require
|
||||||
|
|
||||||
@@ -279,15 +297,13 @@ local function calcWeight(height, x, y, z)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---@param px number
|
---@param px number
|
||||||
---@param py number
|
---@param pz number
|
||||||
---@param x1 number
|
---@param triangle MapGen.Triangle
|
||||||
---@param y1 number
|
local function pointInTriangle(px, pz, triangle)
|
||||||
---@param x2 number
|
local posP = vector.new(px, 0, pz)
|
||||||
---@param y2 number
|
local pos1 = triangle.p1.getPeakPos()
|
||||||
---@param x3 number
|
local pos2 = triangle.p2.getPeakPos()
|
||||||
---@param y3 number
|
local pos3 = triangle.p3.getPeakPos()
|
||||||
local function pointInTriangle(px, py, x1, y1, x2, y2, x3, y3)
|
|
||||||
-- TODO: переписать с учетом `vector`
|
|
||||||
|
|
||||||
-- Function for calculating the vector product
|
-- Function for calculating the vector product
|
||||||
local function crossProduct(ax, ay, bx, by)
|
local function crossProduct(ax, ay, bx, by)
|
||||||
@@ -299,6 +315,10 @@ local function pointInTriangle(px, py, x1, y1, x2, y2, x3, y3)
|
|||||||
local d2 = crossProduct(x3 - x2, y3 - y2, px - x2, py - y2)
|
local d2 = crossProduct(x3 - x2, y3 - y2, px - x2, py - y2)
|
||||||
local d3 = crossProduct(x1 - x3, y1 - y3, px - x3, py - y3)
|
local d3 = crossProduct(x1 - x3, y1 - y3, px - x3, py - y3)
|
||||||
|
|
||||||
|
local d1 = vector.cross(pos2 - pos1, posP - pos1)
|
||||||
|
local d2 = vector.cross(pos3 - pos2, posP - pos2)
|
||||||
|
local d3 = vector.cross(pos1 - pos3, posP - pos3)
|
||||||
|
|
||||||
-- Checking the signs of vector products
|
-- Checking the signs of vector products
|
||||||
local has_positive = (d1 > 0) or (d2 > 0) or (d3 > 0)
|
local has_positive = (d1 > 0) or (d2 > 0) or (d3 > 0)
|
||||||
local has_negative = (d1 < 0) or (d2 < 0) or (d3 < 0)
|
local has_negative = (d1 < 0) or (d2 < 0) or (d3 < 0)
|
||||||
@@ -307,40 +327,23 @@ local function pointInTriangle(px, py, x1, y1, x2, y2, x3, y3)
|
|||||||
return not (has_positive and has_negative)
|
return not (has_positive and has_negative)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Calculates a smoothed 2D noise value based on a preliminary space partitioning.
|
---@param triangle MapGen.Triangle
|
||||||
---
|
|
||||||
---Returns `nil` if the point is not included in any triangle.
|
|
||||||
---@param layer MapGen.Layer
|
|
||||||
---@param x number
|
---@param x number
|
||||||
---@param y number
|
---@param y number
|
||||||
---@param z number
|
---@param z number
|
||||||
---@return number?, number?, number?
|
---@return number, number, number
|
||||||
function MapGen:getNoises2dValues(layer, x, y, z)
|
local function calcNoises2dValues(triangle, x, y, z)
|
||||||
---@param triangle MapGen.Triangle
|
|
||||||
for _, triangle in ipairs(layer.trianglesList) do
|
|
||||||
local peak1 = triangle.p1
|
local peak1 = triangle.p1
|
||||||
local peak2 = triangle.p2
|
local peak2 = triangle.p2
|
||||||
local peak3 = triangle.p3
|
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 noiseHeightValue = 0.0
|
||||||
local noiseTempValue = 0.0
|
local noiseTempValue = 0.0
|
||||||
local noiseHumidityValue = 0.0
|
local noiseHumidityValue = 0.0
|
||||||
|
|
||||||
local totalWeight = 0.0
|
local totalWeight = 0.0
|
||||||
-- ... we calculate the height for this point using smoothing.
|
|
||||||
-- We calculate the height value using the smoothing formula:
|
-- We calculate the noise value using the smoothing formula:
|
||||||
-- ( peak1 * weight1 + peak2 * weight2 + peak3 * weight3) / totalWeight, when
|
-- ( peak1 * weight1 + peak2 * weight2 + peak3 * weight3) / totalWeight, when
|
||||||
-- totalWeight = weight1 + weight2 + weight3
|
-- totalWeight = weight1 + weight2 + weight3
|
||||||
local weight = calcWeight(triangle.h1, x, y, z)
|
local weight = calcWeight(triangle.h1, x, y, z)
|
||||||
@@ -363,6 +366,38 @@ function MapGen:getNoises2dValues(layer, x, y, z)
|
|||||||
|
|
||||||
return mathRound(noiseHeightValue / totalWeight), mathRound(noiseTempValue / totalWeight), mathRound(noiseHumidityValue / totalWeight)
|
return mathRound(noiseHeightValue / totalWeight), mathRound(noiseTempValue / totalWeight), mathRound(noiseHumidityValue / totalWeight)
|
||||||
end
|
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)
|
||||||
|
if x == pastNoise2DCalc.x and z == pastNoise2DCalc.z then
|
||||||
|
return pastNoise2DCalc.noiseValues.Height, pastNoise2DCalc.noiseValues.Temp, pastNoise2DCalc.noiseValues.Humidity
|
||||||
|
end
|
||||||
|
|
||||||
|
if pastNoise2DCalc.triangle ~= nil and pointInTriangle(x, z, pastNoise2DCalc.triangle) then
|
||||||
|
return calcNoises2dValues(pastNoise2DCalc.triangle, x, y, z)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@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,triangle) then
|
||||||
|
return calcNoises2dValues(triangle, x, y, z)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- If the point does not fall within the triangle, it is impossible to calculate the height.
|
-- If the point does not fall within the triangle, it is impossible to calculate the height.
|
||||||
@@ -443,11 +478,11 @@ function MapGen:getNoises3dValues(layer, x, y, z)
|
|||||||
totalWeight = totalWeight + weight
|
totalWeight = totalWeight + weight
|
||||||
|
|
||||||
weight = calcWeight(tetrahedron.h3, x, y, z)
|
weight = calcWeight(tetrahedron.h3, x, y, z)
|
||||||
noiseValue = noiseValue + peak1:getMultinoise().someNoise:get_3d({x = x, y = y, z = z}) * weight
|
noiseValue = noiseValue + peak2:getMultinoise().someNoise:get_3d({x = x, y = y, z = z}) * weight
|
||||||
totalWeight = totalWeight + weight
|
totalWeight = totalWeight + weight
|
||||||
|
|
||||||
weight = calcWeight(tetrahedron.h4, x, y, z)
|
weight = calcWeight(tetrahedron.h4, x, y, z)
|
||||||
noiseValue = noiseValue + peak1:getMultinoise().someNoise:get_2d({x = x, y = y, z = z}) * weight
|
noiseValue = noiseValue + peak3:getMultinoise().someNoise:get_2d({x = x, y = y, z = z}) * weight
|
||||||
totalWeight = totalWeight + weight
|
totalWeight = totalWeight + weight
|
||||||
|
|
||||||
return mathRound(noiseValue / totalWeight)
|
return mathRound(noiseValue / totalWeight)
|
||||||
|
|||||||
Reference in New Issue
Block a user