Adding the shader examples

This commit is contained in:
Casey Reas
2012-08-31 17:13:27 +00:00
parent 8b1e862c35
commit 86d96c363d
25 changed files with 1128 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
/**
* Image Mask
*
* Move the mouse to reveal the image through the dynamic mask.
*/
PShader maskShader;
PImage srcImage;
PGraphics maskImage;
void setup() {
size(640, 360, P2D);
srcImage = loadImage("leaves.jpg");
maskImage = createGraphics(srcImage.width, srcImage.height, P2D);
maskImage.noSmooth();
maskShader = loadShader("mask.glsl");
maskShader.set("maskSampler", maskImage);
background(0);
}
void draw() {
maskImage.beginDraw();
maskImage.background(0);
if (mouseX != 0 && mouseY != 0) {
maskImage.noStroke();
maskImage.fill(255, 0, 0);
maskImage.ellipse(mouseX, mouseY, 50, 50);
}
maskImage.endDraw();
shader(maskShader);
image(srcImage, 0, 0, width, height);
}

View File

@@ -0,0 +1,12 @@
uniform sampler2D textureSampler;
uniform sampler2D maskSampler;
uniform vec2 texcoordOffset;
varying vec4 vertColor;
varying vec4 vertTexcoord;
void main() {
vec4 texColor = texture2D(textureSampler, vertTexcoord.st).rgba;
vec4 maskColor = texture2D(maskSampler, vec2(vertTexcoord.s, 1.0 - vertTexcoord.t)).rgba;
gl_FragColor = mix(texColor, vec4(0, 0, 0, 0), 1.0 - maskColor.r);
}