generated from qq15725/starter-ts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-blur-filter.ts
79 lines (69 loc) · 2.34 KB
/
create-blur-filter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { defineFilter } from './define-filter'
const vertexXShader = `
attribute vec2 aPosition;
varying vec2 vTextureCoord;
uniform float uStrength;
varying vec2 vBlurTextureCoords[5];
void main(void) {
gl_Position = vec4(aPosition, 0, 1);
vTextureCoord = step(0.0, aPosition);
vBlurTextureCoords[0] = vTextureCoord + vec2(-2.0 * uStrength, 0.0);
vBlurTextureCoords[1] = vTextureCoord + vec2(-1.0 * uStrength, 0.0);
vBlurTextureCoords[2] = vTextureCoord + vec2(0.0 * uStrength, 0.0);
vBlurTextureCoords[3] = vTextureCoord + vec2(1.0 * uStrength, 0.0);
vBlurTextureCoords[4] = vTextureCoord + vec2(2.0 * uStrength, 0.0);
}
`
const vertexYShader = `
attribute vec2 aPosition;
varying vec2 vTextureCoord;
uniform float uStrength;
varying vec2 vBlurTextureCoords[5];
void main(void) {
gl_Position = vec4(aPosition, 0, 1);
vTextureCoord = step(0.0, aPosition);
vBlurTextureCoords[0] = vTextureCoord + vec2(0.0, -2.0 * uStrength);
vBlurTextureCoords[1] = vTextureCoord + vec2(0.0, -1.0 * uStrength);
vBlurTextureCoords[2] = vTextureCoord + vec2(0.0, 0.0 * uStrength);
vBlurTextureCoords[3] = vTextureCoord + vec2(0.0, 1.0 * uStrength);
vBlurTextureCoords[4] = vTextureCoord + vec2(0.0, 2.0 * uStrength);
}
`
const fragmentShader = `
varying vec2 vBlurTextureCoords[5];
uniform sampler2D uSampler;
void main(void) {
gl_FragColor = vec4(0.0);
gl_FragColor += texture2D(uSampler, vBlurTextureCoords[0]) * 0.153388;
gl_FragColor += texture2D(uSampler, vBlurTextureCoords[1]) * 0.221461;
gl_FragColor += texture2D(uSampler, vBlurTextureCoords[2]) * 0.250301;
gl_FragColor += texture2D(uSampler, vBlurTextureCoords[3]) * 0.221461;
gl_FragColor += texture2D(uSampler, vBlurTextureCoords[4]) * 0.153388;
}
`
export interface BlurFilterOptions {
strength?: number
quality?: number
}
export function createBlurFilter(options: BlurFilterOptions = {}) {
const {
strength = 6,
quality = 1,
} = options
return defineFilter(texture => {
texture.registerProgram({
vertexShader: vertexXShader,
fragmentShader,
uniforms: {
uStrength: (1 / texture.width) * strength / quality,
},
})
texture.registerProgram({
vertexShader: vertexYShader,
fragmentShader,
uniforms: {
uStrength: (1 / texture.height) * strength / quality,
},
})
})
}