paralaif commited on
Commit
6b5ce6d
·
verified ·
1 Parent(s): dd53693

Add hand editing: click to select, drag to move, edit panel, undo/redo

Browse files
README.md CHANGED
@@ -50,7 +50,19 @@ Open <http://localhost:8080>. Type in English or Spanish:
50
  | Edit | `make the fox green` · `add a giant tree` · `remove the car` · `make it rain` · `zoom out` · `orbit the camera` |
51
  | Ask | `what can you do?` · `how do i enable shadows` · `what objects do you know` |
52
 
53
- Drag to orbit the view. **Export .glb** saves the current frame (opens in Blender); animations are procedural and are not exported.
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  The page also accepts `?q=first prompt;second prompt&t=3` to run prompts on load and freeze time at that second.
56
 
@@ -78,12 +90,14 @@ ml/ lexicon.py scene-language vocabulary and the English and Spanish word
78
  train.py trains on pairs generated on the fly and exports to web/model/
79
  infer.py command-line inference
80
  web/ js/nn/model.js the same Transformer in plain JavaScript, with a KV cache
 
81
  js/lang/scenelang.js tokens → scene JSON, and applying edits
82
  js/scene/ creatures.js, things.js (objects), animate.js (poses),
83
  layout.js (placement and relations), environment.js, stage.js
84
  tests/ check_scene.mjs builds every object × action and place × time × weather combination
85
  check_model.mjs JS ↔ PyTorch parity and hand-written prompts
86
  check_export.mjs .glb export in a real browser
 
87
  ```
88
 
89
  - **Input**: the sentence is split into characters (50 symbols), which makes it tolerant to typos. **Output**: fixed scene-language tokens. Total vocabulary: 235.
@@ -129,7 +143,8 @@ To add an object: an entry in `_TYPES` in `ml/lexicon.py`, a builder in `web/js/
129
 
130
  - It only understands the vocabulary above. Anything unknown is replaced by the closest match or ignored (`a little girl flying a kite` gives a small woman flying).
131
  - It does not generate new geometry: every object is hand-modelled from primitives.
132
- - Edits refer to objects by type (`the dog`), not by colour or position; with two different dogs, both change.
 
133
  - A place has no colour (`in a blue house` is read as a dog and a blue house).
134
  - Known miss: `i'd like the bear to be sleeping` adds a delete that was not asked for.
135
  - Long sentences with three objects and many modifiers are where it fails most.
 
50
  | Edit | `make the fox green` · `add a giant tree` · `remove the car` · `make it rain` · `zoom out` · `orbit the camera` |
51
  | Ask | `what can you do?` · `how do i enable shadows` · `what objects do you know` |
52
 
53
+ ### Hand editing
54
+
55
+ Click an object in the view (or in the list under the **Editar** tab) to select it, then:
56
+
57
+ - **Drag it** in the view to move it. Riders, and objects that orbit, jump over or chase it, move with it.
58
+ - Change its type, action, size, count, speed, relation and colour in the **Editar** tab, or **Duplicar** / **Borrar** it.
59
+ - Add new objects, and change place, time, weather, shot and camera move, from the same tab.
60
+ - Typed edits keep working, and "it" refers to the selection: select the car and type `make it yellow`.
61
+ - `Ctrl+Z` / `Ctrl+Shift+Z` undo and redo every change, typed or by hand. `Delete` removes the selection, `Esc` clears it.
62
+
63
+ Hand edits change the same scene description the model writes, so both can be mixed freely.
64
+
65
+ Drag the background to orbit the view. **Export .glb** saves the current frame (opens in Blender); animations are procedural and are not exported.
66
 
67
  The page also accepts `?q=first prompt;second prompt&t=3` to run prompts on load and freeze time at that second.
68
 
 
90
  train.py trains on pairs generated on the fly and exports to web/model/
91
  infer.py command-line inference
92
  web/ js/nn/model.js the same Transformer in plain JavaScript, with a KV cache
93
+ js/editor.js the hand-editing panel
94
  js/lang/scenelang.js tokens → scene JSON, and applying edits
95
  js/scene/ creatures.js, things.js (objects), animate.js (poses),
96
  layout.js (placement and relations), environment.js, stage.js
97
  tests/ check_scene.mjs builds every object × action and place × time × weather combination
98
  check_model.mjs JS ↔ PyTorch parity and hand-written prompts
99
  check_export.mjs .glb export in a real browser
100
+ check_editor.mjs click, drag, panel edits, undo/redo and delete in a real browser
101
  ```
102
 
103
  - **Input**: the sentence is split into characters (50 symbols), which makes it tolerant to typos. **Output**: fixed scene-language tokens. Total vocabulary: 235.
 
143
 
144
  - It only understands the vocabulary above. Anything unknown is replaced by the closest match or ignored (`a little girl flying a kite` gives a small woman flying).
145
  - It does not generate new geometry: every object is hand-modelled from primitives.
146
+ - Typed edits refer to objects by type (`the dog`), not by colour or position; with two different dogs, both change. Select one and use `it`, or edit it by hand.
147
+ - Hand-moved objects keep an offset from where the automatic layout puts them, so adding or removing other objects can still shift them.
148
  - A place has no colour (`in a blue house` is read as a dog and a blue house).
149
  - Known miss: `i'd like the bear to be sleeping` adds a delete that was not asked for.
150
  - Long sentences with three objects and many modifiers are where it fails most.
