312 lines
10 KiB
JavaScript
312 lines
10 KiB
JavaScript
// SDMU 教务助手 · Hero 粒子背景引擎
|
|
// 基于 Three.js,滚动驱动相机/场景变换
|
|
|
|
(() => {
|
|
if (typeof THREE === 'undefined') {
|
|
console.warn('Three.js not loaded; skipping hero particles.');
|
|
return;
|
|
}
|
|
|
|
const canvas = document.getElementById('hero-particles');
|
|
if (!canvas) return;
|
|
|
|
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true, alpha: true });
|
|
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
|
|
renderer.setSize(canvas.clientWidth, canvas.clientHeight);
|
|
renderer.setClearColor(0x000000, 0);
|
|
|
|
const scene = new THREE.Scene();
|
|
const camera = new THREE.PerspectiveCamera(60, canvas.clientWidth / canvas.clientHeight, 0.1, 2000);
|
|
camera.position.z = 600;
|
|
|
|
const isDark = () => document.documentElement.classList.contains('dark');
|
|
|
|
const palettes = {
|
|
light: {
|
|
accent: '#E6A23C',
|
|
primary: '#111111',
|
|
muted: '#111111',
|
|
bg: '#F5F2EA'
|
|
},
|
|
dark: {
|
|
accent: '#E6A23C',
|
|
primary: '#ffffff',
|
|
muted: '#ffffff',
|
|
bg: '#0a0a0a'
|
|
}
|
|
};
|
|
|
|
let currentPalette = isDark() ? palettes.dark : palettes.light;
|
|
const colorAccent = new THREE.Color(currentPalette.accent);
|
|
const colorPrimary = new THREE.Color(currentPalette.primary);
|
|
const colorMuted = new THREE.Color(currentPalette.muted);
|
|
const colorLight = new THREE.Color(currentPalette.bg);
|
|
|
|
const COUNT = 2200;
|
|
const pos = new Float32Array(COUNT * 3);
|
|
const colors = new Float32Array(COUNT * 3);
|
|
const sizes = new Float32Array(COUNT);
|
|
const phases = new Float32Array(COUNT);
|
|
|
|
for (let i = 0; i < COUNT; i++) {
|
|
const r = Math.pow(Math.random(), 0.55) * 760;
|
|
const a = Math.random() * Math.PI * 2;
|
|
const y = (Math.random() - 0.5) * 360;
|
|
|
|
pos[i * 3] = Math.cos(a) * r;
|
|
pos[i * 3 + 1] = y;
|
|
pos[i * 3 + 2] = Math.sin(a) * r;
|
|
phases[i] = Math.random() * Math.PI * 2;
|
|
|
|
const roll = Math.random();
|
|
const c = roll > 0.95 ? colorAccent : colorPrimary;
|
|
colors[i * 3] = c.r;
|
|
colors[i * 3 + 1] = c.g;
|
|
colors[i * 3 + 2] = c.b;
|
|
sizes[i] = Math.random() * 2.6 + 1.2;
|
|
}
|
|
|
|
const geo = new THREE.BufferGeometry();
|
|
geo.setAttribute('position', new THREE.BufferAttribute(pos, 3));
|
|
geo.setAttribute('color', new THREE.BufferAttribute(colors, 3));
|
|
geo.setAttribute('size', new THREE.BufferAttribute(sizes, 1));
|
|
geo.setAttribute('phase', new THREE.BufferAttribute(phases, 1));
|
|
|
|
const mat = new THREE.ShaderMaterial({
|
|
uniforms: {
|
|
uTime: { value: 0 },
|
|
uPixelRatio: { value: Math.min(window.devicePixelRatio, 2) },
|
|
uMouse: { value: new THREE.Vector3(0, 0, 0) }
|
|
},
|
|
vertexShader: `
|
|
attribute float size;
|
|
attribute vec3 color;
|
|
attribute float phase;
|
|
varying vec3 vColor;
|
|
uniform float uTime;
|
|
uniform float uPixelRatio;
|
|
uniform vec3 uMouse;
|
|
void main() {
|
|
vec3 p = position;
|
|
float t = uTime * 0.6 + phase;
|
|
p.y += sin(t + position.x * 0.012) * 8.0;
|
|
p.x += cos(t * 0.7 + position.z * 0.012) * 5.0;
|
|
p.z += sin(t * 0.5 + position.y * 0.015) * 4.0;
|
|
float mouseDist = length(p - uMouse);
|
|
float mouseGlow = smoothstep(120.0, 0.0, mouseDist);
|
|
vColor = color + vec3(mouseGlow * 0.35);
|
|
vec4 mv = modelViewMatrix * vec4(p, 1.0);
|
|
gl_PointSize = size * uPixelRatio * (300.0 / -mv.z) * (1.0 + mouseGlow * 1.2);
|
|
gl_Position = projectionMatrix * mv;
|
|
}
|
|
`,
|
|
fragmentShader: `
|
|
varying vec3 vColor;
|
|
void main() {
|
|
float d = length(gl_PointCoord - 0.5);
|
|
if (d > 0.5) discard;
|
|
float alpha = 1.0 - d * 2.2;
|
|
gl_FragColor = vec4(vColor, alpha * 0.85);
|
|
}
|
|
`,
|
|
transparent: true,
|
|
depthWrite: false,
|
|
blending: THREE.NormalBlending
|
|
});
|
|
|
|
const points = new THREE.Points(geo, mat);
|
|
scene.add(points);
|
|
|
|
// Subtle connection lines
|
|
const MAX_LINKS = 1800;
|
|
const linkPos = new Float32Array(MAX_LINKS * 6);
|
|
const linkColors = new Float32Array(MAX_LINKS * 6);
|
|
const linkGeo = new THREE.BufferGeometry();
|
|
linkGeo.setAttribute('position', new THREE.BufferAttribute(linkPos, 3));
|
|
linkGeo.setAttribute('color', new THREE.BufferAttribute(linkColors, 3));
|
|
const linkMat = new THREE.LineBasicMaterial({
|
|
vertexColors: true,
|
|
transparent: true,
|
|
opacity: 0.12,
|
|
blending: THREE.NormalBlending,
|
|
depthWrite: false
|
|
});
|
|
const linkMesh = new THREE.LineSegments(linkGeo, linkMat);
|
|
scene.add(linkMesh);
|
|
|
|
const RM = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
const FINE = window.matchMedia('(hover: hover) and (pointer: fine)').matches;
|
|
|
|
let mouseWorld = new THREE.Vector3(0, 0, 0);
|
|
let raycaster = new THREE.Raycaster();
|
|
let mousePlane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);
|
|
let mouseV = new THREE.Vector2();
|
|
let mouseX = 0, mouseY = 0;
|
|
|
|
if (FINE) {
|
|
window.addEventListener('pointermove', (e) => {
|
|
mouseX = (e.clientX / window.innerWidth - 0.5) * 2;
|
|
mouseY = (e.clientY / window.innerHeight - 0.5) * 2;
|
|
mouseV.set((e.clientX / window.innerWidth) * 2 - 1, -(e.clientY / window.innerHeight) * 2 + 1);
|
|
raycaster.setFromCamera(mouseV, camera);
|
|
const hit = new THREE.Vector3();
|
|
raycaster.ray.intersectPlane(mousePlane, hit);
|
|
if (hit) mouseWorld.copy(hit);
|
|
}, { passive: true });
|
|
}
|
|
|
|
// Scroll-driven transform: map scroll progress to camera/scene motion
|
|
let scrollProgress = 0;
|
|
function updateScroll() {
|
|
const docH = document.documentElement.scrollHeight - window.innerHeight;
|
|
scrollProgress = docH > 0 ? Math.min(window.scrollY / docH, 1) : 0;
|
|
}
|
|
window.addEventListener('scroll', updateScroll, { passive: true });
|
|
updateScroll();
|
|
|
|
let isVisible = true;
|
|
const visObs = new IntersectionObserver((entries) => {
|
|
isVisible = entries[0].isIntersecting;
|
|
}, { threshold: 0 });
|
|
visObs.observe(canvas);
|
|
|
|
const tempColor = new THREE.Color();
|
|
const cellSize = 70;
|
|
const grid = new Map();
|
|
|
|
function updateLinks() {
|
|
grid.clear();
|
|
for (let i = 0; i < COUNT; i++) {
|
|
const x = pos[i * 3], y = pos[i * 3 + 1], z = pos[i * 3 + 2];
|
|
const key = `${Math.floor(x / cellSize)},${Math.floor(y / cellSize)},${Math.floor(z / cellSize)}`;
|
|
if (!grid.has(key)) grid.set(key, []);
|
|
grid.get(key).push(i);
|
|
}
|
|
|
|
let linkIdx = 0;
|
|
const linkThreshold = 92;
|
|
const linkThresholdSq = linkThreshold * linkThreshold;
|
|
|
|
for (let i = 0; i < COUNT; i++) {
|
|
if (linkIdx >= MAX_LINKS) break;
|
|
const ix = pos[i * 3], iy = pos[i * 3 + 1], iz = pos[i * 3 + 2];
|
|
const cx = Math.floor(ix / cellSize), cy = Math.floor(iy / cellSize), cz = Math.floor(iz / cellSize);
|
|
|
|
for (let dx = -1; dx <= 1; dx++) {
|
|
for (let dy = -1; dy <= 1; dy++) {
|
|
for (let dz = -1; dz <= 1; dz++) {
|
|
const key = `${cx + dx},${cy + dy},${cz + dz}`;
|
|
const cell = grid.get(key);
|
|
if (!cell) continue;
|
|
for (const j of cell) {
|
|
if (j <= i) continue;
|
|
if (linkIdx >= MAX_LINKS) break;
|
|
const jx = pos[j * 3], jy = pos[j * 3 + 1], jz = pos[j * 3 + 2];
|
|
const dx_ = ix - jx, dy_ = iy - jy, dz_ = iz - jz;
|
|
const distSq = dx_ * dx_ + dy_ * dy_ + dz_ * dz_;
|
|
if (distSq < linkThresholdSq) {
|
|
const alpha = 1.0 - Math.sqrt(distSq) / linkThreshold;
|
|
linkPos[linkIdx * 6] = ix;
|
|
linkPos[linkIdx * 6 + 1] = iy;
|
|
linkPos[linkIdx * 6 + 2] = iz;
|
|
linkPos[linkIdx * 6 + 3] = jx;
|
|
linkPos[linkIdx * 6 + 4] = jy;
|
|
linkPos[linkIdx * 6 + 5] = jz;
|
|
tempColor.setRGB(colors[i * 3], colors[i * 3 + 1], colors[i * 3 + 2]);
|
|
tempColor.lerp(colorLight, 1.0 - alpha);
|
|
linkColors[linkIdx * 6] = tempColor.r;
|
|
linkColors[linkIdx * 6 + 1] = tempColor.g;
|
|
linkColors[linkIdx * 6 + 2] = tempColor.b;
|
|
linkColors[linkIdx * 6 + 3] = tempColor.r;
|
|
linkColors[linkIdx * 6 + 4] = tempColor.g;
|
|
linkColors[linkIdx * 6 + 5] = tempColor.b;
|
|
linkIdx++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
linkGeo.setDrawRange(0, linkIdx * 2);
|
|
linkGeo.attributes.position.needsUpdate = true;
|
|
linkGeo.attributes.color.needsUpdate = true;
|
|
}
|
|
|
|
updateLinks();
|
|
|
|
// Theme change detection
|
|
const themeObs = new MutationObserver(() => {
|
|
const p = isDark() ? palettes.dark : palettes.light;
|
|
colorAccent.set(p.accent);
|
|
colorPrimary.set(p.primary);
|
|
colorMuted.set(p.muted);
|
|
colorLight.set(p.bg);
|
|
for (let i = 0; i < COUNT; i++) {
|
|
const roll = Math.random();
|
|
const c = roll > 0.88 ? colorAccent : roll > 0.42 ? colorPrimary : colorMuted;
|
|
colors[i * 3] = c.r;
|
|
colors[i * 3 + 1] = c.g;
|
|
colors[i * 3 + 2] = c.b;
|
|
}
|
|
geo.attributes.color.needsUpdate = true;
|
|
updateLinks();
|
|
});
|
|
themeObs.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] });
|
|
|
|
function resize() {
|
|
const rect = canvas.getBoundingClientRect();
|
|
const w = rect.width;
|
|
const h = rect.height;
|
|
camera.aspect = w / h;
|
|
camera.updateProjectionMatrix();
|
|
renderer.setSize(w, h);
|
|
mat.uniforms.uPixelRatio.value = Math.min(window.devicePixelRatio, 2);
|
|
updateLinks();
|
|
}
|
|
window.addEventListener('resize', resize);
|
|
|
|
let rafId = null;
|
|
function loop(t) {
|
|
if (!isVisible) {
|
|
rafId = requestAnimationFrame(loop);
|
|
return;
|
|
}
|
|
|
|
if (RM) {
|
|
mat.uniforms.uTime.value = 0;
|
|
camera.position.set(0, 0, 600);
|
|
camera.lookAt(0, 0, 0);
|
|
renderer.render(scene, camera);
|
|
return;
|
|
}
|
|
|
|
const time = t * 0.001;
|
|
mat.uniforms.uTime.value = time;
|
|
mat.uniforms.uMouse.value.copy(mouseWorld);
|
|
|
|
// Scroll-driven motion
|
|
const ease = 1 - Math.pow(1 - scrollProgress, 3);
|
|
points.rotation.y = time * 0.045 + ease * Math.PI * 0.5;
|
|
points.rotation.x = Math.sin(time * 0.08) * 0.04 + ease * 0.15;
|
|
linkMesh.rotation.y = points.rotation.y;
|
|
linkMesh.rotation.x = points.rotation.x;
|
|
|
|
camera.position.z = 600 - ease * 400;
|
|
camera.position.x = mouseX * 30 + ease * 80;
|
|
camera.position.y = -mouseY * 20 - ease * 60;
|
|
camera.lookAt(0, 0, 0);
|
|
|
|
renderer.render(scene, camera);
|
|
rafId = requestAnimationFrame(loop);
|
|
}
|
|
|
|
if (RM) {
|
|
renderer.render(scene, camera);
|
|
} else {
|
|
rafId = requestAnimationFrame(loop);
|
|
}
|
|
|
|
console.log('SDMU hero particles mounted. dark:', isDark(), 'RM:', RM);
|
|
})();
|