MapGen: add getTriangleByPos2D()
This commit is contained in:
@@ -114,7 +114,7 @@ function MapGen:getStaticByHeight(yPos)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---Returns the `MapGen.Peak` at the coordinates. If there is no peak at these coordinates, it returns nil.
|
---Returns the `MapGen.Peak` at the coordinates. If there is no peak at these coordinates, it returns `nil`.
|
||||||
---@param layer string|MapGen.Layer The layer or name of the layer in which the peak is located.
|
---@param layer string|MapGen.Layer The layer or name of the layer in which the peak is located.
|
||||||
---@param category MapGen.Layer.Category The category in which the peak is located.
|
---@param category MapGen.Layer.Category The category in which the peak is located.
|
||||||
---@param pos Position Coordinate of the desired peak.
|
---@param pos Position Coordinate of the desired peak.
|
||||||
@@ -137,6 +137,56 @@ function MapGen:getPeakByPos(layer, category, pos)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Returns `true` if the point is within the triangle's region.
|
||||||
|
---The barycentric coordinate method is used.
|
||||||
|
---@param px integer
|
||||||
|
---@param pz integer
|
||||||
|
---@param triangle MapGen.Triangle
|
||||||
|
local function pointInTriangle2D(px, pz, triangle)
|
||||||
|
local posP = vector.new(px, 0, pz)
|
||||||
|
local pos1 = triangle.p1.getPeakPos()
|
||||||
|
local pos2 = triangle.p2.getPeakPos()
|
||||||
|
local pos3 = triangle.p3.getPeakPos()
|
||||||
|
|
||||||
|
local v0 = pos3 - pos1
|
||||||
|
local v1 = pos2 - pos1
|
||||||
|
local v2 = posP - pos1
|
||||||
|
|
||||||
|
local dot00 = vector.dot(v0, v0)
|
||||||
|
local dot01 = vector.dot(v0, v1)
|
||||||
|
local dot02 = vector.dot(v0, v2)
|
||||||
|
local dot11 = vector.dot(v1, v1)
|
||||||
|
local dot12 = vector.dot(v1, v2)
|
||||||
|
|
||||||
|
local invDenom = 1 / (dot00 * dot11 - dot01 * dot01)
|
||||||
|
local u = (dot11 * dot02 - dot01 * dot12) * invDenom
|
||||||
|
local v = (dot00 * dot12 - dot01 * dot02) * invDenom
|
||||||
|
|
||||||
|
return (u >= 0) and (v >= 0) and (u + v <= 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Returns the `MapGen.Triangle` at the coordinates. If there is no triangle at these coordinates, it returns `nil`.
|
||||||
|
---
|
||||||
|
---**Important:** works only in the XZ plane.
|
||||||
|
---@param layer string|MapGen.Layer The layer or name of the layer the triangle is in.
|
||||||
|
---@param category MapGen.Layer.Category The category in which the triangle is in.
|
||||||
|
---@param pos Position Coordinate of the desired peak.
|
||||||
|
---@return MapGen.Triangle?
|
||||||
|
function MapGen:getTriangleByPos2D(layer, category, pos)
|
||||||
|
if type(layer) == 'string' then
|
||||||
|
local layerName = layer
|
||||||
|
layer = self.layersByName[layerName]
|
||||||
|
|
||||||
|
assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName))
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, triangle in ipairs(layer.trianglesList[category]) do
|
||||||
|
if pointInTriangle2D(pos.x, pos.z, triangle) then
|
||||||
|
return triangle
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- --- REGISTRATION METHODS ---
|
-- --- REGISTRATION METHODS ---
|
||||||
@@ -521,34 +571,6 @@ local function calcWeight(height, x, y, z)
|
|||||||
return 1 - vector.distance(pos2, posP) / height:length()
|
return 1 - vector.distance(pos2, posP) / height:length()
|
||||||
end
|
end
|
||||||
|
|
||||||
---Returns `true` if the point is within the triangle's region.
|
|
||||||
---The barycentric coordinate method is used.
|
|
||||||
---@param px integer
|
|
||||||
---@param pz integer
|
|
||||||
---@param triangle MapGen.Triangle
|
|
||||||
local function pointInTriangle(px, pz, triangle)
|
|
||||||
local posP = vector.new(px, 0, pz)
|
|
||||||
local pos1 = triangle.p1.getPeakPos()
|
|
||||||
local pos2 = triangle.p2.getPeakPos()
|
|
||||||
local pos3 = triangle.p3.getPeakPos()
|
|
||||||
|
|
||||||
local v0 = pos3 - pos1
|
|
||||||
local v1 = pos2 - pos1
|
|
||||||
local v2 = posP - pos1
|
|
||||||
|
|
||||||
local dot00 = vector.dot(v0, v0)
|
|
||||||
local dot01 = vector.dot(v0, v1)
|
|
||||||
local dot02 = vector.dot(v0, v2)
|
|
||||||
local dot11 = vector.dot(v1, v1)
|
|
||||||
local dot12 = vector.dot(v1, v2)
|
|
||||||
|
|
||||||
local invDenom = 1 / (dot00 * dot11 - dot01 * dot01)
|
|
||||||
local u = (dot11 * dot02 - dot01 * dot12) * invDenom
|
|
||||||
local v = (dot00 * dot12 - dot01 * dot02) * invDenom
|
|
||||||
|
|
||||||
return (u >= 0) and (v >= 0) and (u + v <= 1)
|
|
||||||
end
|
|
||||||
|
|
||||||
---Returns the weighted values of 2d noise.
|
---Returns the weighted values of 2d noise.
|
||||||
---@param category MapGen.Layer.Category
|
---@param category MapGen.Layer.Category
|
||||||
---@param layer MapGen.Layer
|
---@param layer MapGen.Layer
|
||||||
@@ -626,7 +648,7 @@ function MapGen:getNoises2dValue(category, layer, x, y, z)
|
|||||||
|
|
||||||
---@param t MapGen.Triangle
|
---@param t MapGen.Triangle
|
||||||
for _, t in ipairs(triangles) do
|
for _, t in ipairs(triangles) do
|
||||||
if pointInTriangle(x, z, t) then
|
if pointInTriangle2D(x, z, t) then
|
||||||
noiseValue = calcNoise2dValue(category, layer, t, x, y, z)
|
noiseValue = calcNoise2dValue(category, layer, t, x, y, z)
|
||||||
|
|
||||||
pastCalc.x = x
|
pastCalc.x = x
|
||||||
@@ -640,7 +662,7 @@ function MapGen:getNoises2dValue(category, layer, x, y, z)
|
|||||||
|
|
||||||
---@param triangle MapGen.Triangle
|
---@param triangle MapGen.Triangle
|
||||||
for _, triangle in ipairs(layer.trianglesList[category]) do
|
for _, triangle in ipairs(layer.trianglesList[category]) do
|
||||||
if pointInTriangle(x, z, triangle) then
|
if pointInTriangle2D(x, z, triangle) then
|
||||||
local noiseValue = calcNoise2dValue(category, layer, triangle, x, y, z)
|
local noiseValue = calcNoise2dValue(category, layer, triangle, x, y, z)
|
||||||
|
|
||||||
pastCalc.triangle = triangle
|
pastCalc.triangle = triangle
|
||||||
|
|||||||
Reference in New Issue
Block a user