generated from qq15725/starter-ts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-adjustment-filter.ts
74 lines (67 loc) · 1.42 KB
/
create-adjustment-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
import { defineFilter } from './define-filter'
const fragmentShader = `
varying vec2 vTextureCoord;
uniform sampler2D uSampler;
uniform float gamma;
uniform float contrast;
uniform float saturation;
uniform float brightness;
uniform float red;
uniform float green;
uniform float blue;
uniform float alpha;
void main(void) {
vec4 c = texture2D(uSampler, vTextureCoord);
if (c.a > 0.0) {
c.rgb /= c.a;
vec3 rgb = pow(c.rgb, vec3(1. / gamma));
rgb = mix(vec3(.5), mix(vec3(dot(vec3(.2125, .7154, .0721), rgb)), rgb, saturation), contrast);
rgb.r *= red;
rgb.g *= green;
rgb.b *= blue;
c.rgb = rgb * brightness;
c.rgb *= c.a;
}
gl_FragColor = c * alpha;
}
`
export interface AdjustmentFilterOptions {
gamma?: number
saturation?: number
contrast?: number
brightness?: number
red?: number
green?: number
blue?: number
alpha?: number
}
export function createAdjustmentFilter(options: AdjustmentFilterOptions = {}) {
const {
saturation = 1,
contrast = 1,
brightness = 1,
red = 1,
green = 1,
blue = 1,
alpha = 1,
} = options
let {
gamma = 1,
} = options
gamma = Math.max(gamma, 0.0001)
return defineFilter(texture => {
texture.registerProgram({
fragmentShader,
uniforms: {
saturation,
contrast,
brightness,
red,
green,
blue,
alpha,
gamma,
},
})
})
}