@vim3d/core 0.2.2 → 0.2.4
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 +7 -0
- package/compile.d.ts +6 -1
- package/compile.js +61 -52
- package/contexts.d.ts +5 -1
- package/contexts.js +45 -0
- package/esm/compile.js +63 -55
- package/esm/contexts.js +42 -0
- package/esm/evaluate.js +8 -2
- package/esm/index.js +1 -0
- package/esm/model.js +2 -1
- package/esm/storage.js +575 -0
- package/evaluate.js +8 -2
- package/index.d.ts +2 -0
- package/index.js +6 -1
- package/model.d.ts +20 -4
- package/model.js +4 -2
- package/package.json +3 -2
- package/storage.d.ts +36 -0
- package/storage.js +581 -0
package/README.md
CHANGED
|
@@ -29,6 +29,13 @@ exposes scalar, position, and transform dependencies. Frame snapshots hold
|
|
|
29
29
|
evaluated scalar values and final local and world transforms; a renderer
|
|
30
30
|
does not run expressions or constraints again.
|
|
31
31
|
|
|
32
|
+
Use `serializeStorageGraph(toStorageGraph(scene.value))` after successful
|
|
33
|
+
compilation to store the scene in the [`@fbp/spec` format](../../../docs/SPEC-2.md#2-storing-the-graph).
|
|
34
|
+
Use `fromStorageGraph(JSON.parse(text))` to load it back into a compiled scene
|
|
35
|
+
without rereading source or assets. The saved graph routes cross-context
|
|
36
|
+
data through subnet boundary ports and excludes view settings.
|
|
37
|
+
`hashStorageGraph` computes the SHA-256 digest of the canonical storage JSON.
|
|
38
|
+
|
|
32
39
|
For a procedural channel, use `add channel` and drive its `value` port
|
|
33
40
|
with `expr`. For live browser sources (such as Web Audio), declare
|
|
34
41
|
`liveChannels` in `compileScene` options and supply per-frame samples in
|
package/compile.d.ts
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type Plan } from './model.js';
|
|
2
|
+
import { type CompiledScene, type CompileOptions, type Result, type SceneSource, type SourceLocation } from './types.js';
|
|
2
3
|
export declare function compileScene(source: SceneSource, options?: CompileOptions): Result<CompiledScene>;
|
|
4
|
+
export declare function finishScene(metadata: Pick<CompiledScene, 'fps' | 'frameCount' | 'ranges' | 'view'>, plan: Omit<Plan, 'order' | 'audioChannels'>, offTimeRefs?: readonly {
|
|
5
|
+
path: string;
|
|
6
|
+
source: SourceLocation;
|
|
7
|
+
}[]): CompiledScene;
|
package/compile.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.compileScene = compileScene;
|
|
4
|
+
exports.finishScene = finishScene;
|
|
4
5
|
const parser_1 = require("@vim3d/parser");
|
|
5
6
|
const channels_js_1 = require("./channels.js");
|
|
6
7
|
const ch_operators_js_1 = require("./ch-operators.js");
|
|
@@ -156,14 +157,12 @@ function compile(lines, root, fps, options) {
|
|
|
156
157
|
return node;
|
|
157
158
|
}
|
|
158
159
|
const getObject = (name, source) => lookup(name, 'obj', source);
|
|
159
|
-
const channelOutput = (path, source
|
|
160
|
+
const channelOutput = (path, source) => {
|
|
160
161
|
const parts = path.split('/');
|
|
161
162
|
if (parts[0] || parts[1] !== 'ch' || (parts.length !== 3 && parts.length !== 4)) {
|
|
162
163
|
(0, types_js_1.fail)('invalid-path', 'Expected a channel output path: ' + path, source);
|
|
163
164
|
}
|
|
164
165
|
const node = lookup(parts[2], 'ch', source);
|
|
165
|
-
if (ramp && !node.ramp)
|
|
166
|
-
(0, types_js_1.fail)('invalid-ramp', 'chramp requires a ch:ramp node: ' + path, source);
|
|
167
166
|
if (parts.length === 3 && (node.ports.size !== 1 || !node.ports.has('value'))) {
|
|
168
167
|
(0, types_js_1.fail)('ambiguous-channel', 'Channel shorthand requires a sole value port: ' + path, source);
|
|
169
168
|
}
|
|
@@ -342,7 +341,12 @@ function compile(lines, root, fps, options) {
|
|
|
342
341
|
(0, types_js_1.fail)('invalid-operands', 'Expected at FRAME COMMAND', source);
|
|
343
342
|
const [frameText, eventCommand, ...values] = args;
|
|
344
343
|
const frame = (0, model_js_1.readFrame)(frameText, source);
|
|
345
|
-
|
|
344
|
+
const inner = commandOf(eventCommand);
|
|
345
|
+
const last = values[values.length - 1];
|
|
346
|
+
if ((model_js_1.editCommands.has(inner) || (inner === 'select' && values.length > 1)) && (0, channels_js_1.boundaryMode)(last)) {
|
|
347
|
+
(0, types_js_1.fail)('invalid-operands', `at ${frameText}: '${last}' is a channel extend mode; timed property edits already persist until changed`, source);
|
|
348
|
+
}
|
|
349
|
+
if (inner === 'select') {
|
|
346
350
|
(0, model_js_1.checkArity)('select', values, [1], source);
|
|
347
351
|
const selection = lookup(values[0], 'either', source);
|
|
348
352
|
if (isChannel(selection)) {
|
|
@@ -373,7 +377,6 @@ function compile(lines, root, fps, options) {
|
|
|
373
377
|
const child = getObject(args[0], source);
|
|
374
378
|
child.parent = args[1] === '/obj' ? undefined : getObject(args[1], source);
|
|
375
379
|
}
|
|
376
|
-
const graph = new graph_js_1.GraphBuilder();
|
|
377
380
|
const ranges = Object.create(null);
|
|
378
381
|
const expressionPorts = new Map();
|
|
379
382
|
for (const channel of channels) {
|
|
@@ -393,28 +396,22 @@ function compile(lines, root, fps, options) {
|
|
|
393
396
|
if (name === 'chramp' && !address.startsWith('/ch/'))
|
|
394
397
|
(0, types_js_1.fail)('invalid-ramp', 'chramp requires a ch:ramp node: ' + address, source);
|
|
395
398
|
const targetStage = path.startsWith('/ch/') ? 'ch' : 'obj';
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
if (!(
|
|
399
|
-
(0, types_js_1.fail)('
|
|
400
|
-
}
|
|
401
|
-
if (from.stage !== 'audio' || name !== 'ch') {
|
|
402
|
-
(0, types_js_1.fail)('off-time-extension-reference', 'Only ch() can read built-in audio scalars: ' + address, source);
|
|
399
|
+
const reject = (code, message) => (0, types_js_1.fail)(code, message, source);
|
|
400
|
+
if (!isBuiltInPath(address)) {
|
|
401
|
+
if (!registry.get(address.split('/')[1])) {
|
|
402
|
+
(0, types_js_1.fail)('invalid-path', 'Expected an object parameter path: ' + address, source);
|
|
403
403
|
}
|
|
404
|
-
|
|
405
|
-
|
|
404
|
+
const from = extensionSource(address, source);
|
|
405
|
+
(0, contexts_js_1.validateExpressionSource)(targetStage, from, name, false, reject);
|
|
406
406
|
if (!contextWires.some(wire => wire.to === path && wire.from.address === from.address)) {
|
|
407
407
|
contextWires.push({ to: path, input: from.address, from });
|
|
408
408
|
}
|
|
409
409
|
return from.address;
|
|
410
410
|
}
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
(0, types_js_1.fail)('off-time-object-reference', name + ' requires a channel: ' + address, source);
|
|
416
|
-
}
|
|
417
|
-
const input = address.startsWith('/ch/') ? channelOutput(address, source, name === 'chramp') : objectScalar(address, source);
|
|
411
|
+
const stage = address.startsWith('/ch/') ? 'ch' : 'obj';
|
|
412
|
+
const channel = stage === 'ch' && channelOutput(address, source);
|
|
413
|
+
(0, contexts_js_1.validateExpressionSource)(targetStage, { stage, address, type: 'number' }, name, !!chNames.get(address.split('/')[2])?.ramp, reject);
|
|
414
|
+
const input = channel || objectScalar(address, source);
|
|
418
415
|
if (name === 'chf' || name === 'cht')
|
|
419
416
|
offTimeRefs.push({ path: input, source });
|
|
420
417
|
depend(path, input);
|
|
@@ -503,15 +500,14 @@ function compile(lines, root, fps, options) {
|
|
|
503
500
|
(0, model_js_1.checkArity)(command, args, [2], source);
|
|
504
501
|
const to = extensionAddress(args[1], source);
|
|
505
502
|
const from = extensionSource(extensionAddress(args[0], source), source);
|
|
503
|
+
const reject = (code, message) => (0, types_js_1.fail)(code, message, source);
|
|
506
504
|
if (isBuiltInPath(to)) {
|
|
507
505
|
if (from.stage === 'audio' || from.stage === 'ch') {
|
|
508
506
|
if (from.type !== 'number')
|
|
509
507
|
(0, types_js_1.fail)('extension-port-type', 'Expected a numeric output at ' + from.address, source);
|
|
510
508
|
const path = to.startsWith('/ch/') ? channelInput(to, source) : objectScalar(to, source);
|
|
511
509
|
const operator = expressionPorts.get(path)?.node.operator;
|
|
512
|
-
|
|
513
|
-
(0, types_js_1.fail)('extension-stage', 'Expected a channel operator input: ' + to, source);
|
|
514
|
-
}
|
|
510
|
+
(0, contexts_js_1.validateScalarConnectionSource)(to, !!operator, from, reject);
|
|
515
511
|
driveScalar(path, { kind: 'reference', name: 'ch', path: from.address }, source);
|
|
516
512
|
if (from.stage === 'ch')
|
|
517
513
|
depend(path, from.path);
|
|
@@ -525,8 +521,7 @@ function compile(lines, root, fps, options) {
|
|
|
525
521
|
const object = getObject(name, source);
|
|
526
522
|
if (object.kind !== 'geo')
|
|
527
523
|
(0, types_js_1.fail)('invalid-subject', 'A geometry connection requires a geometry object', source);
|
|
528
|
-
|
|
529
|
-
(0, types_js_1.fail)('extension-port-type', 'Expected object geometry at ' + to, source);
|
|
524
|
+
(0, contexts_js_1.validateGeometrySource)(to, from, reject);
|
|
530
525
|
if (object.geometry || object.geometrySource)
|
|
531
526
|
(0, types_js_1.fail)('overlapping-drivers', 'Geometry already assigned: ' + to, source);
|
|
532
527
|
object.geometrySource = from.address;
|
|
@@ -546,14 +541,10 @@ function compile(lines, root, fps, options) {
|
|
|
546
541
|
const owner = address.slice(0, address.lastIndexOf('/'));
|
|
547
542
|
const expression = (0, expression_js_1.bindExpression)((0, expression_js_1.parseExpression)(text, source), (ref, name) => {
|
|
548
543
|
const address = normalizePath(ref, owner, source);
|
|
549
|
-
if (name === 'chramp')
|
|
550
|
-
channelOutput(address, source
|
|
544
|
+
if (name === 'chramp' && !address.startsWith('/ch/'))
|
|
545
|
+
channelOutput(address, source);
|
|
551
546
|
const from = extensionSource(address, source);
|
|
552
|
-
|
|
553
|
-
(0, types_js_1.fail)('off-time-extension-reference', name + ' requires a channel: ' + from.address, source);
|
|
554
|
-
}
|
|
555
|
-
if (from.type !== 'number')
|
|
556
|
-
(0, types_js_1.fail)('extension-port-type', 'Expected a number at ' + from.address, source);
|
|
547
|
+
(0, contexts_js_1.validateExpressionSource)('extension', { ...from, address: name === 'chramp' ? address : from.address }, name, !!chNames.get(address.split('/')[2])?.ramp, (code, message) => (0, types_js_1.fail)(code, message, source));
|
|
557
548
|
if (name === 'chf' || name === 'cht')
|
|
558
549
|
offTimeRefs.push({ path: from.address, source });
|
|
559
550
|
references.set(from.address, from);
|
|
@@ -591,10 +582,13 @@ function compile(lines, root, fps, options) {
|
|
|
591
582
|
const target = getObject(args[1], source);
|
|
592
583
|
if (!target.geometry?.vertices?.length)
|
|
593
584
|
(0, types_js_1.fail)('invalid-path-curve', 'Path requires a nonempty curve', source);
|
|
585
|
+
const pos = (0, model_js_1.positionStep)(object);
|
|
594
586
|
addBinding(object.pathBindings, {
|
|
595
587
|
curve: target, offset: [numberAt(2, 0), numberAt(3, 0), numberAt(4, 0)],
|
|
596
|
-
|
|
597
|
-
|
|
588
|
+
...(args[5] === undefined || (0, model_js_1.isNumberLiteral)(args[5]) ? { freq: numberAt(5, 1) }
|
|
589
|
+
: { position: bind(args[5], pos, source) }),
|
|
590
|
+
shift: numberAt(6, 0), ...range(args, 7, source),
|
|
591
|
+
}, pos);
|
|
598
592
|
}
|
|
599
593
|
else {
|
|
600
594
|
(0, model_js_1.checkArity)(command, args, [2, 4], source);
|
|
@@ -641,7 +635,7 @@ function compile(lines, root, fps, options) {
|
|
|
641
635
|
const edit = (0, model_js_1.propertyEdit)(command, args, selected, source);
|
|
642
636
|
edit(state.get(selected));
|
|
643
637
|
validateState(selected, state.get(selected), source);
|
|
644
|
-
events.push({ frame, object: selected, edit });
|
|
638
|
+
events.push({ frame, object: selected, command, args, source, edit });
|
|
645
639
|
if (selected.kind === 'light' && command === 'light') {
|
|
646
640
|
if (!lightChanges.has(selected))
|
|
647
641
|
lightChanges.set(selected, new Map());
|
|
@@ -664,6 +658,24 @@ function compile(lines, root, fps, options) {
|
|
|
664
658
|
}
|
|
665
659
|
}
|
|
666
660
|
}
|
|
661
|
+
return finishScene({
|
|
662
|
+
fps, frameCount: options.frameCount, ranges,
|
|
663
|
+
...((viewCamera || backgroundColor || display) && { view: {
|
|
664
|
+
...(viewCamera && { camera: (0, model_js_1.objectPath)(viewCamera) }),
|
|
665
|
+
...(backgroundColor && { backgroundColor }),
|
|
666
|
+
...(display && { display }),
|
|
667
|
+
} }),
|
|
668
|
+
}, { objects, channels, drivers, events, extensions, inputs, contextWires }, offTimeRefs);
|
|
669
|
+
}
|
|
670
|
+
function finishScene(metadata, plan, offTimeRefs = []) {
|
|
671
|
+
const { fps, frameCount, ranges, view } = metadata;
|
|
672
|
+
const { objects, channels, drivers, events, extensions, inputs, contextWires } = plan;
|
|
673
|
+
const graph = new graph_js_1.GraphBuilder();
|
|
674
|
+
const expressionPorts = new Map();
|
|
675
|
+
for (const channel of channels) {
|
|
676
|
+
for (const [name, port] of channel.ports)
|
|
677
|
+
expressionPorts.set((0, model_js_1.channelPath)(channel) + '/' + name, { node: channel, name, port });
|
|
678
|
+
}
|
|
667
679
|
for (const [path, { node, name, port }] of expressionPorts) {
|
|
668
680
|
const extend = node.extend ? [prop('extend', node.extend)] : [];
|
|
669
681
|
if (node.ramp) {
|
|
@@ -700,24 +712,25 @@ function compile(lines, root, fps, options) {
|
|
|
700
712
|
}
|
|
701
713
|
graph.add(pos, 'transform:position', 'vector3', [prop('target', path),
|
|
702
714
|
...object.pathBindings.map(binding => prop('path', { curve: (0, model_js_1.objectPath)(binding.curve),
|
|
703
|
-
|
|
715
|
+
...(binding.position ? { position: binding.position } : { freq: binding.freq }),
|
|
716
|
+
shift: binding.shift, offset: binding.offset, start: binding.start,
|
|
704
717
|
end: Number.isFinite(binding.end) ? binding.end : null }))], object.pathBindings[0]?.source ?? object.source);
|
|
705
718
|
graph.add(world, 'transform:world', 'matrix4', [prop('target', path),
|
|
706
719
|
...(object.parent ? [prop('parent', (0, model_js_1.objectPath)(object.parent))] : []),
|
|
707
720
|
...object.aimBindings.map(binding => prop('lookat', { target: (0, model_js_1.objectPath)(binding.target), start: binding.start,
|
|
708
721
|
end: Number.isFinite(binding.end) ? binding.end : null }))], object.aimBindings[0]?.source ?? object.source);
|
|
709
|
-
model_js_1.translations.forEach(param => depend(pos, path + '/' + param));
|
|
710
|
-
[...model_js_1.rotations, ...model_js_1.scales].forEach(param => depend(world, path + '/' + param));
|
|
711
|
-
depend(world, pos);
|
|
722
|
+
model_js_1.translations.forEach(param => graph.depend(pos, path + '/' + param));
|
|
723
|
+
[...model_js_1.rotations, ...model_js_1.scales].forEach(param => graph.depend(world, path + '/' + param));
|
|
724
|
+
graph.depend(world, pos);
|
|
712
725
|
if (object.parent) {
|
|
713
|
-
depend(world, (0, model_js_1.worldStep)(object.parent));
|
|
726
|
+
graph.depend(world, (0, model_js_1.worldStep)(object.parent));
|
|
714
727
|
if (!coversTimeline(object.pathBindings))
|
|
715
|
-
depend(pos, (0, model_js_1.worldStep)(object.parent));
|
|
728
|
+
graph.depend(pos, (0, model_js_1.worldStep)(object.parent));
|
|
716
729
|
}
|
|
717
730
|
for (const binding of object.pathBindings)
|
|
718
|
-
depend(pos, (0, model_js_1.worldStep)(binding.curve));
|
|
731
|
+
graph.depend(pos, (0, model_js_1.worldStep)(binding.curve));
|
|
719
732
|
for (const binding of object.aimBindings)
|
|
720
|
-
depend(world, (0, model_js_1.positionStep)(binding.target));
|
|
733
|
+
graph.depend(world, (0, model_js_1.positionStep)(binding.target));
|
|
721
734
|
}
|
|
722
735
|
for (const [path, dependencies] of inputs)
|
|
723
736
|
for (const input of dependencies)
|
|
@@ -756,19 +769,15 @@ function compile(lines, root, fps, options) {
|
|
|
756
769
|
}
|
|
757
770
|
const scene = (0, types_js_1.freeze)({
|
|
758
771
|
fps,
|
|
759
|
-
...(
|
|
772
|
+
...(frameCount !== undefined && { frameCount }),
|
|
760
773
|
graph: finished.graph,
|
|
761
|
-
...(Object.keys(ranges).length && { ranges }),
|
|
774
|
+
...(ranges && Object.keys(ranges).length && { ranges }),
|
|
762
775
|
liveChannels,
|
|
763
|
-
...(
|
|
764
|
-
...(viewCamera && { camera: (0, model_js_1.objectPath)(viewCamera) }),
|
|
765
|
-
...(backgroundColor && { backgroundColor }),
|
|
766
|
-
...(display && { display }),
|
|
767
|
-
} }),
|
|
776
|
+
...(view && { view }),
|
|
768
777
|
});
|
|
769
778
|
objects.sort((a, b) => (0, graph_js_1.comparePaths)((0, model_js_1.objectPath)(a), (0, model_js_1.objectPath)(b)));
|
|
770
779
|
channels.sort((a, b) => (0, graph_js_1.comparePaths)((0, model_js_1.channelPath)(a), (0, model_js_1.channelPath)(b)));
|
|
771
|
-
model_js_1.plans.set(scene, {
|
|
780
|
+
model_js_1.plans.set(scene, { ...plan, order: finished.order, audioChannels });
|
|
772
781
|
return scene;
|
|
773
782
|
}
|
|
774
783
|
function rename(names, node, name, source) {
|
package/contexts.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type GraphBuilder } from './graph.js';
|
|
2
|
-
import { type Expression } from './expression.js';
|
|
2
|
+
import { type Expression, type ReferenceFunction } from './expression.js';
|
|
3
3
|
import { type ContextRegistry, type ContextStage } from './registry.js';
|
|
4
4
|
import { type AudioFrameData, type ContextValue, type ContextValues, type EvaluateFrameOptions, type ExtensionNodeType, type ExtensionPropDefinition, type FrameSnapshot, type GraphPort, type ParameterRange, type SourceLocation } from './types.js';
|
|
5
5
|
export declare function jsonProp(text: string, invalid: () => never): unknown;
|
|
@@ -17,6 +17,10 @@ export interface ExtensionSource {
|
|
|
17
17
|
type: GraphPort['type'];
|
|
18
18
|
stage: ContextStage;
|
|
19
19
|
}
|
|
20
|
+
type RejectSource = (code: string, message: string) => never;
|
|
21
|
+
export declare function validateExpressionSource(target: 'ch' | 'obj' | 'extension', from: Pick<ExtensionSource, 'address' | 'stage' | 'type'>, name: ReferenceFunction, ramp: boolean, reject: RejectSource): void;
|
|
22
|
+
export declare function validateGeometrySource(target: string, from: ExtensionSource, reject: RejectSource): void;
|
|
23
|
+
export declare function validateScalarConnectionSource(target: string, operator: boolean, from: ExtensionSource, reject: RejectSource): void;
|
|
20
24
|
type InputDriver = {
|
|
21
25
|
kind: 'wire';
|
|
22
26
|
from: ExtensionSource;
|
package/contexts.js
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Extensions = void 0;
|
|
4
4
|
exports.jsonProp = jsonProp;
|
|
5
|
+
exports.validateExpressionSource = validateExpressionSource;
|
|
6
|
+
exports.validateGeometrySource = validateGeometrySource;
|
|
7
|
+
exports.validateScalarConnectionSource = validateScalarConnectionSource;
|
|
5
8
|
exports.checkAudioFrame = checkAudioFrame;
|
|
6
9
|
exports.createContextEvaluation = createContextEvaluation;
|
|
7
10
|
const channels_js_1 = require("./channels.js");
|
|
@@ -64,6 +67,48 @@ function definitions(entries, types, kind, owner) {
|
|
|
64
67
|
}
|
|
65
68
|
return map;
|
|
66
69
|
}
|
|
70
|
+
function validateExpressionSource(target, from, name, ramp, reject) {
|
|
71
|
+
const { stage, address, type } = from;
|
|
72
|
+
if (name === 'chramp' && (stage !== 'ch' || !ramp)) {
|
|
73
|
+
reject('invalid-ramp', 'chramp requires a ch:ramp node: ' + address);
|
|
74
|
+
}
|
|
75
|
+
if (target === 'ch' || target === 'obj') {
|
|
76
|
+
if (stage === 'obj') {
|
|
77
|
+
if (target === 'ch')
|
|
78
|
+
reject('channel-object-reference', 'Channel expressions cannot read object parameters: ' + address);
|
|
79
|
+
if (name !== 'ch')
|
|
80
|
+
reject('off-time-object-reference', name + ' requires a channel: ' + address);
|
|
81
|
+
}
|
|
82
|
+
else if (stage !== 'ch') {
|
|
83
|
+
if (!(0, registry_js_1.allowedSource)(target, stage))
|
|
84
|
+
reject('extension-stage', 'Built-in nodes cannot read ' + address);
|
|
85
|
+
if (stage !== 'audio' || name !== 'ch') {
|
|
86
|
+
reject('off-time-extension-reference', 'Only ch() can read built-in audio scalars: ' + address);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else if (name !== 'ch' && stage !== 'ch') {
|
|
91
|
+
reject('off-time-extension-reference', name + ' requires a channel: ' + address);
|
|
92
|
+
}
|
|
93
|
+
if (type !== 'number')
|
|
94
|
+
reject('extension-port-type', 'Expected a number at ' + address);
|
|
95
|
+
}
|
|
96
|
+
function validateGeometrySource(target, from, reject) {
|
|
97
|
+
if (from.stage !== 'geo')
|
|
98
|
+
reject('extension-stage', 'connect cannot drive this built-in input: ' + target);
|
|
99
|
+
if (from.type !== 'object')
|
|
100
|
+
reject('extension-port-type', 'Expected object geometry at ' + target);
|
|
101
|
+
}
|
|
102
|
+
function validateScalarConnectionSource(target, operator, from, reject) {
|
|
103
|
+
if (from.stage !== 'audio' && from.stage !== 'ch') {
|
|
104
|
+
reject('extension-stage', 'connect cannot drive this built-in input: ' + target);
|
|
105
|
+
}
|
|
106
|
+
if (from.type !== 'number')
|
|
107
|
+
reject('extension-port-type', 'Expected a numeric output at ' + from.address);
|
|
108
|
+
if (from.stage === 'ch' && target.startsWith('/ch/') && !operator) {
|
|
109
|
+
reject('extension-stage', 'Expected a channel operator input: ' + target);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
67
112
|
class Extensions {
|
|
68
113
|
registry;
|
|
69
114
|
types = new Map();
|
package/esm/compile.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { expand } from '@vim3d/parser';
|
|
2
2
|
import { boundaryMode, cyclic, loadTable, validSegment } from './channels.js';
|
|
3
3
|
import { channelOperators } from './ch-operators.js';
|
|
4
|
-
import { Extensions, jsonProp } from './contexts.js';
|
|
4
|
+
import { Extensions, jsonProp, validateExpressionSource, validateGeometrySource, validateScalarConnectionSource, } from './contexts.js';
|
|
5
5
|
import { bindExpression, parseExpression } from './expression.js';
|
|
6
6
|
import { comparePaths, GraphBuilder } from './graph.js';
|
|
7
|
-
import {
|
|
7
|
+
import { createContextRegistry } from './registry.js';
|
|
8
8
|
import { rampStops } from './ramp.js';
|
|
9
|
-
import { channelPath, checkArity, copyState, defaults, editCommands, objectPath, plans, positionStep, propertyEdit, readFrame, readNumber, rotations, scales, translations, worldStep } from './model.js';
|
|
9
|
+
import { channelPath, checkArity, copyState, defaults, editCommands, isNumberLiteral, objectPath, plans, positionStep, propertyEdit, readFrame, readNumber, rotations, scales, translations, worldStep } from './model.js';
|
|
10
10
|
import { EvaluationError, fail, freeze } from './types.js';
|
|
11
11
|
const aliases = {
|
|
12
12
|
s: 'select', m: 'translate', mv: 'translate', cam: 'camera', l: 'light',
|
|
@@ -153,14 +153,12 @@ function compile(lines, root, fps, options) {
|
|
|
153
153
|
return node;
|
|
154
154
|
}
|
|
155
155
|
const getObject = (name, source) => lookup(name, 'obj', source);
|
|
156
|
-
const channelOutput = (path, source
|
|
156
|
+
const channelOutput = (path, source) => {
|
|
157
157
|
const parts = path.split('/');
|
|
158
158
|
if (parts[0] || parts[1] !== 'ch' || (parts.length !== 3 && parts.length !== 4)) {
|
|
159
159
|
fail('invalid-path', 'Expected a channel output path: ' + path, source);
|
|
160
160
|
}
|
|
161
161
|
const node = lookup(parts[2], 'ch', source);
|
|
162
|
-
if (ramp && !node.ramp)
|
|
163
|
-
fail('invalid-ramp', 'chramp requires a ch:ramp node: ' + path, source);
|
|
164
162
|
if (parts.length === 3 && (node.ports.size !== 1 || !node.ports.has('value'))) {
|
|
165
163
|
fail('ambiguous-channel', 'Channel shorthand requires a sole value port: ' + path, source);
|
|
166
164
|
}
|
|
@@ -339,7 +337,12 @@ function compile(lines, root, fps, options) {
|
|
|
339
337
|
fail('invalid-operands', 'Expected at FRAME COMMAND', source);
|
|
340
338
|
const [frameText, eventCommand, ...values] = args;
|
|
341
339
|
const frame = readFrame(frameText, source);
|
|
342
|
-
|
|
340
|
+
const inner = commandOf(eventCommand);
|
|
341
|
+
const last = values[values.length - 1];
|
|
342
|
+
if ((editCommands.has(inner) || (inner === 'select' && values.length > 1)) && boundaryMode(last)) {
|
|
343
|
+
fail('invalid-operands', `at ${frameText}: '${last}' is a channel extend mode; timed property edits already persist until changed`, source);
|
|
344
|
+
}
|
|
345
|
+
if (inner === 'select') {
|
|
343
346
|
checkArity('select', values, [1], source);
|
|
344
347
|
const selection = lookup(values[0], 'either', source);
|
|
345
348
|
if (isChannel(selection)) {
|
|
@@ -370,7 +373,6 @@ function compile(lines, root, fps, options) {
|
|
|
370
373
|
const child = getObject(args[0], source);
|
|
371
374
|
child.parent = args[1] === '/obj' ? undefined : getObject(args[1], source);
|
|
372
375
|
}
|
|
373
|
-
const graph = new GraphBuilder();
|
|
374
376
|
const ranges = Object.create(null);
|
|
375
377
|
const expressionPorts = new Map();
|
|
376
378
|
for (const channel of channels) {
|
|
@@ -390,28 +392,22 @@ function compile(lines, root, fps, options) {
|
|
|
390
392
|
if (name === 'chramp' && !address.startsWith('/ch/'))
|
|
391
393
|
fail('invalid-ramp', 'chramp requires a ch:ramp node: ' + address, source);
|
|
392
394
|
const targetStage = path.startsWith('/ch/') ? 'ch' : 'obj';
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
if (!
|
|
396
|
-
fail('
|
|
397
|
-
}
|
|
398
|
-
if (from.stage !== 'audio' || name !== 'ch') {
|
|
399
|
-
fail('off-time-extension-reference', 'Only ch() can read built-in audio scalars: ' + address, source);
|
|
395
|
+
const reject = (code, message) => fail(code, message, source);
|
|
396
|
+
if (!isBuiltInPath(address)) {
|
|
397
|
+
if (!registry.get(address.split('/')[1])) {
|
|
398
|
+
fail('invalid-path', 'Expected an object parameter path: ' + address, source);
|
|
400
399
|
}
|
|
401
|
-
|
|
402
|
-
|
|
400
|
+
const from = extensionSource(address, source);
|
|
401
|
+
validateExpressionSource(targetStage, from, name, false, reject);
|
|
403
402
|
if (!contextWires.some(wire => wire.to === path && wire.from.address === from.address)) {
|
|
404
403
|
contextWires.push({ to: path, input: from.address, from });
|
|
405
404
|
}
|
|
406
405
|
return from.address;
|
|
407
406
|
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
fail('off-time-object-reference', name + ' requires a channel: ' + address, source);
|
|
413
|
-
}
|
|
414
|
-
const input = address.startsWith('/ch/') ? channelOutput(address, source, name === 'chramp') : objectScalar(address, source);
|
|
407
|
+
const stage = address.startsWith('/ch/') ? 'ch' : 'obj';
|
|
408
|
+
const channel = stage === 'ch' && channelOutput(address, source);
|
|
409
|
+
validateExpressionSource(targetStage, { stage, address, type: 'number' }, name, !!chNames.get(address.split('/')[2])?.ramp, reject);
|
|
410
|
+
const input = channel || objectScalar(address, source);
|
|
415
411
|
if (name === 'chf' || name === 'cht')
|
|
416
412
|
offTimeRefs.push({ path: input, source });
|
|
417
413
|
depend(path, input);
|
|
@@ -500,15 +496,14 @@ function compile(lines, root, fps, options) {
|
|
|
500
496
|
checkArity(command, args, [2], source);
|
|
501
497
|
const to = extensionAddress(args[1], source);
|
|
502
498
|
const from = extensionSource(extensionAddress(args[0], source), source);
|
|
499
|
+
const reject = (code, message) => fail(code, message, source);
|
|
503
500
|
if (isBuiltInPath(to)) {
|
|
504
501
|
if (from.stage === 'audio' || from.stage === 'ch') {
|
|
505
502
|
if (from.type !== 'number')
|
|
506
503
|
fail('extension-port-type', 'Expected a numeric output at ' + from.address, source);
|
|
507
504
|
const path = to.startsWith('/ch/') ? channelInput(to, source) : objectScalar(to, source);
|
|
508
505
|
const operator = expressionPorts.get(path)?.node.operator;
|
|
509
|
-
|
|
510
|
-
fail('extension-stage', 'Expected a channel operator input: ' + to, source);
|
|
511
|
-
}
|
|
506
|
+
validateScalarConnectionSource(to, !!operator, from, reject);
|
|
512
507
|
driveScalar(path, { kind: 'reference', name: 'ch', path: from.address }, source);
|
|
513
508
|
if (from.stage === 'ch')
|
|
514
509
|
depend(path, from.path);
|
|
@@ -522,8 +517,7 @@ function compile(lines, root, fps, options) {
|
|
|
522
517
|
const object = getObject(name, source);
|
|
523
518
|
if (object.kind !== 'geo')
|
|
524
519
|
fail('invalid-subject', 'A geometry connection requires a geometry object', source);
|
|
525
|
-
|
|
526
|
-
fail('extension-port-type', 'Expected object geometry at ' + to, source);
|
|
520
|
+
validateGeometrySource(to, from, reject);
|
|
527
521
|
if (object.geometry || object.geometrySource)
|
|
528
522
|
fail('overlapping-drivers', 'Geometry already assigned: ' + to, source);
|
|
529
523
|
object.geometrySource = from.address;
|
|
@@ -543,14 +537,10 @@ function compile(lines, root, fps, options) {
|
|
|
543
537
|
const owner = address.slice(0, address.lastIndexOf('/'));
|
|
544
538
|
const expression = bindExpression(parseExpression(text, source), (ref, name) => {
|
|
545
539
|
const address = normalizePath(ref, owner, source);
|
|
546
|
-
if (name === 'chramp')
|
|
547
|
-
channelOutput(address, source
|
|
540
|
+
if (name === 'chramp' && !address.startsWith('/ch/'))
|
|
541
|
+
channelOutput(address, source);
|
|
548
542
|
const from = extensionSource(address, source);
|
|
549
|
-
|
|
550
|
-
fail('off-time-extension-reference', name + ' requires a channel: ' + from.address, source);
|
|
551
|
-
}
|
|
552
|
-
if (from.type !== 'number')
|
|
553
|
-
fail('extension-port-type', 'Expected a number at ' + from.address, source);
|
|
543
|
+
validateExpressionSource('extension', { ...from, address: name === 'chramp' ? address : from.address }, name, !!chNames.get(address.split('/')[2])?.ramp, (code, message) => fail(code, message, source));
|
|
554
544
|
if (name === 'chf' || name === 'cht')
|
|
555
545
|
offTimeRefs.push({ path: from.address, source });
|
|
556
546
|
references.set(from.address, from);
|
|
@@ -588,10 +578,13 @@ function compile(lines, root, fps, options) {
|
|
|
588
578
|
const target = getObject(args[1], source);
|
|
589
579
|
if (!target.geometry?.vertices?.length)
|
|
590
580
|
fail('invalid-path-curve', 'Path requires a nonempty curve', source);
|
|
581
|
+
const pos = positionStep(object);
|
|
591
582
|
addBinding(object.pathBindings, {
|
|
592
583
|
curve: target, offset: [numberAt(2, 0), numberAt(3, 0), numberAt(4, 0)],
|
|
593
|
-
|
|
594
|
-
|
|
584
|
+
...(args[5] === undefined || isNumberLiteral(args[5]) ? { freq: numberAt(5, 1) }
|
|
585
|
+
: { position: bind(args[5], pos, source) }),
|
|
586
|
+
shift: numberAt(6, 0), ...range(args, 7, source),
|
|
587
|
+
}, pos);
|
|
595
588
|
}
|
|
596
589
|
else {
|
|
597
590
|
checkArity(command, args, [2, 4], source);
|
|
@@ -638,7 +631,7 @@ function compile(lines, root, fps, options) {
|
|
|
638
631
|
const edit = propertyEdit(command, args, selected, source);
|
|
639
632
|
edit(state.get(selected));
|
|
640
633
|
validateState(selected, state.get(selected), source);
|
|
641
|
-
events.push({ frame, object: selected, edit });
|
|
634
|
+
events.push({ frame, object: selected, command, args, source, edit });
|
|
642
635
|
if (selected.kind === 'light' && command === 'light') {
|
|
643
636
|
if (!lightChanges.has(selected))
|
|
644
637
|
lightChanges.set(selected, new Map());
|
|
@@ -661,6 +654,24 @@ function compile(lines, root, fps, options) {
|
|
|
661
654
|
}
|
|
662
655
|
}
|
|
663
656
|
}
|
|
657
|
+
return finishScene({
|
|
658
|
+
fps, frameCount: options.frameCount, ranges,
|
|
659
|
+
...((viewCamera || backgroundColor || display) && { view: {
|
|
660
|
+
...(viewCamera && { camera: objectPath(viewCamera) }),
|
|
661
|
+
...(backgroundColor && { backgroundColor }),
|
|
662
|
+
...(display && { display }),
|
|
663
|
+
} }),
|
|
664
|
+
}, { objects, channels, drivers, events, extensions, inputs, contextWires }, offTimeRefs);
|
|
665
|
+
}
|
|
666
|
+
export function finishScene(metadata, plan, offTimeRefs = []) {
|
|
667
|
+
const { fps, frameCount, ranges, view } = metadata;
|
|
668
|
+
const { objects, channels, drivers, events, extensions, inputs, contextWires } = plan;
|
|
669
|
+
const graph = new GraphBuilder();
|
|
670
|
+
const expressionPorts = new Map();
|
|
671
|
+
for (const channel of channels) {
|
|
672
|
+
for (const [name, port] of channel.ports)
|
|
673
|
+
expressionPorts.set(channelPath(channel) + '/' + name, { node: channel, name, port });
|
|
674
|
+
}
|
|
664
675
|
for (const [path, { node, name, port }] of expressionPorts) {
|
|
665
676
|
const extend = node.extend ? [prop('extend', node.extend)] : [];
|
|
666
677
|
if (node.ramp) {
|
|
@@ -697,24 +708,25 @@ function compile(lines, root, fps, options) {
|
|
|
697
708
|
}
|
|
698
709
|
graph.add(pos, 'transform:position', 'vector3', [prop('target', path),
|
|
699
710
|
...object.pathBindings.map(binding => prop('path', { curve: objectPath(binding.curve),
|
|
700
|
-
|
|
711
|
+
...(binding.position ? { position: binding.position } : { freq: binding.freq }),
|
|
712
|
+
shift: binding.shift, offset: binding.offset, start: binding.start,
|
|
701
713
|
end: Number.isFinite(binding.end) ? binding.end : null }))], object.pathBindings[0]?.source ?? object.source);
|
|
702
714
|
graph.add(world, 'transform:world', 'matrix4', [prop('target', path),
|
|
703
715
|
...(object.parent ? [prop('parent', objectPath(object.parent))] : []),
|
|
704
716
|
...object.aimBindings.map(binding => prop('lookat', { target: objectPath(binding.target), start: binding.start,
|
|
705
717
|
end: Number.isFinite(binding.end) ? binding.end : null }))], object.aimBindings[0]?.source ?? object.source);
|
|
706
|
-
translations.forEach(param => depend(pos, path + '/' + param));
|
|
707
|
-
[...rotations, ...scales].forEach(param => depend(world, path + '/' + param));
|
|
708
|
-
depend(world, pos);
|
|
718
|
+
translations.forEach(param => graph.depend(pos, path + '/' + param));
|
|
719
|
+
[...rotations, ...scales].forEach(param => graph.depend(world, path + '/' + param));
|
|
720
|
+
graph.depend(world, pos);
|
|
709
721
|
if (object.parent) {
|
|
710
|
-
depend(world, worldStep(object.parent));
|
|
722
|
+
graph.depend(world, worldStep(object.parent));
|
|
711
723
|
if (!coversTimeline(object.pathBindings))
|
|
712
|
-
depend(pos, worldStep(object.parent));
|
|
724
|
+
graph.depend(pos, worldStep(object.parent));
|
|
713
725
|
}
|
|
714
726
|
for (const binding of object.pathBindings)
|
|
715
|
-
depend(pos, worldStep(binding.curve));
|
|
727
|
+
graph.depend(pos, worldStep(binding.curve));
|
|
716
728
|
for (const binding of object.aimBindings)
|
|
717
|
-
depend(world, positionStep(binding.target));
|
|
729
|
+
graph.depend(world, positionStep(binding.target));
|
|
718
730
|
}
|
|
719
731
|
for (const [path, dependencies] of inputs)
|
|
720
732
|
for (const input of dependencies)
|
|
@@ -753,19 +765,15 @@ function compile(lines, root, fps, options) {
|
|
|
753
765
|
}
|
|
754
766
|
const scene = freeze({
|
|
755
767
|
fps,
|
|
756
|
-
...(
|
|
768
|
+
...(frameCount !== undefined && { frameCount }),
|
|
757
769
|
graph: finished.graph,
|
|
758
|
-
...(Object.keys(ranges).length && { ranges }),
|
|
770
|
+
...(ranges && Object.keys(ranges).length && { ranges }),
|
|
759
771
|
liveChannels,
|
|
760
|
-
...(
|
|
761
|
-
...(viewCamera && { camera: objectPath(viewCamera) }),
|
|
762
|
-
...(backgroundColor && { backgroundColor }),
|
|
763
|
-
...(display && { display }),
|
|
764
|
-
} }),
|
|
772
|
+
...(view && { view }),
|
|
765
773
|
});
|
|
766
774
|
objects.sort((a, b) => comparePaths(objectPath(a), objectPath(b)));
|
|
767
775
|
channels.sort((a, b) => comparePaths(channelPath(a), channelPath(b)));
|
|
768
|
-
plans.set(scene, {
|
|
776
|
+
plans.set(scene, { ...plan, order: finished.order, audioChannels });
|
|
769
777
|
return scene;
|
|
770
778
|
}
|
|
771
779
|
function rename(names, node, name, source) {
|
package/esm/contexts.js
CHANGED
|
@@ -58,6 +58,48 @@ function definitions(entries, types, kind, owner) {
|
|
|
58
58
|
}
|
|
59
59
|
return map;
|
|
60
60
|
}
|
|
61
|
+
export function validateExpressionSource(target, from, name, ramp, reject) {
|
|
62
|
+
const { stage, address, type } = from;
|
|
63
|
+
if (name === 'chramp' && (stage !== 'ch' || !ramp)) {
|
|
64
|
+
reject('invalid-ramp', 'chramp requires a ch:ramp node: ' + address);
|
|
65
|
+
}
|
|
66
|
+
if (target === 'ch' || target === 'obj') {
|
|
67
|
+
if (stage === 'obj') {
|
|
68
|
+
if (target === 'ch')
|
|
69
|
+
reject('channel-object-reference', 'Channel expressions cannot read object parameters: ' + address);
|
|
70
|
+
if (name !== 'ch')
|
|
71
|
+
reject('off-time-object-reference', name + ' requires a channel: ' + address);
|
|
72
|
+
}
|
|
73
|
+
else if (stage !== 'ch') {
|
|
74
|
+
if (!allowedSource(target, stage))
|
|
75
|
+
reject('extension-stage', 'Built-in nodes cannot read ' + address);
|
|
76
|
+
if (stage !== 'audio' || name !== 'ch') {
|
|
77
|
+
reject('off-time-extension-reference', 'Only ch() can read built-in audio scalars: ' + address);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else if (name !== 'ch' && stage !== 'ch') {
|
|
82
|
+
reject('off-time-extension-reference', name + ' requires a channel: ' + address);
|
|
83
|
+
}
|
|
84
|
+
if (type !== 'number')
|
|
85
|
+
reject('extension-port-type', 'Expected a number at ' + address);
|
|
86
|
+
}
|
|
87
|
+
export function validateGeometrySource(target, from, reject) {
|
|
88
|
+
if (from.stage !== 'geo')
|
|
89
|
+
reject('extension-stage', 'connect cannot drive this built-in input: ' + target);
|
|
90
|
+
if (from.type !== 'object')
|
|
91
|
+
reject('extension-port-type', 'Expected object geometry at ' + target);
|
|
92
|
+
}
|
|
93
|
+
export function validateScalarConnectionSource(target, operator, from, reject) {
|
|
94
|
+
if (from.stage !== 'audio' && from.stage !== 'ch') {
|
|
95
|
+
reject('extension-stage', 'connect cannot drive this built-in input: ' + target);
|
|
96
|
+
}
|
|
97
|
+
if (from.type !== 'number')
|
|
98
|
+
reject('extension-port-type', 'Expected a numeric output at ' + from.address);
|
|
99
|
+
if (from.stage === 'ch' && target.startsWith('/ch/') && !operator) {
|
|
100
|
+
reject('extension-stage', 'Expected a channel operator input: ' + target);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
61
103
|
export class Extensions {
|
|
62
104
|
registry;
|
|
63
105
|
types = new Map();
|