I’m new to shaders (using @Ignatz ever-excellent articles as a jump off point) and am trying to do a simple difference between two images. Here is the code, but I get an odd effect where the top image seems to switch between the current and an older (stale) image. I suspect this may be down to the CAMERA source rather than the shader but any pointers would be appreciated (including if you can replicate the issue). There also appears to be issues with initializing the camera but think I’ve got a workaround included.
To use tap on the screen to update one of the two pictures. The picture which updates switches between the top and bottom with each tap.
-- Camera Compare
-- Use this function to perform your initial setup
function setup()
cameraSource(CAMERA_FRONT)
--without the following line the program fails
snap=image(CAMERA)
frame={}
frame[1]=image("Cargo Bot:Codea Icon") --dummy initial value - if you set to CAMERA you get an error
frame[2]=image("Cargo Bot:Clear Button")
f=2
m=mesh()
m.shader=shader(DiffShader.vertexShader, DiffShader.fragmentShader)
--allow user to set threshold level
m.shader.thresh=0.2
m.shader.texture=frame[1] --forehround
m.shader.texture2=frame[2]--background
u=m:addRect(3*WIDTH/4,HEIGHT/2,WIDTH/4,HEIGHT/4)
m:setRectTex(u,0,0,1,1)
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(44, 44, 117, 255)
sprite(frame[1],WIDTH/4,3*HEIGHT/4,WIDTH/4, HEIGHT/4)
sprite(frame[2],WIDTH/4,2*HEIGHT/4,WIDTH/4, HEIGHT/4)
m.shader.texture=frame[1]
m.shader.texture2=frame[2]
m:draw()
end
function touched(t)
if t.state==ENDED then
takephoto(f)
f=f+1
if f>2 then f=1 end
end
end
function takephoto(fin)
frame[fin]=image(CAMERA)
end
--Difference shader - compare the rgb elements of two images and display the differences
DiffShader = {
vertexShader = [[
//
// 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;
}
]],
fragmentShader = [[
//
// A basic fragment shader
//
//Default precision qualifier
precision highp float;
//This represents the current texture on the mesh
uniform lowp sampler2D texture;
uniform lowp sampler2D texture2;
//The interpolated vertex color for this fragment
varying lowp vec4 vColor;
//The interpolated texture coordinate for this fragment
varying highp vec2 vTexCoord;
uniform lowp float thresh;
void main()
{
//Sample the texture at the interpolated coordinate
lowp vec4 col1 = texture2D( texture, vTexCoord );
lowp vec4 col2 = texture2D( texture2, vTexCoord );
if (abs(col2.r-col1.r)<thresh && abs(col2.g-col1.g)<thresh && abs(col2.b-col1.b)<thresh) discard; else gl_FragColor = col2;
}
]]}