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

171 lines
7.8 KiB
HTML

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>06 - TORUS | 圆环</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: #ffde59;
font-size: 14px; pointer-events: none; z-index: 100;
text-shadow: 0 0 10px rgba(255,222,89,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: #ffde59; min-width: 40px; }
</style>
</head>
<body>
<div id="canvas-container"></div>
<div class="info">
<h1>06 - TORUS</h1>
<p>圆环面(甜甜圈)</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>主半径 R</label>
<input type="range" id="majorR" min="15" max="30" step="1" value="20">
<span id="majorVal">20</span>
</div>
<div class="control-group">
<label>管半径 r</label>
<input type="range" id="minorR" min="3" max="10" step="0.5" value="6">
<span id="minorVal">6</span>
</div>
<div class="control-group">
<label>波浪效果</label>
<input type="range" id="wave" min="0" max="3" step="0.1" value="1">
<span id="waveVal">1</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 = 8000;
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x000000, 0.008);
const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 2000);
camera.position.set(0, 40, 60);
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.8;
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.3; bloomPass.radius = 0.35; 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.OctahedronGeometry(0.3, 0);
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(0, 0, 0));
}
const majorRSlider = document.getElementById('majorR');
const minorRSlider = document.getElementById('minorR');
const waveSlider = document.getElementById('wave');
majorRSlider.addEventListener('input', (e) => document.getElementById('majorVal').textContent = e.target.value);
minorRSlider.addEventListener('input', (e) => document.getElementById('minorVal').textContent = e.target.value);
waveSlider.addEventListener('input', (e) => document.getElementById('waveVal').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(majorRSlider.value);
const r = parseFloat(minorRSlider.value);
const waveAmp = parseFloat(waveSlider.value);
for (let i = 0; i < COUNT; i++) {
// 斐波那契分布在圆环面上
const phi = Math.acos(-1 + (2 * i) / COUNT);
const theta = Math.sqrt(COUNT * Math.PI) * phi;
// 圆环参数方程
const u = theta % (Math.PI * 2);
const v = phi;
// 添加波浪变形
const wave = Math.sin(u * 8 + time * 2) * waveAmp;
const currentR = r + wave;
target.set(
(R + currentR * Math.cos(v)) * Math.cos(u),
currentR * Math.sin(v),
(R + currentR * Math.cos(v)) * Math.sin(u)
);
positions[i].lerp(target, 0.1);
dummy.position.copy(positions[i]);
dummy.rotation.set(time + u, time + v, 0);
dummy.updateMatrix();
instancedMesh.setMatrixAt(i, dummy.matrix);
// 彩虹色
const hue = (u / (Math.PI * 2) + time * 0.1) % 1;
color.setHSL(hue, 0.9, 0.5);
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>