@vim3d/core 0.2.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 +6 -2
- package/compile.js +195 -23
- package/contexts.d.ts +55 -0
- package/contexts.js +310 -0
- package/esm/compile.js +195 -23
- package/esm/contexts.js +304 -0
- package/esm/evaluate.js +56 -7
- package/esm/graph.js +27 -5
- package/esm/index.js +1 -0
- package/esm/registry.js +93 -0
- package/evaluate.d.ts +2 -2
- package/evaluate.js +56 -7
- package/graph.d.ts +3 -0
- package/graph.js +27 -5
- package/index.d.ts +3 -1
- package/index.js +4 -1
- package/model.d.ts +6 -0
- package/package.json +2 -2
- package/registry.d.ts +14 -0
- package/registry.js +98 -0
- package/types.d.ts +72 -3
package/evaluate.js
CHANGED
|
@@ -2,16 +2,17 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.evaluateFrame = evaluateFrame;
|
|
4
4
|
const channels_js_1 = require("./channels.js");
|
|
5
|
+
const contexts_js_1 = require("./contexts.js");
|
|
5
6
|
const expression_js_1 = require("./expression.js");
|
|
6
7
|
const math_js_1 = require("./math.js");
|
|
7
8
|
const model_js_1 = require("./model.js");
|
|
8
9
|
const types_js_1 = require("./types.js");
|
|
9
|
-
function evaluateFrame(scene, frame) {
|
|
10
|
+
function evaluateFrame(scene, frame, options = {}) {
|
|
10
11
|
try {
|
|
11
12
|
if (!Number.isSafeInteger(frame) || frame < 0 || (scene.frameCount !== undefined && frame >= scene.frameCount)) {
|
|
12
13
|
(0, types_js_1.fail)('invalid-frame', 'Frame is outside the scene: ' + frame);
|
|
13
14
|
}
|
|
14
|
-
return { value: evaluate(scene, frame) };
|
|
15
|
+
return { value: evaluate(scene, frame, options) };
|
|
15
16
|
}
|
|
16
17
|
catch (error) {
|
|
17
18
|
if (error instanceof types_js_1.EvaluationError)
|
|
@@ -19,10 +20,39 @@ function evaluateFrame(scene, frame) {
|
|
|
19
20
|
throw error;
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
|
-
function
|
|
23
|
+
function isLiveSample(input) {
|
|
24
|
+
return typeof input === 'object' && input !== null && 'values' in input
|
|
25
|
+
&& typeof input.values === 'object' && input.values !== null;
|
|
26
|
+
}
|
|
27
|
+
function readLivePort(options, channel, portName, path, source) {
|
|
28
|
+
const input = options?.liveChannels?.[channel.name] ?? options?.liveChannels?.[(0, model_js_1.channelPath)(channel)];
|
|
29
|
+
if (!input || typeof input !== 'object') {
|
|
30
|
+
(0, types_js_1.fail)('missing-live-channel', 'Missing live channel input for ' + path, source);
|
|
31
|
+
}
|
|
32
|
+
let values;
|
|
33
|
+
if (isLiveSample(input)) {
|
|
34
|
+
if (input.timestamp !== undefined) {
|
|
35
|
+
(0, types_js_1.finite)(input.timestamp, 'Live channel timestamp for ' + (0, model_js_1.channelPath)(channel), source);
|
|
36
|
+
}
|
|
37
|
+
values = input.values;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
values = input;
|
|
41
|
+
}
|
|
42
|
+
const value = values[portName];
|
|
43
|
+
if (value === undefined)
|
|
44
|
+
(0, types_js_1.fail)('missing-live-channel', 'Missing live channel value for ' + path, source);
|
|
45
|
+
if (typeof value !== 'number')
|
|
46
|
+
(0, types_js_1.fail)('non-finite', path + ' must be finite', source);
|
|
47
|
+
return (0, types_js_1.finite)(value, path, source);
|
|
48
|
+
}
|
|
49
|
+
function evaluate(scene, frame, options) {
|
|
23
50
|
const plan = model_js_1.plans.get(scene);
|
|
24
51
|
if (!plan)
|
|
25
52
|
(0, types_js_1.fail)('invalid-scene', 'Scene was not created by compileScene');
|
|
53
|
+
if (options?.deterministic && scene.liveChannels.length) {
|
|
54
|
+
(0, types_js_1.fail)('non-deterministic-live-channel', 'Deterministic evaluation cannot run a scene with live channels');
|
|
55
|
+
}
|
|
26
56
|
const { fps, frameCount } = scene;
|
|
27
57
|
const time = (0, types_js_1.finite)(frame / fps, 'Scene time');
|
|
28
58
|
const state = new Map(plan.objects.map(object => [object, (0, model_js_1.copyState)(object.authored)]));
|
|
@@ -44,28 +74,45 @@ function evaluate(scene, frame) {
|
|
|
44
74
|
const outputs = new Map();
|
|
45
75
|
for (const channel of plan.channels) {
|
|
46
76
|
for (const [name, port] of channel.ports) {
|
|
47
|
-
outputs.set((0, model_js_1.channelPath)(channel) + '/' + name, { channel, port, cache: new Map() });
|
|
77
|
+
outputs.set((0, model_js_1.channelPath)(channel) + '/' + name, { channel, name, port, cache: new Map() });
|
|
48
78
|
}
|
|
49
79
|
}
|
|
80
|
+
const contexts = (0, contexts_js_1.createContextEvaluation)(plan.extensions, plan.order, frame, time, fps, frameCount, options, read);
|
|
50
81
|
const expr = (ast, q, source) => (0, expression_js_1.evaluateExpression)(ast, { frame: q, fps, frameCount, source, read });
|
|
51
82
|
function read(path, q) {
|
|
83
|
+
if (path.startsWith('/audio/'))
|
|
84
|
+
return contexts.read(path, q);
|
|
52
85
|
if (path.startsWith('/obj/')) {
|
|
53
86
|
const { object, name } = parameters.get(path);
|
|
54
87
|
return state.get(object).parameters[name];
|
|
55
88
|
}
|
|
56
|
-
|
|
89
|
+
if (plan.audioChannels.has(path)) {
|
|
90
|
+
(0, contexts_js_1.checkAudioFrame)(path, q, frame);
|
|
91
|
+
q = frame;
|
|
92
|
+
}
|
|
93
|
+
const { channel, name, port, cache } = outputs.get(path);
|
|
57
94
|
const value = cache.get(q);
|
|
58
95
|
if (value !== undefined)
|
|
59
96
|
return value;
|
|
60
|
-
|
|
97
|
+
let result;
|
|
98
|
+
if (port.live) {
|
|
99
|
+
if (q !== frame)
|
|
100
|
+
(0, types_js_1.fail)('off-time-live-channel', 'Live channel does not support off-time reads: ' + path, port.source);
|
|
101
|
+
result = readLivePort(options, channel, name, path, port.source);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
result = channel.table ? (0, channels_js_1.sample)(channel.table, port, q, fps) : expr(port.expression, q, port.source);
|
|
105
|
+
}
|
|
61
106
|
cache.set(q, result);
|
|
62
107
|
return result;
|
|
63
108
|
}
|
|
64
109
|
const channels = Object.create(null);
|
|
110
|
+
contexts.run('audio');
|
|
65
111
|
for (const path of plan.order) {
|
|
66
112
|
if (outputs.has(path))
|
|
67
113
|
channels[path] = read(path, frame);
|
|
68
114
|
}
|
|
115
|
+
contexts.run('geo');
|
|
69
116
|
for (const path of plan.order) {
|
|
70
117
|
const target = parameters.get(path);
|
|
71
118
|
if (!target)
|
|
@@ -147,6 +194,7 @@ function evaluate(scene, frame) {
|
|
|
147
194
|
path: (0, model_js_1.objectPath)(object), kind: object.kind, ...(object.parent && { parent: (0, model_js_1.objectPath)(object.parent) }),
|
|
148
195
|
localMatrix, worldMatrix, parameters: p, visible: authored.visible,
|
|
149
196
|
...(object.geometry && { geometry: object.geometry }),
|
|
197
|
+
...(object.geometrySource && { proceduralGeometry: contexts.read(object.geometrySource, frame) }),
|
|
150
198
|
...(Object.hasOwn(p, 'diffr') && { material: {
|
|
151
199
|
diffuse: [p.diffr, p.diffg, p.diffb, p.diffa], ambient: [p.ambr, p.ambg, p.ambb, p.amba],
|
|
152
200
|
specular: [p.specr, p.specg, p.specb, p.speca], ...(p.shine !== undefined && { shininess: p.shine }),
|
|
@@ -162,6 +210,7 @@ function evaluate(scene, frame) {
|
|
|
162
210
|
};
|
|
163
211
|
return snapshot;
|
|
164
212
|
});
|
|
165
|
-
|
|
213
|
+
const snapshot = (0, types_js_1.freeze)({ frame, time, fps, objects, channels, cameras: objects.filter(o => o.kind === 'camera'),
|
|
166
214
|
lights: objects.filter(o => o.kind === 'light') });
|
|
215
|
+
return contexts.finish(snapshot);
|
|
167
216
|
}
|
package/graph.d.ts
CHANGED
|
@@ -3,7 +3,10 @@ export declare function comparePaths(a: string, b: string): number;
|
|
|
3
3
|
export declare class GraphBuilder {
|
|
4
4
|
private steps;
|
|
5
5
|
add(path: string, type: string, portType: GraphPort['type'], props: GraphNode['props'], source?: SourceLocation): void;
|
|
6
|
+
addNode(path: string, node: Omit<GraphNode, 'name'>, source?: SourceLocation): void;
|
|
6
7
|
depend(to: string, on: string): void;
|
|
8
|
+
wire(to: string, input: string, from: string, output: string): void;
|
|
9
|
+
expose(path: string, port: GraphPort): void;
|
|
7
10
|
finish(): {
|
|
8
11
|
order: string[];
|
|
9
12
|
graph: EvaluationGraph;
|
package/graph.js
CHANGED
|
@@ -19,12 +19,25 @@ class GraphBuilder {
|
|
|
19
19
|
steps = new Map();
|
|
20
20
|
add(path, type, portType, props, source) {
|
|
21
21
|
const context = path.startsWith('/ch/') ? 'ch' : 'obj';
|
|
22
|
-
this.
|
|
23
|
-
|
|
22
|
+
this.addNode(path, { context, type, props, inputs: [], outputs: [{ name: 'value', type: portType }] }, source);
|
|
23
|
+
}
|
|
24
|
+
addNode(path, node, source) {
|
|
25
|
+
this.steps.set(path, { node: { ...node, name: graphName(path) }, dependencies: new Set(), wires: [], source });
|
|
24
26
|
}
|
|
25
27
|
depend(to, on) {
|
|
26
28
|
this.steps.get(to).dependencies.add(on);
|
|
27
29
|
}
|
|
30
|
+
wire(to, input, from, output) {
|
|
31
|
+
const step = this.steps.get(to);
|
|
32
|
+
step.dependencies.add(from);
|
|
33
|
+
step.wires.push({ path: from, output, input });
|
|
34
|
+
}
|
|
35
|
+
expose(path, port) {
|
|
36
|
+
const step = this.steps.get(path);
|
|
37
|
+
if (!step.node.outputs.some(output => output.name === port.name)) {
|
|
38
|
+
step.node = { ...step.node, outputs: [...step.node.outputs, port] };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
28
41
|
finish() {
|
|
29
42
|
const paths = [...this.steps.keys()].sort(comparePaths);
|
|
30
43
|
const counts = new Map();
|
|
@@ -73,14 +86,23 @@ class GraphBuilder {
|
|
|
73
86
|
}
|
|
74
87
|
const edges = [];
|
|
75
88
|
const nodes = paths.map(path => {
|
|
76
|
-
const { node, dependencies } = this.steps.get(path);
|
|
77
|
-
const
|
|
89
|
+
const { node, dependencies, wires } = this.steps.get(path);
|
|
90
|
+
const typedInputs = wires.sort((a, b) => comparePaths(a.input, b.input) || comparePaths(a.path, b.path) || comparePaths(a.output, b.output)).map(({ path: from, output, input }) => {
|
|
91
|
+
const source = this.steps.get(from).node;
|
|
92
|
+
edges.push({ src: { node: source.name, port: output }, dst: { node: node.name, port: input } });
|
|
93
|
+
return { name: input, type: source.outputs.find(port => port.name === output).type };
|
|
94
|
+
});
|
|
95
|
+
if (node.context !== 'obj' && node.context !== 'ch') {
|
|
96
|
+
return node;
|
|
97
|
+
}
|
|
98
|
+
const wired = new Set(wires.map(wire => wire.path));
|
|
99
|
+
const inputs = [...dependencies].filter(dep => !wired.has(dep)).sort(comparePaths).map(dep => {
|
|
78
100
|
const source = this.steps.get(dep).node;
|
|
79
101
|
const output = source.outputs[0];
|
|
80
102
|
edges.push({ src: { node: source.name, port: output.name }, dst: { node: node.name, port: source.name } });
|
|
81
103
|
return { name: source.name, type: output.type };
|
|
82
104
|
});
|
|
83
|
-
return { ...node, inputs };
|
|
105
|
+
return { ...node, inputs: [...inputs, ...typedInputs] };
|
|
84
106
|
});
|
|
85
107
|
return { order, graph: { name: 'scene', nodes, edges } };
|
|
86
108
|
}
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export declare const name = "@vim3d/core";
|
|
2
2
|
export { compileScene } from './compile.js';
|
|
3
3
|
export { evaluateFrame } from './evaluate.js';
|
|
4
|
+
export { BUILTIN_CONTEXTS, createContextRegistry } from './registry.js';
|
|
5
|
+
export type { ContextDefinition, ContextRegistry, ContextStage } from './registry.js';
|
|
4
6
|
export type { BinaryOperator, Expression, MathFunction, ReferenceFunction, UnaryOperator, Variable } from './expression.js';
|
|
5
|
-
export type { CameraSnapshot, ChannelSnapshot, CompiledScene, CompileOptions, Diagnostic, EvaluationGraph, FrameSnapshot, GeometrySnapshot, GraphEdge, GraphEndpoint, GraphNode, GraphPort, GraphProp, LightKind, LightSnapshot, MaterialSnapshot, Matrix4, ObjectKind, ObjectSnapshot, Result, SceneSource, SourceLocation, Vec3, } from './types.js';
|
|
7
|
+
export type { AudioFrameData, CameraSnapshot, ChannelSnapshot, CompiledScene, CompileOptions, ContextNodeSnapshot, ContextValue, ContextValues, Diagnostic, EvaluateFrameOptions, EvaluateOptions, EvaluationGraph, ExtensionContextDefinition, ExtensionContextStage, ExtensionNodeEvalContext, ExtensionNodeType, ExtensionPropDefinition, FrameSnapshot, GeometrySnapshot, GraphEdge, GraphEndpoint, GraphNode, GraphPort, GraphPortType, GraphProp, LightKind, LightSnapshot, LiveChannelInput, LiveChannelSample, MaterialSnapshot, Matrix4, ObjectKind, ObjectSnapshot, Result, SceneSource, SourceLocation, Vec3, } from './types.js';
|
package/index.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.evaluateFrame = exports.compileScene = exports.name = void 0;
|
|
3
|
+
exports.createContextRegistry = exports.BUILTIN_CONTEXTS = exports.evaluateFrame = exports.compileScene = exports.name = void 0;
|
|
4
4
|
exports.name = '@vim3d/core';
|
|
5
5
|
var compile_js_1 = require("./compile.js");
|
|
6
6
|
Object.defineProperty(exports, "compileScene", { enumerable: true, get: function () { return compile_js_1.compileScene; } });
|
|
7
7
|
var evaluate_js_1 = require("./evaluate.js");
|
|
8
8
|
Object.defineProperty(exports, "evaluateFrame", { enumerable: true, get: function () { return evaluate_js_1.evaluateFrame; } });
|
|
9
|
+
var registry_js_1 = require("./registry.js");
|
|
10
|
+
Object.defineProperty(exports, "BUILTIN_CONTEXTS", { enumerable: true, get: function () { return registry_js_1.BUILTIN_CONTEXTS; } });
|
|
11
|
+
Object.defineProperty(exports, "createContextRegistry", { enumerable: true, get: function () { return registry_js_1.createContextRegistry; } });
|
package/model.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Expression } from './expression.js';
|
|
2
|
+
import type { Extensions } from './contexts.js';
|
|
2
3
|
import { type CompiledScene, type GeometrySnapshot, type LightKind, type Matrix4, type ObjectKind, type SourceLocation, type Vec3 } from './types.js';
|
|
3
4
|
export interface Authored {
|
|
4
5
|
parameters: Record<string, number>;
|
|
@@ -16,6 +17,7 @@ export interface SceneObject {
|
|
|
16
17
|
source: SourceLocation;
|
|
17
18
|
parent?: SceneObject;
|
|
18
19
|
geometry?: GeometrySnapshot;
|
|
20
|
+
geometrySource?: string;
|
|
19
21
|
authored: Authored;
|
|
20
22
|
pathBindings: PathBinding[];
|
|
21
23
|
aimBindings: AimBinding[];
|
|
@@ -31,11 +33,13 @@ export interface Channel {
|
|
|
31
33
|
name: string;
|
|
32
34
|
source: SourceLocation;
|
|
33
35
|
table?: Table;
|
|
36
|
+
live?: boolean;
|
|
34
37
|
ports: Map<string, ChannelPort>;
|
|
35
38
|
}
|
|
36
39
|
export interface ChannelPort {
|
|
37
40
|
samples?: number[];
|
|
38
41
|
expression?: Expression;
|
|
42
|
+
live?: boolean;
|
|
39
43
|
source: SourceLocation;
|
|
40
44
|
}
|
|
41
45
|
export interface ActiveRange {
|
|
@@ -75,6 +79,8 @@ export interface Event {
|
|
|
75
79
|
edit: PropertyEdit;
|
|
76
80
|
}
|
|
77
81
|
export interface Plan {
|
|
82
|
+
extensions: Extensions;
|
|
83
|
+
audioChannels: ReadonlySet<string>;
|
|
78
84
|
objects: SceneObject[];
|
|
79
85
|
channels: Channel[];
|
|
80
86
|
drivers: Map<string, Driver[]>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vim3d/core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "A shared scene model for Vim3D.",
|
|
5
5
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/pyramation/vim3d/tree/master/js/packages/core",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"url": "https://github.com/pyramation/vim3d/issues"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@vim3d/parser": "^0.2.
|
|
24
|
+
"@vim3d/parser": "^0.2.1"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"makage": "^0.8.0"
|
package/registry.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type ExtensionContextDefinition, type ExtensionNodeType } from './types.js';
|
|
2
|
+
export type ContextStage = 'audio' | 'ch' | 'geo' | 'obj' | 'evaluate' | 'output' | 'reserved';
|
|
3
|
+
export interface ContextDefinition {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly stage: ContextStage;
|
|
6
|
+
readonly nodeTypes: readonly ExtensionNodeType[];
|
|
7
|
+
}
|
|
8
|
+
export declare const BUILTIN_CONTEXTS: readonly ContextDefinition[];
|
|
9
|
+
export interface ContextRegistry {
|
|
10
|
+
readonly contexts: readonly ContextDefinition[];
|
|
11
|
+
readonly get: (name: string) => ContextDefinition | undefined;
|
|
12
|
+
}
|
|
13
|
+
export declare function createContextRegistry(extensions?: readonly ExtensionContextDefinition[]): ContextRegistry;
|
|
14
|
+
export declare function allowedSource(target: ContextStage, source: ContextStage): boolean;
|
package/registry.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BUILTIN_CONTEXTS = void 0;
|
|
4
|
+
exports.createContextRegistry = createContextRegistry;
|
|
5
|
+
exports.allowedSource = allowedSource;
|
|
6
|
+
const types_js_1 = require("./types.js");
|
|
7
|
+
const ports = (type, ...names) => names.map(name => ({ name, type }));
|
|
8
|
+
const numeric = (defaults) => Object.entries(defaults)
|
|
9
|
+
.map(([name, value]) => ({ name, type: 'number', default: value }));
|
|
10
|
+
const audioSource = (type) => ({
|
|
11
|
+
type: 'audio:' + type,
|
|
12
|
+
inputs: [],
|
|
13
|
+
outputs: [...ports('object', 'signal'), ...ports('number', 'rms', 'peak')],
|
|
14
|
+
props: type === 'file' ? [{ name: 'asset', type: 'string', required: true }, ...numeric({ gain: 1 })]
|
|
15
|
+
: [{ name: 'device', type: 'string' }, ...numeric({ gain: 1 })],
|
|
16
|
+
evaluate: ({ path, props }, audio) => {
|
|
17
|
+
const gain = Math.abs(props.gain);
|
|
18
|
+
const rms = (0, types_js_1.finite)((audio?.rms ?? 0) * gain, path + '/rms');
|
|
19
|
+
const peak = (0, types_js_1.finite)((audio?.peak ?? 0) * gain, path + '/peak');
|
|
20
|
+
return { signal: { rms, peak }, rms, peak };
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
const audio = [
|
|
24
|
+
audioSource('file'), audioSource('mic'),
|
|
25
|
+
{
|
|
26
|
+
type: 'audio:fft',
|
|
27
|
+
inputs: ports('object', 'signal'),
|
|
28
|
+
outputs: [...ports('number', 'bass', 'mid', 'high', 'centroid', 'flux'), ...ports('object', 'spectrum')],
|
|
29
|
+
props: numeric({ fftSize: 2048, smoothing: 0.8 }),
|
|
30
|
+
evaluate: (_, data) => ({
|
|
31
|
+
bass: data?.bass ?? 0, mid: data?.mid ?? 0, high: data?.high ?? 0, centroid: data?.centroid ?? 0,
|
|
32
|
+
flux: data?.flux ?? 0, spectrum: data?.spectrum ?? [],
|
|
33
|
+
}),
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
type: 'audio:envelope',
|
|
37
|
+
inputs: ports('number', 'value'), outputs: ports('number', 'value'),
|
|
38
|
+
props: numeric({ attack: 0.01, release: 0.1 }),
|
|
39
|
+
evaluate: ({ inputs }, data) => ({ value: data?.rms ?? inputs.value ?? 0 }),
|
|
40
|
+
},
|
|
41
|
+
];
|
|
42
|
+
const geo = [
|
|
43
|
+
{
|
|
44
|
+
type: 'geo:box', inputs: [], outputs: ports('object', 'geometry'), props: numeric({ width: 1, height: 1, depth: 1 }),
|
|
45
|
+
evaluate: ({ props }) => ({ geometry: { primitive: 'box', args: [props.width, props.height, props.depth] } }),
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
type: 'geo:sphere', inputs: [], outputs: ports('object', 'geometry'), props: numeric({ radius: 1, segments: 32 }),
|
|
49
|
+
evaluate: ({ props }) => ({ geometry: { primitive: 'sphere', args: [props.radius, props.radius, props.segments] } }),
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
type: 'geo:transform',
|
|
53
|
+
inputs: [...ports('object', 'geometry'), ...ports('number', 'tx', 'ty', 'tz', 'sx', 'sy', 'sz')],
|
|
54
|
+
outputs: ports('object', 'geometry'), props: [],
|
|
55
|
+
evaluate: ({ inputs }) => ({ geometry: {
|
|
56
|
+
kind: 'transform', geometry: inputs.geometry ?? null,
|
|
57
|
+
translate: [inputs.tx ?? 0, inputs.ty ?? 0, inputs.tz ?? 0], scale: [inputs.sx ?? 1, inputs.sy ?? 1, inputs.sz ?? 1],
|
|
58
|
+
} }),
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
type: 'geo:merge', inputs: ports('object', 'a', 'b'), outputs: ports('object', 'geometry'), props: [],
|
|
62
|
+
evaluate: ({ inputs }) => ({ geometry: { kind: 'merge', geometries: [inputs.a, inputs.b].filter(value => value !== undefined) } }),
|
|
63
|
+
},
|
|
64
|
+
];
|
|
65
|
+
exports.BUILTIN_CONTEXTS = (0, types_js_1.freeze)([
|
|
66
|
+
{ name: 'audio', stage: 'audio', nodeTypes: audio },
|
|
67
|
+
{ name: 'ch', stage: 'ch', nodeTypes: [] },
|
|
68
|
+
{ name: 'geo', stage: 'geo', nodeTypes: geo },
|
|
69
|
+
{ name: 'obj', stage: 'obj', nodeTypes: [] },
|
|
70
|
+
{ name: 'sim', stage: 'reserved', nodeTypes: [] },
|
|
71
|
+
]);
|
|
72
|
+
function createContextRegistry(extensions = []) {
|
|
73
|
+
const contexts = new Map(exports.BUILTIN_CONTEXTS.map(context => [context.name, context]));
|
|
74
|
+
for (const context of extensions) {
|
|
75
|
+
if (typeof context.name !== 'string' || !/^[a-z][a-z0-9_-]*$/.test(context.name) ||
|
|
76
|
+
exports.BUILTIN_CONTEXTS.some(({ name }) => name === context.name)) {
|
|
77
|
+
(0, types_js_1.fail)('invalid-extension-context', 'Invalid or reserved extension context: ' + context.name);
|
|
78
|
+
}
|
|
79
|
+
if (contexts.has(context.name))
|
|
80
|
+
(0, types_js_1.fail)('duplicate-extension-context', 'Duplicate extension context: ' + context.name);
|
|
81
|
+
if ((context.stage !== 'evaluate' && context.stage !== 'output') || !Array.isArray(context.nodeTypes)) {
|
|
82
|
+
(0, types_js_1.fail)('invalid-extension-definition', 'Invalid stage or node types for /' + context.name);
|
|
83
|
+
}
|
|
84
|
+
contexts.set(context.name, Object.freeze({ ...context, nodeTypes: Object.freeze([...context.nodeTypes]) }));
|
|
85
|
+
}
|
|
86
|
+
return Object.freeze({ contexts: Object.freeze([...contexts.values()]), get: (name) => contexts.get(name) });
|
|
87
|
+
}
|
|
88
|
+
function allowedSource(target, source) {
|
|
89
|
+
switch (target) {
|
|
90
|
+
case 'audio': return source === 'audio';
|
|
91
|
+
case 'ch': return source === 'audio' || source === 'ch';
|
|
92
|
+
case 'geo': return source === 'audio' || source === 'ch' || source === 'geo';
|
|
93
|
+
case 'obj': return source === 'audio' || source === 'ch' || source === 'geo' || source === 'obj';
|
|
94
|
+
case 'evaluate':
|
|
95
|
+
case 'output': return source !== 'output' && source !== 'reserved';
|
|
96
|
+
case 'reserved': return false;
|
|
97
|
+
}
|
|
98
|
+
}
|
package/types.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ContextStage } from './registry.js';
|
|
1
2
|
export interface SourceLocation {
|
|
2
3
|
readonly file: string;
|
|
3
4
|
readonly line: number;
|
|
@@ -17,23 +18,31 @@ export interface SceneSource {
|
|
|
17
18
|
readonly path: string;
|
|
18
19
|
readonly text: string;
|
|
19
20
|
}
|
|
21
|
+
export interface LiveChannelSample {
|
|
22
|
+
readonly timestamp?: number;
|
|
23
|
+
readonly values: Readonly<Record<string, number>>;
|
|
24
|
+
}
|
|
25
|
+
export type LiveChannelInput = Readonly<Record<string, number>> | LiveChannelSample;
|
|
20
26
|
export interface CompileOptions {
|
|
21
27
|
readonly readFile?: (path: string) => string | Uint8Array | null;
|
|
22
28
|
readonly fps?: number;
|
|
23
29
|
readonly frameCount?: number;
|
|
30
|
+
readonly liveChannels?: Readonly<Record<string, readonly string[]>>;
|
|
31
|
+
readonly contexts?: readonly ExtensionContextDefinition[];
|
|
24
32
|
}
|
|
33
|
+
export type GraphPortType = 'number' | 'vector3' | 'matrix4' | 'boolean' | 'string' | 'object';
|
|
25
34
|
export interface GraphPort {
|
|
26
35
|
readonly name: string;
|
|
27
|
-
readonly type:
|
|
36
|
+
readonly type: GraphPortType;
|
|
28
37
|
}
|
|
29
38
|
export interface GraphProp {
|
|
30
39
|
readonly name: string;
|
|
31
|
-
readonly type: 'string' | 'number' | 'object';
|
|
40
|
+
readonly type: 'string' | 'number' | 'boolean' | 'object';
|
|
32
41
|
readonly value: unknown;
|
|
33
42
|
}
|
|
34
43
|
export interface GraphNode {
|
|
35
44
|
readonly name: string;
|
|
36
|
-
readonly context:
|
|
45
|
+
readonly context: string;
|
|
37
46
|
readonly type: string;
|
|
38
47
|
readonly inputs: readonly GraphPort[];
|
|
39
48
|
readonly outputs: readonly GraphPort[];
|
|
@@ -56,6 +65,7 @@ export interface CompiledScene {
|
|
|
56
65
|
readonly fps: number;
|
|
57
66
|
readonly frameCount?: number;
|
|
58
67
|
readonly graph: EvaluationGraph;
|
|
68
|
+
readonly liveChannels: readonly string[];
|
|
59
69
|
readonly view?: {
|
|
60
70
|
readonly camera?: string;
|
|
61
71
|
readonly backgroundColor?: Vec3;
|
|
@@ -101,6 +111,7 @@ export interface ObjectSnapshot {
|
|
|
101
111
|
readonly parameters: Readonly<Record<string, number>>;
|
|
102
112
|
readonly visible: boolean;
|
|
103
113
|
readonly geometry?: GeometrySnapshot;
|
|
114
|
+
readonly proceduralGeometry?: ContextValue;
|
|
104
115
|
readonly material?: MaterialSnapshot;
|
|
105
116
|
readonly camera?: CameraSnapshot;
|
|
106
117
|
readonly light?: LightSnapshot;
|
|
@@ -113,6 +124,64 @@ export interface FrameSnapshot {
|
|
|
113
124
|
readonly channels: ChannelSnapshot;
|
|
114
125
|
readonly cameras: readonly ObjectSnapshot[];
|
|
115
126
|
readonly lights: readonly ObjectSnapshot[];
|
|
127
|
+
readonly contexts?: Readonly<Record<string, readonly ContextNodeSnapshot[]>>;
|
|
128
|
+
}
|
|
129
|
+
export type ExtensionContextStage = 'evaluate' | 'output';
|
|
130
|
+
export type ContextValue = string | number | boolean | null | readonly ContextValue[] | {
|
|
131
|
+
readonly [key: string]: ContextValue;
|
|
132
|
+
};
|
|
133
|
+
export type ContextValues = Readonly<Record<string, ContextValue>>;
|
|
134
|
+
export interface ExtensionPropDefinition {
|
|
135
|
+
readonly name: string;
|
|
136
|
+
readonly type: GraphProp['type'];
|
|
137
|
+
readonly required?: boolean;
|
|
138
|
+
readonly default?: ContextValue;
|
|
139
|
+
}
|
|
140
|
+
export interface ExtensionNodeEvalContext {
|
|
141
|
+
readonly path: string;
|
|
142
|
+
readonly frame: number;
|
|
143
|
+
readonly time: number;
|
|
144
|
+
readonly fps: number;
|
|
145
|
+
readonly props: ContextValues;
|
|
146
|
+
readonly inputs: ContextValues;
|
|
147
|
+
readonly snapshot?: FrameSnapshot;
|
|
148
|
+
}
|
|
149
|
+
export interface AudioFrameData {
|
|
150
|
+
readonly rms?: number;
|
|
151
|
+
readonly peak?: number;
|
|
152
|
+
readonly bass?: number;
|
|
153
|
+
readonly mid?: number;
|
|
154
|
+
readonly high?: number;
|
|
155
|
+
readonly centroid?: number;
|
|
156
|
+
readonly flux?: number;
|
|
157
|
+
readonly spectrum?: readonly number[];
|
|
158
|
+
}
|
|
159
|
+
export interface EvaluateFrameOptions {
|
|
160
|
+
readonly audio?: Readonly<Record<string, AudioFrameData>>;
|
|
161
|
+
readonly liveChannels?: Readonly<Record<string, LiveChannelInput>>;
|
|
162
|
+
readonly deterministic?: boolean;
|
|
163
|
+
}
|
|
164
|
+
export type EvaluateOptions = EvaluateFrameOptions;
|
|
165
|
+
export interface ExtensionNodeType {
|
|
166
|
+
readonly type: string;
|
|
167
|
+
readonly inputs: readonly GraphPort[];
|
|
168
|
+
readonly outputs: readonly GraphPort[];
|
|
169
|
+
readonly props: readonly ExtensionPropDefinition[];
|
|
170
|
+
readonly evaluate: (ctx: ExtensionNodeEvalContext) => ContextValues | void;
|
|
171
|
+
}
|
|
172
|
+
export interface ExtensionContextDefinition {
|
|
173
|
+
readonly name: string;
|
|
174
|
+
readonly stage: ExtensionContextStage;
|
|
175
|
+
readonly nodeTypes: readonly ExtensionNodeType[];
|
|
176
|
+
}
|
|
177
|
+
export interface ContextNodeSnapshot {
|
|
178
|
+
readonly path: string;
|
|
179
|
+
readonly context: string;
|
|
180
|
+
readonly type: string;
|
|
181
|
+
readonly stage: ContextStage;
|
|
182
|
+
readonly props: ContextValues;
|
|
183
|
+
readonly inputs: ContextValues;
|
|
184
|
+
readonly outputs: ContextValues;
|
|
116
185
|
}
|
|
117
186
|
export declare class EvaluationError extends Error {
|
|
118
187
|
readonly diagnostic: Diagnostic;
|