-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.js
125 lines (110 loc) · 3.98 KB
/
filter.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
export class LerpFilter extends PIXI.Filter {
constructor(totalLerpTime, vertex, fragment, uniforms = {}) {
super(vertex, fragment, {lerp: 0, ...uniforms});
PIXI.Ticker.shared.add((delta) => {
if (this.uniforms.lerp < 1)
this.uniforms.lerp += delta / totalLerpTime;
if (this.uniforms.lerp > 1)
this.uniforms.lerp = 1;
});
}
}
export class YoyoFilter extends PIXI.Filter {
constructor(totalLerpTime, cb, vertex, fragment, uniforms = {}) {
super(vertex, fragment, {lerp: 0, ...uniforms});
this.progress = 0;
PIXI.Ticker.shared.add((delta) => {
this.progress += delta;
if (this.progress > totalLerpTime) {
cb(this);
this.progress = totalLerpTime;
}
this.uniforms.lerp = Math.sin((this.progress / totalLerpTime) * Math.PI);
});
};
}
function ySlideFragmentShader(phaseScale, heightScale) {
let phaseString = phaseScale.toFixed(2);
let heightString = heightScale.toFixed(2);
return `
varying vec2 vTextureCoord;
uniform float lerp;
uniform float phase;
uniform sampler2D uSampler;
void main(void){
vec2 sampleCoord = vTextureCoord;
sampleCoord.y = sampleCoord.y + lerp * sin(phase + (sampleCoord.x * 6.283) * ${phaseString}) / ${heightString} ;
gl_FragColor = texture2D(uSampler, sampleCoord);
}
`
}
function xSlideFragmentShader(phaseScale, heightScale) {
let phaseString = phaseScale.toFixed(2);
let heightString = heightScale.toFixed(2);
return `
varying vec2 vTextureCoord;
uniform float lerp;
uniform float phase;
uniform sampler2D uSampler;
void main(void){
vec2 sampleCoord = vTextureCoord;
sampleCoord.x = sampleCoord.x + lerp * sin(phase + (sampleCoord.y * 6.283) * ${phaseString}) / ${heightString} ;
gl_FragColor = texture2D(uSampler, sampleCoord);
}
`
}
export class YSlideFilter extends YoyoFilter {
constructor(cb, phaseScale = 1.0, heightScale = 8.0) {
super(500, cb ,undefined, ySlideFragmentShader(phaseScale, heightScale), {phase: Math.random() * Math.PI * 2 })
}
}
export class XSlideFilter extends YoyoFilter {
constructor(cb, phaseScale = 1.0, heightScale = 8.0) {
super(500, cb ,undefined, xSlideFragmentShader(phaseScale, heightScale), {phase: Math.random() * Math.PI * 2 })
}
}
function gradientFragmentShader() {
return `
varying vec2 vTextureCoord;
uniform float lerp;
uniform sampler2D uSampler;
void main(void){
vec4 sampledColor = texture2D(uSampler, vTextureCoord);
vec4 outputColor = vec4(0.0);
outputColor.w = sampledColor.w;
outputColor.x = sampledColor.x * sin(lerp * 6.283) + sampledColor.y * sin(lerp* 6.283 + 2.073) + sampledColor.z * sin(lerp * 6.283 + 4.21);
outputColor.y = sampledColor.x * sin(lerp * 6.283 + 2.073) + sampledColor.y * sin(lerp* 6.283 + 4.21) + sampledColor.z * sin(lerp * 6.283);
outputColor.z = sampledColor.x * sin(lerp * 6.283 + 4.21) + sampledColor.y * sin(lerp* 6.283) + sampledColor.z * sin(lerp * 6.283 + 2.07);
gl_FragColor = outputColor;
}
`;
}
export class GradientFilter extends YoyoFilter {
constructor(cb) {
super(500, cb, undefined, gradientFragmentShader(), {});
}
}
function swizzleFragmentShader(maxRot, maxLengthRot) {
return `
varying vec2 vTextureCoord;
uniform float lerp;
uniform sampler2D uSampler;
void main(void){
vec2 centered = (vTextureCoord * 2.0) - 1.0;
float l = length(centered);
// length based bump
float max_bump = ${maxLengthRot}; // roughly 45degrees
float length_bump = lerp * max_bump * sin(l * 2.0 * 3.141);
float theta = atan(centered.y, centered.x) + lerp * ${maxRot};
theta = theta + length_bump;
vec2 mapped = vec2(l * cos(theta), l * sin(theta));
vec2 samplePoint = (mapped + 1.0) / 2.0;
gl_FragColor = texture2D(uSampler, samplePoint);
}
`;
}
export class SwizzleFilter extends YoyoFilter {
constructor(cb) {
super(500, cb, undefined, swizzleFragmentShader(Math.PI / 2.0, Math.PI / 4.0), {});
}
}