Skip to content

Commit 218f54f

Browse files
authored
Merge pull request #1371 from davepagurek/framebuffers
Add initial resources for p5.Framebuffer
2 parents 053042d + c49fee1 commit 218f54f

File tree

12 files changed

+907
-10
lines changed

12 files changed

+907
-10
lines changed

src/assets/js/vendor/prism.js

Lines changed: 13 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
265 KB
Loading
1.29 MB
Loading
605 KB
Loading

src/data/en.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,9 @@ learn:
887887
getting-started-in-webgl-shaders-glossary-term12-definition: >-
888888
The part of a shader program that is responsible for the color and appearance of each pixel output by the
889889
shader.
890+
getting-started-in-webgl-framebuffers-title: Layered Rendering with Framebuffers
891+
getting-started-in-webgl-framebuffers: >-
892+
Setting up sketches that draw in multiple stages or access 3D depth information.
890893
color-title: Color
891894
color: An introduction to digital color.
892895
coordinate-system-and-shapes-title: Coordinate System and Shapes
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* @name Blur using Framebuffer Depth
3+
* @frame 710,400
4+
* @arialabel A line of five spheres rotating in front of the camera, with ones too close and too far to the camera appearing blurred
5+
* @description A shader that uses depth information from a p5.Framebuffer to
6+
* draw a scene with focal blur.
7+
*/
8+
let layer;
9+
let blur;
10+
11+
function setup() {
12+
createCanvas(windowWidth, windowHeight, WEBGL);
13+
layer = createFramebuffer();
14+
blur = createShader(vert, frag);
15+
noStroke();
16+
}
17+
18+
function draw() {
19+
// Draw a scene
20+
layer.begin();
21+
background(255);
22+
ambientLight(100);
23+
directionalLight(255, 255, 255, -1, 1, -1);
24+
ambientMaterial(255, 0, 0);
25+
fill(255, 255, 100);
26+
specularMaterial(255);
27+
shininess(150);
28+
29+
rotateY(millis() * 0.001);
30+
for (let i = 0; i < 5; i++) {
31+
push();
32+
translate((i-2)*100, 0, 0);
33+
sphere();
34+
pop();
35+
}
36+
layer.end();
37+
38+
// Render the scene with depth of field blur
39+
shader(blur);
40+
blur.setUniform('img', layer.color);
41+
blur.setUniform('depth', layer.depth);
42+
rect(width, height);
43+
}
44+
45+
function windowResized() {
46+
resizeCanvas(windowWidth, windowHeight);
47+
}
48+
49+
let vert = `
50+
precision highp float;
51+
attribute vec3 aPosition;
52+
attribute vec2 aTexCoord;
53+
varying vec2 vTexCoord;
54+
void main() {
55+
vec4 positionVec4 = vec4(aPosition, 1.0);
56+
positionVec4.xy = positionVec4.xy * 2.0 - 1.0;
57+
positionVec4.y *= -1;
58+
gl_Position = positionVec4;
59+
vTexCoord = aTexCoord;
60+
}`;
61+
62+
let frag = `
63+
precision highp float;
64+
varying vec2 vTexCoord;
65+
uniform sampler2D img;
66+
uniform sampler2D depth;
67+
float getBlurriness(float d) {
68+
// Blur more the farther away we go from the
69+
// focal point at depth=0.9
70+
return abs(d - 0.9) * 40.;
71+
}
72+
float maxBlurDistance(float blurriness) {
73+
return blurriness * 0.01;
74+
}
75+
void main() {
76+
vec4 color = texture2D(img, vTexCoord);
77+
float samples = 1.;
78+
float centerDepth = texture2D(depth, vTexCoord).r;
79+
float blurriness = getBlurriness(centerDepth);
80+
for (int sample = 0; sample < 20; sample++) {
81+
// Sample nearby pixels in a spiral going out from the
82+
// current pixel
83+
float angle = float(sample);
84+
float distance = float(sample)/20.
85+
* maxBlurDistance(blurriness);
86+
vec2 offset = vec2(cos(angle), sin(angle)) * distance;
87+
88+
// How close is the object at the nearby pixel?
89+
float sampleDepth = texture2D(depth, vTexCoord + offset).r;
90+
91+
// How far should its blur reach?
92+
float sampleBlurDistance =
93+
maxBlurDistance(getBlurriness(sampleDepth));
94+
95+
// If it's in front of the current pixel, or its blur overlaps
96+
// with the current pixel, add its color to the average
97+
if (
98+
sampleDepth >= centerDepth ||
99+
sampleBlurDistance >= distance
100+
) {
101+
color += texture2D(img, vTexCoord + offset);
102+
samples++;
103+
}
104+
}
105+
color /= samples;
106+
gl_FragColor = color;
107+
}`;
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* @name Blur using Framebuffer Depth
3+
* @frame 710,400
4+
* @arialabel A line of five spheres rotating in front of the camera, with ones too close and too far to the camera appearing blurred
5+
* @description A shader that uses depth information from a p5.Framebuffer to
6+
* draw a scene with focal blur.
7+
*/
8+
let layer;
9+
let blur;
10+
11+
function setup() {
12+
createCanvas(windowWidth, windowHeight, WEBGL);
13+
layer = createFramebuffer();
14+
blur = createShader(vert, frag);
15+
noStroke();
16+
}
17+
18+
function draw() {
19+
// Draw a scene
20+
layer.begin();
21+
background(255);
22+
ambientLight(100);
23+
directionalLight(255, 255, 255, -1, 1, -1);
24+
ambientMaterial(255, 0, 0);
25+
fill(255, 255, 100);
26+
specularMaterial(255);
27+
shininess(150);
28+
29+
rotateY(millis() * 0.001);
30+
for (let i = 0; i < 5; i++) {
31+
push();
32+
translate((i-2)*100, 0, 0);
33+
sphere();
34+
pop();
35+
}
36+
layer.end();
37+
38+
// Render the scene with depth of field blur
39+
shader(blur);
40+
blur.setUniform('img', layer.color);
41+
blur.setUniform('depth', layer.depth);
42+
rect(width, height);
43+
}
44+
45+
function windowResized() {
46+
resizeCanvas(windowWidth, windowHeight);
47+
}
48+
49+
let vert = `
50+
precision highp float;
51+
attribute vec3 aPosition;
52+
attribute vec2 aTexCoord;
53+
varying vec2 vTexCoord;
54+
void main() {
55+
vec4 positionVec4 = vec4(aPosition, 1.0);
56+
positionVec4.xy = positionVec4.xy * 2.0 - 1.0;
57+
positionVec4.y *= -1;
58+
gl_Position = positionVec4;
59+
vTexCoord = aTexCoord;
60+
}`;
61+
62+
let frag = `
63+
precision highp float;
64+
varying vec2 vTexCoord;
65+
uniform sampler2D img;
66+
uniform sampler2D depth;
67+
float getBlurriness(float d) {
68+
// Blur more the farther away we go from the
69+
// focal point at depth=0.9
70+
return abs(d - 0.9) * 40.;
71+
}
72+
float maxBlurDistance(float blurriness) {
73+
return blurriness * 0.01;
74+
}
75+
void main() {
76+
vec4 color = texture2D(img, vTexCoord);
77+
float samples = 1.;
78+
float centerDepth = texture2D(depth, vTexCoord).r;
79+
float blurriness = getBlurriness(centerDepth);
80+
for (int sample = 0; sample < 20; sample++) {
81+
// Sample nearby pixels in a spiral going out from the
82+
// current pixel
83+
float angle = float(sample);
84+
float distance = float(sample)/20.
85+
* maxBlurDistance(blurriness);
86+
vec2 offset = vec2(cos(angle), sin(angle)) * distance;
87+
88+
// How close is the object at the nearby pixel?
89+
float sampleDepth = texture2D(depth, vTexCoord + offset).r;
90+
91+
// How far should its blur reach?
92+
float sampleBlurDistance =
93+
maxBlurDistance(getBlurriness(sampleDepth));
94+
95+
// If it's in front of the current pixel, or its blur overlaps
96+
// with the current pixel, add its color to the average
97+
if (
98+
sampleDepth >= centerDepth ||
99+
sampleBlurDistance >= distance
100+
) {
101+
color += texture2D(img, vTexCoord + offset);
102+
samples++;
103+
}
104+
}
105+
color /= samples;
106+
gl_FragColor = color;
107+
}`;
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* @name Blur using Framebuffer Depth
3+
* @frame 710,400
4+
* @arialabel A line of five spheres rotating in front of the camera, with ones too close and too far to the camera appearing blurred
5+
* @description A shader that uses depth information from a p5.Framebuffer to
6+
* draw a scene with focal blur.
7+
*/
8+
let layer;
9+
let blur;
10+
11+
function setup() {
12+
createCanvas(windowWidth, windowHeight, WEBGL);
13+
layer = createFramebuffer();
14+
blur = createShader(vert, frag);
15+
noStroke();
16+
}
17+
18+
function draw() {
19+
// Draw a scene
20+
layer.begin();
21+
background(255);
22+
ambientLight(100);
23+
directionalLight(255, 255, 255, -1, 1, -1);
24+
ambientMaterial(255, 0, 0);
25+
fill(255, 255, 100);
26+
specularMaterial(255);
27+
shininess(150);
28+
29+
rotateY(millis() * 0.001);
30+
for (let i = 0; i < 5; i++) {
31+
push();
32+
translate((i-2)*100, 0, 0);
33+
sphere();
34+
pop();
35+
}
36+
layer.end();
37+
38+
// Render the scene with depth of field blur
39+
shader(blur);
40+
blur.setUniform('img', layer.color);
41+
blur.setUniform('depth', layer.depth);
42+
rect(width, height);
43+
}
44+
45+
function windowResized() {
46+
resizeCanvas(windowWidth, windowHeight);
47+
}
48+
49+
let vert = `
50+
precision highp float;
51+
attribute vec3 aPosition;
52+
attribute vec2 aTexCoord;
53+
varying vec2 vTexCoord;
54+
void main() {
55+
vec4 positionVec4 = vec4(aPosition, 1.0);
56+
positionVec4.xy = positionVec4.xy * 2.0 - 1.0;
57+
positionVec4.y *= -1;
58+
gl_Position = positionVec4;
59+
vTexCoord = aTexCoord;
60+
}`;
61+
62+
let frag = `
63+
precision highp float;
64+
varying vec2 vTexCoord;
65+
uniform sampler2D img;
66+
uniform sampler2D depth;
67+
float getBlurriness(float d) {
68+
// Blur more the farther away we go from the
69+
// focal point at depth=0.9
70+
return abs(d - 0.9) * 40.;
71+
}
72+
float maxBlurDistance(float blurriness) {
73+
return blurriness * 0.01;
74+
}
75+
void main() {
76+
vec4 color = texture2D(img, vTexCoord);
77+
float samples = 1.;
78+
float centerDepth = texture2D(depth, vTexCoord).r;
79+
float blurriness = getBlurriness(centerDepth);
80+
for (int sample = 0; sample < 20; sample++) {
81+
// Sample nearby pixels in a spiral going out from the
82+
// current pixel
83+
float angle = float(sample);
84+
float distance = float(sample)/20.
85+
* maxBlurDistance(blurriness);
86+
vec2 offset = vec2(cos(angle), sin(angle)) * distance;
87+
88+
// How close is the object at the nearby pixel?
89+
float sampleDepth = texture2D(depth, vTexCoord + offset).r;
90+
91+
// How far should its blur reach?
92+
float sampleBlurDistance =
93+
maxBlurDistance(getBlurriness(sampleDepth));
94+
95+
// If it's in front of the current pixel, or its blur overlaps
96+
// with the current pixel, add its color to the average
97+
if (
98+
sampleDepth >= centerDepth ||
99+
sampleBlurDistance >= distance
100+
) {
101+
color += texture2D(img, vTexCoord + offset);
102+
samples++;
103+
}
104+
}
105+
color /= samples;
106+
gl_FragColor = color;
107+
}`;

0 commit comments

Comments
 (0)