Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
-- StencilMask
-- Use this function to perform your initial setup
function setup()
parameter.boolean("Negative",false)
img=readImage("Cargo Bot:Starry Background")
stencilImg=readImage("Planet Cute:Character Princess Girl")
m=mesh()
u=m:addRect(0,0,img.width,img.height)
m:setRectTex(u,0,0,1,1)
m.texture=img
m.shader = shader(stencilShader.vertexShader, stencilShader.fragmentShader)
m.shader.texture2=stencilImg
m.shader.negative=Negative
end
function draw()
background(200)
pushMatrix()
translate(300,300)
m.shader.negative=Negative
m:draw()
popMatrix()
end
stencilShader = {
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;
uniform bool negative;
//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 col1 = texture2D( texture, vTexCoord );
lowp vec4 col2 = texture2D( texture2, vTexCoord );
if (negative)
{if (col2.a>0.) gl_FragColor = col1; else discard;}
else if (col2.a==0.) gl_FragColor = col1; else discard;
}
]]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.