Files
nanocode/particle-lab/05-helix/index.html
T
Ubuntu 32b43f85bd a
2026-07-03 16:58:28 +08:00

179 lines
8.2 KiB
HTML

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>05 - HELIX | 双螺旋 DNA</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { background: #000; overflow: hidden; font-family: 'Space Mono', monospace; }
#canvas-container { width: 100vw; height: 100vh; }
.info {
position: fixed; top: 20px; left: 20px; color: #00d4ff;
font-size: 14px; pointer-events: none; z-index: 100;
text-shadow: 0 0 10px rgba(0,212,255,0.5);
}
.info h1 { font-size: 20px; margin-bottom: 8px; }
.info p { opacity: 0.8; font-size: 12px; margin: 4px 0; }
.controls {
position: fixed; bottom: 20px; left: 20px; color: #fff;
font-size: 12px; background: rgba(0,0,0,0.7); padding: 15px;
border-radius: 8px; border: 1px solid #333; backdrop-filter: blur(10px);
}
.control-group { margin: 10px 0; display: flex; align-items: center; gap: 10px; }
.control-group label { width: 80px; color: #aaa; }
.control-group input[type="range"] { width: 120px; }
.control-group span { color: #00d4ff; min-width: 40px; }
</style>
</head>
<body>
<div id="canvas-container"></div>
<div class="info">
<h1>05 - HELIX</h1>
<p>双螺旋 DNA 结构</p>
<p>粒子数: <span id="count">0</span></p>
<p>FPS: <span id="fps">0</span></p>
</div>
<div class="controls">
<div class="control-group">
<label>螺旋半径</label>
<input type="range" id="radius" min="5" max="20" step="0.5" value="12">
<span id="radVal">12</span>
</div>
<div class="control-group">
<label>螺旋高度</label>
<input type="range" id="height" min="40" max="100" step="5" value="60">
<span id="htVal">60</span>
</div>
<div class="control-group">
<label>旋转速度</label>
<input type="range" id="twist" min="1" max="10" step="0.5" value="4">
<span id="twistVal">4</span>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/postprocessing/EffectComposer.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/postprocessing/RenderPass.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/postprocessing/ShaderPass.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/shaders/CopyShader.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/shaders/LuminosityHighPassShader.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/postprocessing/UnrealBloomPass.js"></script>
<script>
const COUNT = 6000; // 双链,每链3000
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x000000, 0.01);
const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 2000);
camera.position.set(0, 0, 100);
const renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: "high-performance" });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
document.getElementById('canvas-container').appendChild(renderer.domElement);
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.autoRotate = true;
controls.autoRotateSpeed = 0.5;
const renderScene = new THREE.RenderPass(scene, camera);
const bloomPass = new THREE.UnrealBloomPass(new THREE.Vector2(window.innerWidth, window.innerHeight), 1.5, 0.4, 0);
bloomPass.strength = 1.5; bloomPass.radius = 0.4; bloomPass.threshold = 0;
const composer = new THREE.EffectComposer(renderer);
composer.addPass(renderScene);
composer.addPass(bloomPass);
const dummy = new THREE.Object3D();
const color = new THREE.Color();
const target = new THREE.Vector3();
const geometry = new THREE.SphereGeometry(0.25, 8, 8);
const material = new THREE.MeshBasicMaterial({ color: 0xffffff });
const instancedMesh = new THREE.InstancedMesh(geometry, material, COUNT);
instancedMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
scene.add(instancedMesh);
const positions = [];
for (let i = 0; i < COUNT; i++) {
positions.push(new THREE.Vector3(
(Math.random() - 0.5) * 50,
(Math.random() - 0.5) * 50,
(Math.random() - 0.5) * 50
));
}
const radiusSlider = document.getElementById('radius');
const heightSlider = document.getElementById('height');
const twistSlider = document.getElementById('twist');
radiusSlider.addEventListener('input', (e) => document.getElementById('radVal').textContent = e.target.value);
heightSlider.addEventListener('input', (e) => document.getElementById('htVal').textContent = e.target.value);
twistSlider.addEventListener('input', (e) => document.getElementById('twistVal').textContent = e.target.value);
document.getElementById('count').textContent = COUNT;
let lastTime = performance.now(), frameCount = 0;
const fpsEl = document.getElementById('fps');
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const now = performance.now();
frameCount++;
if (now - lastTime >= 1000) { fpsEl.textContent = frameCount; frameCount = 0; lastTime = now; }
const time = clock.getElapsedTime();
const r = parseFloat(radiusSlider.value);
const h = parseFloat(heightSlider.value);
const twists = parseFloat(twistSlider.value);
const countPerStrand = COUNT / 2;
for (let i = 0; i < COUNT; i++) {
const isFirstStrand = i < countPerStrand;
const idx = isFirstStrand ? i : i - countPerStrand;
const t = idx / countPerStrand; // 0 to 1
const angle = t * Math.PI * 2 * twists + time * 0.5;
const y = (t - 0.5) * h;
// 双螺旋相位差 180度
const phaseOffset = isFirstStrand ? 0 : Math.PI;
const currentAngle = angle + phaseOffset;
// 添加连接桥的动画偏移
const bridgeOffset = Math.sin(t * Math.PI * twists * 2 + time) * 0.5;
target.set(
Math.cos(currentAngle) * (r + bridgeOffset),
y,
Math.sin(currentAngle) * (r + bridgeOffset)
);
positions[i].lerp(target, 0.1);
dummy.position.copy(positions[i]);
dummy.lookAt(0, y, 0);
dummy.updateMatrix();
instancedMesh.setMatrixAt(i, dummy.matrix);
// 颜色:双链不同色 + 渐变
const hue = isFirstStrand ? 0.55 : 0.15; // 蓝 vs 橙
const lightness = 0.5 + Math.sin(t * Math.PI * 10 + time * 2) * 0.2;
color.setHSL(hue, 0.9, lightness);
instancedMesh.setColorAt(i, color);
}
instancedMesh.instanceMatrix.needsUpdate = true;
instancedMesh.instanceColor.needsUpdate = true;
controls.update();
composer.render();
}
animate();
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
composer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>