Skip to content

Commit 4e0086d

Browse files
authored
Merge pull request #16 from luyotw/main
add support for depth of field
2 parents 68cfa19 + 1d6a96b commit 4e0086d

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-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
}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ Methods on `BlurRenderer`:
249249
- `BlurRenderer.prototype.setIntensity(intensity: number)`
250250
- Control the intensity of the blur, between 0 and 1: the lower the intensity, the farther objects have to be from the focal point to be blurred
251251
- Defaults to 0.05
252+
- `BlurRenderer.prototype.setDof(dof: number)`
253+
- Control the depth of field along the z-axis, unsigned number start from 0: the lower the dof, the smaller clear depth range without blur, and the blur amount will start to be gained while the depth is outside the clear range.
254+
- The focal target is located in the centre of the clear range. So assume the focal target's depth value is `z`, then the clear range becomes from `z - dof / 2` to `z + dof / 2`.
255+
- Defaults to 0
252256
- `BlurRenderer.prototype.setSamples(numSamples: number)`
253257
- Control how many random samples to use in the blur shader. More samples will look smoother but is more computationally intensive.
254258
- Defaults to 15

0 commit comments

Comments
 (0)