mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
Added BlurFilter and EdgeFilter examples
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
PShader blur;
|
||||
boolean applyFilter = true;
|
||||
|
||||
void setup() {
|
||||
size(400, 400, P3D);
|
||||
blur = (PShader)loadShader("blur.glsl");
|
||||
noStroke();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
lights();
|
||||
|
||||
translate(width/2, height/2);
|
||||
pushMatrix();
|
||||
rotateX(frameCount * 0.01);
|
||||
rotateY(frameCount * 0.01);
|
||||
box(100);
|
||||
popMatrix();
|
||||
|
||||
if (applyFilter) filter(blur);
|
||||
|
||||
// The sphere doesn't have blur applied on it
|
||||
// because it is drawn after filter() is called.
|
||||
rotateY(frameCount * 0.02);
|
||||
translate(150, 0);
|
||||
sphere(40);
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
applyFilter = !applyFilter;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
uniform sampler2D textureSampler;
|
||||
uniform vec2 texcoordOffset;
|
||||
|
||||
varying vec4 vertColor;
|
||||
varying vec4 vertTexcoord;
|
||||
|
||||
#define KERNEL_SIZE 9
|
||||
|
||||
// Gaussian kernel
|
||||
// 1 2 1
|
||||
// 2 4 2
|
||||
// 1 2 1
|
||||
float kernel[KERNEL_SIZE];
|
||||
|
||||
vec2 offset[KERNEL_SIZE];
|
||||
|
||||
void main(void) {
|
||||
int i = 0;
|
||||
vec4 sum = vec4(0.0);
|
||||
|
||||
offset[0] = vec2(-texcoordOffset.s, -texcoordOffset.t);
|
||||
offset[1] = vec2(0.0, -texcoordOffset.t);
|
||||
offset[2] = vec2(texcoordOffset.s, -texcoordOffset.t);
|
||||
|
||||
offset[3] = vec2(-texcoordOffset.s, 0.0);
|
||||
offset[4] = vec2(0.0, 0.0);
|
||||
offset[5] = vec2(texcoordOffset.s, 0.0);
|
||||
|
||||
offset[6] = vec2(-texcoordOffset.s, texcoordOffset.t);
|
||||
offset[7] = vec2(0.0, texcoordOffset.t);
|
||||
offset[8] = vec2(texcoordOffset.s, texcoordOffset.t);
|
||||
|
||||
kernel[0] = 1.0/16.0; kernel[1] = 2.0/16.0; kernel[2] = 1.0/16.0;
|
||||
kernel[3] = 2.0/16.0; kernel[4] = 4.0/16.0; kernel[5] = 2.0/16.0;
|
||||
kernel[6] = 1.0/16.0; kernel[7] = 2.0/16.0; kernel[8] = 1.0/16.0;
|
||||
|
||||
for(i = 0; i < KERNEL_SIZE; i++) {
|
||||
vec4 tmp = texture2D(textureSampler, vertTexcoord.st + offset[i]);
|
||||
sum += tmp * kernel[i];
|
||||
}
|
||||
|
||||
gl_FragColor = vec4(sum.rgb, 1.0) * vertColor;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
PShader edges;
|
||||
|
||||
void setup() {
|
||||
size(400, 400, P2D);
|
||||
edges = (PShader)loadShader("edges.glsl");
|
||||
}
|
||||
|
||||
void draw() {
|
||||
rect(mouseX, mouseY, 50, 50);
|
||||
filter(edges);
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
uniform sampler2D textureSampler;
|
||||
uniform vec2 texcoordOffset;
|
||||
|
||||
varying vec4 vertColor;
|
||||
varying vec4 vertTexcoord;
|
||||
|
||||
#define KERNEL_SIZE 9
|
||||
|
||||
// Edge detection kernel
|
||||
// -1 -1 -1
|
||||
// -1 +8 -1
|
||||
// -1 -1 -1
|
||||
float kernel[KERNEL_SIZE];
|
||||
|
||||
vec2 offset[KERNEL_SIZE];
|
||||
|
||||
void main(void) {
|
||||
int i = 0;
|
||||
vec4 sum = vec4(0.0);
|
||||
|
||||
offset[0] = vec2(-texcoordOffset.s, -texcoordOffset.t);
|
||||
offset[1] = vec2(0.0, -texcoordOffset.t);
|
||||
offset[2] = vec2(texcoordOffset.s, -texcoordOffset.t);
|
||||
|
||||
offset[3] = vec2(-texcoordOffset.s, 0.0);
|
||||
offset[4] = vec2(0.0, 0.0);
|
||||
offset[5] = vec2(texcoordOffset.s, 0.0);
|
||||
|
||||
offset[6] = vec2(-texcoordOffset.s, texcoordOffset.t);
|
||||
offset[7] = vec2(0.0, texcoordOffset.t);
|
||||
offset[8] = vec2(texcoordOffset.s, texcoordOffset.t);
|
||||
|
||||
kernel[0] = -1.0; kernel[1] = -1.0; kernel[2] = -1.0;
|
||||
kernel[3] = -1.0; kernel[4] = 8.0; kernel[5] = -1.0;
|
||||
kernel[6] = -1.0; kernel[7] = -1.0; kernel[8] = -1.0;
|
||||
|
||||
for(i = 0; i < KERNEL_SIZE; i++) {
|
||||
vec4 tmp = texture2D(textureSampler, vertTexcoord.st + offset[i]);
|
||||
sum += tmp * kernel[i];
|
||||
}
|
||||
|
||||
gl_FragColor = vec4(sum.rgb, 1.0) * vertColor;
|
||||
}
|
||||
Reference in New Issue
Block a user