tiny-scene-three / web /js /scene /layout.js
paralaif's picture
Add generated music and sound: Tone.js band, ambience, beat-synced animation, video export
4a0e872 verified
Raw History Blame Contribute Delete
10.7 kB
// Turns a scene description (plain JSON) into three.js objects, positions and motions.
import * as THREE from 'three';
import { LOCO, SPD_MUL, addZzz, speedOf, updateInstance } from './animate.js';
import { CREATURES } from './creatures.js';
import { buildEnvironment } from './environment.js';
import { PALETTE, hash, rng } from './prims.js';
import { THINGS } from './things.js';
export const BUILDERS = { ...CREATURES, ...THINGS };
export const SIZE_SCALE = { tiny: 0.4, small: 0.7, big: 1.5, giant: 2.6 };
const clamp = (v, a, b) => Math.min(b, Math.max(a, v));
function makeGroup(ent) {
const rand = rng(hash(ent.id + ent.type));
const n = clamp(ent.n ?? 1, 1, 6), s0 = SIZE_SCALE[ent.size] ?? 1, color = ent.color ? PALETTE[ent.color] : undefined;
const build = BUILDERS[ent.type] ?? BUILDERS.box;
const G = { ent, insts: [], center: { x: 0, z: 0 }, deps: [], act: ent.act, R: 0 };
const cols = Math.ceil(Math.sqrt(n));
for (let i = 0; i < n; i++) {
const obj = build(color), rig = obj.userData.rig;
const scale = s0 * (n > 1 ? 0.9 + rand() * 0.2 : 1), d = rig.r * s0 * 2.15;
const col = i % cols, row = Math.floor(i / cols), inRow = Math.min(cols, n - row * cols);
const off = n > 1
? { x: (col - (inRow - 1) / 2) * d + (rand() - 0.5) * d * 0.15, z: -(row - (Math.ceil(n / cols) - 1) / 2) * d * 0.9 + (rand() - 0.5) * d * 0.15 }
: { x: 0, z: 0 };
obj.scale.setScalar(scale);
obj.userData.entId = ent.id;
G.insts.push({
ent, obj, rig, scale, off, base: new THREE.Vector3(), yaw0: (rig.kind === 'vehicle' ? 1.05 : 0.45) + (n > 1 ? (rand() - 0.5) * 0.5 : 0),
phase: n > 1 ? rand() * 1.5 : 0, spdMul: SPD_MUL[ent.spd] ?? 1, act: ent.act, motion: { kind: 'none' }, mount: null,
});
G.R = Math.max(G.R, Math.hypot(off.x, off.z) + rig.r * scale);
G.h = Math.max(G.h ?? 0, rig.h * scale);
}
return G;
}
function defaultMountAct(G) {
const g = G.insts[0].rig;
if (g.kind === 'quad' || g.kind === 'biped' || g.kind === 'bird') return 'walk';
if (g.kind === 'fish') return 'swim';
if (g.kind === 'vehicle') return g.flyer ? 'fly' : 'drive';
return undefined;
}
function motionFor(inst, act, c) {
const R = inst.rig.r * inst.scale;
if (LOCO.has(act)) {
const v = speedOf(inst, act);
return { kind: 'patrol', v, A: clamp(v * 1.6, 2.5, 7) };
}
if (act === 'circle') return { kind: 'circle', cx: c.x, cz: c.z, R: 2.2 + R, v: speedOf(inst, 'circle'), ph: 0 };
if (act === 'fly') {
const Rx = 4.5 + R;
return { kind: 'fly', cx: c.x, cz: c.z, Rx, Rz: 2.2 + R * 0.5, alt: Math.max(2.5, inst.rig.h * inst.scale * 1.3 + 1.2), w: speedOf(inst, 'fly') / Rx, ph: 0 };
}
return { kind: 'none' };
}
export function buildScene(sc) {
const root = new THREE.Group(), actors = new THREE.Group();
root.add(actors);
const groups = sc.ents.map(makeGroup), byId = new Map(groups.map((G) => [G.ent.id, G]));
const space = sc.loc === 'space';
// ---- relation graph
for (const G of groups) {
const T = G.ent.rel && byId.get(G.ent.ref);
if (T && T !== G) { G.target = T; G.rel = G.ent.rel; T.deps.push(G); }
}
for (const G of groups) { // break cycles
const seen = new Set([G]);
for (let T = G.target; T; T = T.target) {
if (seen.has(T)) { G.target.deps = G.target.deps.filter((d) => d !== G); G.target = G.rel = undefined; break; }
seen.add(T);
}
}
for (const G of groups) {
if (G.rel === 'ride' && !G.target.act) G.target.act = defaultMountAct(G.target);
if (G.rel === 'around' && !G.act) G.act = 'walk';
if (G.rel === 'over' && !['jump', 'fly', 'float', 'bounce'].includes(G.act)) G.act = 'jump';
if (G.rel === 'chase') {
G.act = 'run';
if (!LOCO.has(G.target.act) || G.target.act === 'swim') G.target.act = 'run';
G.target.chased = true;
}
}
for (const G of groups) {
if (!G.act) G.act = space ? 'float' : 'idle';
for (const i of G.insts) i.act = G.act;
}
// ---- how much room each free group needs, including whatever depends on it
const chaseR = (G, D) => Math.max(4, G.R + D.R + 2.5);
const reach = (G) => {
let r = G.R + (G.act === 'circle' ? 2.2 + G.R : 0);
for (const D of G.deps) {
if (D.rel === 'near' || D.rel === 'behind' || D.rel === 'front') r += 2 * reach(D) + 0.5;
else if (D.rel === 'around') r += 2 * D.R + 1;
else if (D.rel === 'over') r += D.R + 1;
else if (D.rel === 'chase') r = Math.max(r, chaseR(G, D) + Math.max(G.R, D.R));
}
return r;
};
const free = groups.filter((G) => !G.target);
const roams = (G) => LOCO.has(G.act) && !G.chased;
const row = free.filter((G) => !roams(G)), lanes = free.filter(roams);
const width = row.reduce((a, G) => a + 2 * reach(G), 0) + 0.9 * Math.max(0, row.length - 1);
let x = -width / 2, depth = row.reduce((a, G) => Math.max(a, reach(G)), 0);
for (const G of row) { const r = reach(G); G.center = { x: x + r, z: 0 }; x += 2 * r + 0.9; }
let front = depth, back = depth;
lanes.forEach((G, k) => {
const r = reach(G);
if (!row.length && k === 0) { G.center = { x: 0, z: 0 }; front = back = r; return; }
if (k % 2 === 0) { G.center = { x: 0, z: front + r + 0.5 }; front += 2 * r + 0.5; }
else { G.center = { x: 0, z: -(back + r + 0.5) }; back += 2 * r + 0.5; }
});
// ---- place dependents, targets first
const done = new Set();
const place = (G) => {
if (done.has(G)) return;
done.add(G);
const T = G.target;
if (T) {
place(T);
const c = T.center, gap = G.R + T.R;
T.side = (T.side ?? 0) + 1;
const sgn = T.side % 2 ? -1 : 1, k = Math.ceil(T.side / 2);
if (G.rel === 'near' && (LOCO.has(G.act) || LOCO.has(T.act))) G.center = { x: c.x, z: c.z - sgn * k * (gap + 0.5) }; // side by side lanes
else if (G.rel === 'near') G.center = { x: c.x + sgn * k * (gap + 0.4), z: c.z + 0.25 };
else if (G.rel === 'behind') G.center = { x: c.x + 0.3 * G.R, z: c.z - (gap + 0.5) };
else if (G.rel === 'front') G.center = { x: c.x - 0.3 * G.R, z: c.z + (gap + 0.5) };
else G.center = { x: c.x, z: c.z };
}
const { dx = 0, dz = 0 } = G.ent; // offset set by dragging the object by hand
if ((dx || dz) && G.rel !== 'ride' && G.rel !== 'on') G.center = { x: G.center.x + dx, z: G.center.z + dz };
for (const i of G.insts) {
i.base.set(G.center.x + i.off.x, 0, G.center.z + i.off.z);
i.motion = motionFor(i, G.act, { x: G.center.x, z: G.center.z });
}
if (!T) return;
const c = T.center;
G.insts.forEach((i, k) => {
const r = i.rig.r * i.scale;
if (G.rel === 'ride' || G.rel === 'on') {
const m = T.insts[k];
if (!m) { i.base.set(c.x + T.R + r + 0.3 + k * 0.2, 0, c.z + 0.5 * k); i.motion = { kind: 'none' }; return; }
i.mount = m; i.mode = G.rel; i.seat = m.rig.seat; i.motion = { kind: 'none' };
i.obj.scale.setScalar(i.scale / m.scale);
(m.rig.body ?? m.obj).add(i.obj);
} else if (G.rel === 'around') {
const v = speedOf(i, G.act === 'fly' ? 'fly' : G.act);
const Rc = T.R + G.R + 0.9;
i.motion = G.act === 'fly'
? { kind: 'fly', cx: c.x, cz: c.z, Rx: Rc, Rz: Rc, alt: Math.max(1.5, T.h * 0.6), w: v / Rc, ph: k * 0.6 }
: { kind: 'circle', cx: c.x, cz: c.z, R: Rc, v: LOCO.has(G.act) ? v : 1.6, ph: k * 0.5 };
i.off = { x: 0, z: 0 };
} else if (G.rel === 'over') {
if (G.act === 'jump' || G.act === 'bounce') {
i.act = 'jump';
i.motion = { kind: 'hopover', cx: c.x, cz: c.z, d: T.R + r + 0.7, H: T.h + 0.6 + i.rig.h * i.scale * 0.2, P: 1.7 + T.R * 0.25 };
} else {
const Rc = T.R + 1.4;
i.motion = { kind: 'fly', cx: c.x, cz: c.z, Rx: Rc, Rz: Rc * 0.75, alt: T.h + 1.0, w: 3.2 / Rc, ph: k * 0.7 };
i.act = G.act === 'float' ? 'fly' : G.act;
}
} else if (G.rel === 'chase') {
const Rc = chaseR(T, G), v = Math.min(speedOf(i, 'run'), speedOf(T.insts[0], T.act));
i.motion = { kind: 'circle', cx: c.x, cz: c.z, R: Rc, v, ph: 0 };
i.off = { x: i.off.x * 0.4, z: i.off.z * 0.4 };
for (const t of T.insts) { t.motion = { kind: 'circle', cx: c.x, cz: c.z, R: Rc, v, ph: 0.25 + (T.R + G.R + 0.8) / Rc }; t.off = { x: t.off.x * 0.4, z: t.off.z * 0.4 }; }
}
});
};
groups.forEach(place);
// ---- ocean: land things get an island, swimmers are pushed off it
const insts = groups.flatMap((G) => G.insts);
let island = 0;
if (sc.loc === 'ocean') {
const wet = (G) => G.insts[0].rig.swims || G.act === 'swim' || G.act === 'fly' || (G.insts[0].mount && wet(G.target));
const land = groups.filter((G) => !wet(G));
for (const G of land) island = Math.max(island, Math.hypot(G.center.x, G.center.z) + G.R + (G.insts[0].motion.A ?? G.insts[0].motion.R ?? 0) + 1.2);
if (island) {
let z = island + 0.5;
for (const G of groups) {
if (!wet(G) || G.target) continue;
const dz = z + G.R + 0.8 - G.center.z;
z += 2 * G.R + 1;
for (const D of [G, ...groups.filter((o) => o.target === G)]) {
D.center.z += dz;
for (const i of D.insts) { i.base.z += dz; if (i.motion.cz != null) i.motion.cz += dz; }
}
}
}
}
for (const i of insts) {
if (!i.mount) actors.add(i.obj);
if (i.act === 'sleep') addZzz(i);
}
// ---- measure where everything goes over time, to frame the camera and size the stage
const ctx = { waterY: sc.loc === 'ocean' ? 0 : null };
const box = new THREE.Box3(), fbox = new THREE.Box3(), p = new THREE.Vector3();
const focus = insts[0];
for (let k = 0; k < 48; k++) {
for (const i of insts) updateInstance(i, k * 0.41, ctx);
root.updateMatrixWorld(true);
for (const i of insts) {
i.obj.getWorldPosition(p);
const r = i.rig.r * i.scale, h = i.rig.h * i.scale;
for (const b of i === focus ? [box, fbox] : [box]) {
b.expandByPoint(new THREE.Vector3(p.x - r, Math.max(0, p.y - 0.2), p.z - r));
b.expandByPoint(new THREE.Vector3(p.x + r, p.y + h, p.z + r));
}
}
}
if (!insts.length) { box.set(new THREE.Vector3(-2, 0, -2), new THREE.Vector3(2, 2, 2)); fbox.copy(box); }
const stageR = Math.max(4, Math.abs(box.min.x), Math.abs(box.max.x), Math.abs(box.min.z), Math.abs(box.max.z));
const env = buildEnvironment(sc, { R: stageR, island });
root.add(env.group);
ctx.waterY = env.waterY;
return {
root, actors, env, insts, focus, box, fbox, stageR,
update(t, beat) {
ctx.beat = beat;
for (const i of insts) updateInstance(i, t, ctx);
env.update(t);
},
dispose() { for (const d of env.dispose) d.dispose?.(); },
};
}