Skip to content

Commit 4ec089b

Browse files
committed
add support for depth of field
1 parent 68cfa19 commit 4ec089b

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

BlurRenderer.js

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class BlurRenderer extends Renderer {
33
super(target)
44
this.focus = (target.height / 2) / tan(PI / 6)
55
this.intensity = 0.05
6+
this.dof = 0
67
this.numSamples = 15
78
}
89

@@ -21,6 +22,10 @@ class BlurRenderer extends Renderer {
2122
this.focus = -world.z
2223
}
2324

25+
setDof(dof) {
26+
this.dof = dof
27+
}
28+
2429
setIntensity(intensity) {
2530
this.intensity = intensity
2631
}
@@ -35,6 +40,7 @@ class BlurRenderer extends Renderer {
3540
uDepth: this.fbo.depth,
3641
uSize: [this.target.width, this.target.height],
3742
uIntensity: this.intensity,
43+
uDof: this.dof,
3844
uNumSamples: this.numSamples,
3945
uNear: this.target._renderer._curCamera._near,
4046
uFar: this.target._renderer._curCamera._far,
@@ -75,6 +81,7 @@ uniform sampler2D uImg;
7581
uniform sampler2D uDepth;
7682
uniform vec2 uSize;
7783
uniform float uIntensity;
84+
uniform float uDof;
7885
uniform float maxBlur;
7986
uniform int uNumSamples;
8087
uniform float uTargetZ;
@@ -95,30 +102,34 @@ float depthToZ(float depth) {
95102
}
96103
97104
float calcBlur(float z, float pixelScale) {
98-
return clamp(abs(z - uTargetZ), 0.0, 0.3*pixelScale);
105+
return clamp(abs(z - uTargetZ) - uDof / 2., 0.0, 0.3*pixelScale);
99106
}
100107
101108
void main() {
102-
float pixelScale = max(uSize.x, uSize.y);
103109
float total = 1.0;
104110
float origZ = depthToZ(texture2D(uDepth, vVertTexCoord).x);
105111
vec4 color = texture2D(uImg, vVertTexCoord);
106-
float blurAmt = calcBlur(origZ, pixelScale);
107-
for (int i = 0; i < MAX_NUM_SAMPLES; i++) {
108-
if (i >= uNumSamples) break;
109-
float t = (float(i + 1) / float(uNumSamples));
110-
float angle = (t*12.0) * 2. * PI;
111-
float radius = 1.0 - (t*t*t); // Sample more on the outer edge
112-
angle += 5.*rand(gl_FragCoord.xy);
113-
vec2 offset = (vec2(cos(angle),sin(angle)) * radius * uIntensity * blurAmt)/pixelScale;
114-
float z = depthToZ(texture2D(uDepth, vVertTexCoord + offset).x);
115-
float sampleBlur = calcBlur(z, pixelScale);
116-
117-
float weight = float((z >= origZ) || (sampleBlur >= blurAmt*radius + 5.));
118-
vec4 sample = texture2D(uImg, vVertTexCoord + offset);
119-
color += weight * sample;
120-
total += weight;
112+
113+
if (abs(origZ - uTargetZ) > uDof / 2.) {
114+
float pixelScale = max(uSize.x, uSize.y);
115+
float blurAmt = calcBlur(origZ, pixelScale);
116+
for (int i = 0; i < MAX_NUM_SAMPLES; i++) {
117+
if (i >= uNumSamples) break;
118+
float t = (float(i + 1) / float(uNumSamples));
119+
float angle = (t*12.0) * 2. * PI;
120+
float radius = 1.0 - (t*t*t); // Sample more on the outer edge
121+
angle += 5.*rand(gl_FragCoord.xy);
122+
vec2 offset = (vec2(cos(angle),sin(angle)) * radius * uIntensity * blurAmt)/pixelScale;
123+
float z = depthToZ(texture2D(uDepth, vVertTexCoord + offset).x);
124+
float sampleBlur = calcBlur(z, pixelScale);
125+
126+
float weight = float((z >= origZ) || (sampleBlur >= blurAmt*radius + 5.));
127+
vec4 sample = texture2D(uImg, vVertTexCoord + offset);
128+
color += weight * sample;
129+
total += weight;
130+
}
121131
}
132+
122133
color /= total;
123134
gl_FragColor = color;
124135
}

0 commit comments

Comments
 (0)