Skip to content

Commit ef64da2

Browse files
authored
Merge pull request #1246 from aceslowman/feedbackExample
Created basic WebGL feedback example
2 parents 87bfc58 + 636597b commit ef64da2

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @name Simple Feedback
3+
* @arialabel An example of a simple feedback effect using two buffers.
4+
* @description A simple feedback effect that is achieved by using two WebGL graphics buffers.
5+
* This effect works by drawing the previous frame to a second
6+
* createGraphics() buffer, which can be blended with the current frame. This
7+
* takes advantage of texture mapping in WebGL.
8+
*/
9+
10+
let pg, swap;
11+
12+
function setup() {
13+
createCanvas(710, 400);
14+
15+
// this will hold the previous frame
16+
pg = createGraphics(710, 400, WEBGL);
17+
// this will hold our main graphic
18+
swap = createGraphics(710, 400, WEBGL);
19+
20+
describe(
21+
'a slowly oscillating, radiating white sphere that fades into a dark gray background through a feedback visual effect'
22+
);
23+
}
24+
25+
function draw() {
26+
// clears and resets the p5.Graphics so that 3D objects draw correctly
27+
pg.reset();
28+
29+
// draw the previous frame
30+
pg.texture(swap);
31+
pg.noStroke();
32+
pg.plane(width, height);
33+
34+
// draw our sphere on top
35+
pg.push();
36+
// slowly move the sphere in a circle
37+
pg.translate(sin(millis() / 200) * 5, cos(millis() / 200) * 5, 0);
38+
pg.fill(255);
39+
pg.sphere(90);
40+
pg.pop();
41+
42+
// draw a slightly scaled up copy of the texture
43+
swap.push();
44+
swap.scale(1.1, 1.1);
45+
swap.texture(pg);
46+
swap.noStroke();
47+
swap.plane(width, height);
48+
swap.pop();
49+
50+
// an opaque rectangle is drawn over top to control the feedback decay
51+
swap.fill(0, 50);
52+
swap.rect(-width / 2, -height / 2, width, height);
53+
54+
// draw the output to the screen
55+
image(swap, 0, 0);
56+
}

0 commit comments

Comments
 (0)