tiny-scene-three / web /js /main.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
9.92 kB
// Glue: prompt -> model -> scene language -> three.js scene, plus hand editing with undo.
import { Music } from './audio/engine.js';
import { entLabel, renderEditor, vocabFrom } from './editor.js';
import { applyEdit, deleteEntities, emptyScene, parse } from './lang/scenelang.js';
import { TinyGPT } from './nn/model.js';
import { Stage } from './scene/stage.js';
const $ = (id) => document.getElementById(id);
const log = $('log'), input = $('prompt'), send = $('send'), status = $('status'), panel = $('panel');
const params = new URLSearchParams(location.search);
const EXAMPLES = [
'un zorro rojo saltando sobre un coche azul en el desierto al atardecer',
'an alien riding a horse on the moon',
'a robot dancing on a skateboard in the city at night',
'tres pingüinos caminando en la nieve mientras nieva',
'un dragón volando sobre una torre con tormenta',
'un fantasma flotando en el bosque de noche con música tenebrosa',
'haz el zorro verde', 'añade un árbol gigante', 'que llueva', 'aleja la cámara', 'gira la cámara',
'pon música de jazz alegre', 'haz la música más rápida', 'sin batería',
'¿qué puedes hacer?',
];
const LABEL = { loc: 'lugar', time: 'hora', weather: 'clima', shot: 'plano', cam: 'cámara' };
const stage = new Stage($('canvas'));
let scene = emptyScene(), model = null, qa = {}, vocab = null;
const history = [], undoStack = [], redoStack = [];
let hpos = 0;
const music = new Music();
stage.music = music;
/** Rebuild the 3D scene and retune the music from the current description. */
function rebuild(camera) {
stage.setScene(scene, camera);
music.apply(scene, stage.built.insts);
}
rebuild('reset');
// browsers only allow sound after a click or key press
for (const ev of ['pointerdown', 'keydown']) window.addEventListener(ev, () => music.unlock(), { once: true, capture: true });
function el(tag, cls, text) {
const e = document.createElement(tag);
if (cls) e.className = cls;
if (text != null) e.textContent = text;
return e;
}
function say(kind, build) {
const m = el('div', 'msg ' + kind);
build(m);
log.append(m);
log.scrollTop = log.scrollHeight;
return m;
}
function tokenRow(tokens) {
const row = el('div', 'tokens');
for (const t of tokens) row.append(el('code', t.startsWith('<') ? 's' : '', t));
return row;
}
function showState() {
const box = $('state');
box.replaceChildren();
for (const k of ['loc', 'time', 'weather', 'shot', 'cam']) {
const s = el('span');
s.append(el('b', '', LABEL[k]), scene[k]);
box.append(s);
}
for (const e of scene.ents) {
const s = el('span');
s.append(el('b', '', (e.n > 1 ? e.n + '× ' : '') + entLabel(scene, e)), [e.color, e.size, e.act, e.spd, e.rel && `${e.rel}→`].filter(Boolean).join(' ') || '·');
box.append(s);
}
const m = music.p, chip = el('span');
if (m) chip.append(el('b', '', '♪'), m.on ? `${m.mood} ${m.genre} ${m.bpm}` : 'off');
if (m) box.append(chip);
}
// ------------------------------------------------------------------ one place where the scene changes
function refresh() {
showState();
$('undo').disabled = !undoStack.length;
$('redo').disabled = !redoStack.length;
if (vocab) renderEditor($('editor'), { scene, selected: stage.selected, vocab, change: (fn, camera) => edit(fn, camera), select: (id) => stage.select(id) });
}
/** Replace the scene, recording the previous one for undo. camera: see Stage.setScene. */
function commit(next, camera = 'keep') {
if (JSON.stringify(next) === JSON.stringify(scene)) return;
undoStack.push(scene);
if (undoStack.length > 200) undoStack.shift();
redoStack.length = 0;
scene = next;
rebuild(camera);
refresh();
}
function edit(fn, camera = 'keep') {
const next = structuredClone(scene);
fn(next);
commit(next, camera);
}
function undo() {
if (!undoStack.length) return;
redoStack.push(scene);
scene = undoStack.pop();
rebuild('keep');
refresh();
}
function redo() {
if (!redoStack.length) return;
undoStack.push(scene);
scene = redoStack.pop();
rebuild('keep');
refresh();
}
stage.onSelect = (id) => {
if (id) setTab('edit');
refresh();
};
stage.onMove = (id, dx, dz) => edit((sc) => {
const e = sc.ents.find((x) => x.id === id);
if (!e) return;
e.dx = Math.round(((e.dx ?? 0) + dx) * 100) / 100;
e.dz = Math.round(((e.dz ?? 0) + dz) * 100) / 100;
});
function setTab(name) {
panel.dataset.tab = name;
for (const b of $('tabs').children) b.setAttribute('aria-selected', String(b.dataset.tab === name));
}
for (const b of $('tabs').children) b.addEventListener('click', () => setTab(b.dataset.tab));
// ------------------------------------------------------------------ model
function run(text) {
const { tokens, ms } = model.generate(text);
const res = parse(tokens), notes = [];
let answer = null;
if (res.kind === 'new') {
stage.select(null);
commit(res.scene, 'reset');
} else if (res.kind === 'edit' && res.ops.length) {
const out = applyEdit(scene, res.ops, { it: stage.selected });
for (const n of out.notes) notes.push(n.code === 'empty' ? 'La escena está vacía; describe primero una escena.' : `No hay ningún «${n.type}» en la escena.`);
commit(out.scene, 'auto');
} else if (res.kind === 'qa') {
answer = qa[res.topic]?.[res.lang] ?? qa.help?.es;
} else {
notes.push('No entendí la petición. Prueba con una frase más simple.');
}
say('bot', (m) => {
m.append(tokenRow(tokens));
if (answer) m.append(el('p', '', answer));
for (const n of notes) m.append(el('p', 'note', n));
m.append(el('div', 'meta', `${tokens.length} tokens · ${ms.toFixed(0)} ms`));
});
return { tokens, ms, kind: res.kind, notes };
}
function submit(text) {
text = text.trim();
if (!text || !model) return null;
history.push(text);
hpos = history.length;
say('user', (m) => { m.textContent = text; });
return run(text);
}
$('form').addEventListener('submit', (ev) => {
ev.preventDefault();
const text = input.value;
input.value = '';
send.disabled = true;
setTimeout(() => { submit(text); send.disabled = false; input.focus(); }, 15);
});
input.addEventListener('keydown', (ev) => {
if (ev.key === 'ArrowUp' && hpos > 0) { input.value = history[--hpos]; ev.preventDefault(); }
else if (ev.key === 'ArrowDown' && hpos < history.length) { input.value = history[++hpos] ?? ''; ev.preventDefault(); }
});
for (const ex of EXAMPLES) {
const b = el('button', '', ex);
b.type = 'button';
b.addEventListener('click', () => submit(ex));
$('examples').append(b);
}
// ------------------------------------------------------------------ toolbar and keyboard
$('undo').addEventListener('click', undo);
$('redo').addEventListener('click', redo);
$('pause').addEventListener('click', (ev) => { stage.paused = !stage.paused; music.setPaused(stage.paused); ev.target.textContent = stage.paused ? 'Reanudar' : 'Pausa'; });
$('sound').addEventListener('click', (ev) => { music.setMuted(!music.muted); ev.target.textContent = music.muted ? 'Sonido: no' : 'Sonido: sí'; });
let recording = null, recTimer = 0;
async function stopRecording() {
clearTimeout(recTimer);
const rec = recording;
recording = null;
$('record').textContent = 'Grabar vídeo';
const blob = await rec.stop(), a = el('a');
a.href = URL.createObjectURL(blob);
a.download = 'tiny-scene.webm';
a.click();
setTimeout(() => URL.revokeObjectURL(a.href), 2000);
}
$('record').addEventListener('click', async () => {
if (recording) return stopRecording();
await music.unlock();
recording = music.record($('canvas'));
$('record').textContent = 'Detener (máx. 60 s)';
recTimer = setTimeout(() => recording && stopRecording(), 60000);
});
$('recenter').addEventListener('click', () => stage.frameCamera());
$('clear').addEventListener('click', () => { stage.select(null); commit(emptyScene(), 'reset'); });
$('export').addEventListener('click', async () => {
const blob = await stage.exportGLB(), a = el('a');
a.href = URL.createObjectURL(blob);
a.download = 'tiny-scene.glb';
a.click();
setTimeout(() => URL.revokeObjectURL(a.href), 2000);
});
window.addEventListener('keydown', (ev) => {
const typing = /^(INPUT|TEXTAREA|SELECT)$/.test(document.activeElement?.tagName);
const mod = ev.ctrlKey || ev.metaKey;
if (mod && ev.key.toLowerCase() === 'z' && !typing) { ev.preventDefault(); ev.shiftKey ? redo() : undo(); }
else if (mod && ev.key.toLowerCase() === 'y' && !typing) { ev.preventDefault(); redo(); }
else if (typing) return;
else if ((ev.key === 'Delete' || ev.key === 'Backspace') && stage.selected) { ev.preventDefault(); edit((sc) => deleteEntities(sc, [stage.selected])); }
else if (ev.key === 'Escape') stage.select(null);
});
refresh();
say('bot', (m) => m.append(el('p', 'hint', 'Describe una escena en español o inglés. Después puedes pedir cambios con frases, o hacer clic en un objeto para editarlo a mano y arrastrarlo para moverlo. Arrastra el fondo para girar la vista.')));
try {
[model, qa] = await Promise.all([TinyGPT.load('./model/'), fetch('./model/qa.json').then((r) => r.json())]);
vocab = vocabFrom(model.meta.vocab);
const mt = model.meta.meta ?? {};
status.textContent = `Modelo de ${(mt.params / 1e6).toFixed(2)} M de parámetros, corriendo en tu navegador.`;
input.disabled = send.disabled = false;
input.focus();
refresh();
// ?q=prompt one;prompt two and ?t=seconds (freeze time) make the page scriptable for demos and tests
for (const q of (params.get('q') ?? '').split(';')) submit(q);
if (params.has('t')) { stage.t = Number(params.get('t')); stage.paused = true; music.setPaused(true); if (scene.cam !== 'static') stage.frameCamera(); }
window.tinyScene = { model, stage, music, submit, edit, undo, redo, get scene() { return scene; }, ready: true };
} catch (err) {
status.textContent = 'No se pudo cargar el modelo: ' + err.message;
}