2D Mesh example

I’ve been learning Meshes and thought I’d share my code to learn it. Heavily based on Vega’s post here:
http://codea.io/talk/discussion/1244/mesh-tutorial/p1


--# Main
function setup()
    -- select the mesh type
    parameter.integer("MeshType", 0, 5, 0, function(MeshType) print("Mesh "..MeshType) end )
    parameter.number("MeshSize",2,1000,200) --set the mesh size
    parameter.action("Apply resize",ResizeMeshes) --button to reload the mesh data
    SimpleMesh() --load the first mesh type
    TexturedMesh1() --load the second mesh type
    TexturedMesh2() --load the third mesh type
    RectMesh() --load fourth mesh
    TestMesh() --this is a fifth mesh
    
    Halfwidth=WIDTH/2
    Halfheight=HEIGHT/2
end

function ResizeMeshes() --reload all meshes to redefine sizes
    SimpleMesh() --load the first mesh type
    TexturedMesh1() --load the second mesh type
    TexturedMesh2() --load the third mesh type
    RectMesh() -- load fourth mesh
    TestMesh() --this is a fifth mesh
end

function draw()
    background(40, 40, 50)
    if MeshType == 0 then
    elseif MeshType == 1 then
        MyMesh1: draw()
    elseif MeshType == 2 then
        MyMesh2: draw()
    elseif MeshType == 3 then
        MyMesh3: draw()
    elseif MeshType == 4 then
        MyMesh4: draw()
    elseif MeshType == 5 then
        MyTestMesh: draw()
    end
end

function SimpleMesh()
    red = color(255, 0, 0, 255)
    green = color(0, 255, 0, 255)
    blue = color(0, 0, 255, 255)
    
    MyMesh1 = mesh()
    MyMesh1.vertices = {vec2(0,0),vec2(MeshSize,0),vec2(0,MeshSize)}
    MyMesh1:setColors(255, 255, 255, 255)
    MyMesh1.colors = {red, green, blue}
end

function TexturedMesh1()
    Tex1Mesh = readImage("Planet Cute:Icon") --Get the image

    MyMesh2 = mesh()
    MyMesh2.vertices = {vec2(0,0),vec2(MeshSize,0),vec2(0,MeshSize)}
    MyMesh2:setColors(255, 255, 255, 255)
    MyMesh2.texture = Tex1Mesh --Set the image as texture
    MyMesh2.texCoords = {vec2(0,0),vec2(1,0),vec2(0,1)}
end

function TexturedMesh2()
    Tex2Mesh = readImage("Planet Cute:Icon") --Get the image

    MyMesh3 = mesh()
    MyMesh3.vertices = {vec2(0,0),vec2(MeshSize,0),vec2(0,MeshSize),vec2(0,MeshSize),vec2(MeshSize,MeshSize),vec2(MeshSize,0)}
    MyMesh3:setColors(255, 255, 255, 255)
    MyMesh3.texture = Tex2Mesh --Set the image as texture
    MyMesh3.texCoords = {vec2(0,0),vec2(1,0),vec2(0,1),vec2(0,1),vec2(1,1),vec2(1,0)}
end

function RectMesh()
    Tex3Mesh = readImage("Planet Cute:Icon") 
    MyMesh4 = mesh()
    MyMesh4.texture = Tex3Mesh 
    MyMesh4:addRect(MeshSize/2,MeshSize/2,MeshSize,MeshSize)

end

function TestMesh()
    CurCol = false
    MyTestMesh = mesh()
    MeshTab={}
    MeshColTab={}
    for ycount=0, HEIGHT, MeshSize do
    for xcount=0, WIDTH, MeshSize do     
        if CurCol == true then
                table.insert(MeshTab,vec2(xcount,ycount))
                table.insert(MeshTab,vec2(xcount+MeshSize,ycount+MeshSize))
                table.insert(MeshTab,vec2(xcount+MeshSize,ycount))
                
                table.insert(MeshColTab,color(255, 255, 255, 255))
                table.insert(MeshColTab,color(255, 255, 255, 255))
                table.insert(MeshColTab,color(255, 255, 255, 255))
                
                table.insert(MeshTab,vec2(xcount,ycount+MeshSize))
                table.insert(MeshTab,vec2(xcount+MeshSize,ycount+MeshSize))
                table.insert(MeshTab,vec2(xcount,ycount))
                
                table.insert(MeshColTab,color(255,0,0,255))
                table.insert(MeshColTab,color(255,0,0,255))
                table.insert(MeshColTab,color(255,0,0,255))
                CurCol = false
            else
                table.insert(MeshTab,vec2(xcount,ycount+MeshSize))
                table.insert(MeshTab,vec2(xcount+MeshSize,ycount+MeshSize))
                table.insert(MeshTab,vec2(xcount,ycount))
                
                table.insert(MeshColTab,color(255,0,0,255))
                table.insert(MeshColTab,color(255,0,0,255))
                table.insert(MeshColTab,color(255,0,0,255))
                
                table.insert(MeshTab,vec2(xcount,ycount))
                table.insert(MeshTab,vec2(xcount+MeshSize,ycount+MeshSize))
                table.insert(MeshTab,vec2(xcount+MeshSize,ycount))
                
                table.insert(MeshColTab,color(255, 255, 255, 255))
                table.insert(MeshColTab,color(255, 255, 255, 255))
                table.insert(MeshColTab,color(255, 255, 255, 255))
                CurCol = true
            end
    end
    end
    MyTestMesh.vertices = MeshTab
    MyTestMesh.colors = MeshColTab
    
end

The last one will take a while to load if you reduce mesh size to 2, crashend on my ipad when set to1 :stuck_out_tongue: so changed min size to 2.