Branchwell

AI Resource Pro / website · airesourcepro.com / src / components / home / particle-field.ts

1import * as THREE from "three";
2import { arrow, brainPts, clusterPts, drawPts, film, galaxyPts, glyph } from "./particle-shapes";
3import { FRAGMENT_SHADER, VERTEX_SHADER } from "./particle-shaders";
4
5// One fixed WebGL field of outlined triangles that morphs into each home section's shape as you scroll:
6// brain, >_, { }, cursor arrow, film frame, two clouds, galaxy.
7// Per-section hint colour and how many particles pick it up (the brain stays pure blue and white).
8const HINT: [string, number][] = [["#FFFFFF", 0], ["#7CC4F2", 0.3], ["#BFE3FA", 0.3], ["#3A86E8", 0.35], ["#46AAE9", 0.2], ["#BFE3FA", 0.3], ["#FFFFFF", 0.15]];
9
10export type Pointer = { nx: number; ny: number; active: boolean };
11
12const smooth = (a: number, b: number, x: number) => {
13 const t = Math.min(1, Math.max(0, (x - a) / (b - a)));
14 return t * t * (3 - 2 * t);
15};
16
17export function createParticleField(canvas: HTMLCanvasElement, monoFont: string) {
18 let renderer: THREE.WebGLRenderer;
19 try {
20 renderer = new THREE.WebGLRenderer({ canvas, antialias: false, powerPreference: "high-performance" });
21 } catch (e) {
22 console.warn("Particle field disabled: WebGL is unavailable.", e);
23 return null;
24 }
25 // Colours are authored in display values, so skip three's linear colour conversion
26 THREE.ColorManagement.enabled = false;
27
28 const mobile = () => innerWidth < 900;
29 const N = mobile() ? 9000 : 18000, NA = Math.floor(N * 0.12), M = N - NA, K = HINT.length;
30 const brain = brainPts(M), galaxy = galaxyPts(M);
31 const normals = [brain.points, drawPts(glyph(">_", monoFont), M), drawPts(glyph("{ }", monoFont), M), drawPts(arrow, M), drawPts(film, M), clusterPts(M), galaxy.points];
32 let shapes: Float32Array[] = [], offs: number[][] = [], layout = { visW: 1, visH: 1 };
33
34 function buildShapes() {
35 const camZ = 12, visH = 2 * camZ * Math.tan(Math.PI / 9), visW = visH * (innerWidth / innerHeight);
36 const mob = mobile();
37 const size = mob ? Math.min(visW * 0.9, visH * 0.42) : Math.min(visH * 0.66, visW * 0.36);
38 const side = mob ? 0 : visW * 0.235, yOff = mob ? -visH * 0.16 : 0;
39 layout = { visH, visW };
40 // Narrow screens held sideways (landscape phones, fold-outs, small tablets) keep the stacked layout but give
41 // the brain the desktop size and spot, since a width-based brain would be tiny on a short screen
42 const brainMob = mob && visW / visH < 1.2;
43 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);
44 const brainOff = brainMob ? [0, -visH * 0.17, 0] : [visW * 0.2, -visH * 0.01, 0];
45 const galS = mob ? visW * 0.66 : Math.min(visW * 0.42, visH * 0.8);
46 const scales = [brainS, size, size, size * 0.95, size * 1.05, size * (mob ? 0.7 : 1.25), galS];
47 offs = mob
48 ? [brainOff, [0, yOff, 0], [0, yOff, 0], [0, yOff, 0], [0, yOff, 0], [0, -visH * 0.37, 0], [0, 0, 0]]
49 : [brainOff, [-side, 0, 0], [side, 0, 0], [-side, 0, 0], [side, 0, 0], [side, 0, 0], [0, -visH * 0.24, 0]];
50 const amb = new Float32Array(NA * 3);
51 for (let i = 0; i < NA; i++) {
52 amb[i * 3] = (Math.random() * 1.24 - 0.62) * visW * 1.1;
53 amb[i * 3 + 1] = (Math.random() * 1.24 - 0.62) * visH * 1.1;
54 amb[i * 3 + 2] = Math.random() * 7.5 - 5;
55 }
56 shapes = normals.map((src, k) => {
57 const a = new Float32Array(N * 3); a.set(amb, 0);
58 for (let i = 0; i < M * 3; i++) a[NA * 3 + i] = src[i] * scales[k];
59 return a;
60 });
61 }
62
63 const dpr = Math.min(devicePixelRatio || 1, 2);
64 renderer.setPixelRatio(dpr); renderer.setSize(innerWidth, innerHeight, false); renderer.setClearColor(0x0d1117, 1);
65 const scene = new THREE.Scene(), cam = new THREE.PerspectiveCamera(40, innerWidth / innerHeight, 0.1, 100);
66 cam.position.z = 12;
67 buildShapes();
68
69 const geo = new THREE.BufferGeometry();
70 geo.setAttribute("position", new THREE.BufferAttribute(new Float32Array(N * 3), 3));
71 geo.setAttribute("aFrom", new THREE.BufferAttribute(new Float32Array(shapes[0]), 3));
72 geo.setAttribute("aTo", new THREE.BufferAttribute(new Float32Array(shapes[1]), 3));
73 const rand = new Float32Array(N * 4), ambF = new Float32Array(N), shade = new Float32Array(N * 2);
74 for (let i = 0; i < N; i++) {
75 for (let j = 0; j < 4; j++) rand[i * 4 + j] = Math.random();
76 ambF[i] = i < NA ? 1 : 0;
77 if (i >= NA) { shade[i * 2] = brain.shade[i - NA]; shade[i * 2 + 1] = galaxy.shade[i - NA]; }
78 }
79 geo.setAttribute("aRand", new THREE.BufferAttribute(rand, 4));
80 geo.setAttribute("aAmb", new THREE.BufferAttribute(ambF, 1));
81 geo.setAttribute("aShade", new THREE.BufferAttribute(shade, 2));
82 const uniforms = {
83 uTime: { value: 0 }, uMix: { value: 0 }, uPx: { value: (mobile() ? 8 : 9) * dpr }, uVel: { value: 0 },
84 uDim: { value: mobile() ? 0.62 : 1 }, uBrain: { value: 1 }, uGal: { value: 0 }, uZoomA: { value: 1 }, uSpinB: { value: 0 },
85 uMouse: { value: new THREE.Vector2(99, 99) }, uRot: { value: new THREE.Matrix3() },
86 uOffA: { value: new THREE.Vector3() }, uOffB: { value: new THREE.Vector3() },
87 uHintA: { value: new THREE.Color(HINT[0][0]) }, uHintB: { value: new THREE.Color(HINT[1][0]) }, uHA: { value: 0 }, uHB: { value: 0 },
88 };
89 const mat = new THREE.ShaderMaterial({
90 uniforms, transparent: true, depthWrite: false, blending: THREE.AdditiveBlending,
91 vertexShader: VERTEX_SHADER, fragmentShader: FRAGMENT_SHADER,
92 });
93 const pts = new THREE.Points(geo, mat);
94 pts.frustumCulled = false;
95 scene.add(pts);
96
97 let cur0 = -1, msx = 0, msy = 0;
98 const euler = new THREE.Euler(0, 0, 0, "XYZ"), rotM4 = new THREE.Matrix4(), mouseTarget = new THREE.Vector2();
99 // [tilt, turn] for each section's shape; the brain faces forward-left and sways around its own centre
100 const pose = (k: number, t: number) => {
101 if (k === 0) return [0.26 - msy * 0.12, 0.42 + Math.sin(t * 0.25) * 0.3 + msx * 0.25];
102 if (k === K - 1) return [0.82 - msy * 0.08, msx * 0.1];
103 return [-msy * 0.1, Math.sin(t * 0.3) * 0.14 + msx * 0.2];
104 };
105
106 return {
107 // stage runs from 0 (hero) to K - 1 (last section); fractions are mid-transition
108 render(stage: number, t: number, vel: number, pointer: Pointer) {
109 const i = Math.min(Math.floor(stage), K - 2), f = stage - i;
110 // the brain holds for the first screen and a half before it bursts
111 const m = i === 0 ? smooth(0.5, 0.92, f) : smooth(0.12, 0.88, f);
112 if (i !== cur0) {
113 (geo.attributes.aFrom as THREE.BufferAttribute).copyArray(shapes[i]); geo.attributes.aFrom.needsUpdate = true;
114 (geo.attributes.aTo as THREE.BufferAttribute).copyArray(shapes[i + 1]); geo.attributes.aTo.needsUpdate = true;
115 uniforms.uHintA.value.set(HINT[i][0]); uniforms.uHintB.value.set(HINT[i + 1][0]);
116 uniforms.uHA.value = HINT[i][1]; uniforms.uHB.value = HINT[i + 1][1];
117 cur0 = i;
118 }
119 const mx = pointer.active ? pointer.nx : 0, my = pointer.active ? pointer.ny : 0;
120 msx += (mx - msx) * 0.05; msy += (my - msy) * 0.05;
121 uniforms.uMix.value = m; uniforms.uTime.value = t; uniforms.uVel.value = vel;
122 uniforms.uBrain.value = i === 0 ? 1 - m : 0;
123 uniforms.uGal.value = i === K - 2 ? m : 0;
124 uniforms.uZoomA.value = i === 0 ? 1 + 0.14 * smooth(0, 0.5, f) : 1;
125 uniforms.uSpinB.value = i + 1 === K - 1 ? t * 0.05 : 0;
126 const A = pose(i, t), B = pose(i + 1, t);
127 euler.set(A[0] + (B[0] - A[0]) * m, A[1] + (B[1] - A[1]) * m, 0);
128 rotM4.makeRotationFromEuler(euler); uniforms.uRot.value.setFromMatrix4(rotM4);
129 uniforms.uOffA.value.fromArray(offs[i]); uniforms.uOffB.value.fromArray(offs[i + 1]);
130 if (pointer.active) mouseTarget.set((mx * layout.visW) / 2, (my * layout.visH) / 2);
131 else mouseTarget.set(99, 99);
132 uniforms.uMouse.value.lerp(mouseTarget, 0.12);
133 cam.position.x += (msx * 0.35 - cam.position.x) * 0.04; cam.position.y += (msy * 0.25 - cam.position.y) * 0.04;
134 cam.lookAt(0, 0, 0);
135 renderer.render(scene, cam);
136 },
137 resize() {
138 renderer.setSize(innerWidth, innerHeight, false);
139 cam.aspect = innerWidth / innerHeight; cam.updateProjectionMatrix();
140 buildShapes(); cur0 = -1;
141 },
142 dispose() {
143 geo.dispose(); mat.dispose(); renderer.dispose();
144 },
145 };
146}
147
148export type ParticleField = NonNullable<ReturnType<typeof createParticleField>>;