155 lines
7.0 KiB
HTML
155 lines
7.0 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>10 - GALAXY | 螺旋星系</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: #ffd700;
|
|
font-size: 14px; pointer-events: none; z-index: 100;
|
|
text-shadow: 0 0 10px rgba(255,215,0,0.8);
|
|
}
|
|
.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>10 - GALAXY</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 = 10000;
|
|
const ARMS = 4; // 4条旋臂
|
|
|
|
const scene = new THREE.Scene();
|
|
scene.fog = new THREE.FogExp2(0x000000, 0.003);
|
|
const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 2000);
|
|
camera.position.set(0, 80, 0); // 俯视角度
|
|
|
|
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.2;
|
|
|
|
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.5; 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 geometry = new THREE.SphereGeometry(0.2, 6, 6);
|
|
const material = new THREE.MeshBasicMaterial({ color: 0xffffff });
|
|
const instancedMesh = new THREE.InstancedMesh(geometry, material, COUNT);
|
|
instancedMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
|
|
scene.add(instancedMesh);
|
|
|
|
// 星系数据
|
|
const stars = [];
|
|
for (let i = 0; i < COUNT; i++) {
|
|
stars.push({
|
|
arm: Math.floor(Math.random() * ARMS),
|
|
dist: Math.pow(Math.random(), 0.7) * 50, // 更多粒子在外围
|
|
angleOffset: (Math.random() - 0.5) * 0.5, // 旋臂宽度
|
|
height: (Math.random() - 0.5) * (2 + Math.random() * 3), // 盘面厚度
|
|
speed: 0,
|
|
size: 0.5 + Math.random() * 1.5,
|
|
type: Math.random() < 0.1 ? 'blue' : (Math.random() < 0.3 ? 'red' : 'yellow') // 恒星类型
|
|
});
|
|
}
|
|
|
|
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++) {
|
|
const s = stars[i];
|
|
|
|
// 对数螺旋: r = a * e^(b*θ)
|
|
const armAngle = (s.arm / ARMS) * Math.PI * 2;
|
|
const spiralTightness = 0.3;
|
|
const angle = armAngle + s.dist * spiralTightness * 0.1 + time * (20 / (s.dist + 5)) + s.angleOffset;
|
|
|
|
// 旋转
|
|
const currentAngle = angle + time * 0.1;
|
|
|
|
// 位置
|
|
const x = Math.cos(currentAngle) * s.dist;
|
|
const z = Math.sin(currentAngle) * s.dist;
|
|
const y = s.height * Math.exp(-s.dist / 20) + Math.sin(time + s.dist * 0.1) * 0.5;
|
|
|
|
dummy.position.set(x, y, z);
|
|
dummy.scale.setScalar(s.size * (1 + Math.sin(time * 2 + i) * 0.2));
|
|
dummy.updateMatrix();
|
|
instancedMesh.setMatrixAt(i, dummy.matrix);
|
|
|
|
// 颜色:基于恒星类型和距离
|
|
let hue, sat, light;
|
|
if (s.dist < 8) {
|
|
// 中心核球: 黄白色
|
|
hue = 0.08; sat = 0.3; light = 0.7;
|
|
} else if (s.type === 'blue') {
|
|
hue = 0.6; sat = 0.8; light = 0.6; // 蓝巨星
|
|
} else if (s.type === 'red') {
|
|
hue = 0.02; sat = 0.9; light = 0.5; // 红矮星
|
|
} else {
|
|
// 主序星: 黄白色渐变
|
|
hue = 0.12 + (s.dist / 50) * 0.05;
|
|
sat = 0.6;
|
|
light = 0.5 + (1 - s.dist / 50) * 0.2;
|
|
}
|
|
color.setHSL(hue, sat, light);
|
|
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>
|