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
// This approach creates an interactive 3D scene using Three.js to display particles in a rotating donut shape.
// The scene is rendered client-side using WebGL.
export default async function server(request: Request): Promise<Response> {
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic Donut Particles</title>
<style>
html, body { width: 100%; height: 100%; }
body { margin: 0; }
canvas { display: block; }
</style>
</head>
<body>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.module.js';
const PARTICLE_COUNT = 100000;
const FLY_AWAY_RATIO = 0.05; // 5% of particles will be "fly away" particles
const SPEED_VARIATION_RATIO = 0.2; // 20% of particles will have varied speeds
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xffffff);
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 100);
camera.position.z = 3;
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const geometry = new THREE.SphereGeometry(0.0025, 8, 8);
const material = new THREE.MeshBasicMaterial({ color: 0x000000 });
const instancedMesh = new THREE.InstancedMesh(geometry, material, PARTICLE_COUNT);
const tempObject = new THREE.Object3D();
const R = 1.2; // Major radius of the torus (distance from center to center of tube)
const r = 0.2; // Minor radius of the torus (radius of the tube)
const particleSpeeds = new Float32Array(PARTICLE_COUNT);
const particleRadialSpeeds = new Float32Array(PARTICLE_COUNT);
const particleAngularSpeeds = new Float32Array(PARTICLE_COUNT);
const isFlyAway = new Uint8Array(PARTICLE_COUNT);
for (let i = 0; i < PARTICLE_COUNT; i++) {
const theta = Math.random() * Math.PI * 2;
const phi = Math.random() * Math.PI * 2;
let u;
if (Math.random() < FLY_AWAY_RATIO) {
// Fly away particle
u = 1 + Math.random() * 0.5; // 1 to 1.5 times the normal radius
isFlyAway[i] = 1;
} else {
// Normal particle
u = Math.sqrt(Math.random()); // For uniform area distribution
isFlyAway[i] = 0;
}
// Position particles in a circular cross-section
const x = (R + r * u * Math.cos(theta)) * Math.cos(phi);
const y = (R + r * u * Math.cos(theta)) * Math.sin(phi);
const z = r * u * Math.sin(theta);
tempObject.position.set(x, y, z);
tempObject.updateMatrix();
instancedMesh.setMatrixAt(i, tempObject.matrix);
// Assign speeds with greater variation
if (Math.random() < SPEED_VARIATION_RATIO) {
const speedFactor = Math.pow(Math.random(), 2);
if (speedFactor < 0.2) {
particleSpeeds[i] = 0.05 + Math.random() * 0.15; // Very slow particles
} else if (speedFactor > 0.8) {
particleSpeeds[i] = 0.5 + Math.random() * 0.5; // Very fast particles
} else {
particleSpeeds[i] = 0.2 + Math.random() * 0.6; // Wide range of speeds
}
} else {
particleSpeeds[i] = 0.2 + Math.random() * 0.3; // Normal speed range
}
particleRadialSpeeds[i] = (Math.random() - 0.5) * 0.1;
particleAngularSpeeds[i] = (Math.random() - 0.5) * 0.1;
}
instancedMesh.instanceMatrix.needsUpdate = true;
scene.add(instancedMesh);
const clock = new THREE.Clock();
let isDragging = false;
let previousMousePosition = { x: 0, y: 0 };
function animate() {
requestAnimationFrame(animate);
const delta = clock.getDelta();
for (let i = 0; i < PARTICLE_COUNT; i++) {
instancedMesh.getMatrixAt(i, tempObject.matrix);
tempObject.position.setFromMatrixPosition(tempObject.matrix);