@vim3d/core 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,38 @@
1
1
  # @vim3d/core
2
2
 
3
- This package is reserved for the shared Vim3D scene model.
3
+ Compile a scene once, then evaluate any frame without a renderer. Frames are
4
+ zero-based. Evaluating a frame out of order gives the same result as replaying
5
+ from frame zero.
6
+
7
+ ```ts
8
+ import { compileScene, evaluateFrame } from '@vim3d/core';
9
+
10
+ const scene = compileScene({
11
+ path: 'demo.vim3d',
12
+ text: 'add sphere 4\nname hero\nexpr /obj/hero/ty "sin($T)"',
13
+ });
14
+
15
+ if ('value' in scene) {
16
+ const frame = evaluateFrame(scene.value, 30);
17
+ if ('value' in frame) console.log(frame.value.objects);
18
+ else console.error(frame.diagnostics);
19
+ } else {
20
+ console.error(scene.diagnostics);
21
+ }
22
+ ```
23
+
24
+ Pass `{ readFile, fps, frameCount }` as compile options as needed. Core has
25
+ no filesystem or browser dependency: the host resolves included scenes and
26
+ channel assets. Includes are resolved relative to the including scene; paths
27
+ in `load channel` are passed to the host as written. The compiled graph
28
+ exposes scalar, position, and transform dependencies. Frame snapshots hold
29
+ evaluated scalar values and final local and world transforms; a renderer
30
+ does not run expressions or constraints again.
31
+
32
+ For a procedural channel, use `add channel` and drive its `value` port
33
+ with `expr`. For live browser sources (such as Web Audio), declare
34
+ `liveChannels` in `compileScene` options and supply per-frame samples in
35
+ `evaluateFrame(scene, frame, { liveChannels })`; off-time reads (`chf`,
36
+ `cht`, or non-unit `register`) and `{ deterministic: true }` reject live
37
+ graphs. For authored path vertices, use `begin draw`, one XYZ triplet per
38
+ line, and `end`.
package/channels.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { type ChannelPort, type Table } from './model.js';
2
+ import { type SourceLocation } from './types.js';
3
+ export declare const validSegment: (text: string) => boolean;
4
+ export declare function loadTable(asset: string, data: string | Uint8Array | null, source: SourceLocation): {
5
+ table: Table;
6
+ ports: Map<string, ChannelPort>;
7
+ };
8
+ export declare function sample(table: Table, port: ChannelPort, frame: number, fps: number): number;
package/channels.js ADDED
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validSegment = void 0;
4
+ exports.loadTable = loadTable;
5
+ exports.sample = sample;
6
+ const math_js_1 = require("./math.js");
7
+ const model_js_1 = require("./model.js");
8
+ const types_js_1 = require("./types.js");
9
+ const validSegment = (text) => !!text && text !== '.' && text !== '..' && !/[\/\x00-\x1f\x7f]/.test(text);
10
+ exports.validSegment = validSegment;
11
+ function loadTable(asset, data, source) {
12
+ if (data == null)
13
+ (0, types_js_1.fail)('missing-asset', 'Cannot read channel asset: ' + asset, source);
14
+ const text = typeof data === 'string' ? data : new TextDecoder().decode(data);
15
+ const table = { asset, interpolation: 'step', boundary: 'wrap' };
16
+ const ports = new Map();
17
+ const invalid = (message) => (0, types_js_1.fail)('invalid-channel-table', asset + ': ' + message, source);
18
+ if (asset.endsWith('.chan')) {
19
+ const rows = text.trim().split(/\r?\n/).filter(row => row.trim());
20
+ if (!rows.length)
21
+ invalid('expected a nonempty table');
22
+ const samples = rows.map(row => {
23
+ const fields = row.trim().split(/[ \t]+/);
24
+ if (fields.length !== 2)
25
+ invalid('expected two numeric fields per row');
26
+ (0, model_js_1.readNumber)(fields[0], source);
27
+ return (0, model_js_1.readNumber)(fields[1], source);
28
+ });
29
+ ports.set('value', { samples, source });
30
+ }
31
+ else if (asset.endsWith('.json')) {
32
+ let raw;
33
+ try {
34
+ raw = JSON.parse(text);
35
+ }
36
+ catch {
37
+ invalid('invalid JSON');
38
+ }
39
+ if (!raw || !Number.isFinite(raw.sampleRate) || raw.sampleRate <= 0)
40
+ invalid('expected a positive sampleRate');
41
+ if (raw.boundary !== undefined && raw.boundary !== 'wrap' && raw.boundary !== 'clamp')
42
+ invalid('invalid boundary mode');
43
+ if (raw.interpolation !== undefined && raw.interpolation !== 'step' && raw.interpolation !== 'linear')
44
+ invalid('invalid interpolation');
45
+ if (!raw.channels || typeof raw.channels !== 'object' || Array.isArray(raw.channels))
46
+ invalid('expected named channels');
47
+ const tracks = Object.entries(raw.channels);
48
+ if (!tracks.length)
49
+ invalid('expected at least one channel');
50
+ let length;
51
+ for (const [name, samples] of tracks) {
52
+ if (!(0, exports.validSegment)(name) || !Array.isArray(samples) || !samples.length ||
53
+ !samples.every(Number.isFinite) || (length !== undefined && length !== samples.length)) {
54
+ invalid('expected named, finite, nonempty tracks of equal length');
55
+ }
56
+ length = samples.length;
57
+ ports.set(name, { samples, source });
58
+ }
59
+ Object.assign(table, { sampleRate: raw.sampleRate,
60
+ interpolation: raw.interpolation ?? 'step', boundary: raw.boundary ?? 'wrap' });
61
+ }
62
+ else
63
+ invalid('expected a .chan or .json asset');
64
+ return { table, ports };
65
+ }
66
+ function sample(table, port, frame, fps) {
67
+ const rows = port.samples;
68
+ const r = (0, types_js_1.finite)(table.sampleRate === undefined ? frame : frame * table.sampleRate / fps, 'Row coordinate', port.source);
69
+ const i = Math.floor(r);
70
+ const index = (j) => table.boundary === 'wrap' ? (0, math_js_1.wrap)(j, rows.length) : Math.max(0, Math.min(rows.length - 1, j));
71
+ const value = rows[index(i)];
72
+ return table.interpolation === 'step' ? value
73
+ : (0, types_js_1.finite)(value + (rows[index(i + 1)] - value) * (r - i), 'Interpolated sample', port.source);
74
+ }
package/compile.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { type CompiledScene, type CompileOptions, type Result, type SceneSource } from './types.js';
2
+ export declare function compileScene(source: SceneSource, options?: CompileOptions): Result<CompiledScene>;