-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShaderSlider.js
More file actions
374 lines (319 loc) Β· 14.4 KB
/
Copy pathShaderSlider.js
File metadata and controls
374 lines (319 loc) Β· 14.4 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import * as THREE from 'three';
// βββ Vertex shader ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const VERT = /* glsl */`
varying vec2 vUv;
void main() { vUv = uv; gl_Position = vec4(position, 1.0); }
`;
// βββ Fragment shader header βββββββββββββββββββββββββββββββββββββββββββββββββββ
const HEAD = /* glsl */`
precision highp float;
varying vec2 vUv;
uniform sampler2D tA, tB;
uniform float progress;
uniform vec2 res;
`;
// βββ ΠΡΡΡΠΎΠ΅Π½Π½ΡΠ΅ ΡΡΡΠ΅ΠΊΡΡ βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
export const EFFECTS = {
fade: `
void main() {
gl_FragColor = mix(texture2D(tA,vUv), texture2D(tB,vUv), progress);
}`,
wind: `
float rand(vec2 co){ return fract(sin(dot(co,vec2(12.9898,78.233)))*43758.5453); }
void main() {
float r = rand(vec2(0.,vUv.y));
float m = smoothstep(0.,-.2, vUv.x*.8 + .2*r - progress*1.2);
gl_FragColor = mix(texture2D(tA,vUv), texture2D(tB,vUv), m);
}`,
wave: `
void main() {
float prog = (1.-progress)*.8-.05;
float t = sin(vUv.x*12.-progress*8.)*.03*(1.-progress);
vec2 uv = vUv+vec2(0.,t);
float i = pow(abs(smoothstep(0.,1.,prog*2.-uv.x+.5)),8.);
gl_FragColor = mix(texture2D(tB,(uv-.5)*(1.-i)+.5), texture2D(tA,(uv-.5)*i+.5), i);
}`,
ripple: `
void main() {
float prog=progress*.66, w=.35, r=.9;
float c=1.-smoothstep(-w,0., r*distance(vec2(.5),vUv)-prog*(1.+w));
gl_FragColor=mix(texture2D(tA,(vUv-.5)*(1.-c)+.5), texture2D(tB,(vUv-.5)*c+.5), c);
}`,
pixelize: `
void main() {
float d=min(progress,1.-progress);
vec2 sq=2.*ceil(d*20.)/20./vec2(60.);
vec2 p=d>0.?(floor(vUv/sq)+.5)*sq:vUv;
gl_FragColor=mix(texture2D(tA,p), texture2D(tB,p), progress);
}`,
dots: `
void main() {
bool n=distance(fract(vUv*20.),vec2(.5))<(progress/distance(vUv,vec2(.5)));
gl_FragColor=n?texture2D(tB,vUv):texture2D(tA,vUv);
}`,
flyeye: `
void main() {
float inv=1.-progress;
vec2 d=.04*vec2(cos(100.*vUv.x),sin(100.*vUv.y));
vec4 f=vec4(texture2D(tA,vUv+progress*d*.7).r,
texture2D(tA,vUv+progress*d).g,
texture2D(tA,vUv+progress*d*1.3).b,1.);
gl_FragColor=texture2D(tB,vUv+inv*d)*progress+f*inv;
}`,
slices: `
void main() {
float pr=smoothstep(-.5,0.,vUv.x-progress*1.5);
gl_FragColor=mix(texture2D(tA,vUv),texture2D(tB,vUv),step(pr,fract(20.*vUv.x)));
}`,
squares: `
void main() {
vec2 v=vec2(1.,-.5); v/=abs(v.x)+abs(v.y);
float pr=smoothstep(-1.6,0.,dot(v,vUv)-(dot(v,vec2(.5))-.5+progress*2.6));
vec2 sp=fract(vUv*10.),mn=vec2(pr/2.),mx=vec2(1.-pr/2.);
float a=(1.-step(progress,0.))*step(mn.x,sp.x)*step(mn.y,sp.y)*step(sp.x,mx.x)*step(sp.y,mx.y);
gl_FragColor=mix(texture2D(tA,vUv),texture2D(tB,vUv),a);
}`,
shutters: `
mat2 rot(float a){float s=sin(a),c=cos(a);return mat2(c,-s,s,c);}
void main() {
vec2 d=fract(vUv*vec2(50.,1.));
gl_FragColor=mix(texture2D(tA,vUv+rot(.785)*d*progress*.1),
texture2D(tB,vUv+rot(.785)*d*(1.-progress)*.1),progress);
}`,
stretch: `
float hash(vec2 p){return fract(1e4*sin(17.*p.x+p.y*.1)*(0.1+abs(sin(p.y*13.+p.x))));}
float noise(vec2 x){vec2 i=floor(x),f=fract(x);
float a=hash(i),b=hash(i+vec2(1,0)),c=hash(i+vec2(0,1)),d=hash(i+vec2(1,1));
vec2 u=f*f*(3.-2.*f); return mix(a,b,u.x)+(c-a)*u.y*(1.-u.x)+(d-b)*u.x*u.y;}
void main() {
float n=noise(vUv*res/100.);
vec2 dir=vec2(0.,normalize(vec2(.5)-vUv).y);
gl_FragColor=mix(texture2D(tA,vUv+dir*progress/5.*(1.+n/2.)),
texture2D(tB,vUv-dir*(1.-progress)/5.*(1.+n/2.)),progress);
}`,
'morph-x': `
mat2 rotM(float a){float s=sin(a),c=cos(a);return mat2(c,-s,s,c);}
void main() {
vec2 dv=texture2D(tA,vUv).rg;
gl_FragColor=mix(texture2D(tA,vUv+rotM(.785)*dv*progress),
texture2D(tB,vUv+rotM(-2.356)*dv*(1.-progress)),progress);
}`,
'morph-y': `
void main() {
float dp1=(texture2D(tA,vUv).r+texture2D(tA,vUv).g+texture2D(tA,vUv).b)*.33;
float dp2=(texture2D(tB,vUv).r+texture2D(tB,vUv).g+texture2D(tB,vUv).b)*.33;
gl_FragColor=mix(texture2D(tA,vec2(vUv.x,vUv.y+progress*dp2*.3)),
texture2D(tB,vec2(vUv.x,vUv.y+(1.-progress)*dp1*.3)),progress);
}`,
'peel-x': `
void main() {
float x=smoothstep(0.,1.,progress*2.+vUv.x-1.);
gl_FragColor=mix(texture2D(tA,(vUv-.5)*(1.-x)+.5),texture2D(tB,(vUv-.5)*x+.5),x);
}`,
'peel-y': `
void main() {
float x=smoothstep(0.,1.,progress*2.+vUv.y-1.);
gl_FragColor=mix(texture2D(tA,(vUv-.5)*(1.-x)+.5),texture2D(tB,(vUv-.5)*x+.5),x);
}`,
'page-curl': `
const float PI=3.14159,MIN=-0.16,MAX=1.5,CR=1./PI/2.;
vec4 aa(vec4 c1,vec4 c2,float d){d*=512.;if(d<0.)return c2;if(d>2.)return c1;return mix(c1,c2,pow(1.-d/2.,3.));}
float dte(vec3 p){float dx=p.x<0.?-p.x:p.x>1.?p.x-1.:abs(p.x>.5?1.-p.x:p.x);float dy=p.y<0.?-p.y:p.y>1.?p.y-1.:abs(p.y>.5?1.-p.y:p.y);if((p.x<0.||p.x>1.)&&(p.y<0.||p.y>1.))return sqrt(dx*dx+dy*dy);return min(dx,dy);}
void main(){
float amt=progress*(MAX-MIN)+MIN, cA=2.*PI*amt;
const float ang=100.*PI/180.;
mat3 M=mat3(cos(-ang),sin(-ang),0.,-sin(-ang),cos(-ang),0.,-0.801,0.89,1.);
mat3 R=mat3(cos(ang),sin(ang),0.,-sin(ang),cos(ang),0.,0.985,0.985,1.);
vec3 pt=M*vec3(vUv,1.); float yc=pt.y-amt;
if(yc>CR){gl_FragColor=texture2D(tA,vUv);return;}
if(yc<-CR){float sh=(1.-((-CR-yc)/amt*7.))/6.*(1.-abs(pt.x-.5));gl_FragColor=vec4(texture2D(tB,vUv).rgb-max(sh,0.),1.);return;}
float hA=(acos(clamp(yc/CR,-1.,1.))+cA)-PI,hM=mod(hA,2.*PI);
if((hM>PI&&amt<.5)||(hM>PI/2.&&amt<0.)){
vec3 p2=R*vec3(pt.x,(PI-(acos(clamp(yc/CR,-1.,1.))-cA))/(2.*PI),1.);
if(yc<=0.&&(p2.x<0.||p2.y<0.||p2.x>1.||p2.y>1.)){gl_FragColor=texture2D(tB,vUv);return;}
if(yc>0.){gl_FragColor=texture2D(tA,vUv);return;}
gl_FragColor=aa(texture2D(tA,p2.xy),vec4(0.),dte(p2));return;}
vec3 p3=R*vec3(pt.x,hA/(2.*PI),1.);
if(p3.x<0.||p3.y<0.||p3.x>1.||p3.y>1.){
vec3 p2=R*vec3(pt.x,(PI-(acos(clamp(yc/CR,-1.,1.))-cA))/(2.*PI),1.);
vec4 sc=texture2D(tB,vUv); if(yc<=0.)sc.rgb-=max((1.-dte(p2)*30.)/3.,0.)*amt;
gl_FragColor=sc;return;}
vec4 col=texture2D(tA,p3.xy);
float g=(col.r+col.b+col.g)/15.+.8*(pow(1.-abs(yc/CR),.2)/2.+.5);
col.rgb=vec3(g);
gl_FragColor=aa(col,yc<0.?vec4(0.,0.,0.,pow(-yc/CR,3.)*.5):texture2D(tA,vUv),CR-abs(yc));
}`,
};
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
/**
* ShaderSlider
*
* ΠΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠ΅:
*
* import ShaderSlider, { EFFECTS } from './ShaderSlider.js'
*
* const slider = new ShaderSlider({
* container : document.getElementById('my-slider'),
* images : ['a.jpg', 'b.jpg', 'c.jpg'],
* effect : 'wind', // Π½Π°Π·Π²Π°Π½ΠΈΠ΅ ΡΡΡΠ΅ΠΊΡΠ° ΠΈΠ»ΠΈ GLSL-ΡΡΡΠΎΠΊΠ°
* duration : 900, // ΠΌΡ Π°Π½ΠΈΠΌΠ°ΡΠΈΠΈ ΠΏΠ΅ΡΠ΅Ρ
ΠΎΠ΄Π°
* autoplay : 4000, // ΠΌΡ ΠΌΠ΅ΠΆΠ΄Ρ ΡΠ»Π°ΠΉΠ΄Π°ΠΌΠΈ, false = Π²ΡΠΊΠ»
* loop : true,
* onChange : (index) => {},// ΠΊΠΎΠ»Π»Π±Π΅ΠΊ ΠΏΠΎΡΠ»Π΅ ΡΠΌΠ΅Π½Ρ ΡΠ»Π°ΠΉΠ΄Π°
* })
*
* // API:
* slider.goTo(2)
* slider.next()
* slider.prev()
* slider.setEffect('ripple')
* slider.setEffect(myCustomGLSL) // ΡΠ²ΠΎΡ GLSL ΡΡΡΠΎΠΊΠ°
* slider.destroy()
*/
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
export default class ShaderSlider {
constructor(options = {}) {
const {
container,
images = [],
effect = 'wind',
duration = 900,
autoplay = false,
loop = true,
onChange = null,
} = options;
if (!container) throw new Error('ShaderSlider: container is required');
if (!images.length) throw new Error('ShaderSlider: images array is empty');
this._container = container;
this._images = images;
this._duration = duration;
this._loop = loop;
this._onChange = onChange;
this._current = 0;
this._busy = false;
this._raf = null;
this._autoTimer = null;
this._autoDelay = autoplay;
this._textures = [];
this._matCache = {};
this._initThree();
this._loadTextures().then(() => {
this._uniforms.tA.value = this._textures[0];
this._uniforms.tB.value = this._textures[1] ?? this._textures[0];
this.setEffect(effect);
if (autoplay) this._startAuto();
});
}
// βββ Three.js setup ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
_initThree() {
const el = this._container;
this._renderer = new THREE.WebGLRenderer({ antialias: false, alpha: false });
this._renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
this._renderer.setSize(el.clientWidth, el.clientHeight);
// canvas ΠΏΠΎΠ·ΠΈΡΠΈΠΎΠ½ΠΈΡΡΠ΅ΠΌ Π°Π±ΡΠΎΠ»ΡΡΠ½ΠΎ Π²Π½ΡΡΡΠΈ container
Object.assign(this._renderer.domElement.style, {
position: 'absolute', inset: '0',
width: '100%', height: '100%', display: 'block',
});
el.appendChild(this._renderer.domElement);
this._scene = new THREE.Scene();
this._camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);
this._uniforms = {
tA: { value: null },
tB: { value: null },
progress: { value: 0 },
res: { value: new THREE.Vector2(el.clientWidth, el.clientHeight) },
};
this._mesh = new THREE.Mesh(new THREE.PlaneGeometry(2, 2), null);
this._scene.add(this._mesh);
this._renderer.setAnimationLoop(() =>
this._renderer.render(this._scene, this._camera));
this._onResize = () => {
this._renderer.setSize(el.clientWidth, el.clientHeight);
this._uniforms.res.value.set(el.clientWidth, el.clientHeight);
};
window.addEventListener('resize', this._onResize);
}
// βββ ΠΠ°Π³ΡΡΠ·ΠΊΠ° ΡΠ΅ΠΊΡΡΡΡ ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
_loadTextures() {
const loader = new THREE.TextureLoader();
return Promise.all(this._images.map(src =>
new Promise(resolve => {
loader.load(src, tex => {
tex.colorSpace = THREE.SRGBColorSpace;
this._textures.push(tex);
resolve();
});
})
));
}
// βββ ΠΠΎΠΌΠΏΠΈΠ»ΡΡΠΈΡ / ΠΊΠ΅Ρ ΠΌΠ°ΡΠ΅ΡΠΈΠ°Π»ΠΎΠ² ββββββββββββββββββββββββββββββββββββββββββ
_getMaterial(effectKey) {
if (!this._matCache[effectKey]) {
// effect ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΈΠΌΠ΅Π½Π΅ΠΌ ΠΈΠ· EFFECTS ΠΈΠ»ΠΈ ΠΏΡΠΎΠΈΠ·Π²ΠΎΠ»ΡΠ½ΠΎΠΉ GLSL ΡΡΡΠΎΠΊΠΎΠΉ
const frag = EFFECTS[effectKey] ?? effectKey;
this._matCache[effectKey] = new THREE.ShaderMaterial({
uniforms: this._uniforms,
vertexShader: VERT,
fragmentShader: HEAD + frag,
});
}
return this._matCache[effectKey];
}
// βββ ΠΡΠ±Π»ΠΈΡΠ½ΠΎΠ΅ API βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
/** ΠΠ΅ΡΠ΅ΠΊΠ»ΡΡΠΈΡΡ ΡΡΡΠ΅ΠΊΡ ΠΏΠΎ ΠΈΠΌΠ΅Π½ΠΈ ΠΈΠ»ΠΈ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ GLSL ΡΡΡΠΎΠΊΡ Π½Π°ΠΏΡΡΠΌΡΡ */
setEffect(effect) {
this._effect = effect;
this._mesh.material = this._getMaterial(effect);
}
/** ΠΠ΅ΡΠ΅ΠΉΡΠΈ ΠΊ ΡΠ»Π°ΠΉΠ΄Ρ ΠΏΠΎ ΠΈΠ½Π΄Π΅ΠΊΡΡ */
goTo(index) {
const n = this._textures.length;
if (!n || this._busy) return;
let next = index;
if (this._loop) next = ((index % n) + n) % n;
else next = Math.max(0, Math.min(n - 1, index));
if (next === this._current) return;
this._busy = true;
const from = this._current;
const to = next;
this._uniforms.tA.value = this._textures[from];
this._uniforms.tB.value = this._textures[to];
this._uniforms.progress.value = 0;
const start = performance.now();
const ease = t => t < .5 ? 2*t*t : -1+(4-2*t)*t;
const tick = () => {
const raw = Math.min((performance.now() - start) / this._duration, 1);
this._uniforms.progress.value = ease(raw);
if (raw < 1) { this._raf = requestAnimationFrame(tick); return; }
this._current = to;
this._busy = false;
this._onChange?.(to);
};
this._raf = requestAnimationFrame(tick);
}
next() { this.goTo(this._current + 1) }
prev() { this.goTo(this._current - 1) }
get currentIndex() { return this._current }
get total() { return this._textures.length }
// βββ Autoplay ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
_startAuto() {
this._stopAuto();
if (!this._autoDelay) return;
this._autoTimer = setInterval(() => this.next(), this._autoDelay);
}
_stopAuto() { clearInterval(this._autoTimer) }
pauseAutoplay() { this._stopAuto() }
resumeAutoplay() { this._startAuto() }
// βββ Cleanup βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
destroy() {
this._stopAuto();
cancelAnimationFrame(this._raf);
window.removeEventListener('resize', this._onResize);
this._renderer.setAnimationLoop(null);
this._textures.forEach(t => t.dispose());
Object.values(this._matCache).forEach(m => m.dispose());
this._renderer.dispose();
this._container.removeChild(this._renderer.domElement);
}
}