File size: 9,915 Bytes
6b5ce6d
4a0e872
6b5ce6d
 
dd53693
 
 
 
6b5ce6d
dd53693
 
 
 
 
 
 
 
4a0e872
dd53693
4a0e872
dd53693
 
 
 
 
6b5ce6d
 
dd53693
4a0e872
 
 
 
 
 
 
 
 
 
dd53693
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b5ce6d
dd53693
 
4a0e872
 
 
dd53693
 
6b5ce6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a0e872
6b5ce6d
 
 
 
 
 
 
 
 
 
 
4a0e872
6b5ce6d
 
 
 
 
 
4a0e872
6b5ce6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd53693
 
 
 
 
6b5ce6d
 
dd53693
6b5ce6d
dd53693
6b5ce6d
dd53693
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b5ce6d
 
 
 
4a0e872
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd53693
6b5ce6d
dd53693
 
 
 
 
 
 
6b5ce6d
 
 
 
 
 
 
 
 
dd53693
6b5ce6d
 
dd53693
 
 
6b5ce6d
dd53693
 
 
 
6b5ce6d
dd53693
 
4a0e872
 
dd53693
 
 
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// 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;
}