tests/check_editor.mjs ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Drives the hand editor in headless Chromium: click-select, drag, panel edits, undo/redo, delete.
2
+ // node tests/check_editor.mjs [screenshot.png]
3
+ import fs from 'node:fs';
4
+ import http from 'node:http';
5
+ import path from 'node:path';
6
+ import { chromium } from 'playwright-core';
7
+
8
+ const WEB = new URL('../web/', import.meta.url).pathname;
9
+ const MIME = { '.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css', '.json': 'application/json' };
10
+ const server = http.createServer((req, res) => {
11
+ const f = path.join(WEB, decodeURIComponent(req.url.split('?')[0]).replace(/\/$/, '/index.html'));
12
+ if (!fs.existsSync(f)) { res.writeHead(404); return res.end(); }
13
+ res.writeHead(200, { 'content-type': MIME[path.extname(f)] ?? 'application/octet-stream' });
14
+ fs.createReadStream(f).pipe(res);
15
+ }).listen(0);
16
+ const root = `${process.env.HOME}/.cache/ms-playwright`;
17
+ const exe = process.env.CHROME ?? fs.readdirSync(root).filter((d) => d.startsWith('chromium_headless_shell'))
18
+ .flatMap((d) => fs.readdirSync(`${root}/${d}`).map((s) => `${root}/${d}/${s}/chrome-headless-shell`)).find((p) => fs.existsSync(p));
19
+ const browser = await chromium.launch({ executablePath: exe, args: ['--use-gl=angle', '--use-angle=swiftshader', '--enable-unsafe-swiftshader'] });
20
+ const page = await browser.newPage({ viewport: { width: 1280, height: 760 } });
21
+ const errors = [];
22
+ page.on('pageerror', (e) => errors.push(String(e)));
23
+ page.on('console', (m) => { if (m.type() === 'error') errors.push(m.text()); });
24
+
25
+ let fail = 0;
26
+ const check = (ok, msg) => { console.log((ok ? 'ok ' : 'FAIL ') + msg); if (!ok) fail++; };
27
+ const sc = () => page.evaluate(() => window.tinyScene.scene);
28
+ const screenOf = (id) => page.evaluate((id) => { // screen position of an entity's centre
29
+ const s = window.tinyScene.stage, i = s.built.insts.find((x) => x.ent.id === id);
30
+ const THREE_Box = s.selBox.constructor, b = new THREE_Box().setFromObject(i.obj), p = b.getCenter(b.min.clone()).project(s.camera);
31
+ const r = s.canvas.getBoundingClientRect();
32
+ return { x: r.left + (p.x + 1) / 2 * r.width, y: r.top + (1 - p.y) / 2 * r.height };
33
+ }, id);
34
+
35
+ await page.goto(`http://localhost:${server.address().port}/?q=${encodeURIComponent('a red dog next to a blue car')}&t=1`);
36
+ await page.waitForFunction(() => window.tinyScene?.ready, null, { timeout: 60000 });
37
+ await page.waitForTimeout(300);
38
+ let s = await sc();
39
+ check(s.ents.length === 2, 'scene from prompt has 2 objects: ' + s.ents.map((e) => e.type).join(', '));
40
+ const car = s.ents.find((e) => e.type === 'car');
41
+
42
+ // click selects
43
+ let p = await screenOf(car.id);
44
+ await page.mouse.click(p.x, p.y);
45
+ check(await page.evaluate(() => window.tinyScene.stage.selected) === car.id, 'clicking the car selects it');
46
+ check(await page.evaluate(() => document.getElementById('panel').dataset.tab) === 'edit', 'selecting opens the Editar tab');
47
+
48
+ // drag moves
49
+ p = await screenOf(car.id);
50
+ await page.mouse.move(p.x, p.y);
51
+ await page.mouse.down();
52
+ for (let k = 1; k <= 10; k++) await page.mouse.move(p.x + k * 18, p.y + k * 4);
53
+ await page.mouse.up();
54
+ s = await sc();
55
+ const moved = s.ents.find((e) => e.id === car.id);
56
+ check(Math.hypot(moved.dx ?? 0, moved.dz ?? 0) > 0.5, `dragging moves the car (dx=${moved.dx}, dz=${moved.dz})`);
57
+ const camAfter = await page.evaluate(() => window.tinyScene.stage.controls.enabled);
58
+ check(camAfter, 'orbit controls are re-enabled after the drag');
59
+
60
+ // panel edits
61
+ await page.click('.sw[title="green"]');
62
+ check((await sc()).ents.find((e) => e.id === car.id).color === 'green', 'colour swatch paints the car green');
63
+ await page.selectOption('label.field:has(span:text-is("Acción")) select', 'drive');
64
+ check((await sc()).ents.find((e) => e.id === car.id).act === 'drive', 'action select sets drive');
65
+ await page.selectOption('label.field:has(span:text-is("Cantidad")) select', '3');
66
+ check((await sc()).ents.find((e) => e.id === car.id).n === 3, 'count select sets 3');
67
+
68
+ // model edit with "it" targets the selection
69
+ await page.evaluate(() => window.tinyScene.submit('make it yellow'));
70
+ check((await sc()).ents.find((e) => e.id === car.id).color === 'yellow', '"make it yellow" applies to the selected car');
71
+
72
+ // undo / redo
73
+ await page.keyboard.press('Control+z');
74
+ check((await sc()).ents.find((e) => e.id === car.id).color === 'green', 'Ctrl+Z undoes the colour');
75
+ await page.keyboard.press('Control+Shift+z');
76
+ check((await sc()).ents.find((e) => e.id === car.id).color === 'yellow', 'Ctrl+Shift+Z redoes it');
77
+
78
+ // add from the panel, then delete with the keyboard
79
+ await page.selectOption('label.field:has(span:text-is("Nuevo objeto")) select', 'tree');
80
+ await page.click('button:text-is("Añadir")');
81
+ s = await sc();
82
+ check(s.ents.some((e) => e.type === 'tree'), 'Añadir adds a tree');
83
+ const tree = s.ents.find((e) => e.type === 'tree');
84
+ check(await page.evaluate(() => window.tinyScene.stage.selected) === tree.id, 'the new tree is selected');
85
+ await page.screenshot({ path: process.argv[2] ?? '/dev/null' });
86
+ await page.evaluate(() => document.activeElement?.blur());
87
+ await page.keyboard.press('Delete');
88
+ check(!(await sc()).ents.some((e) => e.type === 'tree'), 'Delete key removes the selected tree');
89
+
90
+ // click on empty sky deselects
91
+ const sky = await page.evaluate(() => { const r = document.getElementById('canvas').getBoundingClientRect(); return { x: r.left + 30, y: r.top + 80 }; });
92
+ await page.mouse.click(sky.x, sky.y);
93
+ check(await page.evaluate(() => window.tinyScene.stage.selected) === null, 'clicking empty space deselects');
94
+
95
+ check(!errors.length, 'no page errors' + (errors.length ? ': ' + errors.join(' | ') : ''));
96
+ await browser.close();
97
+ server.close();
98
+ process.exit(fail ? 1 : 0);
web/index.html CHANGED
@@ -15,23 +15,32 @@
15
  <canvas id="canvas"></canvas>
16
  <div id="state" aria-live="polite"></div>
17
  <div id="tools">
 
 
18
  <button id="pause" title="Pausar o reanudar la animación">Pausa</button>
19
  <button id="recenter" title="Volver al encuadre de la escena">Reencuadrar</button>
20
  <button id="export" title="Descargar la escena como archivo .glb">Exportar .glb</button>
21
  <button id="clear" title="Vaciar la escena">Vaciar</button>
22
  </div>
23
  </section>
24
- <aside id="panel">
25
  <header>
26
  <h1>Tiny Scene</h1>
27
  <p id="status">Cargando el modelo…</p>
 
 
 
 
28
  </header>
29
- <div id="log" aria-live="polite"></div>
30
- <div id="examples"></div>
31
- <form id="form" autocomplete="off">
32
- <input id="prompt" type="text" placeholder="Describe una escena o pide un cambio…" maxlength="160" disabled>
33
- <button id="send" type="submit" disabled>Enviar</button>
34
- </form>
 
 
 
35
  </aside>
36
  </main>
37
  <script type="module" src="./js/main.js"></script>
 
