-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
287 lines (250 loc) · 8.39 KB
/
index.html
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
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto:400,700&display=swap" rel="stylesheet">
<title>Luca Rossi</title>
</head>
<style>
body {
margin: 0;
}
canvas {
display: block;
}
h1 {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-family: 'Roboto', sans-serif;
color: white;
-webkit-text-stroke: 1px #929292;
-moz-user-select: none;
-khtml-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
}
a {
color: inherit;
}
a:link {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
<div id="container">
<canvas id="canvas"></canvas>
<h1><a href="mailto:[email protected]">[email protected]</a></h1>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/110/three.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chroma-js/2.0.3/chroma.min.js"></script>
<script>
/* By Kevin Levron */
function App() {
const conf = {
nx: 40,
ny: 100,
cscale: chroma.scale(['2B292E']).mode('lab'),
darken: 1,
angle: Math.PI / 3,
timeCoef: 0.1
};
let renderer, scene, camera;
let width, height;
const { randFloat: rnd } = THREE.Math;
const uTime = { value: 0 }, uTimeCoef = { value: conf.timeCoef };
const polylines = [];
init();
function init() {
renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('canvas'), antialias: true });
camera = new THREE.PerspectiveCamera();
updateSize();
window.addEventListener('resize', updateSize, false);
document.body.addEventListener('click', initRandomScene);
initScene();
requestAnimationFrame(animate);
}
function initScene() {
scene = new THREE.Scene();
const vertexShader = `
uniform float uTime, uTimeCoef;
uniform float uSize;
uniform mat2 uMat2;
uniform vec3 uRnd1;
uniform vec3 uRnd2;
uniform vec3 uRnd3;
uniform vec3 uRnd4;
uniform vec3 uRnd5;
attribute vec3 next, prev;
attribute float side;
varying vec2 vUv;
vec2 dp(vec2 sv) {
return (1.5 * sv * uMat2);
}
void main() {
vUv = uv;
vec2 pos = dp(position.xy);
// Well... I know I should update geometry instead...
// Computing normal here is not needed
// vec2 sprev = dp(prev.xy);
// vec2 snext = dp(next.xy);
// vec2 tangent = normalize(snext - sprev);
// vec2 normal = vec2(-tangent.y, tangent.x);
// float dist = length(snext - sprev);
// normal *= smoothstep(0.0, 0.02, dist);
vec2 normal = dp(vec2(1, 0));
normal *= uSize;
float time = uTime * uTimeCoef;
vec3 rnd1 = vec3(cos(time * uRnd1.x + uRnd3.x), cos(time * uRnd1.y + uRnd3.y), cos(time * uRnd1.z + uRnd3.z));
vec3 rnd2 = vec3(cos(time * uRnd2.x + uRnd4.x), cos(time * uRnd2.y + uRnd4.y), cos(time * uRnd2.z + uRnd4.z));
normal *= 1.0
+ uRnd5.x * (cos((position.y + rnd1.x) * 20.0 * rnd1.y) + 1.0)
+ uRnd5.y * (sin((position.y + rnd2.x) * 20.0 * rnd2.y) + 1.0)
+ uRnd5.z * (cos((position.y + rnd1.z) * 20.0 * rnd2.z) + 1.0);
pos.xy -= normal * side;
gl_Position = vec4(pos, 0.0, 1.0);
}
`;
const fragmentShader = `
uniform vec3 uColor1;
uniform vec3 uColor2;
varying vec2 vUv;
void main() {
gl_FragColor = vec4(mix(uColor1, uColor2, vUv.x), 1.0);
}
`;
const dx = 2 / (conf.nx), dy = -2 / (conf.ny - 1);
const ox = -1 + dx / 2, oy = 1;
const mat2 = Float32Array.from([Math.cos(conf.angle), -Math.sin(conf.angle), Math.sin(conf.angle), Math.cos(conf.angle)]);
for (let i = 0; i < conf.nx; i++) {
const points = [];
for (let j = 0; j < conf.ny; j++) {
const x = ox + i * dx, y = oy + j * dy;
points.push(new THREE.Vector3(x, y, 0));
}
const polyline = new Polyline({ points });
polylines.push(polyline);
const material = new THREE.ShaderMaterial({
uniforms: {
uTime,
uTimeCoef,
uMat2: { value: mat2 },
uSize: { value: 1.5 / conf.nx },
uRnd1: { value: new THREE.Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)) },
uRnd2: { value: new THREE.Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)) },
uRnd3: { value: new THREE.Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)) },
uRnd4: { value: new THREE.Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)) },
uRnd5: { value: new THREE.Vector3(rnd(0.2, 0.5), rnd(0.3, 0.6), rnd(0.4, 0.7)) },
uColor1: { value: new THREE.Color(conf.cscale(i / conf.nx).hex()) },
uColor2: { value: new THREE.Color(conf.cscale(i / conf.nx).darken(conf.darken).hex()) }
},
vertexShader,
fragmentShader
});
const mesh = new THREE.Mesh(polyline.geometry, material);
scene.add(mesh);
}
}
function initRandomScene() {
conf.nx = Math.floor(rnd(20, 200));
conf.cscale = randomCScale();
conf.darken = rnd(0, 1) > 0.5 ? rnd(-4, -0.5) : rnd(0.5, 4);
conf.angle = rnd(0, 2 * Math.PI);
uTimeCoef.value = rnd(0.05, 0.2);
disposeScene();
initScene();
}
function disposeScene() {
for (let i=0; i<scene.children.length; i++) {
const mesh = scene.children[i];
scene.remove(mesh);
mesh.geometry.dispose();
mesh.material.dispose();
}
scene.dispose();
}
function randomCScale() {
const colors = [], n = 2 + Math.floor(rnd(0, 4));
for (let i = 0; i < n; i++) {
colors.push(chroma.random());
}
return chroma.scale(colors).mode('lch');
}
function animate(t) {
uTime.value = t * 0.001;
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
function updateSize() {
width = window.innerWidth;
height = window.innerHeight;
renderer.setSize(width, height);
}
}
// adapted from https://github.com/oframe/ogl/blob/master/src/extras/Polyline.js
const Polyline = (function () {
const tmp = new THREE.Vector3();
class Polyline {
constructor(params) {
const { points } = params;
this.points = points;
this.count = points.length;
this.init();
this.updateGeometry();
}
init() {
this.geometry = new THREE.BufferGeometry();
this.position = new Float32Array(this.count * 3 * 2);
this.prev = new Float32Array(this.count * 3 * 2);
this.next = new Float32Array(this.count * 3 * 2);
const side = new Float32Array(this.count * 1 * 2);
const uv = new Float32Array(this.count * 2 * 2);
const index = new Uint16Array((this.count - 1) * 3 * 2);
for (let i = 0; i < this.count; i++) {
const i2 = i * 2;
side.set([-1, 1], i2);
const v = i / (this.count - 1);
uv.set([0, v, 1, v], i * 4);
if (i === this.count - 1) continue;
index.set([i2 + 0, i2 + 1, i2 + 2], (i2 + 0) * 3);
index.set([i2 + 2, i2 + 1, i2 + 3], (i2 + 1) * 3);
}
this.geometry.setAttribute('position', new THREE.BufferAttribute(this.position, 3));
this.geometry.setAttribute('prev', new THREE.BufferAttribute(this.prev, 3));
this.geometry.setAttribute('next', new THREE.BufferAttribute(this.next, 3));
this.geometry.setAttribute('side', new THREE.BufferAttribute(side, 1));
this.geometry.setAttribute('uv', new THREE.BufferAttribute(uv, 2));
this.geometry.setIndex(new THREE.BufferAttribute(index, 1));
}
updateGeometry() {
this.points.forEach((p, i) => {
p.toArray(this.position, i * 3 * 2);
p.toArray(this.position, i * 3 * 2 + 3);
if (!i) {
tmp.copy(p).sub(this.points[i + 1]).add(p);
tmp.toArray(this.prev, i * 3 * 2);
tmp.toArray(this.prev, i * 3 * 2 + 3);
} else {
p.toArray(this.next, (i - 1) * 3 * 2);
p.toArray(this.next, (i - 1) * 3 * 2 + 3);
}
if (i === this.points.length - 1) {
tmp.copy(p).sub(this.points[i - 1]).add(p);
tmp.toArray(this.next, i * 3 * 2);
tmp.toArray(this.next, i * 3 * 2 + 3);
} else {
p.toArray(this.prev, (i + 1) * 3 * 2);
p.toArray(this.prev, (i + 1) * 3 * 2 + 3);
}
});
this.geometry.attributes.position.needsUpdate = true;
this.geometry.attributes.prev.needsUpdate = true;
this.geometry.attributes.next.needsUpdate = true;
}
}
return Polyline;
})();
App();
/* By Kevin Levron */
</script>