回答編集履歴

1

文章構成の修正

2020/03/13 00:11

投稿

s0320318
s0320318

スコア4

test CHANGED
@@ -1,429 +1 @@
1
1
  自己解決できました!
2
-
3
-
4
-
5
-
6
-
7
- ```JavaScript
8
-
9
- export default class Slider {
10
-
11
- constructor() {
12
-
13
- this.scene = new THREE.Scene();
14
-
15
-
16
-
17
- this.uniforms = {
18
-
19
- intensity: { value: 1, type: 'f', min: 0., max: 3 }
20
-
21
- };
22
-
23
-
24
-
25
- this.renderer = new THREE.WebGLRenderer();
26
-
27
- this.width = window.innerWidth;
28
-
29
- this.height = window.innerHeight;
30
-
31
- this.renderer.setPixelRatio(window.devicePixelRatio);
32
-
33
- this.renderer.setSize(this.width, this.height);
34
-
35
- this.renderer.setClearColor(0xeeeeee, 1);
36
-
37
- this.easing = 'easeInOut'
38
-
39
- this.container = document.getElementById('slider');
40
-
41
- this.images = JSON.parse(this.container.getAttribute('data-images'));
42
-
43
- this.width = this.container.offsetWidth;
44
-
45
- this.height = this.container.offsetHeight;
46
-
47
- this.container.appendChild(this.renderer.domElement);
48
-
49
-
50
-
51
- this.camera = new THREE.PerspectiveCamera(
52
-
53
- 70,
54
-
55
- window.innerWidth / window.innerHeight,
56
-
57
- 0.001,
58
-
59
- 1000
60
-
61
- );
62
-
63
-
64
-
65
- this.camera.position.set(0, 0, 2);
66
-
67
- this.time = 0;
68
-
69
- this.current = 0;
70
-
71
- this.textures = [];
72
-
73
- }
74
-
75
-
76
-
77
- init(complete) {
78
-
79
- this.initiate(() => {
80
-
81
- window.addEventListener('resize', this.resize.bind(this));
82
-
83
-
84
-
85
- this.settings = { progress: 0.5 };
86
-
87
-
88
-
89
- Object.keys(this.uniforms).forEach((item) => {
90
-
91
- this.settings[item] = this.uniforms[item].value;
92
-
93
- });
94
-
95
-
96
-
97
- this.material = new THREE.ShaderMaterial({
98
-
99
- extensions: {
100
-
101
- derivatives: '#extension GL_OES_standard_derivatives : enable'
102
-
103
- },
104
-
105
-
106
-
107
- side: THREE.DoubleSide,
108
-
109
-
110
-
111
- uniforms: {
112
-
113
- time: { type: 'f', value: 0 },
114
-
115
- progress: { type: 'f', value: 0 },
116
-
117
- border: { type: 'f', value: 0 },
118
-
119
- intensity: { type: 'f', value: 0 },
120
-
121
- scaleX: { type: 'f', value: 40 },
122
-
123
- scaleY: { type: 'f', value: 40 },
124
-
125
- transition: { type: 'f', value: 40 },
126
-
127
- swipe: { type: 'f', value: 0 },
128
-
129
- width: { type: 'f', value: 0 },
130
-
131
- radius: { type: 'f', value: 0 },
132
-
133
- texture1: { type: 'f', value: this.textures[0] },
134
-
135
- texture2: { type: 'f', value: this.textures[1] },
136
-
137
- displacement: { type: 'f', value: new THREE.TextureLoader().load('disp1.jpg') },
138
-
139
- resolution: { type: 'v4', value: new THREE.Vector4() },
140
-
141
- direction: { type: 'f', value: 1 },
142
-
143
- },
144
-
145
-
146
-
147
- vertexShader: `varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }`,
148
-
149
-
150
-
151
- fragmentShader: `
152
-
153
- uniform float time;
154
-
155
- uniform float progress;
156
-
157
- uniform float intensity;
158
-
159
- uniform float width;
160
-
161
- uniform float scaleX;
162
-
163
- uniform float scaleY;
164
-
165
- uniform float transition;
166
-
167
- uniform float radius;
168
-
169
- uniform float swipe;
170
-
171
- uniform float direction;
172
-
173
-
174
-
175
- uniform sampler2D texture1;
176
-
177
- uniform sampler2D texture2;
178
-
179
- uniform sampler2D displacement;
180
-
181
-
182
-
183
- uniform vec4 resolution;
184
-
185
- varying vec2 vUv;
186
-
187
- mat2 getRotM(float angle) {
188
-
189
- float s = sin(angle);
190
-
191
- float c = cos(angle);
192
-
193
- return mat2(c, -s, s, c);
194
-
195
- }
196
-
197
- const float PI = 3.1415;
198
-
199
- const float angle1 = PI *0.25;
200
-
201
- const float angle2 = -PI *0.75;
202
-
203
-
204
-
205
- void main() {
206
-
207
- vec2 newUV = (vUv - vec2(0.5))*resolution.zw + vec2(0.5);
208
-
209
-
210
-
211
- vec4 disp = texture2D(displacement, newUV);
212
-
213
- vec2 dispVec = vec2(disp.r, disp.g);
214
-
215
-
216
-
217
- vec2 distortedPosition1 = newUV + getRotM(angle1) * direction * dispVec * intensity * progress;
218
-
219
- vec4 t1 = texture2D(texture1, distortedPosition1);
220
-
221
-
222
-
223
- vec2 distortedPosition2 = newUV + getRotM(angle2) * direction * dispVec * intensity * (1.0 - progress);
224
-
225
- vec4 t2 = texture2D(texture2, distortedPosition2);
226
-
227
-
228
-
229
- gl_FragColor = mix(t1, t2, progress);
230
-
231
- }
232
-
233
- `
234
-
235
- });
236
-
237
-
238
-
239
- this.geometry = new THREE.PlaneGeometry(1, 1, 2, 2);
240
-
241
- this.plane = new THREE.Mesh(this.geometry, this.material);
242
-
243
- this.scene.add(this.plane);
244
-
245
-
246
-
247
- this.resize();
248
-
249
- this.render();
250
-
251
-
252
-
253
- if (typeof complete === 'function') {
254
-
255
- complete();
256
-
257
- }
258
-
259
- });
260
-
261
- }
262
-
263
-
264
-
265
- initiate(cb) {
266
-
267
- let that = this;
268
-
269
- let count = 0;
270
-
271
-
272
-
273
- const resolve = (complete) => {
274
-
275
- that.textures[count] = new THREE.TextureLoader().load(this.images[count], () => {
276
-
277
- count++;
278
-
279
- if (count < this.images.length) {
280
-
281
- resolve(complete);
282
-
283
- } else {
284
-
285
- complete();
286
-
287
- }
288
-
289
- });
290
-
291
- };
292
-
293
-
294
-
295
- resolve(() => {
296
-
297
- cb();
298
-
299
- });
300
-
301
- }
302
-
303
-
304
-
305
- resize() {
306
-
307
- this.width = this.container.offsetWidth;
308
-
309
- this.height = this.container.offsetHeight;
310
-
311
- this.renderer.setSize(this.width, this.height);
312
-
313
- this.camera.aspect = this.width / this.height;
314
-
315
-
316
-
317
- this.imageAspect = this.textures[0].image.height/this.textures[0].image.width;
318
-
319
- let a1; let a2;
320
-
321
- if(this.height/this.width>this.imageAspect) {
322
-
323
- a1 = (this.width/this.height) * this.imageAspect ;
324
-
325
- a2 = 1;
326
-
327
- } else{
328
-
329
- a1 = 1;
330
-
331
- a2 = (this.height/this.width) / this.imageAspect;
332
-
333
- }
334
-
335
-
336
-
337
- this.material.uniforms.resolution.value.x = this.width;
338
-
339
- this.material.uniforms.resolution.value.y = this.height;
340
-
341
- this.material.uniforms.resolution.value.z = a1;
342
-
343
- this.material.uniforms.resolution.value.w = a2;
344
-
345
-
346
-
347
- const dist = this.camera.position.z;
348
-
349
- const height = 1;
350
-
351
- this.camera.fov = 2*(180/Math.PI)*Math.atan(height/(2*dist));
352
-
353
-
354
-
355
- this.plane.scale.x = this.camera.aspect;
356
-
357
- this.plane.scale.y = 1;
358
-
359
-
360
-
361
- this.camera.updateProjectionMatrix();
362
-
363
- }
364
-
365
-
366
-
367
- itemSwitch(dir, next, duration, complete) {
368
-
369
- this.material.uniforms.direction.value = dir;
370
-
371
- this.material.uniforms.texture2.value = this.textures[next];
372
-
373
- let tl = new TimelineMax();
374
-
375
-
376
-
377
- tl.to(this.material.uniforms.progress, duration, {
378
-
379
- value: 1,
380
-
381
- ease: Power2[this.easing],
382
-
383
- onComplete: () => {
384
-
385
- this.current = next;
386
-
387
- this.material.uniforms.texture1.value = this.textures[next];
388
-
389
- this.material.uniforms.progress.value = 0;
390
-
391
-
392
-
393
- if (typeof complete === 'function') {
394
-
395
- complete();
396
-
397
- }
398
-
399
- }})
400
-
401
- }
402
-
403
-
404
-
405
- render() {
406
-
407
- this.time += 0.05;
408
-
409
- this.material.uniforms.time.value = this.time;
410
-
411
-
412
-
413
- Object.keys(this.uniforms).forEach((item)=> {
414
-
415
- this.material.uniforms[item].value = this.settings[item];
416
-
417
- });
418
-
419
-
420
-
421
- requestAnimationFrame(this.render.bind(this));
422
-
423
- this.renderer.render(this.scene, this.camera);
424
-
425
- }
426
-
427
- }
428
-
429
- ```