Hi,
I’ve been trying to get my head around meshes, but really struggling. I’ve used the indexed-mode example from the docs (mesh1, below) and added circles to move the vertices. It works as expected, but when I try with different vertices (mesh2), it doesn’t create the square mesh composed of two triangles that I’m expecting. Could anyone explain what I’m doing wrong and just generally explain what happens with both the vertices table and the indices table? I’m not clear at all on how to do anything different from the given examples.
I’m also unsure as to the values that go into the vertices table. At first I thought values of 0/100 just arbitrarily map to the left/top and right/bottom of the image, but when I halved those numbers, it produced the same mesh, just half the size. Using a non-zero minimum number was different still, with my circles not appearing at the vertices, which brings me to the next question. How do I reliably convert vertex coordinates to content coordinates?
Sorry if this is all a bit rambling but I’m just a bit lost. Thanks for your time.
local mesh1 = display.newMesh(
{
x = 50,
y = 100,
mode = "indexed",
vertices = {
0,0, 0,100, 50,0, 100,100, 100,0
},
indices = {
1,2,3,
2,3,4,
3,4,5
}
})
mesh1:translate( mesh1.path:getVertexOffset() )
mesh1.fill = { type="image", filename="Icon.png" }
local mesh2 = display.newMesh(
{
x = 200,
y = 100,
mode = "indexed",
vertices = {
0,0, 0,100, 100,0, 100,0
},
indices = {
1,2,3,
2,3,4,
}
})
mesh2:translate( mesh2.path:getVertexOffset() )
mesh2.fill = { type="image", filename="Icon.png" }
local vertexStartX, vertexStartY, vertexX, vertexY
local function vertexTouch(e)
if (e.phase ~= "began") and not startX then return true
end
local v, mesh = e.target, e.target.p
local x, y = mesh:contentToLocal(e.x, e.y)
v.x, v.y = e.x, e.y
x, y = x + mesh.width*0.5, y + mesh.height*0.5
mesh.path:setVertex(v.id, x, y)
if e.phase == "began" then
display.currentStage:setFocus(v)
startX, startY, vertexStartX, vertexStartY = mesh:contentToLocal(e.x, e.y), mesh.path:getVertex(v.id)
elseif e.phase == "moved" then
vertexX, vertexY = mesh:contentToLocal(e.x, e.y)
mesh.path:setVertex(v.id, vertexX + mesh.width*0.5, vertexY + mesh.height*0.5)
elseif e.phase == "ended" or e.phase == "cancelled" then
display.currentStage:setFocus(nil)
startX = nil
end
return true
end
mesh1.vertexDots = {}
mesh2.vertexDots = {}
local i = 1
while mesh1.path:getVertex(i) do
local x, y = mesh1.path:getVertex(i)
x, y = mesh1:localToContent(x, y)
x = x - mesh1.width*0.5
y = y - mesh1.height*0.5
mesh1.vertexDots[#mesh1.vertexDots+1] = display.newCircle(x, y, 3)
local v = mesh1.vertexDots[#mesh1.vertexDots]
v:addEventListener("touch", vertexTouch)
v.p = mesh1
v.id = i
i = i + 1
end
i = 1
while mesh2.path:getVertex(i) do
local x, y = mesh2.path:getVertex(i)
x, y = mesh2:localToContent(x, y)
x = x - mesh2.width*0.5
y = y - mesh2.height*0.5
mesh2.vertexDots[#mesh2.vertexDots+1] = display.newCircle(x, y, 3)
local v = mesh2.vertexDots[#mesh2.vertexDots]
v:addEventListener("touch", vertexTouch)
v.p = mesh2
v.id = i
i = i + 1
end