Probably not of any use to you, but was fun to do:
function setup()
displayMode(FULLSCREEN)
w,h = WIDTH,HEIGHT
img = image(w,h)
imga = image(w,h)
setContext(img)
noSmooth()
background(255, 255, 255, 255)
local x,y,rw,rh,c
local colours = {
color(255, 0, 0, 255),
color(0, 255, 0, 255),
color(0,0,255,255),
color(0,255,255,255),
color(255,0,255,255),
color(255,255,0,255),
color(0,127,255,255),
color(255,0,127,255),
color(127,255,0,255)
}
for n=1,100 do
x = math.random(1,w)
y = math.random(1,h)
rw = math.random(10,100)
rh = math.random(10,100)
c = math.random(1,9)
fill(colours[c])
rect(x,y,rw,rh)
end
setContext()
m = mesh()
m.texture = img
m:addRect(w/2,h/2,w,h)
m.shader = shader(FloodShader())
m.shader.width = w*2
m.shader.height = h*2
m.shader.fillFrom = color(0, 0, 0, 0)
m.shader.fillTo = color(115, 123, 50, 255)
end
function draw()
background(0, 0, 0, 255)
noSmooth()
setContext(imga)
background(0, 0, 0, 255)
if pt then
blendMode(ONE,ZERO,ZERO,ONE)
else
blendMode(ONE,ZERO,ONE,ZERO)
end
m:draw()
setContext()
img,imga = imga,img
m.texture = img
spriteMode(CORNER)
sprite(img,(WIDTH-w)/2,(HEIGHT-h)/2)
if pt then
local x = math.floor(pt.x - (WIDTH-w)/2)
local y = math.floor(pt.y - (HEIGHT-h)/2)
local r,g,b,a = img:get(x,y)
m.shader.fillFrom = color(r,g,b,a)
img:set(x,y,r,g,b,0)
pt = false
end
end
function touched(t)
if t.state == ENDED then
pt = vec2(t.x,t.y)
end
end
function FloodShader()
return [[
//
// A basic vertex shader
//
//This is the current model * view * projection matrix
// Codea sets it automatically
uniform mat4 modelViewProjection;
//This is the current mesh vertex position, color and tex coord
// Set automatically
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
//This is an output variable that will be passed to the fragment shader
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
void main()
{
//Pass the mesh color to the fragment shader
vColor = color;
vTexCoord = texCoord;
//Multiply the vertex position by our combined transform
gl_Position = modelViewProjection * position;
}
]],[[
//
// A basic fragment shader
//
//Default precision qualifier
precision highp float;
//This represents the current texture on the mesh
uniform lowp sampler2D texture;
uniform float width;
uniform float height;
float rw = 1./width;
float rh = 1./height;
uniform lowp vec4 fillFrom;
lowp vec4 fillFromA = vec4(fillFrom.xyz,0.);
uniform lowp vec4 fillTo;
//The interpolated vertex color for this fragment
varying lowp vec4 vColor;
//The interpolated texture coordinate for this fragment
varying highp vec2 vTexCoord;
void main()
{
//Sample the texture at the interpolated coordinate
lowp vec4 col = texture2D( texture, vTexCoord );
lowp float a;
if (col == fillFromA) {col = fillTo;} else {
if (col == fillFrom) {
a = texture2D(texture, vTexCoord + vec2(rw,0.)).a;
if (a < .5) {
col.a = 0.;
} else {
a = texture2D(texture, vTexCoord + vec2(-rw,0.)).a;
if (a < .5) {
col.a = 0.;
} else {
a = texture2D(texture, vTexCoord + vec2(0.,rh)).a;
if (a < .5) {
col.a = 0.;
} else {
a = texture2D(texture, vTexCoord + vec2(0.,-rh)).a;
if (a < .5) {
col.a = 0.;
}
}
}
}
}
}
//Set the output color to the texture color
gl_FragColor = col;
}
]]
end