Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Main
- -- Chroma Keying
- function setup()
- displayMode(OVERLAY)
- parameter.color("bg", color(0))
- parameter.boolean("cameraFront", false, function(cam)
- if cam then
- cameraSource(CAMERA_FRONT)
- else
- cameraSource(CAMERA_BACK)
- end
- end)
- parameter.number("minDistance", 0, 3, 0)
- parameter.number("maxDistance", 0, 3, 0.25)
- prevMin = minDistance
- prevMax = maxDistance
- m = mesh()
- rIdx = m:addRect(WIDTH / 2, HEIGHT / 2, WIDTH, HEIGHT)
- m.texture = CAMERA
- m.shader = shader(Shaders.ChromaKey.vS, Shaders.ChromaKey.fS)
- m.shader.minDistance = minDistance
- m.shader.maxDistance = maxDistance
- m.shader.key = color(0)
- end
- function draw()
- background(bg)
- if minDistance ~= prevMin then
- m.shader.minDistance = minDistance
- prevMin = minDistance
- end
- if maxDistance ~= prevMax then
- m.shader.maxDistance = maxDistance
- prevMax = maxDistance
- end
- m:draw()
- end
- function touched(touch)
- if touch.state == BEGAN then
- img = image(CAMERA)
- local x, y = math.floor(touch.x / WIDTH * img.width + 0.5), math.floor(touch.y / HEIGHT * img.height + 0.5)
- local r, g, b = img:get(x, y)
- m.shader.key = color(r, g, b)
- end
- end
- --# Shaders
- Shaders = {
- --shaderstart:ChromaKey
- ChromaKey = {
- vS = [[
- //
- // 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;
- }
- ]],
- fS = [[
- //
- // A basic fragment shader
- //
- //Default precision qualifier
- precision highp float;
- //This represents the current texture on the mesh
- uniform lowp sampler2D texture;
- uniform vec4 key;
- uniform float minDistance;
- uniform float maxDistance;
- //The interpolated vertex color for this fragment
- varying lowp vec4 vColor;
- //The interpolated texture coordinate for this fragment
- varying highp vec2 vTexCoord;
- void main()
- {
- lowp vec4 col = texture2D(texture, vTexCoord);
- vec4 delta = col - key;
- float distance2 = dot(delta, delta);
- float weight = clamp((distance2 - minDistance) / (maxDistance - minDistance), 0.0, 1.0);
- //Set the output color to the texture color
- gl_FragColor = col * weight * vColor;
- }
- ]],
- },
- --shaderend:ChromaKey
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement