import * as THREE from "three"; import { arrow, brainPts, clusterPts, drawPts, film, galaxyPts, glyph } from "./particle-shapes"; import { FRAGMENT_SHADER, VERTEX_SHADER } from "./particle-shaders"; // One fixed WebGL field of outlined triangles that morphs into each home section's shape as you scroll: // brain, >_, { }, cursor arrow, film frame, two clouds, galaxy. // Per-section hint colour and how many particles pick it up (the brain stays pure blue and white). const HINT: [string, number][] = [["#FFFFFF", 0], ["#7CC4F2", 0.3], ["#BFE3FA", 0.3], ["#3A86E8", 0.35], ["#46AAE9", 0.2], ["#BFE3FA", 0.3], ["#FFFFFF", 0.15]]; export type Pointer = { nx: number; ny: number; active: boolean }; const smooth = (a: number, b: number, x: number) => { const t = Math.min(1, Math.max(0, (x - a) / (b - a))); return t * t * (3 - 2 * t); }; export function createParticleField(canvas: HTMLCanvasElement, monoFont: string) { let renderer: THREE.WebGLRenderer; try { renderer = new THREE.WebGLRenderer({ canvas, antialias: false, powerPreference: "high-performance" }); } catch (e) { console.warn("Particle field disabled: WebGL is unavailable.", e); return null; } // Colours are authored in display values, so skip three's linear colour conversion THREE.ColorManagement.enabled = false; const mobile = () => innerWidth < 900; const N = mobile() ? 9000 : 18000, NA = Math.floor(N * 0.12), M = N - NA, K = HINT.length; const brain = brainPts(M), galaxy = galaxyPts(M); const normals = [brain.points, drawPts(glyph(">_", monoFont), M), drawPts(glyph("{ }", monoFont), M), drawPts(arrow, M), drawPts(film, M), clusterPts(M), galaxy.points]; let shapes: Float32Array[] = [], offs: number[][] = [], layout = { visW: 1, visH: 1 }; function buildShapes() { const camZ = 12, visH = 2 * camZ * Math.tan(Math.PI / 9), visW = visH * (innerWidth / innerHeight); const mob = mobile(); const size = mob ? Math.min(visW * 0.9, visH * 0.42) : Math.min(visH * 0.66, visW * 0.36); const side = mob ? 0 : visW * 0.235, yOff = mob ? -visH * 0.16 : 0; layout = { visH, visW }; // Narrow screens held sideways (landscape phones, fold-outs, small tablets) keep the stacked layout but give // the brain the desktop size and spot, since a width-based brain would be tiny on a short screen const brainMob = mob && visW / visH < 1.2; const brainS = brainMob ? Math.min((visW * 0.92) / 3, (visH * 0.42) / 2.3) : Math.min((visW * 0.5) / 3, (visH * 0.8) / 2.3); const brainOff = brainMob ? [0, -visH * 0.17, 0] : [visW * 0.2, -visH * 0.01, 0]; const galS = mob ? visW * 0.66 : Math.min(visW * 0.42, visH * 0.8); const scales = [brainS, size, size, size * 0.95, size * 1.05, size * (mob ? 0.7 : 1.25), galS]; offs = mob ? [brainOff, [0, yOff, 0], [0, yOff, 0], [0, yOff, 0], [0, yOff, 0], [0, -visH * 0.37, 0], [0, 0, 0]] : [brainOff, [-side, 0, 0], [side, 0, 0], [-side, 0, 0], [side, 0, 0], [side, 0, 0], [0, -visH * 0.24, 0]]; const amb = new Float32Array(NA * 3); for (let i = 0; i < NA; i++) { amb[i * 3] = (Math.random() * 1.24 - 0.62) * visW * 1.1; amb[i * 3 + 1] = (Math.random() * 1.24 - 0.62) * visH * 1.1; amb[i * 3 + 2] = Math.random() * 7.5 - 5; } shapes = normals.map((src, k) => { const a = new Float32Array(N * 3); a.set(amb, 0); for (let i = 0; i < M * 3; i++) a[NA * 3 + i] = src[i] * scales[k]; return a; }); } const dpr = Math.min(devicePixelRatio || 1, 2); renderer.setPixelRatio(dpr); renderer.setSize(innerWidth, innerHeight, false); renderer.setClearColor(0x0d1117, 1); const scene = new THREE.Scene(), cam = new THREE.PerspectiveCamera(40, innerWidth / innerHeight, 0.1, 100); cam.position.z = 12; buildShapes(); const geo = new THREE.BufferGeometry(); geo.setAttribute("position", new THREE.BufferAttribute(new Float32Array(N * 3), 3)); geo.setAttribute("aFrom", new THREE.BufferAttribute(new Float32Array(shapes[0]), 3)); geo.setAttribute("aTo", new THREE.BufferAttribute(new Float32Array(shapes[1]), 3)); const rand = new Float32Array(N * 4), ambF = new Float32Array(N), shade = new Float32Array(N * 2); for (let i = 0; i < N; i++) { for (let j = 0; j < 4; j++) rand[i * 4 + j] = Math.random(); ambF[i] = i < NA ? 1 : 0; if (i >= NA) { shade[i * 2] = brain.shade[i - NA]; shade[i * 2 + 1] = galaxy.shade[i - NA]; } } geo.setAttribute("aRand", new THREE.BufferAttribute(rand, 4)); geo.setAttribute("aAmb", new THREE.BufferAttribute(ambF, 1)); geo.setAttribute("aShade", new THREE.BufferAttribute(shade, 2)); const uniforms = { uTime: { value: 0 }, uMix: { value: 0 }, uPx: { value: (mobile() ? 8 : 9) * dpr }, uVel: { value: 0 }, uDim: { value: mobile() ? 0.62 : 1 }, uBrain: { value: 1 }, uGal: { value: 0 }, uZoomA: { value: 1 }, uSpinB: { value: 0 }, uMouse: { value: new THREE.Vector2(99, 99) }, uRot: { value: new THREE.Matrix3() }, uOffA: { value: new THREE.Vector3() }, uOffB: { value: new THREE.Vector3() }, uHintA: { value: new THREE.Color(HINT[0][0]) }, uHintB: { value: new THREE.Color(HINT[1][0]) }, uHA: { value: 0 }, uHB: { value: 0 }, }; const mat = new THREE.ShaderMaterial({ uniforms, transparent: true, depthWrite: false, blending: THREE.AdditiveBlending, vertexShader: VERTEX_SHADER, fragmentShader: FRAGMENT_SHADER, }); const pts = new THREE.Points(geo, mat); pts.frustumCulled = false; scene.add(pts); let cur0 = -1, msx = 0, msy = 0; const euler = new THREE.Euler(0, 0, 0, "XYZ"), rotM4 = new THREE.Matrix4(), mouseTarget = new THREE.Vector2(); // [tilt, turn] for each section's shape; the brain faces forward-left and sways around its own centre const pose = (k: number, t: number) => { if (k === 0) return [0.26 - msy * 0.12, 0.42 + Math.sin(t * 0.25) * 0.3 + msx * 0.25]; if (k === K - 1) return [0.82 - msy * 0.08, msx * 0.1]; return [-msy * 0.1, Math.sin(t * 0.3) * 0.14 + msx * 0.2]; }; return { // stage runs from 0 (hero) to K - 1 (last section); fractions are mid-transition render(stage: number, t: number, vel: number, pointer: Pointer) { const i = Math.min(Math.floor(stage), K - 2), f = stage - i; // the brain holds for the first screen and a half before it bursts const m = i === 0 ? smooth(0.5, 0.92, f) : smooth(0.12, 0.88, f); if (i !== cur0) { (geo.attributes.aFrom as THREE.BufferAttribute).copyArray(shapes[i]); geo.attributes.aFrom.needsUpdate = true; (geo.attributes.aTo as THREE.BufferAttribute).copyArray(shapes[i + 1]); geo.attributes.aTo.needsUpdate = true; uniforms.uHintA.value.set(HINT[i][0]); uniforms.uHintB.value.set(HINT[i + 1][0]); uniforms.uHA.value = HINT[i][1]; uniforms.uHB.value = HINT[i + 1][1]; cur0 = i; } const mx = pointer.active ? pointer.nx : 0, my = pointer.active ? pointer.ny : 0; msx += (mx - msx) * 0.05; msy += (my - msy) * 0.05; uniforms.uMix.value = m; uniforms.uTime.value = t; uniforms.uVel.value = vel; uniforms.uBrain.value = i === 0 ? 1 - m : 0; uniforms.uGal.value = i === K - 2 ? m : 0; uniforms.uZoomA.value = i === 0 ? 1 + 0.14 * smooth(0, 0.5, f) : 1; uniforms.uSpinB.value = i + 1 === K - 1 ? t * 0.05 : 0; const A = pose(i, t), B = pose(i + 1, t); euler.set(A[0] + (B[0] - A[0]) * m, A[1] + (B[1] - A[1]) * m, 0); rotM4.makeRotationFromEuler(euler); uniforms.uRot.value.setFromMatrix4(rotM4); uniforms.uOffA.value.fromArray(offs[i]); uniforms.uOffB.value.fromArray(offs[i + 1]); if (pointer.active) mouseTarget.set((mx * layout.visW) / 2, (my * layout.visH) / 2); else mouseTarget.set(99, 99); uniforms.uMouse.value.lerp(mouseTarget, 0.12); cam.position.x += (msx * 0.35 - cam.position.x) * 0.04; cam.position.y += (msy * 0.25 - cam.position.y) * 0.04; cam.lookAt(0, 0, 0); renderer.render(scene, cam); }, resize() { renderer.setSize(innerWidth, innerHeight, false); cam.aspect = innerWidth / innerHeight; cam.updateProjectionMatrix(); buildShapes(); cur0 = -1; }, dispose() { geo.dispose(); mat.dispose(); renderer.dispose(); }, }; } export type ParticleField = NonNullable>;