Advertisement
SkyTheCoder

Chroma Keying

Aug 13th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.85 KB | None | 0 0
  1.  
  2. --# Main
  3. -- Chroma Keying
  4.  
  5. function setup()
  6.     displayMode(OVERLAY)
  7.     parameter.color("bg", color(0))
  8.     parameter.boolean("cameraFront", false, function(cam)
  9.         if cam then
  10.             cameraSource(CAMERA_FRONT)
  11.         else
  12.             cameraSource(CAMERA_BACK)
  13.         end
  14.     end)
  15.     parameter.number("minDistance", 0, 3, 0)
  16.     parameter.number("maxDistance", 0, 3, 0.25)
  17.     prevMin = minDistance
  18.     prevMax = maxDistance
  19.     m = mesh()
  20.     rIdx = m:addRect(WIDTH / 2, HEIGHT / 2, WIDTH, HEIGHT)
  21.     m.texture = CAMERA
  22.     m.shader = shader(Shaders.ChromaKey.vS, Shaders.ChromaKey.fS)
  23.     m.shader.minDistance = minDistance
  24.     m.shader.maxDistance = maxDistance
  25.     m.shader.key = color(0)
  26. end
  27.  
  28. function draw()
  29.     background(bg)
  30.     if minDistance ~= prevMin then
  31.         m.shader.minDistance = minDistance
  32.         prevMin = minDistance
  33.     end
  34.     if maxDistance ~= prevMax then
  35.         m.shader.maxDistance = maxDistance
  36.         prevMax = maxDistance
  37.     end
  38.     m:draw()
  39. end
  40.  
  41. function touched(touch)
  42.     if touch.state == BEGAN then
  43.         img = image(CAMERA)
  44.         local x, y = math.floor(touch.x / WIDTH * img.width + 0.5), math.floor(touch.y / HEIGHT * img.height + 0.5)
  45.         local r, g, b = img:get(x, y)
  46.         m.shader.key = color(r, g, b)
  47.     end
  48. end
  49.  
  50. --# Shaders
  51. Shaders = {
  52. --shaderstart:ChromaKey
  53. ChromaKey = {
  54. vS = [[
  55. //
  56. // A basic vertex shader
  57. //
  58.  
  59. //This is the current model * view * projection matrix
  60. // Codea sets it automatically
  61. uniform mat4 modelViewProjection;
  62.  
  63. //This is the current mesh vertex position, color and tex coord
  64. // Set automatically
  65. attribute vec4 position;
  66. attribute vec4 color;
  67. attribute vec2 texCoord;
  68.  
  69. //This is an output variable that will be passed to the fragment shader
  70. varying lowp vec4 vColor;
  71. varying highp vec2 vTexCoord;
  72.  
  73. void main()
  74. {
  75.     //Pass the mesh color to the fragment shader
  76.     vColor = color;
  77.     vTexCoord = texCoord;
  78.    
  79.     //Multiply the vertex position by our combined transform
  80.     gl_Position = modelViewProjection * position;
  81. }
  82. ]],
  83. fS = [[
  84. //
  85. // A basic fragment shader
  86. //
  87.  
  88. //Default precision qualifier
  89. precision highp float;
  90.  
  91. //This represents the current texture on the mesh
  92. uniform lowp sampler2D texture;
  93.  
  94. uniform vec4 key;
  95.  
  96. uniform float minDistance;
  97.  
  98. uniform float maxDistance;
  99.  
  100. //The interpolated vertex color for this fragment
  101. varying lowp vec4 vColor;
  102.  
  103. //The interpolated texture coordinate for this fragment
  104. varying highp vec2 vTexCoord;
  105.  
  106. void main()
  107. {
  108.     lowp vec4 col = texture2D(texture, vTexCoord);
  109.    
  110.     vec4 delta = col - key;
  111.     float distance2 = dot(delta, delta);
  112.     float weight = clamp((distance2 - minDistance) / (maxDistance - minDistance), 0.0, 1.0);
  113.  
  114.     //Set the output color to the texture color
  115.     gl_FragColor = col * weight * vColor;
  116. }
  117. ]],
  118. },
  119. --shaderend:ChromaKey
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement