Here’s an example that uses a shader to create a wire frame from the triangles in a mesh. One restriction is the triangle vertices have to be red, green, and blue. Tap the screen to switch between the colored meshes and it’s wire frame.
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)
function setup()
img=image(WIDTH,HEIGHT)
-- this mesh creates colored boxes
m=mesh()
m.vertices={vec2(10,100),vec2(WIDTH-10,100),vec2((WIDTH-20)/2,500)}
m:addRect(200,450,200,200,45)
m:addRect(800,400,100,200,32)
m:addRect(800,600,50,200,68)
m:addRect(600,600,200,100,13)
m:addRect(300,650,200,100,70)
m:setColors(255,255,255,255)
m:color(1,255,0,0)
m:color(2,0,255,0)
m:color(3,0,0,255)
for z=1,10 do
m:color(z*3+1,255,0,0,255)
m:color(z*3+2,0,0,255,255)
m:color(z*3+3,0,255,0,255)
end
setContext(img)
m:draw() -- create img with 5 boxes
setContext()
-- this mesh uses img created from above for the wire frame
m=mesh()
m:addRect(WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
m.texture=img
end
function draw()
background(0)
m:draw()
fill(255)
if m.shader==nil then
text("Tap screen for wire frame",WIDTH/2,HEIGHT-50)
else
text("Tap screen for color fill",WIDTH/2,HEIGHT-50)
end
end
function touched(t) -- flip between colored boxes or wire frame
if t.state==BEGAN then
if m.shader==nil then
m.shader=shader(vShader,fShader)
else
m.shader=nil
end
end
end
-- shader to create wire frames
vShader = [[
uniform mat4 modelViewProjection;
attribute vec4 position;
attribute vec2 texCoord;
varying highp vec2 vTexCoord;
void main()
{ vTexCoord = texCoord;
gl_Position = modelViewProjection * position;
} ]]
fShader = [[
uniform lowp sampler2D texture;
varying highp vec2 vTexCoord;
void main()
{ lowp vec4 col = texture2D( texture, vTexCoord);
if (col.r<.01 && col.g>0.0 && col.b>0.0 ||
col.g<.01 && col.r>0.0 && col.b>0.0 ||
col.b<.01 && col.r>0.0 && col.g>0.0)
{ col.r=0.0; col.g=1.0; col.b=0.0; }
else discard;
gl_FragColor=col;
} ]]