|
| 1 | +class GaussianBlurRenderer extends BlurRenderer { |
| 2 | + constructor(target) { |
| 3 | + super(target) |
| 4 | + this.fbo2 = target.createFramebuffer() |
| 5 | + this.intensity = 0.1 |
| 6 | + this.numSamples = 20 |
| 7 | + } |
| 8 | + |
| 9 | + frag() { |
| 10 | + return GaussianBlurRenderer.frag |
| 11 | + } |
| 12 | + |
| 13 | + getUniforms() { |
| 14 | + const uniforms = super.getUniforms() |
| 15 | + delete uniforms.uImg |
| 16 | + return uniforms |
| 17 | + } |
| 18 | + |
| 19 | + draw(cb) { |
| 20 | + const prevCamera = this.target._renderer._curCamera |
| 21 | + this.fbo.draw(() => { |
| 22 | + this.target.push() |
| 23 | + cb() |
| 24 | + this.target.pop() |
| 25 | + }) |
| 26 | + |
| 27 | + const uniforms = this.getUniforms() |
| 28 | + |
| 29 | + this.target.push() |
| 30 | + this.target.setCamera(this.cam) |
| 31 | + this.cam.move(0, 0, 0) |
| 32 | + |
| 33 | + this.fbo2.draw(() => { |
| 34 | + this.target.push() |
| 35 | + this.target.noStroke() |
| 36 | + this.target.rectMode(CENTER) |
| 37 | + this.target.shader(this.shader) |
| 38 | + for (const key in uniforms) { |
| 39 | + this.shader.setUniform(key, uniforms[key]) |
| 40 | + this.shader.setUniform('uDirection', 0) |
| 41 | + this.shader.setUniform('uImg', this.fbo.color) |
| 42 | + } |
| 43 | + this.target.rect(0, 0, this.target.width, -this.target.height) |
| 44 | + this.target.pop() |
| 45 | + }) |
| 46 | + |
| 47 | + this.target.noStroke() |
| 48 | + this.target.rectMode(CENTER) |
| 49 | + this.target.shader(this.shader) |
| 50 | + for (const key in uniforms) { |
| 51 | + this.shader.setUniform(key, uniforms[key]) |
| 52 | + } |
| 53 | + this.shader.setUniform('uDirection', 1) |
| 54 | + this.shader.setUniform('uImg', this.fbo2.color) |
| 55 | + this.target.rect(0, 0, this.target.width, -this.target.height) |
| 56 | + this.target.pop() |
| 57 | + this.target.setCamera(prevCamera) |
| 58 | + } |
| 59 | + |
| 60 | + remove() { |
| 61 | + super.remove() |
| 62 | + this.fbo2.remove() |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +p5.prototype.createGaussianBlurRenderer = function() { |
| 67 | + return new GaussianBlurRenderer(this) |
| 68 | +} |
| 69 | + |
| 70 | +GaussianBlurRenderer.frag = ` |
| 71 | +precision highp float; |
| 72 | +varying highp vec2 vVertTexCoord; |
| 73 | +uniform sampler2D uImg; |
| 74 | +uniform sampler2D uDepth; |
| 75 | +uniform vec2 uSize; |
| 76 | +uniform float uIntensity; |
| 77 | +uniform float uDof; |
| 78 | +uniform float maxBlur; |
| 79 | +uniform int uNumSamples; |
| 80 | +uniform float uTargetZ; |
| 81 | +uniform float uNear; |
| 82 | +uniform float uFar; |
| 83 | +uniform int uDirection; |
| 84 | +#define s ${0.5/3} |
| 85 | +const int MAX_NUM_SAMPLES = 50; |
| 86 | +float depthToZ(float depth) { |
| 87 | + float depthNormalized = 2.0 * depth - 1.0; |
| 88 | + return 2.0 * uNear * uFar / (uFar + uNear - depthNormalized * (uFar - uNear)); |
| 89 | +} |
| 90 | +float calcBlur(float z, float pixelScale) { |
| 91 | + return clamp(abs(z - uTargetZ) - uDof / 2., 0.0, 0.3*pixelScale); |
| 92 | +} |
| 93 | +void main() { |
| 94 | + float total = 1.0; |
| 95 | + float origZ = depthToZ(texture2D(uDepth, vVertTexCoord).x); |
| 96 | + vec4 color = texture2D(uImg, vVertTexCoord); |
| 97 | + if (abs(origZ - uTargetZ) > uDof / 2.) { |
| 98 | + float pixelScale = max(uSize.x, uSize.y); |
| 99 | + float blurAmt = calcBlur(origZ, pixelScale); |
| 100 | + for (int i = 0; i < MAX_NUM_SAMPLES; i++) { |
| 101 | + if (i >= uNumSamples) break; |
| 102 | + float t = (float(i) / float(uNumSamples - 1)); |
| 103 | + float radius = (t * 2. - 1.); |
| 104 | + float distAway = radius * uIntensity * blurAmt; |
| 105 | + vec2 offset = (uDirection == 0 ? vec2(1.,0.) : vec2(0.,1.)) * distAway / pixelScale; |
| 106 | + float z = depthToZ(texture2D(uDepth, vVertTexCoord + offset).x); |
| 107 | + float sampleBlur = calcBlur(z, pixelScale); |
| 108 | + float t2 = distAway / (sampleBlur * uIntensity); |
| 109 | + float weight = ${1/Math.sqrt(2*Math.PI)} / s * exp(-0.5*pow(t2/s,2.)); |
| 110 | + vec4 sample = texture2D(uImg, vVertTexCoord + offset); |
| 111 | + color += weight * sample; |
| 112 | + total += weight; |
| 113 | + } |
| 114 | + } |
| 115 | + color /= total; |
| 116 | + gl_FragColor = color; |
| 117 | +} |
| 118 | +` |
0 commit comments