| 1 | /* 登录页背景:巨构变换(Canvas 2D,零依赖)。 |
| 2 | 四种巨型形态——星球(球面)→ 环形都市(环面)→ 城市平原(起伏网格)→ |
| 3 | 波动巨碑(竖直巨墙)。结构持续缓慢自转(运动本身很便宜);形态切换不做 |
| 4 | 逐点插值变形(那才是卡顿来源),而是整体淡出 → 换形态 → 淡入,浮现时 |
| 5 | 带一点由小到大的生长感。透视投影、结构大于视口(巨物应当出画)。 |
| 6 | 性能:形态几何只在切换时重算进 TypedArray;点按亮度分桶、每桶一次 fill; |
| 7 | DPR 封顶 1.5。纯中性白灰;prefers-reduced-motion 时静止单帧。 */ |
| 8 | import { useEffect, useRef } from "react"; |
| 9 | |
| 10 | const U = 48; // 经向点数 |
| 11 | const V = 26; // 纬向点数 |
| 12 | const N = U * V; |
| 13 | const HOLD_MS = 7000; // 完全可见的停留 |
| 14 | const FADE_MS = 1400; // 淡出 / 淡入各自时长 |
| 15 | const ROT_SPEED = 0.000024; // 弧度/毫秒(约 260s 一周,巨物应当迟缓) |
| 16 | const BUCKETS = 12; // 点亮度分桶数 |
| 17 | |
| 18 | type Vec3 = [number, number, number]; |
| 19 | |
| 20 | /** 形态族:每个函数把 (u,v)∈[0,1) 映射到 ~[-1,1]³ */ |
| 21 | const FORMS: ((u: number, v: number) => Vec3)[] = [ |
| 22 | // 星球:球面 |
| 23 | (u, v) => { |
| 24 | const lon = u * Math.PI * 2; |
| 25 | const lat = (v - 0.5) * Math.PI * 0.92; |
| 26 | return [Math.cos(lat) * Math.cos(lon), Math.sin(lat) * 0.95, Math.cos(lat) * Math.sin(lon)]; |
| 27 | }, |
| 28 | // 环形都市:环面 |
| 29 | (u, v) => { |
| 30 | const a = u * Math.PI * 2; |
| 31 | const b = v * Math.PI * 2; |
| 32 | const R = 0.74; |
| 33 | const r = 0.32; |
| 34 | return [ |
| 35 | (R + r * Math.cos(b)) * Math.cos(a), |
| 36 | r * Math.sin(b) * 1.05, |
| 37 | (R + r * Math.cos(b)) * Math.sin(a), |
| 38 | ]; |
| 39 | }, |
| 40 | // 城市平原:起伏网格 |
| 41 | (u, v) => { |
| 42 | const x = (u - 0.5) * 2.5; |
| 43 | const z = (v - 0.5) * 2.5; |
| 44 | const y = Math.sin(x * 2.3) * 0.14 + Math.cos(z * 2.1 + x * 1.2) * 0.12 - 0.15; |
| 45 | return [x, y, z]; |
| 46 | }, |
| 47 | // 波动巨碑:竖直巨墙 |
| 48 | (u, v) => { |
| 49 | const x = (u - 0.5) * 2.3; |
| 50 | const y = (v - 0.5) * 1.5; |
| 51 | const z = Math.sin(x * 2.8 + y * 1.6) * 0.22; |
| 52 | return [x, y, z]; |
| 53 | }, |
| 54 | ]; |
| 55 | |
| 56 | function smoothstep(t: number): number { |
| 57 | return t * t * (3 - 2 * t); |
| 58 | } |
| 59 | |
| 60 | export function LoginScene({ leaving }: { leaving?: boolean }) { |
| 61 | const canvasRef = useRef<HTMLCanvasElement>(null); |
| 62 | |
| 63 | useEffect(() => { |
| 64 | const canvas = canvasRef.current; |
| 65 | if (!canvas) return; |
| 66 | const ctx = canvas.getContext("2d"); |
| 67 | if (!ctx) return; |
| 68 | |
| 69 | const reduced = window.matchMedia("(prefers-reduced-motion: reduce)").matches; |
| 70 | |
| 71 | // 点阵参数坐标 |
| 72 | const pu = new Float32Array(N); |
| 73 | const pv = new Float32Array(N); |
| 74 | for (let j = 0; j < V; j++) |
| 75 | for (let i = 0; i < U; i++) { |
| 76 | const k = j * U + i; |
| 77 | pu[k] = i / U; |
| 78 | pv[k] = j / (V - 1); |
| 79 | } |
| 80 | // 网格邻接边,扁平存储 |
| 81 | const edgeIdx: number[] = []; |
| 82 | for (let j = 0; j < V; j++) |
| 83 | for (let i = 0; i < U; i++) { |
| 84 | const a = j * U + i; |
| 85 | if (i < U - 1) edgeIdx.push(a, a + 1); |
| 86 | if (j < V - 1) edgeIdx.push(a, a + U); |
| 87 | } |
| 88 | const edges = new Int32Array(edgeIdx); |
| 89 | // u 首尾回绕边:只在 u 方向闭合的形态(球面/环面)上绘制—— |
| 90 | // 摊平形态(平原/巨碑)画它会横穿整个画面 |
| 91 | const wrapIdx: number[] = []; |
| 92 | for (let j = 0; j < V; j++) wrapIdx.push(j * U + U - 1, j * U); |
| 93 | const wrapEdges = new Int32Array(wrapIdx); |
| 94 | const U_CLOSED = [true, true, false, false]; // 与 FORMS 一一对应 |
| 95 | |
| 96 | // 当前形态几何:只在切换时重算,帧内零分配 |
| 97 | const fx = new Float32Array(N), fy = new Float32Array(N), fz = new Float32Array(N); |
| 98 | let cachedForm = -1; |
| 99 | const fillForm = (idx: number) => { |
| 100 | const map = FORMS[idx]; |
| 101 | for (let k = 0; k < N; k++) { |
| 102 | const P = map(pu[k], pv[k]); |
| 103 | fx[k] = P[0]; |
| 104 | fy[k] = P[1]; |
| 105 | fz[k] = P[2]; |
| 106 | } |
| 107 | cachedForm = idx; |
| 108 | }; |
| 109 | |
| 110 | const px = new Float32Array(N); |
| 111 | const py = new Float32Array(N); |
| 112 | const pa = new Float32Array(N); // 深度→亮度 0..1 |
| 113 | |
| 114 | // 亮度分桶:预生成样式串,帧内无字符串拼接 |
| 115 | const bucketStyle: string[] = []; |
| 116 | for (let b = 0; b < BUCKETS; b++) |
| 117 | bucketStyle.push(`rgba(255,255,255,${(0.1 + (b / (BUCKETS - 1)) * 0.34).toFixed(3)})`); |
| 118 | const buckets: number[][] = Array.from({ length: BUCKETS }, () => []); |
| 119 | |
| 120 | // 原地闪烁:弱化为氛围层(主秀是下面的光脉冲) |
| 121 | const TWINKLES = 40; |
| 122 | const twIdx = new Int32Array(TWINKLES); |
| 123 | const twPhase = new Float32Array(TWINKLES); |
| 124 | const twSpeed = new Float32Array(TWINKLES); |
| 125 | for (let n = 0; n < TWINKLES; n++) { |
| 126 | twIdx[n] = (n * 1013 + 389) % N; // 确定性伪随机散布 |
| 127 | twPhase[n] = ((n * 7919) % 628) / 100; // 0..2π |
| 128 | twSpeed[n] = 0.0008 + ((n * 271) % 100) / 100 * 0.0016; // 弧度/毫秒 |
| 129 | } |
| 130 | |
| 131 | // 光脉冲:从一个顶点沿边亮向另一个顶点,拖渐隐尾迹(信号在巨构上传导) |
| 132 | const PULSES = 12; |
| 133 | const TRAIL_MAX = 5; // 尾迹保留的节点数 |
| 134 | const TRAIL_LEN = 3.2; // 尾迹可见长度(边数) |
| 135 | type Pulse = { |
| 136 | trail: number[]; // 已过节点,旧→新 |
| 137 | next: number; // 正在亮向的节点 |
| 138 | t: number; // 当前边上的进度 0..1 |
| 139 | speed: number; // 边/毫秒 |
| 140 | edgesLeft: number; |
| 141 | fade: number; // 出生/消亡包络 |
| 142 | dying: boolean; |
| 143 | delay: number; // 重生倒计时(毫秒) |
| 144 | }; |
| 145 | const nbuf: number[] = []; |
| 146 | const neighborsOf = (k: number) => { |
| 147 | nbuf.length = 0; |
| 148 | const i = k % U; |
| 149 | const j = (k / U) | 0; |
| 150 | if (i > 0) nbuf.push(k - 1); |
| 151 | if (i < U - 1) nbuf.push(k + 1); |
| 152 | if (j > 0) nbuf.push(k - U); |
| 153 | if (j < V - 1) nbuf.push(k + U); |
| 154 | }; |
| 155 | const spawnPulse = (p: Pulse, first: boolean) => { |
| 156 | const k = (Math.random() * N) | 0; |
| 157 | neighborsOf(k); |
| 158 | p.trail = [k]; |
| 159 | p.next = nbuf[(Math.random() * nbuf.length) | 0]; |
| 160 | p.t = 0; |
| 161 | p.speed = (1.6 + Math.random() * 1.8) / 1000; |
| 162 | p.edgesLeft = 5 + ((Math.random() * 8) | 0); |
| 163 | p.fade = 0; |
| 164 | p.dying = false; |
| 165 | p.delay = first ? Math.random() * 4000 : 600 + Math.random() * 3500; |
| 166 | }; |
| 167 | const pulses: Pulse[] = []; |
| 168 | for (let n = 0; n < PULSES; n++) { |
| 169 | const p = {} as Pulse; |
| 170 | spawnPulse(p, true); |
| 171 | pulses.push(p); |
| 172 | } |
| 173 | |
| 174 | let raf = 0; |
| 175 | let w = 0; |
| 176 | let h = 0; |
| 177 | const dpr = Math.min(window.devicePixelRatio || 1, 1.5); |
| 178 | const resize = () => { |
| 179 | w = canvas.clientWidth; |
| 180 | h = canvas.clientHeight; |
| 181 | canvas.width = Math.max(1, Math.round(w * dpr)); |
| 182 | canvas.height = Math.max(1, Math.round(h * dpr)); |
| 183 | }; |
| 184 | resize(); |
| 185 | const ro = new ResizeObserver(resize); |
| 186 | ro.observe(canvas); |
| 187 | |
| 188 | let lastNow = -1; |
| 189 | const draw = (now: number) => { |
| 190 | const dt = lastNow < 0 ? 16 : Math.min(50, now - lastNow); |
| 191 | lastNow = now; |
| 192 | ctx.setTransform(dpr, 0, 0, dpr, 0, 0); |
| 193 | ctx.clearRect(0, 0, w, h); |
| 194 | |
| 195 | // 推进光脉冲(与场景可见性无关,淡出期间也在走) |
| 196 | for (const p of pulses) { |
| 197 | if (p.delay > 0) { |
| 198 | p.delay -= dt; |
| 199 | continue; |
| 200 | } |
| 201 | if (p.dying) { |
| 202 | p.fade -= dt / 350; |
| 203 | if (p.fade <= 0) spawnPulse(p, false); |
| 204 | continue; |
| 205 | } |
| 206 | p.fade = Math.min(1, p.fade + dt / 350); |
| 207 | p.t += p.speed * dt; |
| 208 | while (p.t >= 1) { |
| 209 | p.t -= 1; |
| 210 | const prev = p.trail[p.trail.length - 1]; |
| 211 | const cur = p.next; |
| 212 | p.trail.push(cur); |
| 213 | if (p.trail.length > TRAIL_MAX) p.trail.shift(); |
| 214 | if (--p.edgesLeft <= 0) { |
| 215 | p.dying = true; |
| 216 | break; |
| 217 | } |
| 218 | // 选下一条边:偏好直行(信号感),不走回头路 |
| 219 | const straight = cur + (cur - prev); |
| 220 | neighborsOf(cur); |
| 221 | if (nbuf.includes(straight) && Math.random() < 0.72) { |
| 222 | p.next = straight; |
| 223 | } else { |
| 224 | let pick = prev; |
| 225 | let seen = 0; |
| 226 | for (const cand of nbuf) { |
| 227 | if (cand === prev) continue; |
| 228 | seen++; |
| 229 | if (Math.random() < 1 / seen) pick = cand; |
| 230 | } |
| 231 | p.next = pick; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // 时间轴:淡入 → 停留 → 淡出,边界处(不可见时)切换形态 |
| 237 | const cycle = HOLD_MS + FADE_MS * 2; |
| 238 | const tt = now % (FORMS.length * cycle); |
| 239 | const slot = Math.floor(tt / cycle); |
| 240 | const tc = tt - slot * cycle; |
| 241 | if (slot !== cachedForm) fillForm(slot); |
| 242 | let vis: number; |
| 243 | if (tc < FADE_MS) vis = smoothstep(tc / FADE_MS); |
| 244 | else if (tc < FADE_MS + HOLD_MS) vis = 1; |
| 245 | else vis = 1 - smoothstep((tc - FADE_MS - HOLD_MS) / FADE_MS); |
| 246 | if (vis <= 0.004) { |
| 247 | if (!reduced) raf = requestAnimationFrame(draw); |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | const ry = now * ROT_SPEED; |
| 252 | const sinY = Math.sin(ry); |
| 253 | const cosY = Math.cos(ry); |
| 254 | const sinX = Math.sin(0.4); |
| 255 | const cosX = Math.cos(0.4); |
| 256 | |
| 257 | // 巨物尺度:大于视口;浮现时带一点生长感 |
| 258 | const scale = Math.max(w, h) * 0.62 * (0.96 + 0.04 * vis); |
| 259 | const cx = w * 0.5; |
| 260 | const cy = h * 0.66; |
| 261 | |
| 262 | for (let k = 0; k < N; k++) { |
| 263 | let x = fx[k]; |
| 264 | let y = fy[k]; |
| 265 | let z = fz[k]; |
| 266 | const x1 = x * cosY + z * sinY; |
| 267 | const z1 = -x * sinY + z * cosY; |
| 268 | const y1 = y * cosX - z1 * sinX; |
| 269 | const z2 = y * sinX + z1 * cosX; |
| 270 | x = x1; |
| 271 | y = y1; |
| 272 | z = z2; |
| 273 | const persp = 2.6 / (2.6 - z * 0.9); |
| 274 | px[k] = cx + x * scale * persp; |
| 275 | py[k] = cy - y * scale * persp; |
| 276 | pa[k] = Math.min(1, Math.max(0, (z + 1.15) / 2.1)); // 近处亮 |
| 277 | } |
| 278 | |
| 279 | // 整体可见度只动 globalAlpha 这一个旋钮 |
| 280 | ctx.globalAlpha = vis; |
| 281 | |
| 282 | // 边:极淡的白,一次 stroke |
| 283 | ctx.lineWidth = 1; |
| 284 | ctx.strokeStyle = "rgba(255,255,255,0.045)"; |
| 285 | ctx.beginPath(); |
| 286 | for (let e = 0; e < edges.length; e += 2) { |
| 287 | const a = edges[e]; |
| 288 | const b = edges[e + 1]; |
| 289 | ctx.moveTo(px[a], py[a]); |
| 290 | ctx.lineTo(px[b], py[b]); |
| 291 | } |
| 292 | if (U_CLOSED[slot]) { |
| 293 | for (let e = 0; e < wrapEdges.length; e += 2) { |
| 294 | const a = wrapEdges[e]; |
| 295 | const b = wrapEdges[e + 1]; |
| 296 | ctx.moveTo(px[a], py[a]); |
| 297 | ctx.lineTo(px[b], py[b]); |
| 298 | } |
| 299 | } |
| 300 | ctx.stroke(); |
| 301 | |
| 302 | // 点:按亮度分桶,每桶一次 fill(矩形,1~2px 下与圆不可分辨) |
| 303 | for (let b = 0; b < BUCKETS; b++) buckets[b].length = 0; |
| 304 | for (let k = 0; k < N; k++) { |
| 305 | const b = Math.min(BUCKETS - 1, (pa[k] * BUCKETS) | 0); |
| 306 | buckets[b].push(k); |
| 307 | } |
| 308 | for (let b = 0; b < BUCKETS; b++) { |
| 309 | const list = buckets[b]; |
| 310 | if (!list.length) continue; |
| 311 | ctx.fillStyle = bucketStyle[b]; |
| 312 | ctx.beginPath(); |
| 313 | for (let n = 0; n < list.length; n++) { |
| 314 | const k = list[n]; |
| 315 | const r = 0.8 + pa[k] * 1.1; |
| 316 | ctx.rect(px[k] - r, py[k] - r, r * 2, r * 2); |
| 317 | } |
| 318 | ctx.fill(); |
| 319 | } |
| 320 | |
| 321 | // 氛围闪烁:尖峰脉冲(sin⁶),弱化版 |
| 322 | ctx.fillStyle = "rgba(255,255,255,0.92)"; |
| 323 | for (let n = 0; n < TWINKLES; n++) { |
| 324 | const s = Math.sin(now * twSpeed[n] + twPhase[n]); |
| 325 | if (s <= 0) continue; |
| 326 | const glint = s * s * s * s * s * s; |
| 327 | if (glint < 0.02) continue; |
| 328 | const k = twIdx[n]; |
| 329 | ctx.globalAlpha = vis * glint * (0.35 + pa[k] * 0.65) * 0.5; |
| 330 | const r = 0.9 + glint * 1.3; |
| 331 | ctx.beginPath(); |
| 332 | ctx.arc(px[k], py[k], r, 0, Math.PI * 2); |
| 333 | ctx.fill(); |
| 334 | } |
| 335 | |
| 336 | // 光脉冲:头部亮点 + 沿走过的边渐隐的尾迹 |
| 337 | ctx.strokeStyle = "#ffffff"; |
| 338 | ctx.fillStyle = "#ffffff"; |
| 339 | ctx.lineWidth = 1.2; |
| 340 | for (const p of pulses) { |
| 341 | if (p.delay > 0 || p.fade <= 0) continue; |
| 342 | const tail = p.trail; |
| 343 | const cur = tail[tail.length - 1]; |
| 344 | const hx = px[cur] + (px[p.next] - px[cur]) * p.t; |
| 345 | const hy = py[cur] + (py[p.next] - py[cur]) * p.t; |
| 346 | const base = vis * p.fade * (0.35 + pa[cur] * 0.65); |
| 347 | |
| 348 | // 尾迹:先画 cur→头部 的半段,再逐段回溯,按距头部的边数渐隐 |
| 349 | let x2 = hx; |
| 350 | let y2 = hy; |
| 351 | let dist = 0; // 段中点距头部的边数 |
| 352 | for (let s = tail.length - 1; s >= 0; s--) { |
| 353 | const k = tail[s]; |
| 354 | const segLen = s === tail.length - 1 ? p.t : 1; |
| 355 | const a = Math.max(0, 1 - (dist + segLen / 2) / TRAIL_LEN); |
| 356 | if (a <= 0.01) break; |
| 357 | ctx.globalAlpha = base * a * 0.55; |
| 358 | ctx.beginPath(); |
| 359 | ctx.moveTo(px[k], py[k]); |
| 360 | ctx.lineTo(x2, y2); |
| 361 | ctx.stroke(); |
| 362 | x2 = px[k]; |
| 363 | y2 = py[k]; |
| 364 | dist += segLen; |
| 365 | } |
| 366 | |
| 367 | // 头部:光晕 + 亮核 |
| 368 | ctx.globalAlpha = base * 0.16; |
| 369 | ctx.beginPath(); |
| 370 | ctx.arc(hx, hy, 3.8, 0, Math.PI * 2); |
| 371 | ctx.fill(); |
| 372 | ctx.globalAlpha = base * 0.95; |
| 373 | ctx.beginPath(); |
| 374 | ctx.arc(hx, hy, 1.5, 0, Math.PI * 2); |
| 375 | ctx.fill(); |
| 376 | } |
| 377 | ctx.globalAlpha = 1; |
| 378 | |
| 379 | if (!reduced) raf = requestAnimationFrame(draw); |
| 380 | }; |
| 381 | |
| 382 | raf = requestAnimationFrame(draw); |
| 383 | return () => { |
| 384 | cancelAnimationFrame(raf); |
| 385 | ro.disconnect(); |
| 386 | }; |
| 387 | }, []); |
| 388 | |
| 389 | return ( |
| 390 | <canvas |
| 391 | ref={canvasRef} |
| 392 | className={`pointer-events-none fixed inset-0 h-full w-full ${ |
| 393 | leaving ? "u-scene-depart" : "" |
| 394 | }`} |
| 395 | aria-hidden |
| 396 | /> |
| 397 | ); |
| 398 | } |