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

156 lines
6.9 KiB
HTML

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>08 - NAVE | 飞船导航</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: #4ade80;
font-size: 14px; pointer-events: none; z-index: 100;
text-shadow: 0 0 10px rgba(74,222,128,0.5);
}
.info h1 { font-size: 20px; margin-bottom: 8px; }
.info p { opacity: 0.8; font-size: 12px; margin: 4px 0; }
</style>
</head>
<body>
<div id="canvas-container"></div>
<div class="info">
<h1>08 - NAVE</h1>
<p>飞船导航箭头 + 星空尾迹</p>
<p>粒子数: <span id="count">0</span></p>
<p>FPS: <span id="fps">0</span></p>
</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.005);
const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 2000);
camera.position.set(0, 30, 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.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.ConeGeometry(0.2, 0.6, 4);
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 arrowPoints = [];
// 箭头头部(三角形)
for (let y = 0; y < 15; y++) {
const width = (15 - y) * 0.8;
for (let x = -width; x <= width; x += 0.5) {
arrowPoints.push({x: x, y: y + 5, z: 0});
}
}
// 箭尾
for (let y = -15; y < 5; y++) {
for (let x = -2; x <= 2; x += 0.5) {
arrowPoints.push({x: x, y: y, z: 0});
}
}
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();
for (let i = 0; i < COUNT; i++) {
if (i < arrowPoints.length) {
// 主箭头形状
const pt = arrowPoints[i];
// 波浪运动
const waveZ = Math.sin(time * 3 + pt.y * 0.3) * 2;
const waveX = Math.cos(time * 2 + pt.y * 0.2) * 1;
target.set(pt.x + waveX, pt.y, pt.z + waveZ);
dummy.position.copy(positions[i].lerp(target, 0.1));
// 箭头指向运动方向
dummy.lookAt(target.x, target.y + 1, target.z);
} else {
// 尾迹粒子 - 流动效果
const trailIdx = i - arrowPoints.length;
const t = trailIdx / (COUNT - arrowPoints.length);
const angle = t * Math.PI * 2 + time * 0.5;
const radius = 20 + t * 30;
const y = -30 + t * 20 + Math.sin(time * 2 + t * 10) * 5;
target.set(Math.cos(angle) * radius, y, Math.sin(angle) * radius);
dummy.position.copy(positions[i].lerp(target, 0.05));
dummy.lookAt(0, y + 10, 0);
}
dummy.updateMatrix();
instancedMesh.setMatrixAt(i, dummy.matrix);
// 颜色:箭头亮绿,尾迹渐变
const isArrow = i < arrowPoints.length;
const hue = isArrow ? 0.35 : 0.3 + (i / COUNT) * 0.2;
const lightness = isArrow ? 0.6 : 0.3 + Math.sin(time + i * 0.01) * 0.1;
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>