Download tests/check_scene.mjs from paralaif/tiny-scene-three: direct link, hf CLI and curl.
- Browser
- Download file 3.23 kB
-
https://huggingface.co/paralaif/tiny-scene-three/resolve/main/tests/check_scene.mjs
- Command line
-
hf download hf://paralaif/tiny-scene-three/tests/check_scene.mjs
-
curl -L -o check_scene.mjs https://huggingface.co/paralaif/tiny-scene-three/resolve/main/tests/check_scene.mjs
3.23 kB
| // Builds every object, action, place, time and weather the model can name and checks nothing breaks. | |
| // node tests/check_scene.mjs | |
| import fs from 'node:fs'; | |
| import * as THREE from 'three'; | |
| import { applyEdit, emptyScene, parse } from '../web/js/lang/scenelang.js'; | |
| import { BUILDERS, buildScene } from '../web/js/scene/layout.js'; | |
| const meta = JSON.parse(fs.readFileSync(new URL('../web/model/model.json', import.meta.url))); | |
| const vals = (k) => meta.vocab.filter((t) => t.startsWith(k + '=')).map((t) => t.split('=')[1]); | |
| const types = vals('type').filter((t) => t !== 'it' && t !== 'all'), acts = vals('act'); | |
| let fail = 0; | |
| const bad = (msg) => { fail++; console.log('FAIL', msg); }; | |
| const missing = types.filter((t) => !BUILDERS[t]); | |
| if (missing.length) bad('no builder for: ' + missing.join(', ')); | |
| function finite(root) { | |
| let ok = true; | |
| root.updateMatrixWorld(true); | |
| root.traverse((o) => { for (const v of o.matrixWorld.elements) if (!Number.isFinite(v)) ok = false; }); | |
| return ok; | |
| } | |
| function run(sc, label) { | |
| try { | |
| const b = buildScene(sc); | |
| for (const t of [0, 0.37, 1.9, 7.3, 31.4]) { b.update(t); if (!finite(b.root)) return bad(label + ' non-finite at t=' + t); } | |
| if (!Number.isFinite(b.stageR) || b.box.isEmpty()) bad(label + ' bad bounds'); | |
| b.dispose(); | |
| } catch (e) { bad(label + ' threw ' + e.stack.split('\n').slice(0, 3).join(' | ')); } | |
| } | |
| const scene = (ents, extra = {}) => ({ ...emptyScene(), ...extra, ents: ents.map((e, i) => ({ id: 'e' + (i + 1), ...e })) }); | |
| let n = 0; | |
| for (const type of types) for (const act of [undefined, ...acts]) { run(scene([{ type, act }]), `${type}/${act}`); n++; } | |
| for (const loc of vals('loc')) for (const time of vals('time')) for (const weather of vals('weather')) { | |
| run(scene([{ type: 'dog', act: 'walk' }, { type: 'boat' }], { loc, time, weather }), `${loc}/${time}/${weather}`); n++; | |
| } | |
| for (const rel of vals('rel')) for (const a of ['alien', 'fox', 'bird', 'ball', 'car']) for (const b of ['horse', 'skateboard', 'tree', 'ufo', 'chair']) { | |
| run(scene([{ type: a, rel, ref: 'e2', act: rel === 'over' ? 'jump' : 'dance', n: 2 }, { type: b }]), `${a} ${rel} ${b}`); n++; | |
| } | |
| for (const size of vals('size')) run(scene([{ type: 'cow', size, n: 5, color: 'gold' }]), 'size ' + size), n++; | |
| // scene language round trip | |
| const p = parse('<new> loc=beach time=sunset <ent> type=fox color=red act=jump rel=over <ent> type=car color=blue'.split(' ')); | |
| if (p.kind !== 'new' || p.scene.ents.length !== 2 || p.scene.ents[0].ref !== 'e2') bad('parse new'); | |
| const e = parse('<edit> <set> type=fox color=green <add> type=tree n=2 <del> type=car <cam> shot=out <env> weather=rain'.split(' ')); | |
| const r = applyEdit(p.scene, e.ops); | |
| if (r.scene.ents.length !== 2 || r.scene.ents[0].color !== 'green' || r.scene.ents[0].rel || r.scene.zoom !== 1 || r.scene.weather !== 'rain') bad('apply edit ' + JSON.stringify(r.scene)); | |
| if (applyEdit(p.scene, parse('<edit> <set> type=cow color=red'.split(' ')).ops).notes[0]?.code !== 'missing') bad('missing note'); | |
| run(r.scene, 'edited scene'); | |
| console.log(`${n} scenes built, ${types.length} types, ${acts.length} actions, three r${THREE.REVISION}: ${fail ? fail + ' FAILED' : 'all ok'}`); | |
| process.exit(fail ? 1 : 0); | |