-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanivoronoi.html
358 lines (307 loc) · 13.1 KB
/
anivoronoi.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
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
<!DOCTYPE html>
<html>
<head>
<title>Optimized Voronoi Animation</title>
<style>
body { margin: 0; }
canvas { display: block; }
#controls {
position: fixed;
top: 10px;
left: 10px;
background: rgba(0,0,0,0.7);
color: white;
padding: 10px;
border-radius: 5px;
}
</style>
</head>
<body>
<div id="controls">
<button id="toggleAnimation">Pause/Play</button>
<button id="toggleColor">Toggle Colors</button>
<button id="toggleWildMode">Wild Mode</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/0.154.0/three.min.js"></script>
<script>
const GRID_SIZE = 15;
const DOT_COUNT = Math.floor(GRID_SIZE * GRID_SIZE * 0.85);
let isPaused = false;
let lastUpdateTime = 0;
let useColors = false;
let isWildMode = false;
const WILD_MODE_INTERVAL = 50; // milliseconds
const WILD_MODE_DOT_RATIO = 0.2;
let dotColors = Array(DOT_COUNT).fill().map(() => ({
r: Math.random() * 0.5 + 0.4, // Pastel red
g: Math.random() * 0.5 + 0.4, // Pastel green
b: Math.random() * 0.5 + 0.4 // Pastel blue
}));
const CELL_SIZE = 2;
const CELLS_PER_ROW = Math.ceil(GRID_SIZE / CELL_SIZE);
const MOVE_CHANCE = 0.1;
let grid = Array(GRID_SIZE).fill().map(() => Array(GRID_SIZE).fill(-1));
let dots = [];
let spatialGrid = Array(CELLS_PER_ROW).fill().map(() =>
Array(CELLS_PER_ROW).fill().map(() => []));
function updateSpatialGrid() {
spatialGrid.forEach(row => row.forEach(cell => cell.length = 0));
dots.forEach((dot, index) => {
const cellX = Math.floor(dot.x / CELL_SIZE);
const cellY = Math.floor(dot.y / CELL_SIZE);
if (cellX >= 0 && cellX < CELLS_PER_ROW &&
cellY >= 0 && cellY < CELLS_PER_ROW) {
spatialGrid[cellY][cellX].push(index);
}
});
}
for(let i = 0; i < DOT_COUNT; i++) {
let x, y;
do {
x = Math.floor(Math.random() * GRID_SIZE);
y = Math.floor(Math.random() * GRID_SIZE);
} while(grid[y][x] !== -1);
grid[y][x] = i;
dots.push({
x: x,
y: y,
targetX: x,
targetY: y,
moving: false
});
}
updateSpatialGrid();
const scene = new THREE.Scene();
const camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0.1, 10);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const material = new THREE.ShaderMaterial({
uniforms: {
uTime: { value: 0.0 },
uResolution: { value: new THREE.Vector2() },
uDots: { value: [] },
uCellSize: { value: CELL_SIZE },
uCellsPerRow: { value: CELLS_PER_ROW },
uSpatialGrid: { value: [] },
uSpatialCounts: { value: [] },
uDotColors: { value: [] },
uUseColors: { value: false }
},
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
uniform vec2 uResolution;
uniform float uTime;
uniform vec2 uDots[${DOT_COUNT}];
uniform float uCellSize;
uniform int uCellsPerRow;
uniform int uSpatialGrid[${CELLS_PER_ROW * CELLS_PER_ROW * 20}];
uniform int uSpatialCounts[${CELLS_PER_ROW * CELLS_PER_ROW}];
uniform vec3 uDotColors[${DOT_COUNT}];
uniform bool uUseColors;
varying vec2 vUv;
float euclideanDistance(vec2 p1, vec2 p2) {
vec2 diff = p1 - p2;
return sqrt(diff.x * diff.x + diff.y * diff.y);
}
float ring(float r, float x, float range) {
if (x < r - range) return 0.0;
if (x < r) return smoothstep(r - range, r, x);
if (x < r + range) return 1.0 - smoothstep(r, r + range, x);
return 0.0;
}
void main() {
vec2 uv = vUv * ${GRID_SIZE}.0;
int cellX = int(floor(uv.x / uCellSize));
int cellY = int(floor(uv.y / uCellSize));
float closest = 1000.0;
float secondClosest = 1000.0;
int closestDotIndex = -1;
for(int dy = -1; dy <= 1; dy++) {
for(int dx = -1; dx <= 1; dx++) {
int nx = cellX + dx;
int ny = cellY + dy;
if (nx < 0 || nx >= uCellsPerRow ||
ny < 0 || ny >= uCellsPerRow) continue;
int cellIndex = ny * uCellsPerRow + nx;
int dotCount = uSpatialCounts[cellIndex];
int baseIndex = cellIndex * 20;
for(int i = 0; i < 20; i++) {
if (i >= dotCount) break;
int dotIndex = uSpatialGrid[baseIndex + i];
float dist = euclideanDistance(uv, uDots[dotIndex]);
if (dist < closest) {
secondClosest = closest;
closest = dist;
closestDotIndex = dotIndex;
} else if (dist < secondClosest) {
secondClosest = dist;
}
}
}
}
float edgeDist = (secondClosest - closest) * 0.5;
float dots = ring(0.05, closest, 0.02);
float rings = ring(0.05, edgeDist, 0.02)
+ ring(0.2, edgeDist, 0.02)
+ ring(0.4, edgeDist, 0.02);
vec3 color;
if (uUseColors && closestDotIndex >= 0) {
color = uDotColors[closestDotIndex] * (dots + rings);
} else {
color = vec3(dots + rings);
}
gl_FragColor = vec4(color, 1.0);
}
`
});
const plane = new THREE.PlaneGeometry(2, 2);
const mesh = new THREE.Mesh(plane, material);
scene.add(mesh);
camera.position.z = 1;
const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]];
function tryMoveToEmpty(x, y, nextGrid, movedSpots) {
if (grid[y][x] !== -1) return false;
for (let [dx, dy] of directions) {
const nx = x + dx;
const ny = y + dy;
if (nx < 0 || nx >= GRID_SIZE || ny < 0 || ny >= GRID_SIZE) continue;
if (grid[ny][nx] === -1) continue;
if (movedSpots.has(`${ny},${nx}`)) continue;
if (Math.random() > MOVE_CHANCE) continue;
const dotIndex = grid[ny][nx];
nextGrid[y][x] = dotIndex;
nextGrid[ny][nx] = -1;
movedSpots.add(`${ny},${nx}`);
dots[dotIndex].targetX = x;
dots[dotIndex].targetY = y;
dots[dotIndex].moving = true;
return true;
}
return false;
}
function updateDotPositions() {
const nextGrid = grid.map(row => [...row]);
const movedSpots = new Set();
for(let y = 0; y < GRID_SIZE; y++) {
for(let x = 0; x < GRID_SIZE; x++) {
tryMoveToEmpty(x, y, nextGrid, movedSpots);
}
}
grid = nextGrid;
updateSpatialGrid();
}
function interpolateDotPositions() {
const speed = 0.1; // Units per frame
for(let dot of dots) {
if (!dot.moving) continue;
const dx = dot.targetX - dot.x;
const dy = dot.targetY - dot.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance <= speed) {
dot.x = dot.targetX;
dot.y = dot.targetY;
dot.moving = false;
} else {
const dirX = dx / distance;
const dirY = dy / distance;
dot.x += dirX * speed;
dot.y += dirY * speed;
}
}
}
function animate() {
requestAnimationFrame(animate);
if (isPaused) return;
const currentTime = Date.now();
if (isWildMode) {
if (currentTime - lastUpdateTime >= WILD_MODE_INTERVAL) {
// In wild mode, only move a small subset of dots
const dotsToMove = Math.max(1, Math.floor(DOT_COUNT * WILD_MODE_DOT_RATIO));
for (let i = 0; i < dotsToMove; i++) {
const x = Math.floor(Math.random() * GRID_SIZE);
const y = Math.floor(Math.random() * GRID_SIZE);
const nextGrid = grid.map(row => [...row]);
const movedSpots = new Set();
tryMoveToEmpty(x, y, nextGrid, movedSpots);
grid = nextGrid;
}
updateSpatialGrid();
lastUpdateTime = currentTime;
}
} else {
const currentSecond = Math.floor(currentTime / 1000);
if (currentSecond !== Math.floor(lastUpdateTime / 1000)) {
updateDotPositions();
lastUpdateTime = currentTime;
}
}
interpolateDotPositions();
material.uniforms.uTime.value = Date.now() / 1000;
material.uniforms.uResolution.value.set(window.innerWidth, window.innerHeight);
const positions = new Float32Array(DOT_COUNT * 2);
for(let i = 0; i < dots.length; i++) {
positions[i * 2] = dots[i].x;
positions[i * 2 + 1] = dots[i].y;
}
material.uniforms.uDots.value = positions;
const spatialIndices = new Int32Array(CELLS_PER_ROW * CELLS_PER_ROW * 20);
const spatialCounts = new Int32Array(CELLS_PER_ROW * CELLS_PER_ROW);
spatialGrid.forEach((row, y) => {
row.forEach((cell, x) => {
const cellIndex = y * CELLS_PER_ROW + x;
spatialCounts[cellIndex] = Math.min(cell.length, 20);
cell.slice(0, 20).forEach((dotIndex, i) => {
spatialIndices[cellIndex * 20 + i] = dotIndex;
});
});
});
material.uniforms.uSpatialGrid.value = spatialIndices;
material.uniforms.uSpatialCounts.value = spatialCounts;
// Update colors
const colors = new Float32Array(DOT_COUNT * 3);
for(let i = 0; i < dotColors.length; i++) {
colors[i * 3] = dotColors[i].r;
colors[i * 3 + 1] = dotColors[i].g;
colors[i * 3 + 2] = dotColors[i].b;
}
material.uniforms.uDotColors.value = colors;
material.uniforms.uUseColors.value = useColors;
renderer.render(scene, camera);
}
window.addEventListener('resize', () => {
const aspect = window.innerWidth / window.innerHeight;
if (aspect > 1) {
camera.left = -aspect;
camera.right = aspect;
camera.top = 1;
camera.bottom = -1;
} else {
camera.left = -1;
camera.right = 1;
camera.top = 1/aspect;
camera.bottom = -1/aspect;
}
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
document.getElementById('toggleAnimation').addEventListener('click', () => {
isPaused = !isPaused;
});
document.getElementById('toggleColor').addEventListener('click', () => {
useColors = !useColors;
});
document.getElementById('toggleWildMode').addEventListener('click', () => {
isWildMode = !isWildMode;
lastUpdateTime = Date.now();
});
animate();
</script>
</body>
</html>