generated from qq15725/starter-ts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-emboss-filter.ts
39 lines (34 loc) · 934 Bytes
/
create-emboss-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
import { defineFilter } from './define-filter'
const fragmentShader = `
precision mediump float;
uniform sampler2D uSampler;
uniform vec4 uInputSize;
uniform float uStrength;
varying vec2 vTextureCoord;
void main(void) {
vec2 onePixel = uInputSize.zw;
vec4 color;
color.rgb = vec3(0.5);
color -= texture2D(uSampler, vTextureCoord - onePixel) * uStrength;
color += texture2D(uSampler, vTextureCoord + onePixel) * uStrength;
color.rgb = vec3((color.r + color.g + color.b) / 3.0);
float alpha = texture2D(uSampler, vTextureCoord).a;
gl_FragColor = vec4(color.rgb * alpha, alpha);
}
`
export interface EmbossFilterOptions {
strength?: number
}
export function createEmbossFilter(options: EmbossFilterOptions = {}) {
const {
strength = 5,
} = options
return defineFilter(texture => {
texture.registerProgram({
fragmentShader,
uniforms: {
uStrength: strength,
},
})
})
}