Spaces:
Running

earakely-scale's picture isaacng-scale's picture
Cooperation Bench
3de9c7a
Raw History Blame Contribute Delete
9.97 kB
/* Three small 3D dioramas for the rule cards: white model on a dark studio, like the film.
Needs three.js r128 (global THREE). Falls back to the SVG scenes when WebGL is not available. */
(() => {
const live = new Set(), LOOP = 9;
const C = { clay: 0xf1ede6, plinth: 0xcfc8bd, dark: 0xeaeaea, glow: 0xe8604f, ok: 0x4fae7b, bad: 0xe0503f, tan: 0xa8927c, green: 0x193a29, dim: 0x55524e };
const ease = x => x < 0 ? 0 : x > 1 ? 1 : x * x * (3 - 2 * x), seg = (t, a, b) => ease((t - a) / (b - a));
function mount(el, kind) {
const T = window.THREE; if (!T) return false;
let renderer; try { renderer = new T.WebGLRenderer({ antialias: true, alpha: false }); } catch (e) { return false; }
const W = () => el.clientWidth || 320, H = () => el.clientHeight || 180;
renderer.setPixelRatio(Math.min(2, devicePixelRatio || 1)); renderer.setSize(W(), H()); renderer.setClearColor(C.dark);
renderer.shadowMap.enabled = true; renderer.shadowMap.type = T.PCFSoftShadowMap; el.appendChild(renderer.domElement);
const scene = new T.Scene(), cam = new T.OrthographicCamera(-1, 1, 1, -1, 0.1, 100);
const fit = () => { const a = W() / H(), s = Math.max(3.3, 5.9 / a); cam.left = -s * a; cam.right = s * a; cam.top = s; cam.bottom = -s; cam.updateProjectionMatrix(); renderer.setSize(W(), H()); };
cam.position.set(9, 8.2, 10); cam.lookAt(0, 1.1, 0); fit();
scene.add(new T.HemisphereLight(0xffffff, 0xc4c4c4, 0.78));
const key = new T.DirectionalLight(0xfff4e6, 0.72); key.position.set(-6, 11, 5); key.castShadow = true; key.shadow.mapSize.set(1024, 1024);
Object.assign(key.shadow.camera, { left: -9, right: 9, top: 9, bottom: -9, near: 1, far: 30 }); key.shadow.bias = -0.0015; scene.add(key); { const g = new T.Mesh(new T.PlaneGeometry(60, 60), new T.ShadowMaterial({ opacity: 0.16 })); g.rotation.x = -Math.PI / 2; g.position.y = 0.001; g.receiveShadow = true; scene.add(g); }
const M = (c, o) => new T.MeshStandardMaterial(Object.assign({ color: c, roughness: 0.82, metalness: 0 }, o));
const box = (w, h, d, x, y, z, c, o) => { const m = new T.Mesh(new T.BoxGeometry(w, h, d), M(c == null ? C.clay : c, o)); m.position.set(x, y, z); m.castShadow = m.receiveShadow = true; scene.add(m); return m; };
const person = (x, z, seated) => { const g = new T.Group(), body = new T.Mesh(new T.CylinderGeometry(0.2, 0.26, seated ? 0.62 : 1.0, 20), M(C.clay)), head = new T.Mesh(new T.SphereGeometry(0.2, 20, 16), M(C.clay));
body.position.y = seated ? 0.31 : 0.5; head.position.y = (seated ? 0.62 : 1.0) + 0.22; body.castShadow = head.castShadow = true; g.add(body, head); g.position.set(x, 0.3, z); scene.add(g); return g; };
const plinth = (x, z, w, d) => box(w, 0.3, d, x, 0.15, z, C.plinth);
const glowM = c => new T.MeshBasicMaterial({ color: c });
const agent = (x, y, z) => { const g = new T.Group(), core = new T.Mesh(new T.OctahedronGeometry(0.26), glowM(0x111111)), halo = new T.Mesh(new T.SphereGeometry(0.42, 20, 16), new T.MeshBasicMaterial({ color: C.glow, transparent: true, opacity: 0.34 })), l = new T.PointLight(C.glow, 0.9, 5);
g.add(core, halo, l); g.position.set(x, y, z); scene.add(g); g.userData.core = core; return g; };
const tube = (pts, c, r) => { const curve = new T.CatmullRomCurve3(pts.map(p => new T.Vector3(p[0], p[1], p[2]))), S = 48, R = 8, m = new T.Mesh(new T.TubeGeometry(curve, S, r || 0.035, R, false), glowM(c == null ? C.glow : c)); scene.add(m);
m.userData = { curve, grow: p => m.geometry.setDrawRange(0, Math.floor(Math.max(0, Math.min(1, p)) * S) * R * 6) }; m.userData.grow(0); return m; };
const bead = c => { const m = new T.Mesh(new T.SphereGeometry(0.13, 16, 12), glowM(c == null ? C.glow : c)); scene.add(m); m.visible = false; return m; };
const tile = (x, y, z) => { const m = box(0.7, 0.7, 0.12, x, y, z, 0xfafafa, { emissive: C.glow, emissiveIntensity: 0 }); m.rotation.y = 0.72; return m; };
const label = (txt, bg) => { const cv = document.createElement("canvas"); cv.width = 256; cv.height = 96; const g = cv.getContext("2d"); g.fillStyle = bg; g.fillRect(0, 0, 256, 96); g.fillStyle = "#fff"; g.font = "600 46px 'Geist Mono', ui-monospace, monospace"; g.textAlign = "center"; g.textBaseline = "middle"; g.fillText(txt, 128, 50); return new T.CanvasTexture(cv); };
let tick;
if (kind === 1) { // the agent acts for one employee
plinth(-1.6, 0.4, 5.2, 4.2); box(1.6, 0.5, 0.8, -2.4, 0.55, 0.6); person(-2.4, 1.5, true); box(0.5, 0.36, 0.06, -2.4, 0.98, 0.5);
plinth(3.6, -1.2, 2.2, 2.2); person(3.6, -1.2, false); const wall = box(0.12, 1.3, 2.4, 2.3, 0.95, -1.2, C.clay, { transparent: true, opacity: 0.9 });
const ag = agent(-1.3, 1.6, 0.9), tiles = [tile(-0.2, 3.3, -1.6), tile(0.8, 2.6, -0.5), tile(1.5, 1.8, 0.7)];
const lines = tiles.map(t => tube([[-1.3, 1.6, 0.9], [(t.position.x - 1.3) / 2, t.position.y + 0.3, (t.position.z + 0.9) / 2], [t.position.x, t.position.y, t.position.z]], 0x1f8a55, 0.04)); tiles.forEach(t => t.material.emissive.setHex(0x1f8a55));
const probe = tube([[-1.3, 1.6, 0.9], [0.6, 3.9, 0.2], [2.25, 1.4, -1.1]], C.bad, 0.03);
tick = t => { ag.position.x = -1.3 + (1 - seg(t, 0, .1)) * 5; ag.visible = t < .96; ag.userData.core.rotation.y = t * 12;
tiles.forEach((tl, k) => { const p = seg(t, .14 + k * .1, .24 + k * .1) * (1 - seg(t, .92, .98)); lines[k].userData.grow(p); tl.material.emissiveIntensity = p * 0.55; tl.position.y += (0) ; tl.scale.setScalar(1 + 0.12 * Math.sin(Math.min(1, p) * Math.PI)); });
const pr = seg(t, .52, .66) * (1 - seg(t, .78, .86)); probe.userData.grow(pr); wall.material.color.setHex(t > .64 && t < .86 ? C.bad : C.clay); };
} else if (kind === 2) { // people answer only through real channels
plinth(-3.2, 0.6, 2.8, 3); box(1.3, 0.5, 0.7, -3.3, 0.55, 0.5); const ag = agent(-3.2, 1.5, 0.6);
plinth(3.3, -0.4, 2.8, 3); const js = person(3.3, -0.4, false); box(0.9, 0.7, 0.5, 4.1, 0.65, -1.1);
const ch = tile(0, 2.3, 0.1); ch.scale.setScalar(1.5); ch.material.emissive.setHex(0x1f8a55); box(0.14, 1.7, 0.14, 0, 0.85, 0.1, C.dim);
const P = [[-3.2, 1.5, 0.6], [-1.6, 2.5, 0.4], [0, 2.3, 0.1], [1.7, 2.4, -0.1], [3.3, 1.75, -0.4]], path = tube(P, 0x8a857f, 0.026); path.userData.grow(1);
const direct = tube([[-3.2, 1.5, 0.6], [0, 4.6, 0.1], [3.3, 1.75, -0.4]], C.bad, 0.02), b = bead(), cur = path.userData.curve;
tick = t => { ag.userData.core.rotation.y = t * 12; const d = seg(t, .03, .13) * (1 - seg(t, .16, .22)); direct.userData.grow(d);
const go = seg(t, .24, .46), back = seg(t, .64, .86); b.visible = (t > .24 && t < .47) || (t > .64 && t < .88);
const u = t < .5 ? go : 1 - back; b.position.copy(cur.getPoint(Math.max(0, Math.min(1, u)))); b.material.color.setHex(0x1f8a55);
ch.material.emissiveIntensity = Math.max(0, 0.6 - Math.abs(u - 0.5) * 3) * (b.visible ? 1 : 0);
js.position.y = 0.3 + (t > .46 && t < .64 ? Math.abs(Math.sin((t - .46) * 52)) * 0.12 : 0); js.rotation.y = t > .46 && t < .9 ? -0.9 : 0; };
} else { // the correct answer changes mid-run
plinth(0, 0.2, 7.6, 4.4); box(1.9, 1.3, 1.6, -2.3, 0.95, -0.7); const roof = box(2.2, 0.14, 1.9, -2.3, 1.67, -0.7); person(-0.8, 1.2, false);
const board = new T.Group(), mats = [M(C.clay), M(C.clay), M(C.clay), M(C.clay), new T.MeshBasicMaterial({ map: label("HOLD", "#A8927C") }), (() => { const tx = label("BOOK", "#193A29"); tx.center.set(.5, .5); tx.rotation = Math.PI; return new T.MeshBasicMaterial({ map: tx }); })()];
const panel = new T.Mesh(new T.BoxGeometry(2.3, 0.9, 0.1), mats); panel.castShadow = true; board.add(panel); board.position.set(1.9, 2.5, -0.9); board.rotation.y = 0.72; scene.add(board); box(0.1, 1.9, 0.1, 1.9, 1.2, -0.9, C.dim);
const track = tube([[-3.4, 0.34, 1.9], [0, 0.34, 1.9], [3.4, 0.34, 1.9]], 0x8a857f, 0.025); track.userData.grow(1); const b = bead(0x111111); b.visible = true;
const env = box(0.6, 0.06, 0.42, 0.2, 6, 1.9, 0xffffff, { emissive: 0xffffff, emissiveIntensity: 0.25 }), ring = new T.Mesh(new T.TorusGeometry(0.42, 0.03, 8, 40), glowM(C.glow)); ring.rotation.x = Math.PI / 2; ring.position.set(0.2, 0.36, 1.9); scene.add(ring);
tick = t => { b.position.set(-3.4 + 6.8 * Math.min(1, t / .82), 0.36, 1.9); const drop = seg(t, .3, .44); env.position.y = 6 - 5.6 * drop; env.visible = t < .95; env.rotation.y = (1 - drop) * 2;
const f = seg(t, .44, .56) * (1 - seg(t, .94, 1)); panel.rotation.x = Math.PI * f; ring.scale.setScalar(1 + seg(t, .44, .7) * 2.2); ring.material.opacity = 1; ring.visible = t > .44 && t < .7; b.material.color.setHex(t > .46 ? 0x1f7a4d : 0x111111); roof.position.y = 1.67; };
}
const st = { el, on: false, raf: 0, t0: performance.now() - (kind - 1) * 600 };
const frame = now => { if (!st.on) return; tick((((now - st.t0) / 1000) % LOOP) / LOOP); renderer.render(scene, cam); st.raf = requestAnimationFrame(frame); };
const still = matchMedia("(prefers-reduced-motion:reduce)").matches || /[?&]still/.test(location.search);
const io = new IntersectionObserver(es => es.forEach(e => { const was = st.on; st.on = e.isIntersecting && !still && !st.manual; if (st.on && !was) st.raf = requestAnimationFrame(frame); }), { threshold: 0.1 });
io.observe(el); const ro = new ResizeObserver(() => { fit(); if (!st.on) { tick(0.7); renderer.render(scene, cam); } }); ro.observe(el);
tick(0.7); renderer.render(scene, cam);
st.dispose = () => { st.on = false; cancelAnimationFrame(st.raf); io.disconnect(); ro.disconnect(); renderer.dispose(); renderer.forceContextLoss && renderer.forceContextLoss(); renderer.domElement.remove(); };
st.scrub = t => { st.manual = true; st.on = false; cancelAnimationFrame(st.raf); tick(Math.max(0, Math.min(.999, t))); renderer.render(scene, cam); }; live.add(st); return st;
}
window.Diorama = { mount, disposeAll() { live.forEach(s => s.dispose()); live.clear(); } };
})();