Just started with Codea and began exploring meshes. I am terrible at this. So much that I did read a very nice tutorial that helped get me started, but unfortunately I still am unable to get some things to work. Example: I am attempting to write a function that ads a triangle to an existing mesh. Tried a few things and can’t seem to get it to work. So I’m swallowing my pride and asking the awesome Codea community for help.
just use myMesh:addRect(x,y,w,h) or myMesh:addRect(x,y,w,h,r) where r is rotation.
But the object I am trying to create is not a rectangular shape.
then do it this way: this will create triange for example
myMesh = mesh()
myMesh.vertices = {vec2(0,0),vec2(100,0),vec2(0,100)}
To add one or more triangles to an existing mesh, I think you need to have an array (table) of the existing vertices, add the new vertices to that array and then link (by assignment) the vertices
property of the mesh userdata to that extended table. The same with any associated vertex colours and texture coordinates.
Tell me why this doesn’t work, please. Actually, it works on the first triangle but when I add a second it gives me an error.
function MeshAddTri(m, t)
local vx = m.vertices
table.insert(vx, t[1])
table.insert(vx, t[2])
table.insert(vx, t[3])
m:clear()
m.vertices = vx
return m
end
I fixed it. Just had to use a vec3 instead of vec2. No idea why.
With meshes you should, from my experience
1- pass a whole table to ms.vertice, ms.color, ms.texcoords rather that editing the table (like insert). The problem is that these tables are modifyied when used by the mesh, and directly setting the values will not work for colors and texCoords.
2- once the table exist you can modify individual values with almost no performance loss by using the built in functions: setTexCoords, setColor etc… This take care of the difference between mesh memory vs what you passed to it, so you dont get bad surprise.
Good luck.
Hello @metzyn. I think the answer to your last comment is this:
All meshes are actually three dimensional, (x, y, z). When you create a mesh using vec2
, the assumption made is that you are creating a mesh with vertices in the x-y plane where z is zero.
I assume that myMesh.vertices
returns a table (an array) of vec3
(x, y, z) values. If you then insert vec2
(x, y) data on the end of that array, you have a ‘mixed’ array of vec3
and vec2
data. I also assume that Codea complains if you try to populate myMesh.vertices
from a mixed array.
(Update) Something to bear in mind is that, despite its syntax, myMesh.vertices = myVertexTable
does not cause myMesh.vertices
to contain a reference to the Lua table referred to by myVertexTable
. What is actually happening is more akin to the function syntax myMesh:setVertices(myVertexTable)
. Similarly, myVertexTable = myMesh.vertices
is more akin to the syntax myVertexTable = myMesh:getVertices()
.