Spaces:
Running
Running
File size: 12,424 Bytes
dc445bf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | /* Cartesian Corp — Three.js components.
* <div data-three="stack" data-layers='[...]'> interactive architecture stack (Services)
* <div data-three="globe"> dotted globe with delivery arcs (About, Contact)
* <div data-three="orb"> AI "thinking" orb (AI Scoper)
* Each component renders only while visible and degrades to nothing if WebGL is unavailable. */
(function () {
if (typeof THREE === 'undefined') return;
const reduced = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
const COL = { navy: 0x0b1d2e, blue: 0x2b4cff, blue2: 0x7d97ff, soft: 0xeef1ff, coral: 0xff5a36, grey: 0x8795a6 };
function setup(el, fov = 40) {
const canvas = document.createElement('canvas');
canvas.setAttribute('aria-hidden', 'true');
el.appendChild(canvas);
let renderer;
try { renderer = new THREE.WebGLRenderer({ canvas, antialias: true, alpha: true }); } catch (e) { canvas.remove(); return null; }
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setClearColor(0x000000, 0);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(fov, 1, .1, 100);
const resize = () => {
const w = el.clientWidth, h = el.clientHeight;
if (!w || !h) return;
renderer.setSize(w, h, false); camera.aspect = w / h; camera.updateProjectionMatrix();
};
if (window.ResizeObserver) new ResizeObserver(resize).observe(el); else window.addEventListener('resize', resize);
resize();
let visible = false;
new IntersectionObserver(([e]) => { visible = e.isIntersecting; }).observe(el);
const mouse = { x: 0, y: 0, tx: 0, ty: 0, px: null, py: null };
el.addEventListener('pointermove', (e) => {
const r = el.getBoundingClientRect();
mouse.tx = (e.clientX - r.left) / r.width * 2 - 1; mouse.ty = (e.clientY - r.top) / r.height * 2 - 1;
mouse.px = mouse.tx; mouse.py = -mouse.ty;
});
el.addEventListener('pointerleave', () => { mouse.tx = 0; mouse.ty = 0; mouse.px = null; });
return {
renderer, scene, camera, mouse,
loop(fn) {
const clock = new THREE.Clock();
const tick = () => {
requestAnimationFrame(tick);
if (!visible) return;
mouse.x += (mouse.tx - mouse.x) * .06; mouse.y += (mouse.ty - mouse.y) * .06;
fn(reduced ? 2 : clock.getElapsedTime());
renderer.render(scene, camera);
};
tick();
el.classList.add('three-ready');
}
};
}
/* ---------------- Architecture stack ---------------- */
function stack(el) {
const g = setup(el, 32); if (!g) return;
const { scene, camera, mouse } = g;
camera.position.set(5.9, 5.1, 6.8); camera.lookAt(0, 1.2, 0);
scene.add(new THREE.AmbientLight(0xffffff, .75));
const dl = new THREE.DirectionalLight(0xffffff, .55); dl.position.set(4, 8, 5); scene.add(dl);
const root = new THREE.Group(); scene.add(root);
const n = JSON.parse(el.dataset.layers || '[]').length || 5;
const GAP = .62, S = 3.1;
const layers = [];
for (let i = 0; i < n; i++) {
const geo = new THREE.BoxGeometry(S, .14, S);
const mat = new THREE.MeshStandardMaterial({ color: 0xffffff, transparent: true, opacity: .9, roughness: .6, metalness: 0 });
const mesh = new THREE.Mesh(geo, mat);
const edges = new THREE.LineSegments(new THREE.EdgesGeometry(geo), new THREE.LineBasicMaterial({ color: COL.blue, transparent: true, opacity: .55 }));
mesh.add(edges);
// inner grid on each layer to read as a "plane of components"
const grid = new THREE.GridHelper(S * .9, 6, COL.blue2, COL.blue2);
grid.position.y = .075; grid.material.transparent = true; grid.material.opacity = .25;
mesh.add(grid);
mesh.position.y = i * GAP;
mesh.userData = { i, base: i * GAP, lift: 0 };
root.add(mesh); layers.push({ mesh, mat, edges, grid });
}
// vertical "data" particles flowing up through the layers
const P = 70, pGeo = new THREE.BufferGeometry(), pPos = new Float32Array(P * 3), seeds = [];
for (let i = 0; i < P; i++) seeds.push({ x: (Math.random() - .5) * S * .8, z: (Math.random() - .5) * S * .8, o: Math.random(), s: .18 + Math.random() * .2 });
pGeo.setAttribute('position', new THREE.BufferAttribute(pPos, 3));
const pts = new THREE.Points(pGeo, new THREE.PointsMaterial({ color: COL.coral, size: .11, transparent: true, opacity: .95 }));
root.add(pts);
// corner pillars
const pillarMat = new THREE.LineBasicMaterial({ color: COL.blue, transparent: true, opacity: .2 });
[[-1, -1], [1, -1], [-1, 1], [1, 1]].forEach(([a, b]) => {
const geo = new THREE.BufferGeometry().setFromPoints([new THREE.Vector3(a * S / 2, 0, b * S / 2), new THREE.Vector3(a * S / 2, (n - 1) * GAP, b * S / 2)]);
root.add(new THREE.Line(geo, pillarMat));
});
let active = -1, hoverActive = -1, lastAuto = 0;
const setActive = (i) => { active = i; el.dispatchEvent(new CustomEvent('stack:active', { detail: i })); };
el.addEventListener('stack:hover', (e) => { hoverActive = e.detail; });
const ray = new THREE.Raycaster(), v2 = new THREE.Vector2();
g.loop((t) => {
// pointer picking
let picked = -1;
if (mouse.px !== null) {
v2.set(mouse.px, mouse.py); ray.setFromCamera(v2, camera);
const hit = ray.intersectObjects(layers.map((l) => l.mesh))[0];
if (hit) picked = hit.object.userData.i;
}
let want = hoverActive >= 0 ? hoverActive : picked;
if (want < 0) { if (t - lastAuto > 2.6) { lastAuto = t; want = (active + 1) % n; } else want = active; } else lastAuto = t;
if (want !== active) setActive(want);
layers.forEach(({ mesh, mat, edges, grid }, i) => {
const on = i === active;
mesh.userData.lift += ((on ? .22 : 0) - mesh.userData.lift) * .12;
mesh.position.y = mesh.userData.base + mesh.userData.lift;
mat.color.lerp(new THREE.Color(on ? COL.blue : 0xffffff), .12);
edges.material.opacity += ((on ? 1 : .5) - edges.material.opacity) * .12;
grid.material.opacity += ((on ? .5 : .22) - grid.material.opacity) * .12;
});
const top = (n - 1) * GAP;
seeds.forEach((s, i) => {
const y = ((s.o + t * s.s) % 1) * top;
pPos[i * 3] = s.x; pPos[i * 3 + 1] = y; pPos[i * 3 + 2] = s.z;
});
pGeo.attributes.position.needsUpdate = true;
root.rotation.y = -.35 + Math.sin(t * .25) * .12 + mouse.x * .35;
root.rotation.x = mouse.y * .06;
});
}
/* ---------------- Globe ---------------- */
function globe(el) {
const g = setup(el, 34); if (!g) return;
const { scene, camera, mouse } = g;
camera.position.set(0, .15, 7.2);
const root = new THREE.Group(); root.rotation.x = .6; scene.add(root);
const R = 1.75;
// occluder so the back of the globe is hidden
root.add(new THREE.Mesh(new THREE.SphereGeometry(R * .985, 48, 48), new THREE.MeshBasicMaterial({ color: new THREE.Color(el.dataset.bg || '#ffffff') })));
const N = 1800, pos = new Float32Array(N * 3);
for (let i = 0; i < N; i++) {
const y = 1 - (i / (N - 1)) * 2, r = Math.sqrt(1 - y * y), th = Math.PI * (3 - Math.sqrt(5)) * i;
pos[i * 3] = Math.cos(th) * r * R; pos[i * 3 + 1] = y * R; pos[i * 3 + 2] = Math.sin(th) * r * R;
}
const dg = new THREE.BufferGeometry(); dg.setAttribute('position', new THREE.BufferAttribute(pos, 3));
root.add(new THREE.Points(dg, new THREE.PointsMaterial({ color: COL.navy, size: .028, transparent: true, opacity: .55 })));
// equator + meridian rings
const ringMat = new THREE.LineBasicMaterial({ color: COL.blue, transparent: true, opacity: .18 });
const circle = (rx) => { const pts = []; for (let a = 0; a <= 64; a++) { const th = a / 64 * Math.PI * 2; pts.push(new THREE.Vector3(Math.cos(th) * R * 1.003, 0, Math.sin(th) * R * 1.003)); } const l = new THREE.Line(new THREE.BufferGeometry().setFromPoints(pts), ringMat); l.rotation.x = rx; return l; };
root.add(circle(0)); root.add(circle(Math.PI / 2));
const ll = (lat, lon, r = R) => { const phi = (90 - lat) * Math.PI / 180, th = (lon + 180) * Math.PI / 180; return new THREE.Vector3(-r * Math.sin(phi) * Math.cos(th), r * Math.cos(phi), r * Math.sin(phi) * Math.sin(th)); };
const cities = [[17.4, 78.5], [51.5, -.1], [50.1, 8.7], [40.7, -74], [37.8, -122.4], [1.35, 103.8], [25.2, 55.3]];
const marks = cities.map(([la, lo], i) => {
const p = ll(la, lo);
const m = new THREE.Mesh(new THREE.SphereGeometry(.045, 16, 16), new THREE.MeshBasicMaterial({ color: i === 0 ? COL.coral : COL.blue }));
m.position.copy(p); root.add(m);
const ring = new THREE.Mesh(new THREE.RingGeometry(.06, .075, 32), new THREE.MeshBasicMaterial({ color: i === 0 ? COL.coral : COL.blue, transparent: true, side: THREE.DoubleSide }));
ring.position.copy(ll(la, lo, R * 1.005)); ring.lookAt(0, 0, 0); root.add(ring);
return { p, ring, ph: i * .7 };
});
const pairs = [[0, 1], [0, 2], [0, 3], [1, 3], [3, 4], [0, 5], [0, 6], [2, 6]];
const arcs = pairs.map(([a, b], k) => {
const A = marks[a].p, B = marks[b].p;
const mid = A.clone().add(B).multiplyScalar(.5);
const h = 1 + A.distanceTo(B) * .16;
const curve = new THREE.QuadraticBezierCurve3(A, mid.normalize().multiplyScalar(R * h), B);
const pts = curve.getPoints(60);
const line = new THREE.Line(new THREE.BufferGeometry().setFromPoints(pts), new THREE.LineBasicMaterial({ color: COL.blue, transparent: true, opacity: .45 }));
root.add(line);
const comet = new THREE.Mesh(new THREE.SphereGeometry(.032, 12, 12), new THREE.MeshBasicMaterial({ color: COL.coral }));
root.add(comet);
return { curve, comet, off: k * .37 };
});
g.loop((t) => {
root.rotation.y = -2.27 + Math.sin(t * .12) * .9 + mouse.x * .5;
root.rotation.x = .6 + mouse.y * .15;
marks.forEach((m) => { const s = 1 + ((t * .8 + m.ph) % 1) * 1.6; m.ring.scale.set(s, s, s); m.ring.material.opacity = 1 - ((t * .8 + m.ph) % 1); });
arcs.forEach((a) => { a.comet.position.copy(a.curve.getPoint((t * .22 + a.off) % 1)); });
});
}
/* ---------------- Thinking orb ---------------- */
function orb(el) {
const g = setup(el, 36); if (!g) return;
const { scene, camera, mouse } = g;
camera.position.set(0, 0, 4.2);
const geo = new THREE.IcosahedronGeometry(1, 3);
const base = geo.attributes.position.array.slice();
const wire = new THREE.Mesh(geo, new THREE.MeshBasicMaterial({ color: COL.blue, wireframe: true, transparent: true, opacity: .16 }));
const dots = new THREE.Points(geo, new THREE.PointsMaterial({ color: COL.blue, size: .035, transparent: true, opacity: .85 }));
const core = new THREE.Mesh(new THREE.SphereGeometry(.42, 32, 32), new THREE.MeshBasicMaterial({ color: COL.coral, transparent: true, opacity: .9 }));
const grp = new THREE.Group(); grp.add(wire, dots, core); scene.add(grp);
let energy = 0, target = 0, phase = 0, last = 0;
window.addEventListener('cc:thinking', (e) => { target = e.detail ? 1 : 0; });
g.loop((t) => {
const dt = Math.min(.05, t - last); last = t;
energy += (target - energy) * .05;
phase += dt * (0.6 + energy * 3.2);
const p = geo.attributes.position.array, amp = .06 + energy * .16;
for (let i = 0; i < p.length; i += 3) {
const x = base[i], y = base[i + 1], z = base[i + 2];
const d = 1 + amp * Math.sin(x * 3.1 + phase) * Math.cos(y * 2.7 + phase * 1.3) + amp * .6 * Math.sin(z * 4 + phase * .7);
p[i] = x * d; p[i + 1] = y * d; p[i + 2] = z * d;
}
geo.attributes.position.needsUpdate = true;
grp.rotation.y = phase * .35 + mouse.x * .4;
grp.rotation.x = phase * .15 + mouse.y * .3;
const s = 1 + Math.sin(phase * 2) * .05 * (1 + energy);
core.scale.set(s, s, s);
wire.material.opacity = .14 + energy * .16;
});
}
const C = { stack, globe, orb };
document.querySelectorAll('[data-three]').forEach((el) => {
const fn = C[el.dataset.three];
if (!fn) return;
// lazy init when the element first approaches the viewport
const io = new IntersectionObserver(([e]) => { if (e.isIntersecting) { io.disconnect(); fn(el); } }, { rootMargin: '200px' });
io.observe(el);
});
})();
|