-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdna-helix.html
More file actions
560 lines (494 loc) · 19.9 KB
/
Copy pathdna-helix.html
File metadata and controls
560 lines (494 loc) · 19.9 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DNA Helix — Particles</title>
<meta name="description" content="Double helix structure with connecting bridges. Two intertwined strands scrolling through space.">
<meta name="author" content="Alexandru DAN">
<meta name="theme-color" content="#050505">
<link rel="canonical" href="https://particles.alexandrudan.com/dna-helix.html">
<link rel="icon" type="image/svg+xml" href="favicon.svg">
<!-- Open Graph -->
<meta property="og:type" content="website">
<meta property="og:url" content="https://particles.alexandrudan.com/dna-helix.html">
<meta property="og:title" content="DNA Helix — Particles">
<meta property="og:description" content="Double helix structure with connecting bridges. Two intertwined strands scrolling through space.">
<meta property="og:image" content="https://particles.alexandrudan.com/og-image.svg">
<meta property="og:site_name" content="Particles">
<!-- Twitter -->
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="DNA Helix — Particles">
<meta name="twitter:description" content="Double helix structure with connecting bridges. Two intertwined strands scrolling through space.">
<meta name="twitter:image" content="https://particles.alexandrudan.com/og-image.svg">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { background: #000; overflow: hidden; font-family: 'Courier New', monospace; }
canvas { display: block; }
#controls {
position: fixed;
bottom: 24px;
left: 50%;
transform: translateX(-50%);
display: flex;
gap: 20px;
background: rgba(0,0,0,0.6);
backdrop-filter: blur(12px);
border: 1px solid rgba(255,255,255,0.08);
border-radius: 12px;
padding: 14px 28px;
z-index: 10;
align-items: center;
}
.ctrl-group {
display: flex; flex-direction: column; align-items: center; gap: 4px;
}
.ctrl-group label {
color: rgba(255,255,255,0.4); font-size: 9px;
text-transform: uppercase; letter-spacing: 1.5px;
}
.ctrl-group input[type="range"] {
-webkit-appearance: none; width: 100px; height: 3px;
background: rgba(255,255,255,0.15); border-radius: 2px; outline: none;
}
.ctrl-group input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none; width: 12px; height: 12px;
border-radius: 50%; background: #fff; cursor: pointer;
}
#info {
position: fixed; top: 24px; left: 28px; z-index: 10;
}
#info h1 {
color: rgba(255,255,255,0.7); font-size: 14px; font-weight: 400;
letter-spacing: 3px; text-transform: uppercase;
}
#info p {
color: rgba(255,255,255,0.25); font-size: 10px;
margin-top: 6px; letter-spacing: 1px;
}
#fps {
position: fixed; top: 24px; right: 28px;
color: rgba(255,255,255,0.2); font-size: 10px;
letter-spacing: 1px; z-index: 10;
}
#back {
position: fixed; top: 24px; left: 50%; transform: translateX(-50%);
z-index: 10;
}
#back a {
color: rgba(255,255,255,0.2); font-size: 9px; text-decoration: none;
letter-spacing: 2px; text-transform: uppercase;
transition: color 0.3s;
}
#back a:hover { color: rgba(255,255,255,0.5); }
.zoom-btn {
width: 32px; height: 32px; border-radius: 50%;
background: rgba(255,255,255,0.06); border: 1px solid rgba(255,255,255,0.12);
color: rgba(255,255,255,0.5); font-size: 18px; cursor: pointer;
display: flex; align-items: center; justify-content: center;
transition: background 0.2s;
font-family: monospace;
}
.zoom-btn:hover { background: rgba(255,255,255,0.12); }
</style>
</head>
<body>
<div id="info">
<h1>DNA Helix</h1>
<p id="particle-count"></p>
</div>
<div id="fps"></div>
<div id="back"><a href="index.html">Gallery</a></div>
<div id="controls">
<div class="ctrl-group">
<input type="range" id="speed" min="0.1" max="5" step="0.1" value="1">
<label>Speed</label>
</div>
<div class="ctrl-group">
<input type="range" id="bloom" min="0" max="3" step="0.1" value="0.2">
<label>Bloom</label>
</div>
<div class="ctrl-group">
<input type="range" id="size" min="0.5" max="4" step="0.1" value="0.8">
<label>Size</label>
</div>
<div class="ctrl-group">
<input type="range" id="hueShift" min="0" max="1" step="0.01" value="0">
<label>Hue</label>
</div>
<button class="zoom-btn" id="zoomOut">−</button>
<button class="zoom-btn" id="zoomIn">+</button>
</div>
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js';
import { GPUComputationRenderer } from 'three/addons/misc/GPUComputationRenderer.js';
// =============================================
// CONFIG
// =============================================
// Texture size: WIDTH x WIDTH = total particles
const WIDTH = 256; // 256x256 = 65,536 particles
const PARTICLE_COUNT = WIDTH * WIDTH;
// DNA parameters
const HELIX_RADIUS = 2.0;
const HELIX_PITCH = 2.5;
const T_MAX = 6.0 * Math.PI; // 3 full turns
const TOTAL_HEIGHT = T_MAX * HELIX_PITCH / (2.0 * Math.PI);
const BRIDGE_FRACTION = 0.1; // ~10% bridge particles
// =============================================
// SCENE
// =============================================
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(60, innerWidth / innerHeight, 0.1, 2000);
camera.position.set(0, 0, 18);
const renderer = new THREE.WebGLRenderer({ antialias: false, powerPreference: 'high-performance' });
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(1); // Force 1x for performance; bloom is the bottleneck at 2x
document.body.appendChild(renderer.domElement);
const orbitControls = new OrbitControls(camera, renderer.domElement);
orbitControls.enableDamping = true;
orbitControls.dampingFactor = 0.05;
orbitControls.autoRotate = true;
orbitControls.autoRotateSpeed = 0.8;
// =============================================
// POST-PROCESSING
// =============================================
const composer = new EffectComposer(renderer);
composer.addPass(new RenderPass(scene, camera));
// Half-resolution bloom for performance
const bloomPass = new UnrealBloomPass(
new THREE.Vector2(innerWidth / 2, innerHeight / 2),
0.2, // strength
0.2, // radius
0.9 // threshold
);
composer.addPass(bloomPass);
// =============================================
// GPU COMPUTATION (DNA Helix on GPU)
// =============================================
const gpuCompute = new GPUComputationRenderer(WIDTH, WIDTH, renderer);
// Seed texture: stores (t_param, strand_id, bridge_flag, norm_index) per particle
const seedTex = gpuCompute.createTexture();
// Position texture: stores (x, y, z, w) per particle — computed from seed
const posTex0 = gpuCompute.createTexture();
// Seed particles with DNA helix parameters
function initSeedData(tex) {
const data = tex.image.data; // Float32Array, 4 values per pixel (RGBA)
for (let i = 0; i < PARTICLE_COUNT; i++) {
const idx = i * 4;
const isBridge = Math.random() < BRIDGE_FRACTION;
const tParam = (i / PARTICLE_COUNT) * T_MAX;
const strandId = isBridge ? 0.0 : (Math.random() < 0.5 ? 0.0 : 1.0);
const normIndex = Math.random(); // For bridges: interpolation factor; for strands: unused
data[idx] = tParam; // t_param
data[idx + 1] = strandId; // strand_id (0 or 1)
data[idx + 2] = isBridge ? 1.0 : 0.0; // bridge_flag
data[idx + 3] = normIndex; // norm_index (bridge interpolation)
}
}
// Initialize seed positions from helix parameters (so first frame isn't blank)
function initPositionData(posTex, seedTexRef) {
const posData = posTex.image.data;
const seedData = seedTexRef.image.data;
for (let i = 0; i < PARTICLE_COUNT; i++) {
const si = i * 4;
const tParam = seedData[si];
const strandId = seedData[si + 1];
const bridgeFlag = seedData[si + 2];
const normIndex = seedData[si + 3];
let x, y, z;
if (bridgeFlag > 0.5) {
// Bridge: interpolate between strand 0 and strand 1
const x0 = HELIX_RADIUS * Math.cos(tParam);
const z0 = HELIX_RADIUS * Math.sin(tParam);
const x1 = HELIX_RADIUS * Math.cos(tParam + Math.PI);
const z1 = HELIX_RADIUS * Math.sin(tParam + Math.PI);
const yVal = tParam * HELIX_PITCH / (2.0 * Math.PI) - TOTAL_HEIGHT / 2.0;
x = x0 + (x1 - x0) * normIndex;
y = yVal;
z = z0 + (z1 - z0) * normIndex;
} else {
// Strand particle
const angle = tParam + strandId * Math.PI;
x = HELIX_RADIUS * Math.cos(angle);
z = HELIX_RADIUS * Math.sin(angle);
y = tParam * HELIX_PITCH / (2.0 * Math.PI) - TOTAL_HEIGHT / 2.0;
}
const pi = i * 4;
posData[pi] = x;
posData[pi + 1] = y;
posData[pi + 2] = z;
posData[pi + 3] = seedData[si + 2]; // pass bridge_flag for coloring
}
}
initSeedData(seedTex);
initPositionData(posTex0, seedTex);
// GPU shader: DNA helix computed entirely on GPU
const positionShader = `
uniform float uTime;
uniform float uSpeed;
uniform float uFrame;
uniform sampler2D uSeedTexture;
// Simple GPU-friendly hash noise
float hash(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * 0.1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
float noise(vec2 p) {
return hash(p) * 2.0 - 1.0; // range [-1, 1]
}
void main() {
vec2 uv = gl_FragCoord.xy / resolution.xy;
// Read seed data: (t_param, strand_id, bridge_flag, norm_index)
vec4 seed = texture2D(uSeedTexture, uv);
float tParam = seed.x;
float strandId = seed.y;
float bridgeFlag = seed.z;
float normIndex = seed.w;
// Helix parameters
float R = 2.0;
float pitch = 2.5;
float tMax = 6.0 * 3.14159265;
float totalHeight = tMax * pitch / (2.0 * 3.14159265);
// Slowly scroll the helix
float t = tParam + uTime * 0.3 * uSpeed;
// Compute strand 0 position
float x0 = R * cos(t);
float z0 = R * sin(t);
float yVal = tParam * pitch / (2.0 * 3.14159265) - totalHeight * 0.5;
// Compute strand 1 position (offset by PI)
float x1 = R * cos(t + 3.14159265);
float z1 = R * sin(t + 3.14159265);
float x, y, z;
if (bridgeFlag > 0.5) {
// Bridge particle: interpolate between strand 0 and strand 1
x = mix(x0, x1, normIndex);
y = yVal;
z = mix(z0, z1, normIndex);
} else {
// Strand particle
float angle = t + strandId * 3.14159265;
x = R * cos(angle);
z = R * sin(angle);
y = yVal;
}
// Add very subtle noise
vec2 noiseSeed = uv * 1000.0 + uFrame;
x += noise(noiseSeed) * 0.015;
y += noise(noiseSeed + 100.0) * 0.015;
z += noise(noiseSeed + 200.0) * 0.015;
// Pack bridge_flag into w for coloring in vertex shader
gl_FragColor = vec4(x, y, z, bridgeFlag + strandId * 2.0);
}
`;
const posVar = gpuCompute.addVariable('texturePosition', positionShader, posTex0);
gpuCompute.setVariableDependencies(posVar, [posVar]);
posVar.material.uniforms.uTime = { value: 0.0 };
posVar.material.uniforms.uSpeed = { value: 1.0 };
posVar.material.uniforms.uFrame = { value: 0.0 };
posVar.material.uniforms.uSeedTexture = { value: seedTex };
// Wrap mode for the texture
posVar.wrapS = THREE.RepeatWrapping;
posVar.wrapT = THREE.RepeatWrapping;
const err = gpuCompute.init();
if (err !== null) {
console.error('GPU Computation init error:', err);
}
// =============================================
// PARTICLE RENDERING (custom shader reads GPU texture)
// =============================================
const vertexShader = `
uniform vec3 uMouseWorld;
uniform float uMouseStrength;
uniform sampler2D uPositionTexture;
uniform float uHueShift;
uniform float uPointSize;
attribute vec2 aTexCoord;
varying vec3 vColor;
vec3 hsl2rgb(float h, float s, float l) {
float c = (1.0 - abs(2.0 * l - 1.0)) * s;
float h6 = h * 6.0;
float x = c * (1.0 - abs(mod(h6, 2.0) - 1.0));
float m = l - c * 0.5;
vec3 rgb;
if (h6 < 1.0) rgb = vec3(c, x, 0.0);
else if (h6 < 2.0) rgb = vec3(x, c, 0.0);
else if (h6 < 3.0) rgb = vec3(0.0, c, x);
else if (h6 < 4.0) rgb = vec3(0.0, x, c);
else if (h6 < 5.0) rgb = vec3(x, 0.0, c);
else rgb = vec3(c, 0.0, x);
return rgb + m;
}
void main() {
vec4 posData = texture2D(uPositionTexture, aTexCoord);
vec3 pos = posData.xyz;
// Mouse displacement
vec3 toM = uMouseWorld - pos;
float mDist = length(toM);
pos += toM * uMouseStrength * 0.5 / (mDist * mDist + 2.0);
// Decode w channel: bridgeFlag + strandId * 2.0
// bridge: w = 1.0 (strand0) or 3.0 (strand0, but bridges use strand0)
// strand0: w = 0.0
// strand1: w = 2.0
float packed = posData.w;
float bridgeFlag = step(0.5, mod(packed, 2.0));
float strandId = floor(packed / 2.0);
float hue;
float saturation = 0.8;
float lightness;
if (bridgeFlag > 0.5) {
// Bridge particles: white/yellow
hue = fract(0.15 + uHueShift); // yellow hue
saturation = 0.3;
lightness = 0.15;
} else if (strandId < 0.5) {
// Strand 0: cyan hues
hue = fract(0.5 + uHueShift);
lightness = 0.08 + 0.07;
} else {
// Strand 1: magenta hues
hue = fract(0.85 + uHueShift);
lightness = 0.08 + 0.07;
}
vColor = hsl2rgb(hue, saturation, lightness);
vec4 mvPosition = modelViewMatrix * vec4(pos, 1.0);
gl_PointSize = uPointSize * (40.0 / -mvPosition.z);
gl_PointSize = max(gl_PointSize, 0.3);
gl_Position = projectionMatrix * mvPosition;
}
`;
const fragmentShader = `
varying vec3 vColor;
void main() {
float d = length(gl_PointCoord - 0.5) * 2.0;
if (d > 1.0) discard;
// Soft gaussian falloff with bright core
float glow = exp(-d * d * 3.0);
float core = exp(-d * d * 12.0);
float alpha = (glow * 0.15 + core * 0.3);
// Slight color boost at core (hotter center)
vec3 col = vColor + core * 0.15;
gl_FragColor = vec4(col, alpha);
}
`;
// Create geometry with texture coordinates to look up GPU results
const geometry = new THREE.BufferGeometry();
const texCoords = new Float32Array(PARTICLE_COUNT * 2);
for (let j = 0; j < WIDTH; j++) {
for (let i = 0; i < WIDTH; i++) {
const idx = (j * WIDTH + i) * 2;
texCoords[idx] = (i + 0.5) / WIDTH;
texCoords[idx + 1] = (j + 0.5) / WIDTH;
}
}
// Dummy positions (will be overridden by vertex shader)
const dummyPositions = new Float32Array(PARTICLE_COUNT * 3);
geometry.setAttribute('position', new THREE.BufferAttribute(dummyPositions, 3));
geometry.setAttribute('aTexCoord', new THREE.BufferAttribute(texCoords, 2));
const material = new THREE.ShaderMaterial({
vertexShader,
fragmentShader,
uniforms: {
uMouseWorld: { value: new THREE.Vector3(0, 0, -1000) },
uMouseStrength: { value: 0.0 },
uPositionTexture: { value: null },
uHueShift: { value: 0.0 },
uPointSize: { value: 1.0 },
},
transparent: true,
blending: THREE.AdditiveBlending,
depthWrite: false,
});
const points = new THREE.Points(geometry, material);
const pivot = new THREE.Group(); pivot.add(points); scene.add(pivot);
// =============================================
// UI
// =============================================
document.getElementById('particle-count').textContent =
`${PARTICLE_COUNT.toLocaleString()} particles — fully GPU computed`;
const speedSlider = document.getElementById('speed');
const bloomSlider = document.getElementById('bloom');
const sizeSlider = document.getElementById('size');
const hueSlider = document.getElementById('hueShift');
const fpsEl = document.getElementById('fps');
document.getElementById('zoomIn').addEventListener('click', () => {
material.uniforms.uScale.value *= 1.15;
});
document.getElementById('zoomOut').addEventListener('click', () => {
material.uniforms.uScale.value *= 0.85;
});
// =============================================
// Mouse interaction
const mouse = new THREE.Vector2();
const mouse3D = new THREE.Vector3();
const raycaster = new THREE.Raycaster();
let mouseActive = false;
renderer.domElement.addEventListener('mousemove', (e) => {
mouse.x = (e.clientX / innerWidth) * 2 - 1;
mouse.y = -(e.clientY / innerHeight) * 2 + 1;
mouseActive = true;
raycaster.setFromCamera(mouse, camera);
mouse3D.copy(raycaster.ray.direction).multiplyScalar(15).add(camera.position);
});
renderer.domElement.addEventListener('mouseleave', () => { mouseActive = false; });
// ANIMATION LOOP (CPU does zero particle work)
// =============================================
let lastTime = performance.now();
let frameCount = 0;
function animate(now) {
requestAnimationFrame(animate);
frameCount++;
if (now - lastTime >= 1000) {
fpsEl.textContent = `${frameCount} FPS`;
frameCount = 0;
lastTime = now;
}
// Update uniforms from UI
posVar.material.uniforms.uSpeed.value = parseFloat(speedSlider.value);
posVar.material.uniforms.uTime.value += 0.016; // ~60fps time step
posVar.material.uniforms.uFrame.value += 1.0;
bloomPass.strength = parseFloat(bloomSlider.value);
material.uniforms.uHueShift.value = parseFloat(hueSlider.value);
material.uniforms.uPointSize.value = parseFloat(sizeSlider.value);
// Update mouse uniforms
if (mouseActive) {
material.uniforms.uMouseWorld.value.copy(mouse3D);
material.uniforms.uMouseStrength.value = 1.0;
} else {
material.uniforms.uMouseStrength.value *= 0.95;
}
// Run GPU computation (DNA helix on GPU)
gpuCompute.compute();
// Pass computed position texture to the render shader
material.uniforms.uPositionTexture.value =
gpuCompute.getCurrentRenderTarget(posVar).texture;
pivot.rotation.x += 0.0008;
orbitControls.update();
composer.render();
}
animate(performance.now());
// =============================================
// RESIZE
// =============================================
window.addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
composer.setSize(innerWidth, innerHeight);
});
</script>
<script src="shared-ui.js"></script>
</body>
</html>