update to convert poly to mesh
This commit is contained in:
45
src/Util.lua
45
src/Util.lua
@@ -70,4 +70,47 @@ function print_s(segments)
|
||||
for k, s in ipairs(segments) do
|
||||
s:debug(tostring(k))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- convert a list of points forming a polygon {x1, y1, x2, y2, ...} into a mesh
|
||||
|
||||
function poly2mesh(points)
|
||||
-- remove duplicates???
|
||||
local polypts = love.math.triangulate(points)
|
||||
local tlist
|
||||
|
||||
local vnums = {}
|
||||
local vcoords = {}
|
||||
do
|
||||
local verthash = {}
|
||||
local n = 0
|
||||
local v
|
||||
-- use unique vertices by using a coordinate hash table
|
||||
for i = 1, #polypts do
|
||||
for j = 1, 3 do
|
||||
local px = polypts[i][j * 2 - 1]
|
||||
local py = polypts[i][j * 2]
|
||||
if not verthash[px] then verthash[px] = {} end
|
||||
if not verthash[px][py] then
|
||||
n = n + 1
|
||||
verthash[px][py] = n
|
||||
vcoords[n * 2 - 1] = px
|
||||
vcoords[n * 2] = py
|
||||
v = n
|
||||
else
|
||||
v = verthash[px][py]
|
||||
end
|
||||
vnums[(i - 1) * 3 + j] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
local mesh = love.graphics.newMesh(#vcoords, "triangles", "static")
|
||||
for i = 1, #vcoords / 2 do
|
||||
local x, y = vcoords[i * 2 - 1], vcoords[i * 2]
|
||||
|
||||
-- Here's where the UVs are assigned
|
||||
mesh:setVertex(i, x, y, x / 50, y / 50, .5, .5, .9, 1)
|
||||
end
|
||||
mesh:setVertexMap(vnums)
|
||||
return mesh
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user