15
  <canvas id="canvas"></canvas>
16
  <div id="state" aria-live="polite"></div>
17
  <div id="tools">
18
+ <button id="undo" title="Deshacer (Ctrl+Z)" disabled>Deshacer</button>
19
+ <button id="redo" title="Rehacer (Ctrl+Shift+Z)" disabled>Rehacer</button>
20
  <button id="pause" title="Pausar o reanudar la animación">Pausa</button>
21
  <button id="recenter" title="Volver al encuadre de la escena">Reencuadrar</button>
22
  <button id="export" title="Descargar la escena como archivo .glb">Exportar .glb</button>
23
  <button id="clear" title="Vaciar la escena">Vaciar</button>
24
  </div>
25
  </section>
26
+ <aside id="panel" data-tab="chat">
27
  <header>
28
  <h1>Tiny Scene</h1>
29
  <p id="status">Cargando el modelo…</p>
30
+ <nav id="tabs" role="tablist">
31
+ <button type="button" role="tab" data-tab="chat" aria-selected="true">Chat</button>
32
+ <button type="button" role="tab" data-tab="edit" aria-selected="false">Editar</button>
33
+ </nav>
34
  </header>
35
+ <div id="chat" class="tab">
36
+ <div id="log" aria-live="polite"></div>
37
+ <div id="examples"></div>
38
+ <form id="form" autocomplete="off">
39
+ <input id="prompt" type="text" placeholder="Describe una escena o pide un cambio…" maxlength="160" disabled>
40
+ <button id="send" type="submit" disabled>Enviar</button>
41
+ </form>
42
+ </div>
43
+ <div id="editor" class="tab"></div>
44
  </aside>
45
  </main>
46
  <script type="module" src="./js/main.js"></script>
web/js/editor.js ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Hand-editing panel: scene settings, the object list and the selected object's properties.
2
+ // It never touches three.js directly: every change produces a new scene description.
3
+ import { PALETTE } from './scene/prims.js';
4
+
5
+ const SCENE_KEYS = [['loc', 'Lugar'], ['time', 'Hora'], ['weather', 'Clima'], ['shot', 'Plano'], ['cam', 'Cámara']];
6
+ const SIZES = ['tiny', 'small', 'big', 'giant'];
7
+ const COUNTS = [1, 2, 3, 4, 5, 6];
8
+
9
+ function h(tag, props = {}, ...kids) {
10
+ const el = document.createElement(tag);
11
+ for (const [k, v] of Object.entries(props)) {
12
+ if (k === 'class') el.className = v;
13
+ else if (k.startsWith('on')) el.addEventListener(k.slice(2), v);
14
+ else if (v !== undefined && v !== false) el.setAttribute(k, v === true ? '' : v);
15
+ }
16
+ for (const k of kids.flat()) if (k != null) el.append(k);
17
+ return el;
18
+ }
19
+
20
+ /** "dog", or "dog 2" when the scene has several dogs. */
21
+ export function entLabel(scene, e) {
22
+ const same = scene.ents.filter((o) => o.type === e.type);
23
+ return same.length > 1 ? `${e.type} ${same.indexOf(e) + 1}` : e.type;
24
+ }
25
+
26
+ /** Lists of values for every field, read from the model's vocabulary so the editor offers exactly what the model knows. */
27
+ export function vocabFrom(tokens) {
28
+ const v = {};
29
+ for (const t of tokens) {
30
+ const i = t.indexOf('=');
31
+ if (i < 1) continue;
32
+ (v[t.slice(0, i)] ??= []).push(t.slice(i + 1));
33
+ }
34
+ v.type = v.type.filter((x) => x !== 'it' && x !== 'all');
35
+ v.shot = v.shot.filter((x) => x !== 'in' && x !== 'out');
36
+ return v;
37
+ }
38
+
39
+ function select(label, value, options, onset, none) {
40
+ const s = h('select', { onchange: () => onset(s.value === '' ? undefined : s.value) },
41
+ none != null ? h('option', { value: '' }, none) : null,
42
+ options.map(([v, text]) => h('option', { value: v }, text)));
43
+ s.value = value ?? '';
44
+ return h('label', { class: 'field' }, h('span', {}, label), s);
45
+ }
46
+ const opts = (list) => list.map((x) => [x, x]);
47
+
48
+ /**
49
+ * root: container element. ctx: { scene, selected, vocab, change(fn), select(id), notice }
50
+ * change(fn, camera?) receives a copy of the scene to mutate; the caller rebuilds and records undo.
51
+ * camera 'auto' reframes if the scene grew (used when adding objects).
52
+ */
53
+ export function renderEditor(root, { scene, selected, vocab, change, select: pick }) {
54
+ const ent = scene.ents.find((e) => e.id === selected);
55
+ const setEnt = (key, transform = (x) => x) => (value) => change((sc) => {
56
+ const e = sc.ents.find((x) => x.id === selected);
57
+ if (!e) return;
58
+ const v = value === undefined ? undefined : transform(value);
59
+ if (v === undefined) delete e[key]; else e[key] = v;
60
+ if (key === 'rel') {
61
+ if (!v) delete e.ref;
62
+ else if (!sc.ents.some((x) => x.id === e.ref && x.id !== e.id)) e.ref = sc.ents.find((x) => x.id !== e.id)?.id;
63
+ if (!e.ref) delete e.rel;
64
+ }
65
+ });
66
+
67
+ // ---- scene settings
68
+ const sceneBox = h('section', { class: 'ed-sec' }, h('h3', {}, 'Escena'),
69
+ h('div', { class: 'grid' }, SCENE_KEYS.map(([k, label]) => select(label, scene[k], opts(vocab[k]), (v) => change((sc) => {
70
+ sc[k] = v ?? (k === 'loc' ? 'studio' : vocab[k][0]);
71
+ if (k === 'shot') sc.zoom = 0;
72
+ })))));
73
+
74
+ // ---- object list + add
75
+ let addType = vocab.type[0];
76
+ const list = scene.ents.length
77
+ ? h('div', { class: 'ent-list' }, scene.ents.map((e) => h('button', {
78
+ type: 'button', class: 'ent' + (e.id === selected ? ' on' : ''), onclick: () => pick(e.id === selected ? null : e.id),
79
+ }, e.color ? h('i', { class: 'dot', style: `background:#${PALETTE[e.color].toString(16).padStart(6, '0')}` }) : null,
80
+ (e.n > 1 ? e.n + '× ' : '') + entLabel(scene, e), e.act ? h('small', {}, ' ' + e.act) : null)))
81
+ : h('p', { class: 'hint' }, 'La escena está vacía.');
82
+ const addSel = select('Nuevo objeto', addType, opts(vocab.type), (v) => { addType = v; });
83
+ const objBox = h('section', { class: 'ed-sec' }, h('h3', {}, 'Objetos'), list,
84
+ h('div', { class: 'row' }, addSel, h('button', {
85
+ type: 'button', onclick: () => {
86
+ const id = 'e' + scene.nextId;
87
+ change((sc) => { sc.ents.push({ id: 'e' + sc.nextId++, type: addType }); }, 'auto');
88
+ pick(id);
89
+ },
90
+ }, 'Añadir')));
91
+
92
+ // ---- selected object
93
+ let inspector = h('section', { class: 'ed-sec' }, h('p', { class: 'hint' }, 'Haz clic en un objeto de la vista o de la lista para editarlo. Arrástralo en la vista para moverlo.'));
94
+ if (ent) {
95
+ const others = scene.ents.filter((e) => e.id !== ent.id);
96
+ const swatches = h('div', { class: 'swatches' },
97
+ h('button', { type: 'button', title: 'original', class: 'sw none' + (ent.color ? '' : ' on'), onclick: () => setEnt('color')(undefined) }, '×'),
98
+ vocab.color.map((c) => h('button', {
99
+ type: 'button', title: c, class: 'sw' + (ent.color === c ? ' on' : ''),
100
+ style: `background:#${PALETTE[c].toString(16).padStart(6, '0')}`, onclick: () => setEnt('color')(c),
101
+ })));
102
+ inspector = h('section', { class: 'ed-sec' },
103
+ h('h3', {}, entLabel(scene, ent)),
104
+ h('div', { class: 'grid' },
105
+ select('Tipo', ent.type, opts(vocab.type), setEnt('type')),
106
+ select('Acción', ent.act, opts(vocab.act), setEnt('act'), '(ninguna)'),
107
+ select('Tamaño', ent.size, opts(SIZES), setEnt('size'), 'normal'),
108
+ select('Cantidad', String(ent.n ?? 1), COUNTS.map((n) => [String(n), String(n)]), setEnt('n', (v) => (Number(v) > 1 ? Number(v) : undefined))),
109
+ select('Velocidad', ent.spd, [['slow', 'slow'], ['fast', 'fast']], setEnt('spd'), 'normal'),
110
+ select('Relación', ent.rel, opts(vocab.rel), setEnt('rel'), '(ninguna)'),
111
+ ent.rel ? select('Con', ent.ref, others.map((e) => [e.id, entLabel(scene, e)]), setEnt('ref')) : null),
112
+ h('div', { class: 'field' }, h('span', {}, 'Color'), swatches),
113
+ h('div', { class: 'row' },
114
+ ent.dx || ent.dz ? h('button', { type: 'button', onclick: () => change((sc) => { const e = sc.ents.find((x) => x.id === ent.id); delete e.dx; delete e.dz; }) }, 'Posición automática') : null,
115
+ h('button', {
116
+ type: 'button', onclick: () => {
117
+ const id = 'e' + scene.nextId;
118
+ change((sc) => {
119
+ const src = sc.ents.find((x) => x.id === ent.id), copy = { ...structuredClone(src), id: 'e' + sc.nextId++ };
120
+ delete copy.rel; delete copy.ref; delete copy.dx; delete copy.dz;
121
+ sc.ents.push(copy);
122
+ }, 'auto');
123
+ pick(id);
124
+ },
125
+ }, 'Duplicar'),
126
+ h('button', { type: 'button', class: 'danger', onclick: () => change((sc) => { sc.ents = sc.ents.filter((x) => x.id !== ent.id); for (const e of sc.ents) if (e.ref === ent.id) { delete e.rel; delete e.ref; } }) }, 'Borrar')));
127
+ }
128
+
129
+ root.replaceChildren(inspector, objBox, sceneBox);
130
+ }
web/js/lang/scenelang.js CHANGED
@@ -38,8 +38,18 @@ function addEntity(scene, attrs) {
38
  return e;
39
  }
40
 
41
- /** Returns { scene, notes } where notes lists what could not be applied. The input scene is not modified. */
42
- export function applyEdit(current, ops) {
 
 
 
 
 
 
 
 
 
 
43
  const scene = structuredClone(current), notes = [];
44
  for (const { op, attrs } of ops) {
45
  if (op === 'add') {
@@ -53,12 +63,10 @@ export function applyEdit(current, ops) {
53
  if (attrs.cam) scene.cam = attrs.cam;
54
  } else {
55
  const t = attrs.type;
56
- const hit = t === 'all' ? [...scene.ents] : t === 'it' ? scene.ents.slice(-1) : scene.ents.filter((e) => e.type === t);
57
  if (!hit.length) { notes.push({ code: scene.ents.length ? 'missing' : 'empty', type: t }); continue; }
58
  if (op === 'del') {
59
- const gone = new Set(hit.map((e) => e.id));
60
- scene.ents = scene.ents.filter((e) => !gone.has(e.id));
61
- for (const e of scene.ents) if (gone.has(e.ref)) { delete e.rel; delete e.ref; }
62
  } else {
63
  for (const e of hit) {
64
  if (attrs.color) e.color = attrs.color;
 
38
  return e;
39
  }
40
 
41
+ /** Remove entities by id, and drop relations that pointed at them. Mutates `scene`. */
42
+ export function deleteEntities(scene, ids) {
43
+ const gone = new Set(ids);
44
+ scene.ents = scene.ents.filter((e) => !gone.has(e.id));
45
+ for (const e of scene.ents) if (gone.has(e.ref)) { delete e.rel; delete e.ref; }
46
+ }
47
+
48
+ /**
49
+ * Returns { scene, notes } where notes lists what could not be applied. The input scene is not modified.
50
+ * opts.it: id of the selected entity, used for "it" ("make it blue"); otherwise "it" is the last one added.
51
+ */
52
+ export function applyEdit(current, ops, opts = {}) {
53
  const scene = structuredClone(current), notes = [];
54
  for (const { op, attrs } of ops) {
55
  if (op === 'add') {
 
63
  if (attrs.cam) scene.cam = attrs.cam;
64
  } else {
65
  const t = attrs.type;
66
+ const hit = t === 'all' ? [...scene.ents] : t === 'it' ? (opts.it && scene.ents.some((e) => e.id === opts.it) ? scene.ents.filter((e) => e.id === opts.it) : scene.ents.slice(-1)) : scene.ents.filter((e) => e.type === t);
67
  if (!hit.length) { notes.push({ code: scene.ents.length ? 'missing' : 'empty', type: t }); continue; }
68
  if (op === 'del') {
69
+ deleteEntities(scene, hit.map((e) => e.id));
 
 
70
  } else {
71
  for (const e of hit) {
72
  if (attrs.color) e.color = attrs.color;
web/js/main.js CHANGED
@@ -1,10 +1,11 @@
1
- // Glue: prompt -> model -> scene language -> three.js scene.
2
- import { applyEdit, emptyScene, parse } from './lang/scenelang.js';
 
3
  import { TinyGPT } from './nn/model.js';
4
  import { Stage } from './scene/stage.js';
5
 
6
  const $ = (id) => document.getElementById(id);
7
- const log = $('log'), input = $('prompt'), send = $('send'), status = $('status');
8
  const params = new URLSearchParams(location.search);
9
 
10
  const EXAMPLES = [
@@ -19,8 +20,8 @@ const EXAMPLES = [
19
  const LABEL = { loc: 'lugar', time: 'hora', weather: 'clima', shot: 'plano', cam: 'cámara' };
20
 
21
  const stage = new Stage($('canvas'));
22
- let scene = emptyScene(), model = null, qa = {};
23
- const history = [];
24
  let hpos = 0;
25
  stage.setScene(scene);
26
 
@@ -52,29 +53,83 @@ function showState() {
52
  }
53
  for (const e of scene.ents) {
54
  const s = el('span');
55
- s.append(el('b', '', (e.n > 1 ? e.n + '× ' : '') + e.type), [e.color, e.size, e.act, e.spd, e.rel && `${e.rel}→`].filter(Boolean).join(' ') || '·');
56
  box.append(s);
57
  }
58
  }
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  function run(text) {
61
  const { tokens, ms } = model.generate(text);
62
  const res = parse(tokens), notes = [];
63
  let answer = null;
64
  if (res.kind === 'new') {
65
- scene = res.scene;
66
- stage.setScene(scene, true);
67
  } else if (res.kind === 'edit' && res.ops.length) {
68
- const out = applyEdit(scene, res.ops);
69
  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.`);
70
- scene = out.scene;
71
- stage.setScene(scene, false);
72
  } else if (res.kind === 'qa') {
73
  answer = qa[res.topic]?.[res.lang] ?? qa.help?.es;
74
  } else {
75
  notes.push('No entendí la petición. Prueba con una frase más simple.');
76
  }
77
- showState();
78
  say('bot', (m) => {
79
  m.append(tokenRow(tokens));
80
  if (answer) m.append(el('p', '', answer));
@@ -110,9 +165,13 @@ for (const ex of EXAMPLES) {
110
  b.addEventListener('click', () => submit(ex));
111
  $('examples').append(b);
112
  }
 
 
 
 
113
  $('pause').addEventListener('click', (ev) => { stage.paused = !stage.paused; ev.target.textContent = stage.paused ? 'Reanudar' : 'Pausa'; });
114
  $('recenter').addEventListener('click', () => stage.frameCamera());
115
- $('clear').addEventListener('click', () => { scene = emptyScene(); stage.setScene(scene, true); showState(); });
116
  $('export').addEventListener('click', async () => {
117
  const blob = await stage.exportGLB(), a = el('a');
118
  a.href = URL.createObjectURL(blob);
@@ -120,20 +179,31 @@ $('export').addEventListener('click', async () => {
120
  a.click();
121
  setTimeout(() => URL.revokeObjectURL(a.href), 2000);
122
  });
 
 
 
 
 
 
 
 
 
123
 
124
- showState();
125
- say('bot', (m) => m.append(el('p', 'hint', 'Describe una escena en español o inglés. Después puedes pedir cambios: colores, tamaños, acciones, clima, lugar o cámara. Arrastra para mover la vista.')));
126
 
127
  try {
128
  [model, qa] = await Promise.all([TinyGPT.load('./model/'), fetch('./model/qa.json').then((r) => r.json())]);
 
129
  const mt = model.meta.meta ?? {};
130
  status.textContent = `Modelo de ${(mt.params / 1e6).toFixed(2)} M de parámetros, corriendo en tu navegador.`;
131
  input.disabled = send.disabled = false;
132
  input.focus();
 
133
  // ?q=prompt one;prompt two and ?t=seconds (freeze time) make the page scriptable for demos and tests
134
  for (const q of (params.get('q') ?? '').split(';')) submit(q);
135
  if (params.has('t')) { stage.t = Number(params.get('t')); stage.paused = true; if (scene.cam !== 'static') stage.frameCamera(); }
136
- window.tinyScene = { model, stage, submit, get scene() { return scene; }, ready: true };
137
  } catch (err) {
138
  status.textContent = 'No se pudo cargar el modelo: ' + err.message;
139
  }
 
1
+ // Glue: prompt -> model -> scene language -> three.js scene, plus hand editing with undo.
2
+ import { entLabel, renderEditor, vocabFrom } from './editor.js';
3
+ import { applyEdit, deleteEntities, emptyScene, parse } from './lang/scenelang.js';
4
  import { TinyGPT } from './nn/model.js';
5
  import { Stage } from './scene/stage.js';
6
 
7
  const $ = (id) => document.getElementById(id);
8
+ const log = $('log'), input = $('prompt'), send = $('send'), status = $('status'), panel = $('panel');
9
  const params = new URLSearchParams(location.search);
10
 
11
  const EXAMPLES = [
 
20
  const LABEL = { loc: 'lugar', time: 'hora', weather: 'clima', shot: 'plano', cam: 'cámara' };
21
 
22
  const stage = new Stage($('canvas'));
23
+ let scene = emptyScene(), model = null, qa = {}, vocab = null;
24
+ const history = [], undoStack = [], redoStack = [];
25
  let hpos = 0;
26
  stage.setScene(scene);
27
 
 
53
  }
54
  for (const e of scene.ents) {
55
  const s = el('span');
56
+ 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(' ') || '·');
57
  box.append(s);
58
  }
59
  }
60
 
61
+ // ------------------------------------------------------------------ one place where the scene changes
62
+ function refresh() {
63
+ showState();
64
+ $('undo').disabled = !undoStack.length;
65
+ $('redo').disabled = !redoStack.length;
66
+ if (vocab) renderEditor($('editor'), { scene, selected: stage.selected, vocab, change: (fn, camera) => edit(fn, camera), select: (id) => stage.select(id) });
67
+ }
68
+
69
+ /** Replace the scene, recording the previous one for undo. camera: see Stage.setScene. */
70
+ function commit(next, camera = 'keep') {
71
+ if (JSON.stringify(next) === JSON.stringify(scene)) return;
72
+ undoStack.push(scene);
73
+ if (undoStack.length > 200) undoStack.shift();
74
+ redoStack.length = 0;
75
+ scene = next;
76
+ stage.setScene(scene, camera);
77
+ refresh();
78
+ }
79
+ function edit(fn, camera = 'keep') {
80
+ const next = structuredClone(scene);
81
+ fn(next);
82
+ commit(next, camera);
83
+ }
84
+ function undo() {
85
+ if (!undoStack.length) return;
86
+ redoStack.push(scene);
87
+ scene = undoStack.pop();
88
+ stage.setScene(scene, 'keep');
89
+ refresh();
90
+ }
91
+ function redo() {
92
+ if (!redoStack.length) return;
93
+ undoStack.push(scene);
94
+ scene = redoStack.pop();
95
+ stage.setScene(scene, 'keep');
96
+ refresh();
97
+ }
98
+
99
+ stage.onSelect = (id) => {
100
+ if (id) setTab('edit');
101
+ refresh();
102
+ };
103
+ stage.onMove = (id, dx, dz) => edit((sc) => {
104
+ const e = sc.ents.find((x) => x.id === id);
105
+ if (!e) return;
106
+ e.dx = Math.round(((e.dx ?? 0) + dx) * 100) / 100;
107
+ e.dz = Math.round(((e.dz ?? 0) + dz) * 100) / 100;
108
+ });
109
+
110
+ function setTab(name) {
111
+ panel.dataset.tab = name;
112
+ for (const b of $('tabs').children) b.setAttribute('aria-selected', String(b.dataset.tab === name));
113
+ }
114
+ for (const b of $('tabs').children) b.addEventListener('click', () => setTab(b.dataset.tab));
115
+
116
+ // ------------------------------------------------------------------ model
117
  function run(text) {
118
  const { tokens, ms } = model.generate(text);
119
  const res = parse(tokens), notes = [];
120
  let answer = null;
121
  if (res.kind === 'new') {
122
+ stage.select(null);
123
+ commit(res.scene, 'reset');
124
  } else if (res.kind === 'edit' && res.ops.length) {
125
+ const out = applyEdit(scene, res.ops, { it: stage.selected });
126
  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.`);
127
+ commit(out.scene, 'auto');
 
128
  } else if (res.kind === 'qa') {
129
  answer = qa[res.topic]?.[res.lang] ?? qa.help?.es;
130
  } else {
131
  notes.push('No entendí la petición. Prueba con una frase más simple.');
132
  }
 
133
  say('bot', (m) => {
134
  m.append(tokenRow(tokens));
135
  if (answer) m.append(el('p', '', answer));
 
165
  b.addEventListener('click', () => submit(ex));
166
  $('examples').append(b);
167
  }
168
+
169
+ // ------------------------------------------------------------------ toolbar and keyboard
170
+ $('undo').addEventListener('click', undo);
171
+ $('redo').addEventListener('click', redo);
172
  $('pause').addEventListener('click', (ev) => { stage.paused = !stage.paused; ev.target.textContent = stage.paused ? 'Reanudar' : 'Pausa'; });
173
  $('recenter').addEventListener('click', () => stage.frameCamera());
174
+ $('clear').addEventListener('click', () => { stage.select(null); commit(emptyScene(), 'reset'); });
175
  $('export').addEventListener('click', async () => {
176
  const blob = await stage.exportGLB(), a = el('a');
177
  a.href = URL.createObjectURL(blob);
 
179
  a.click();
180
  setTimeout(() => URL.revokeObjectURL(a.href), 2000);
181
  });
182
+ window.addEventListener('keydown', (ev) => {
183
+ const typing = /^(INPUT|TEXTAREA|SELECT)$/.test(document.activeElement?.tagName);
184
+ const mod = ev.ctrlKey || ev.metaKey;
185
+ if (mod && ev.key.toLowerCase() === 'z' && !typing) { ev.preventDefault(); ev.shiftKey ? redo() : undo(); }
186
+ else if (mod && ev.key.toLowerCase() === 'y' && !typing) { ev.preventDefault(); redo(); }
187
+ else if (typing) return;
188
+ else if ((ev.key === 'Delete' || ev.key === 'Backspace') && stage.selected) { ev.preventDefault(); edit((sc) => deleteEntities(sc, [stage.selected])); }
189
+ else if (ev.key === 'Escape') stage.select(null);
190
+ });
191
 
192
+ refresh();
193
+ 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.')));
194
 
195
  try {
196
  [model, qa] = await Promise.all([TinyGPT.load('./model/'), fetch('./model/qa.json').then((r) => r.json())]);
197
+ vocab = vocabFrom(model.meta.vocab);
198
  const mt = model.meta.meta ?? {};
199
  status.textContent = `Modelo de ${(mt.params / 1e6).toFixed(2)} M de parámetros, corriendo en tu navegador.`;
200
  input.disabled = send.disabled = false;
201
  input.focus();
202
+ refresh();
203
  // ?q=prompt one;prompt two and ?t=seconds (freeze time) make the page scriptable for demos and tests
204
  for (const q of (params.get('q') ?? '').split(';')) submit(q);
205
  if (params.has('t')) { stage.t = Number(params.get('t')); stage.paused = true; if (scene.cam !== 'static') stage.frameCamera(); }
206
+ window.tinyScene = { model, stage, submit, edit, undo, redo, get scene() { return scene; }, ready: true };
207
  } catch (err) {
208
  status.textContent = 'No se pudo cargar el modelo: ' + err.message;
209
  }
web/js/scene/animate.js CHANGED
@@ -350,6 +350,7 @@ export function updateInstance(inst, t, ctx = {}) {
350
  const on = act === 'fly' || act === 'drive' || LOCO.has(act) || inst.motion.kind !== 'none';
351
  for (const sp of g.spinners) if (on || sp.userData.always) sp.rotation[sp.userData.axis] += T * (sp.userData.always && !on ? 2 : 24);
352
  }
 
353
  o.position.set(s.x, s.y, s.z);
354
  o.rotation.set(s.pitch, s.yaw, s.roll);
355
  if (inst.sprites) updateZzz(inst, T);
 
350
  const on = act === 'fly' || act === 'drive' || LOCO.has(act) || inst.motion.kind !== 'none';
351
  for (const sp of g.spinners) if (on || sp.userData.always) sp.rotation[sp.userData.axis] += T * (sp.userData.always && !on ? 2 : 24);
352
  }
353
+ if (inst.shift && !inst.mount) { s.x += inst.shift.x; s.z += inst.shift.z; } // live drag preview
354
  o.position.set(s.x, s.y, s.z);
355
  o.rotation.set(s.pitch, s.yaw, s.roll);
356
  if (inst.sprites) updateZzz(inst, T);
web/js/scene/layout.js CHANGED
@@ -24,6 +24,7 @@ function makeGroup(ent) {
24
  ? { 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 }
25
  : { x: 0, z: 0 };
26
  obj.scale.setScalar(scale);
 
27
  G.insts.push({
28
  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),
29
  phase: n > 1 ? rand() * 1.5 : 0, spdMul: SPD_MUL[ent.spd] ?? 1, act: ent.act, motion: { kind: 'none' }, mount: null,
@@ -132,6 +133,8 @@ export function buildScene(sc) {
132
  else if (G.rel === 'front') G.center = { x: c.x - 0.3 * G.R, z: c.z + (gap + 0.5) };
133
  else G.center = { x: c.x, z: c.z };
134
  }
 
 
135
  for (const i of G.insts) {
136
  i.base.set(G.center.x + i.off.x, 0, G.center.z + i.off.z);
137
  i.motion = motionFor(i, G.act, { x: G.center.x, z: G.center.z });
 
24
  ? { 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 }
25
  : { x: 0, z: 0 };
26
  obj.scale.setScalar(scale);
27
+ obj.userData.entId = ent.id;
28
  G.insts.push({
29
  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),
30
  phase: n > 1 ? rand() * 1.5 : 0, spdMul: SPD_MUL[ent.spd] ?? 1, act: ent.act, motion: { kind: 'none' }, mount: null,
 
133
  else if (G.rel === 'front') G.center = { x: c.x - 0.3 * G.R, z: c.z + (gap + 0.5) };
134
  else G.center = { x: c.x, z: c.z };
135
  }
136
+ const { dx = 0, dz = 0 } = G.ent; // offset set by dragging the object by hand
137
+ if ((dx || dz) && G.rel !== 'ride' && G.rel !== 'on') G.center = { x: G.center.x + dx, z: G.center.z + dz };
138
  for (const i of G.insts) {
139
  i.base.set(G.center.x + i.off.x, 0, G.center.z + i.off.z);
140
  i.motion = motionFor(i, G.act, { x: G.center.x, z: G.center.z });
web/js/scene/stage.js CHANGED
@@ -24,6 +24,17 @@ export class Stage {
24
  this.controls.maxPolarAngle = Math.PI / 2 - 0.01;
25
  this.t = 0; this.last = performance.now(); this.paused = false; this.built = null; this.sc = null;
26
  this._p = new THREE.Vector3(); this._prev = new THREE.Vector3(); this._zoom = 1; this._pan = 0;
 
 
 
 
 
 
 
 
 
 
 
27
  new ResizeObserver(() => this.resize()).observe(canvas.parentElement);
28
  this.resize();
29
  this.renderer.setAnimationLoop(() => this.frame());
@@ -36,8 +47,11 @@ export class Stage {
36
  this.camera.updateProjectionMatrix();
37
  }
38
 
39
- /** Rebuild everything from a scene description. resetCamera=false keeps the user's current view. */
40
- setScene(sc, resetCamera = true) {
 
 
 
41
  const old = this.built, prevBox = old?.box;
42
  if (old) { this.scene.remove(old.root); old.dispose(); }
43
  const b = this.built = buildScene(sc);
@@ -50,13 +64,99 @@ export class Stage {
50
  b.root.updateMatrixWorld(true);
51
  const view = `${sc.shot}|${sc.zoom}|${sc.cam}`;
52
  const moved = !prevBox || prevBox.min.distanceTo(b.box.min) + prevBox.max.distanceTo(b.box.max) > 0.6;
53
- if (resetCamera || view !== this._view || moved) this.frameCamera();
 
54
  this._view = view;
55
  this.controls.autoRotate = sc.cam === 'orbit';
56
  this.controls.autoRotateSpeed = 2.2;
57
  if (b.focus) b.focus.obj.getWorldPosition(this._prev);
58
  }
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  frameCamera() {
61
  const b = this.built, sc = this.sc;
62
  const tight = (sc.shot === 'close' || sc.cam === 'follow') && b.focus;
@@ -105,6 +205,12 @@ export class Stage {
105
  this._zoom = z;
106
  }
107
  }
 
 
 
 
 
 
108
  this.controls.update();
109
  this.renderer.render(this.scene, this.camera);
110
  }
 
24
  this.controls.maxPolarAngle = Math.PI / 2 - 0.01;
25
  this.t = 0; this.last = performance.now(); this.paused = false; this.built = null; this.sc = null;
26
  this._p = new THREE.Vector3(); this._prev = new THREE.Vector3(); this._zoom = 1; this._pan = 0;
27
+ // hand editing: click to select, drag to move
28
+ this.selected = null; this.onSelect = () => {}; this.onMove = () => {};
29
+ this.ray = new THREE.Raycaster(); this.ptr = new THREE.Vector2();
30
+ this.selBox = new THREE.Box3();
31
+ this.helper = new THREE.Box3Helper(this.selBox, 0xffb454);
32
+ this.helper.visible = false;
33
+ this.scene.add(this.helper);
34
+ this._drag = null;
35
+ canvas.parentElement.addEventListener('pointerdown', (e) => this._down(e), { capture: true }); // before OrbitControls
36
+ window.addEventListener('pointermove', (e) => this._dragMove(e));
37
+ window.addEventListener('pointerup', (e) => this._up(e));
38
  new ResizeObserver(() => this.resize()).observe(canvas.parentElement);
39
  this.resize();
40
  this.renderer.setAnimationLoop(() => this.frame());
 
47
  this.camera.updateProjectionMatrix();
48
  }
49
 
50
+ /**
51
+ * Rebuild everything from a scene description.
52
+ * camera: 'reset' always reframes, 'auto' reframes when the scene's extent changes, 'keep' only when shot/zoom/cam change.
53
+ */
54
+ setScene(sc, camera = 'reset') {
55
  const old = this.built, prevBox = old?.box;
56
  if (old) { this.scene.remove(old.root); old.dispose(); }
57
  const b = this.built = buildScene(sc);
 
64
  b.root.updateMatrixWorld(true);
65
  const view = `${sc.shot}|${sc.zoom}|${sc.cam}`;
66
  const moved = !prevBox || prevBox.min.distanceTo(b.box.min) + prevBox.max.distanceTo(b.box.max) > 0.6;
67
+ if (camera === 'reset' || view !== this._view || (camera === 'auto' && moved)) this.frameCamera();
68
+ if (this.selected && !sc.ents.some((e) => e.id === this.selected)) this.select(null);
69
  this._view = view;
70
  this.controls.autoRotate = sc.cam === 'orbit';
71
  this.controls.autoRotateSpeed = 2.2;
72
  if (b.focus) b.focus.obj.getWorldPosition(this._prev);
73
  }
74
 
75
+ // ---------------------------------------------------------------- selection and dragging
76
+ select(id) {
77
+ if (id === this.selected) return;
78
+ this.selected = id;
79
+ this.helper.visible = !!id;
80
+ this.onSelect(id);
81
+ }
82
+
83
+ _pick(e) {
84
+ if (!this.built) return null;
85
+ const r = this.canvas.getBoundingClientRect();
86
+ this.ptr.set(((e.clientX - r.left) / r.width) * 2 - 1, -((e.clientY - r.top) / r.height) * 2 + 1);
87
+ this.ray.setFromCamera(this.ptr, this.camera);
88
+ for (const hit of this.ray.intersectObject(this.built.actors, true)) {
89
+ for (let o = hit.object; o; o = o.parent) if (o.userData.entId) return { id: o.userData.entId, point: hit.point };
90
+ }
91
+ return null;
92
+ }
93
+
94
+ /** The entity that actually owns the position: riders, orbiters and chasers move with their target. */
95
+ _anchor(id) {
96
+ const ents = this.sc.ents, seen = new Set();
97
+ let e = ents.find((x) => x.id === id);
98
+ while (e && e.ref && ['ride', 'on', 'around', 'over', 'chase'].includes(e.rel) && !seen.has(e.id)) {
99
+ seen.add(e.id);
100
+ e = ents.find((x) => x.id === e.ref) ?? e;
101
+ }
102
+ return e?.id;
103
+ }
104
+
105
+ /** Entities whose placement depends on `id` (including itself). */
106
+ _followers(id) {
107
+ const ents = this.sc.ents, out = new Set();
108
+ for (const e of ents) {
109
+ const seen = new Set();
110
+ for (let x = e; x && !seen.has(x.id); x = ents.find((y) => y.id === x.ref)) {
111
+ seen.add(x.id);
112
+ if (x.id === id) { out.add(e.id); break; }
113
+ }
114
+ }
115
+ return out;
116
+ }
117
+
118
+ _down(e) {
119
+ if (e.target !== this.canvas || e.button !== 0 || !e.isPrimary) return;
120
+ const hit = this._pick(e);
121
+ this._press = { x: e.clientX, y: e.clientY, hit: !!hit };
122
+ if (!hit) return;
123
+ this.select(hit.id);
124
+ const anchor = this._anchor(hit.id), movers = this._followers(anchor);
125
+ const plane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -hit.point.y);
126
+ const insts = this.built.insts.filter((i) => movers.has(i.ent.id) && !i.mount);
127
+ for (const i of insts) i.shift = { x: 0, z: 0 };
128
+ this._drag = { anchor, plane, start: hit.point.clone(), insts, d: new THREE.Vector3(), moved: false };
129
+ this.controls.enabled = false;
130
+ }
131
+
132
+ _dragMove(e) {
133
+ const d = this._drag;
134
+ if (!d) return;
135
+ if (Math.hypot(e.clientX - this._press.x, e.clientY - this._press.y) > 4) d.moved = true;
136
+ const r = this.canvas.getBoundingClientRect();
137
+ this.ptr.set(((e.clientX - r.left) / r.width) * 2 - 1, -((e.clientY - r.top) / r.height) * 2 + 1);
138
+ this.ray.setFromCamera(this.ptr, this.camera);
139
+ const p = this.ray.ray.intersectPlane(d.plane, new THREE.Vector3());
140
+ if (!p) return;
141
+ d.d.subVectors(p, d.start);
142
+ const lim = 60;
143
+ d.d.x = Math.max(-lim, Math.min(lim, d.d.x)); d.d.z = Math.max(-lim, Math.min(lim, d.d.z));
144
+ for (const i of d.insts) { i.shift.x = d.d.x; i.shift.z = d.d.z; }
145
+ }
146
+
147
+ _up(e) {
148
+ const d = this._drag, p = this._press;
149
+ this._press = null;
150
+ if (d) {
151
+ this._drag = null;
152
+ this.controls.enabled = true;
153
+ for (const i of d.insts) i.shift = null;
154
+ if (d.moved) this.onMove(d.anchor, d.d.x, d.d.z);
155
+ } else if (p && !p.hit && Math.hypot(e.clientX - p.x, e.clientY - p.y) < 5) {
156
+ this.select(null);
157
+ }
158
+ }
159
+
160
  frameCamera() {
161
  const b = this.built, sc = this.sc;
162
  const tight = (sc.shot === 'close' || sc.cam === 'follow') && b.focus;
 
205
  this._zoom = z;
206
  }
207
  }
208
+ if (this.selected && b) {
209
+ b.root.updateMatrixWorld(true);
210
+ this.selBox.makeEmpty();
211
+ for (const i of b.insts) if (i.ent.id === this.selected) this.selBox.expandByObject(i.obj);
212
+ this.helper.visible = !this.selBox.isEmpty();
213
+ }
214
  this.controls.update();
215
  this.renderer.render(this.scene, this.camera);
216
  }
web/style.css CHANGED
@@ -46,3 +46,31 @@ h1 { margin: 0; font-size: 19px; letter-spacing: .2px; }
46
  #panel header { padding: 10px 16px 8px; }
47
  #examples { max-height: 76px; }
48
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  #panel header { padding: 10px 16px 8px; }
47
  #examples { max-height: 76px; }
48
  }
49
+
50
+ /* ---------------------------------------------------------------- tabs and hand editor */
51
+ #tabs { display: flex; gap: 4px; margin-top: 10px; }
52
+ #tabs button { flex: 1; background: transparent; border-radius: 8px; }
53
+ #tabs button[aria-selected="true"] { background: var(--chip); border-color: var(--accent); color: var(--accent); }
54
+ .tab { flex: 1; min-height: 0; display: flex; flex-direction: column; }
55
+ #panel[data-tab="chat"] #editor, #panel[data-tab="edit"] #chat { display: none; }
56
+ #editor { overflow-y: auto; padding: 4px 16px 16px; gap: 0; }
57
+ .ed-sec { padding: 12px 0; border-bottom: 1px solid var(--line); }
58
+ .ed-sec:last-child { border-bottom: 0; }
59
+ .ed-sec h3 { margin: 0 0 10px; font-size: 13px; text-transform: uppercase; letter-spacing: .6px; color: var(--dim); }
60
+ .ed-sec .hint { margin: 0; }
61
+ .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 10px; }
62
+ .field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--dim); min-width: 0; }
63
+ .field select { font: inherit; font-size: 13.5px; color: var(--text); background: #0f1117; border: 1px solid var(--line); border-radius: 7px; padding: 6px 8px; min-width: 0; }
64
+ .field select:focus { outline: none; border-color: var(--accent); }
65
+ .row { display: flex; flex-wrap: wrap; gap: 8px; align-items: flex-end; margin-top: 10px; }
66
+ .row .field { flex: 1; }
67
+ .swatches { display: flex; flex-wrap: wrap; gap: 6px; }
68
+ .sw { width: 26px; height: 26px; padding: 0; border-radius: 50%; border: 2px solid transparent; box-shadow: inset 0 0 0 1px rgba(255,255,255,.15); }
69
+ .sw.none { background: var(--chip); color: var(--dim); font-size: 13px; line-height: 1; }
70
+ .sw.on { border-color: var(--accent); }
71
+ .ent-list { display: flex; flex-wrap: wrap; gap: 6px; }
72
+ .ent { display: inline-flex; align-items: center; gap: 6px; background: var(--chip); border-radius: 999px; padding: 5px 11px; }
73
+ .ent small { color: var(--dim); }
74
+ .ent.on { border-color: var(--accent); color: var(--accent); }
75
+ .dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
76
+ button.danger:hover { border-color: #ff6b6b; color: #ff8a8a; }