@vim3d/core 0.0.1 → 0.2.0
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 +32 -1
- package/channels.d.ts +8 -0
- package/channels.js +74 -0
- package/compile.d.ts +2 -0
- package/compile.js +486 -0
- package/esm/channels.js +68 -0
- package/esm/compile.js +483 -0
- package/esm/evaluate.js +164 -0
- package/esm/expression.js +179 -0
- package/esm/graph.js +83 -0
- package/esm/index.js +2 -0
- package/esm/math.js +81 -0
- package/esm/model.js +165 -0
- package/esm/types.js +23 -0
- package/evaluate.d.ts +2 -0
- package/evaluate.js +167 -0
- package/expression.d.ts +65 -0
- package/expression.js +184 -0
- package/graph.d.ts +11 -0
- package/graph.js +88 -0
- package/index.d.ts +4 -0
- package/index.js +5 -1
- package/math.d.ts +25 -0
- package/math.js +101 -0
- package/model.d.ts +101 -0
- package/model.js +181 -0
- package/package.json +13 -9
- package/types.d.ts +123 -0
- package/types.js +30 -0
- package/esm/index.d.ts +0 -1
package/esm/types.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export class EvaluationError extends Error {
|
|
2
|
+
diagnostic;
|
|
3
|
+
constructor(diagnostic) {
|
|
4
|
+
super(diagnostic.message);
|
|
5
|
+
this.diagnostic = diagnostic;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export function fail(code, message, source, dependencyPath) {
|
|
9
|
+
throw new EvaluationError({ code, message, ...(source && { source }), ...(dependencyPath && { dependencyPath }) });
|
|
10
|
+
}
|
|
11
|
+
export function finite(value, description, source) {
|
|
12
|
+
if (!Number.isFinite(value))
|
|
13
|
+
fail('non-finite', `${description} must be finite`, source);
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
export function freeze(value) {
|
|
17
|
+
if (value !== null && typeof value === 'object' && !Object.isFrozen(value)) {
|
|
18
|
+
for (const child of Object.values(value))
|
|
19
|
+
freeze(child);
|
|
20
|
+
Object.freeze(value);
|
|
21
|
+
}
|
|
22
|
+
return value;
|
|
23
|
+
}
|
package/evaluate.d.ts
ADDED
package/evaluate.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.evaluateFrame = evaluateFrame;
|
|
4
|
+
const channels_js_1 = require("./channels.js");
|
|
5
|
+
const expression_js_1 = require("./expression.js");
|
|
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
|
+
function evaluateFrame(scene, frame) {
|
|
10
|
+
try {
|
|
11
|
+
if (!Number.isSafeInteger(frame) || frame < 0 || (scene.frameCount !== undefined && frame >= scene.frameCount)) {
|
|
12
|
+
(0, types_js_1.fail)('invalid-frame', 'Frame is outside the scene: ' + frame);
|
|
13
|
+
}
|
|
14
|
+
return { value: evaluate(scene, frame) };
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
if (error instanceof types_js_1.EvaluationError)
|
|
18
|
+
return { diagnostics: [error.diagnostic] };
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function evaluate(scene, frame) {
|
|
23
|
+
const plan = model_js_1.plans.get(scene);
|
|
24
|
+
if (!plan)
|
|
25
|
+
(0, types_js_1.fail)('invalid-scene', 'Scene was not created by compileScene');
|
|
26
|
+
const { fps, frameCount } = scene;
|
|
27
|
+
const time = (0, types_js_1.finite)(frame / fps, 'Scene time');
|
|
28
|
+
const state = new Map(plan.objects.map(object => [object, (0, model_js_1.copyState)(object.authored)]));
|
|
29
|
+
for (const event of plan.events) {
|
|
30
|
+
if (event.frame > frame)
|
|
31
|
+
break;
|
|
32
|
+
event.edit(state.get(event.object));
|
|
33
|
+
}
|
|
34
|
+
const parameters = new Map();
|
|
35
|
+
const positions = new Map();
|
|
36
|
+
const worlds = new Map();
|
|
37
|
+
for (const object of plan.objects) {
|
|
38
|
+
const path = (0, model_js_1.objectPath)(object);
|
|
39
|
+
for (const name of Object.keys(object.authored.parameters))
|
|
40
|
+
parameters.set(path + '/' + name, { object, name });
|
|
41
|
+
positions.set((0, model_js_1.positionStep)(object), object);
|
|
42
|
+
worlds.set((0, model_js_1.worldStep)(object), object);
|
|
43
|
+
}
|
|
44
|
+
const outputs = new Map();
|
|
45
|
+
for (const channel of plan.channels) {
|
|
46
|
+
for (const [name, port] of channel.ports) {
|
|
47
|
+
outputs.set((0, model_js_1.channelPath)(channel) + '/' + name, { channel, port, cache: new Map() });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const expr = (ast, q, source) => (0, expression_js_1.evaluateExpression)(ast, { frame: q, fps, frameCount, source, read });
|
|
51
|
+
function read(path, q) {
|
|
52
|
+
if (path.startsWith('/obj/')) {
|
|
53
|
+
const { object, name } = parameters.get(path);
|
|
54
|
+
return state.get(object).parameters[name];
|
|
55
|
+
}
|
|
56
|
+
const { channel, port, cache } = outputs.get(path);
|
|
57
|
+
const value = cache.get(q);
|
|
58
|
+
if (value !== undefined)
|
|
59
|
+
return value;
|
|
60
|
+
const result = channel.table ? (0, channels_js_1.sample)(channel.table, port, q, fps) : expr(port.expression, q, port.source);
|
|
61
|
+
cache.set(q, result);
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
const channels = Object.create(null);
|
|
65
|
+
for (const path of plan.order) {
|
|
66
|
+
if (outputs.has(path))
|
|
67
|
+
channels[path] = read(path, frame);
|
|
68
|
+
}
|
|
69
|
+
for (const path of plan.order) {
|
|
70
|
+
const target = parameters.get(path);
|
|
71
|
+
if (!target)
|
|
72
|
+
continue;
|
|
73
|
+
const { object, name } = target;
|
|
74
|
+
const values = state.get(object).parameters;
|
|
75
|
+
const binding = plan.drivers.get(path)?.find(range => (0, model_js_1.active)(frame, range));
|
|
76
|
+
let value = values[name];
|
|
77
|
+
if (binding?.kind === 'expr')
|
|
78
|
+
value = expr(binding.expression, frame, binding.source);
|
|
79
|
+
else if (binding?.kind === 'ref')
|
|
80
|
+
value = binding.scale * read(binding.input, frame) + binding.offset;
|
|
81
|
+
else if (binding?.kind === 'register') {
|
|
82
|
+
const at = (f) => read(binding.input, (0, types_js_1.finite)(binding.freq * f + binding.shift, 'Registration sample frame', binding.source));
|
|
83
|
+
if (name === 'rx' || name === 'ry' || name === 'rz') {
|
|
84
|
+
if (frame !== binding.start)
|
|
85
|
+
value += binding.scale * (at(binding.start) - at(frame));
|
|
86
|
+
}
|
|
87
|
+
else
|
|
88
|
+
value = binding.scale * at(frame) + binding.offset;
|
|
89
|
+
}
|
|
90
|
+
(0, types_js_1.finite)(value, path, binding?.source ?? object.source);
|
|
91
|
+
values[name] = (0, model_js_1.isColor)(name) ? Math.min(1, Math.max(0, value)) : value;
|
|
92
|
+
}
|
|
93
|
+
const worldPositions = new Map();
|
|
94
|
+
const localMatrices = new Map();
|
|
95
|
+
const worldMatrices = new Map();
|
|
96
|
+
for (const path of plan.order) {
|
|
97
|
+
const positioned = positions.get(path);
|
|
98
|
+
if (positioned) {
|
|
99
|
+
const authored = state.get(positioned);
|
|
100
|
+
const binding = positioned.pathBindings.find(range => (0, model_js_1.active)(frame, range));
|
|
101
|
+
let worldPosition;
|
|
102
|
+
if (binding) {
|
|
103
|
+
const vertices = binding.curve.geometry.vertices;
|
|
104
|
+
const index = (0, math_js_1.wrap)(Math.floor((0, types_js_1.finite)(binding.freq * frame + binding.shift, 'Path vertex index', binding.source)), vertices.length);
|
|
105
|
+
worldPosition = (0, math_js_1.add)((0, math_js_1.point)(worldMatrices.get(binding.curve), vertices[index]), binding.offset);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
const parent = positioned.parent ? worldMatrices.get(positioned.parent) : math_js_1.identity;
|
|
109
|
+
worldPosition = (0, math_js_1.point)(parent, (0, model_js_1.vector)(authored.parameters, model_js_1.translations));
|
|
110
|
+
}
|
|
111
|
+
for (const coordinate of worldPosition)
|
|
112
|
+
(0, types_js_1.finite)(coordinate, 'Final position', binding?.source ?? positioned.source);
|
|
113
|
+
worldPositions.set(positioned, worldPosition);
|
|
114
|
+
}
|
|
115
|
+
const object = worlds.get(path);
|
|
116
|
+
if (!object)
|
|
117
|
+
continue;
|
|
118
|
+
const authored = state.get(object);
|
|
119
|
+
const parent = object.parent ? worldMatrices.get(object.parent) : math_js_1.identity;
|
|
120
|
+
const sizes = (0, model_js_1.vector)(authored.parameters, model_js_1.scales);
|
|
121
|
+
const binding = object.aimBindings.find(range => (0, model_js_1.active)(frame, range));
|
|
122
|
+
let axes = (0, math_js_1.euler)((0, model_js_1.vector)(authored.parameters, model_js_1.rotations));
|
|
123
|
+
if (binding) {
|
|
124
|
+
if (object.kind === 'light' && authored.light !== 'spot') {
|
|
125
|
+
(0, types_js_1.fail)('invalid-lookat-subject', 'Only spotlights may use lookat', binding.source);
|
|
126
|
+
}
|
|
127
|
+
const up = object.kind === 'camera' ? authored.up : math_js_1.worldUp;
|
|
128
|
+
const forward = object.kind === 'camera' || object.kind === 'light' ? -1 : 1;
|
|
129
|
+
axes = (0, math_js_1.aim)(parent, worldPositions.get(object), worldPositions.get(binding.target), up, forward, sizes, binding.source) ?? axes;
|
|
130
|
+
}
|
|
131
|
+
const pathBinding = object.pathBindings.find(range => (0, model_js_1.active)(frame, range));
|
|
132
|
+
const localPosition = pathBinding
|
|
133
|
+
? (0, math_js_1.inverseDirection)(parent, (0, math_js_1.sub)(worldPositions.get(object), (0, math_js_1.column)(parent, 3)), pathBinding.source)
|
|
134
|
+
: (0, model_js_1.vector)(authored.parameters, model_js_1.translations);
|
|
135
|
+
const local = authored.matrix ?? (0, math_js_1.matrix)(localPosition, axes, sizes);
|
|
136
|
+
const world = (0, math_js_1.multiply)(parent, local);
|
|
137
|
+
for (const component of [...local, ...world])
|
|
138
|
+
(0, types_js_1.finite)(component, 'Final matrix component', binding?.source ?? object.source);
|
|
139
|
+
localMatrices.set(object, local);
|
|
140
|
+
worldMatrices.set(object, world);
|
|
141
|
+
}
|
|
142
|
+
const objects = plan.objects.map(object => {
|
|
143
|
+
const authored = state.get(object);
|
|
144
|
+
const p = authored.parameters;
|
|
145
|
+
const localMatrix = localMatrices.get(object), worldMatrix = worldMatrices.get(object);
|
|
146
|
+
const snapshot = {
|
|
147
|
+
path: (0, model_js_1.objectPath)(object), kind: object.kind, ...(object.parent && { parent: (0, model_js_1.objectPath)(object.parent) }),
|
|
148
|
+
localMatrix, worldMatrix, parameters: p, visible: authored.visible,
|
|
149
|
+
...(object.geometry && { geometry: object.geometry }),
|
|
150
|
+
...(Object.hasOwn(p, 'diffr') && { material: {
|
|
151
|
+
diffuse: [p.diffr, p.diffg, p.diffb, p.diffa], ambient: [p.ambr, p.ambg, p.ambb, p.amba],
|
|
152
|
+
specular: [p.specr, p.specg, p.specb, p.speca], ...(p.shine !== undefined && { shininess: p.shine }),
|
|
153
|
+
} }),
|
|
154
|
+
...(object.kind === 'camera' && { camera: {
|
|
155
|
+
position: (0, math_js_1.column)(worldMatrix, 3), direction: (0, math_js_1.normalize)((0, math_js_1.scale)((0, math_js_1.column)(worldMatrix, 2), -1)),
|
|
156
|
+
up: (0, math_js_1.normalize)((0, math_js_1.column)(worldMatrix, 1)), fovy: p.fovy, znear: p.znear, zfar: p.zfar,
|
|
157
|
+
} }),
|
|
158
|
+
...(object.kind === 'light' && { light: {
|
|
159
|
+
kind: authored.light, enabled: authored.enabled, position: (0, math_js_1.column)(worldMatrix, 3),
|
|
160
|
+
direction: (0, math_js_1.normalize)((0, math_js_1.scale)((0, math_js_1.column)(worldMatrix, 2), -1)),
|
|
161
|
+
} }),
|
|
162
|
+
};
|
|
163
|
+
return snapshot;
|
|
164
|
+
});
|
|
165
|
+
return (0, types_js_1.freeze)({ frame, time, fps, objects, channels, cameras: objects.filter(o => o.kind === 'camera'),
|
|
166
|
+
lights: objects.filter(o => o.kind === 'light') });
|
|
167
|
+
}
|
package/expression.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { type SourceLocation } from './types.js';
|
|
2
|
+
declare const variables: readonly ["$F", "$T", "$FPS", "$NFRAMES", "$PI", "$TAU", "$E"];
|
|
3
|
+
export type Variable = typeof variables[number];
|
|
4
|
+
export type ReferenceFunction = 'ch' | 'chf' | 'cht';
|
|
5
|
+
export type UnaryOperator = '+' | '-';
|
|
6
|
+
export type BinaryOperator = '+' | '-' | '*' | '/' | '%' | '^';
|
|
7
|
+
export type Expression = {
|
|
8
|
+
readonly kind: 'number';
|
|
9
|
+
readonly value: number;
|
|
10
|
+
} | {
|
|
11
|
+
readonly kind: 'variable';
|
|
12
|
+
readonly name: Variable;
|
|
13
|
+
} | {
|
|
14
|
+
readonly kind: 'unary';
|
|
15
|
+
readonly op: UnaryOperator;
|
|
16
|
+
readonly operand: Expression;
|
|
17
|
+
} | {
|
|
18
|
+
readonly kind: 'binary';
|
|
19
|
+
readonly op: BinaryOperator;
|
|
20
|
+
readonly left: Expression;
|
|
21
|
+
readonly right: Expression;
|
|
22
|
+
} | {
|
|
23
|
+
readonly kind: 'call';
|
|
24
|
+
readonly name: MathFunction;
|
|
25
|
+
readonly args: readonly Expression[];
|
|
26
|
+
} | {
|
|
27
|
+
readonly kind: 'reference';
|
|
28
|
+
readonly name: ReferenceFunction;
|
|
29
|
+
readonly path: string;
|
|
30
|
+
readonly time?: Expression;
|
|
31
|
+
};
|
|
32
|
+
declare function noise(x: number, seed?: number): number;
|
|
33
|
+
declare const functions: {
|
|
34
|
+
sin: [number, number, (x: number) => number];
|
|
35
|
+
cos: [number, number, (x: number) => number];
|
|
36
|
+
tan: [number, number, (x: number) => number];
|
|
37
|
+
atan2: [number, number, (y: number, x: number) => number];
|
|
38
|
+
abs: [number, number, (x: number) => number];
|
|
39
|
+
floor: [number, number, (x: number) => number];
|
|
40
|
+
ceil: [number, number, (x: number) => number];
|
|
41
|
+
round: [number, number, (x: number) => number];
|
|
42
|
+
min: [number, number, (...values: number[]) => number];
|
|
43
|
+
max: [number, number, (...values: number[]) => number];
|
|
44
|
+
clamp: [number, number, (x: number, lo: number, hi: number) => number];
|
|
45
|
+
lerp: [number, number, (a: number, b: number, u: number) => number];
|
|
46
|
+
fit: [number, number, (x: number, a: number, b: number, c: number, d: number) => number];
|
|
47
|
+
smoothstep: [number, number, (a: number, b: number, x: number) => number];
|
|
48
|
+
pow: [number, number, (x: number, y: number) => number];
|
|
49
|
+
sqrt: [number, number, (x: number) => number];
|
|
50
|
+
exp: [number, number, (x: number) => number];
|
|
51
|
+
log: [number, number, (x: number) => number];
|
|
52
|
+
noise: [number, number, typeof noise];
|
|
53
|
+
};
|
|
54
|
+
export type MathFunction = keyof typeof functions;
|
|
55
|
+
export declare function parseExpression(text: string, source: SourceLocation): Expression;
|
|
56
|
+
export declare function bindExpression(ast: Expression, resolve: (path: string, name: ReferenceFunction) => string): Expression;
|
|
57
|
+
interface ExpressionContext {
|
|
58
|
+
frame: number;
|
|
59
|
+
fps: number;
|
|
60
|
+
frameCount?: number;
|
|
61
|
+
source: SourceLocation;
|
|
62
|
+
read: (path: string, frame: number) => number;
|
|
63
|
+
}
|
|
64
|
+
export declare function evaluateExpression(ast: Expression, context: ExpressionContext): number;
|
|
65
|
+
export {};
|
package/expression.js
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseExpression = parseExpression;
|
|
4
|
+
exports.bindExpression = bindExpression;
|
|
5
|
+
exports.evaluateExpression = evaluateExpression;
|
|
6
|
+
const types_js_1 = require("./types.js");
|
|
7
|
+
const variables = ['$F', '$T', '$FPS', '$NFRAMES', '$PI', '$TAU', '$E'];
|
|
8
|
+
const lerp = (a, b, u) => a + (b - a) * u;
|
|
9
|
+
const unit = (u) => Math.max(0, Math.min(1, u));
|
|
10
|
+
const smooth = (u) => u * u * (3 - 2 * u);
|
|
11
|
+
function noise(x, seed = 0) {
|
|
12
|
+
const i = Math.floor(x);
|
|
13
|
+
if (![i, i + 1, seed].every(Number.isSafeInteger))
|
|
14
|
+
return NaN;
|
|
15
|
+
const hash = (j) => {
|
|
16
|
+
let n = j ^ seed;
|
|
17
|
+
n = Math.imul(n ^ (n >>> 16), 0x7feb352d);
|
|
18
|
+
n = Math.imul(n ^ (n >>> 15), 0x846ca68b);
|
|
19
|
+
return 2 * ((n ^ (n >>> 16)) >>> 0) / 4294967295 - 1;
|
|
20
|
+
};
|
|
21
|
+
return lerp(hash(i), hash(i + 1), smooth(x - i));
|
|
22
|
+
}
|
|
23
|
+
const functions = {
|
|
24
|
+
sin: [1, 1, Math.sin], cos: [1, 1, Math.cos], tan: [1, 1, Math.tan],
|
|
25
|
+
atan2: [2, 2, (y, x) => x === 0 && y === 0 ? NaN : Math.atan2(y, x)],
|
|
26
|
+
abs: [1, 1, Math.abs], floor: [1, 1, Math.floor], ceil: [1, 1, Math.ceil],
|
|
27
|
+
round: [1, 1, Math.round], min: [1, Infinity, Math.min], max: [1, Infinity, Math.max],
|
|
28
|
+
clamp: [3, 3, (x, lo, hi) => lo <= hi ? Math.max(lo, Math.min(hi, x)) : NaN],
|
|
29
|
+
lerp: [3, 3, lerp],
|
|
30
|
+
fit: [5, 5, (x, a, b, c, d) => a < b ? lerp(c, d, unit((x - a) / (b - a))) : NaN],
|
|
31
|
+
smoothstep: [3, 3, (a, b, x) => a < b ? smooth(unit((x - a) / (b - a))) : NaN],
|
|
32
|
+
pow: [2, 2, Math.pow], sqrt: [1, 1, Math.sqrt], exp: [1, 1, Math.exp],
|
|
33
|
+
log: [1, 1, Math.log], noise: [1, 2, noise],
|
|
34
|
+
};
|
|
35
|
+
function parseExpression(text, source) {
|
|
36
|
+
let pos = 0;
|
|
37
|
+
const token = /[ \t]*(\d+(?:\.\d*)?(?:[eE][+-]?\d+)?|\.\d+(?:[eE][+-]?\d+)?|\$?[a-zA-Z][a-zA-Z0-9]*|'[^'\x00-\x1f\x7f]*'|"[^"\x00-\x1f\x7f]*"|[()+*/%^-]|,)/y;
|
|
38
|
+
let current = '';
|
|
39
|
+
function next() {
|
|
40
|
+
token.lastIndex = pos;
|
|
41
|
+
const match = token.exec(text);
|
|
42
|
+
if (!match && !/^[ \t]*$/.test(text.slice(pos)))
|
|
43
|
+
invalid('Unexpected token');
|
|
44
|
+
current = match?.[1] ?? '';
|
|
45
|
+
pos = match ? token.lastIndex : text.length;
|
|
46
|
+
}
|
|
47
|
+
function invalid(message) {
|
|
48
|
+
(0, types_js_1.fail)('expression-syntax', message + ' at expression column ' + (pos + 1), source);
|
|
49
|
+
}
|
|
50
|
+
function take(expected) {
|
|
51
|
+
if (current !== expected)
|
|
52
|
+
return false;
|
|
53
|
+
next();
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
function expect(expected) {
|
|
57
|
+
if (!take(expected))
|
|
58
|
+
invalid('Expected ' + expected);
|
|
59
|
+
}
|
|
60
|
+
function primary() {
|
|
61
|
+
if (take('(')) {
|
|
62
|
+
const value = sum();
|
|
63
|
+
expect(')');
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
const word = current;
|
|
67
|
+
next();
|
|
68
|
+
if (/^[\d.]/.test(word))
|
|
69
|
+
return { kind: 'number', value: (0, types_js_1.finite)(Number(word), 'Number literal', source) };
|
|
70
|
+
if (variables.includes(word))
|
|
71
|
+
return { kind: 'variable', name: word };
|
|
72
|
+
if (word === 'ch' || word === 'chf' || word === 'cht') {
|
|
73
|
+
expect('(');
|
|
74
|
+
const path = current;
|
|
75
|
+
if (!/^['"]/.test(path))
|
|
76
|
+
invalid('Expected a literal path');
|
|
77
|
+
next();
|
|
78
|
+
let time;
|
|
79
|
+
if (word !== 'ch') {
|
|
80
|
+
expect(',');
|
|
81
|
+
time = sum();
|
|
82
|
+
}
|
|
83
|
+
expect(')');
|
|
84
|
+
return { kind: 'reference', name: word, path: path.slice(1, -1), ...(time && { time }) };
|
|
85
|
+
}
|
|
86
|
+
if (!Object.hasOwn(functions, word))
|
|
87
|
+
invalid('Unknown expression name ' + JSON.stringify(word));
|
|
88
|
+
const name = word;
|
|
89
|
+
expect('(');
|
|
90
|
+
const args = [sum()];
|
|
91
|
+
while (take(','))
|
|
92
|
+
args.push(sum());
|
|
93
|
+
expect(')');
|
|
94
|
+
const [min, max] = functions[name];
|
|
95
|
+
if (args.length < min || args.length > max)
|
|
96
|
+
invalid('Wrong number of arguments to ' + name);
|
|
97
|
+
return { kind: 'call', name, args };
|
|
98
|
+
}
|
|
99
|
+
function unary() {
|
|
100
|
+
if (current === '+' || current === '-') {
|
|
101
|
+
const op = current;
|
|
102
|
+
next();
|
|
103
|
+
return { kind: 'unary', op, operand: unary() };
|
|
104
|
+
}
|
|
105
|
+
const left = primary();
|
|
106
|
+
return take('^') ? { kind: 'binary', op: '^', left, right: unary() } : left;
|
|
107
|
+
}
|
|
108
|
+
function chain(read, ops) {
|
|
109
|
+
let left = read();
|
|
110
|
+
while (ops.includes(current)) {
|
|
111
|
+
const op = current;
|
|
112
|
+
next();
|
|
113
|
+
left = { kind: 'binary', op, left, right: read() };
|
|
114
|
+
}
|
|
115
|
+
return left;
|
|
116
|
+
}
|
|
117
|
+
const sum = () => chain(() => chain(unary, ['*', '/', '%']), ['+', '-']);
|
|
118
|
+
next();
|
|
119
|
+
const ast = sum();
|
|
120
|
+
if (current)
|
|
121
|
+
invalid('Unexpected token ' + current);
|
|
122
|
+
return ast;
|
|
123
|
+
}
|
|
124
|
+
function bindExpression(ast, resolve) {
|
|
125
|
+
const bind = (expr) => bindExpression(expr, resolve);
|
|
126
|
+
switch (ast.kind) {
|
|
127
|
+
case 'reference': return { ...ast, path: resolve(ast.path, ast.name), ...(ast.time && { time: bind(ast.time) }) };
|
|
128
|
+
case 'unary': return { ...ast, operand: bind(ast.operand) };
|
|
129
|
+
case 'binary': return { ...ast, left: bind(ast.left), right: bind(ast.right) };
|
|
130
|
+
case 'call': return { ...ast, args: ast.args.map(bind) };
|
|
131
|
+
default: return ast;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function evaluateExpression(ast, context) {
|
|
135
|
+
const { frame, fps, frameCount, source, read } = context;
|
|
136
|
+
const visit = (node) => {
|
|
137
|
+
let value;
|
|
138
|
+
switch (node.kind) {
|
|
139
|
+
case 'number': return node.value;
|
|
140
|
+
case 'variable':
|
|
141
|
+
if (node.name === '$NFRAMES' && frameCount === undefined) {
|
|
142
|
+
(0, types_js_1.fail)('missing-frame-count', '$NFRAMES requires a configured frame count', source);
|
|
143
|
+
}
|
|
144
|
+
value = { $F: frame, $T: frame / fps, $FPS: fps, $NFRAMES: frameCount,
|
|
145
|
+
$PI: Math.PI, $TAU: Math.PI * 2, $E: Math.E }[node.name];
|
|
146
|
+
break;
|
|
147
|
+
case 'reference': {
|
|
148
|
+
const time = node.time ? visit(node.time) : frame;
|
|
149
|
+
return read(node.path, (0, types_js_1.finite)(node.name === 'cht' ? time * fps : time, 'Sample frame', source));
|
|
150
|
+
}
|
|
151
|
+
case 'unary': return node.op === '-' ? -visit(node.operand) : visit(node.operand);
|
|
152
|
+
case 'binary': {
|
|
153
|
+
const a = visit(node.left);
|
|
154
|
+
const b = visit(node.right);
|
|
155
|
+
switch (node.op) {
|
|
156
|
+
case '+':
|
|
157
|
+
value = a + b;
|
|
158
|
+
break;
|
|
159
|
+
case '-':
|
|
160
|
+
value = a - b;
|
|
161
|
+
break;
|
|
162
|
+
case '*':
|
|
163
|
+
value = a * b;
|
|
164
|
+
break;
|
|
165
|
+
case '/':
|
|
166
|
+
value = a / b;
|
|
167
|
+
break;
|
|
168
|
+
case '%':
|
|
169
|
+
value = a % b;
|
|
170
|
+
break;
|
|
171
|
+
case '^':
|
|
172
|
+
value = a ** b;
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
case 'call':
|
|
178
|
+
value = functions[node.name][2](...node.args.map(visit));
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
return (0, types_js_1.finite)(value, 'Expression value', source);
|
|
182
|
+
};
|
|
183
|
+
return visit(ast);
|
|
184
|
+
}
|
package/graph.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type EvaluationGraph, type GraphNode, type GraphPort, type SourceLocation } from './types.js';
|
|
2
|
+
export declare function comparePaths(a: string, b: string): number;
|
|
3
|
+
export declare class GraphBuilder {
|
|
4
|
+
private steps;
|
|
5
|
+
add(path: string, type: string, portType: GraphPort['type'], props: GraphNode['props'], source?: SourceLocation): void;
|
|
6
|
+
depend(to: string, on: string): void;
|
|
7
|
+
finish(): {
|
|
8
|
+
order: string[];
|
|
9
|
+
graph: EvaluationGraph;
|
|
10
|
+
};
|
|
11
|
+
}
|
package/graph.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GraphBuilder = void 0;
|
|
4
|
+
exports.comparePaths = comparePaths;
|
|
5
|
+
const types_js_1 = require("./types.js");
|
|
6
|
+
const encoder = new TextEncoder();
|
|
7
|
+
const graphName = (path) => path.replaceAll('%', '%25').replaceAll('/', '%2F');
|
|
8
|
+
function comparePaths(a, b) {
|
|
9
|
+
if (a === b)
|
|
10
|
+
return 0;
|
|
11
|
+
const x = encoder.encode(a), y = encoder.encode(b);
|
|
12
|
+
for (let i = 0; i < Math.min(x.length, y.length); i++) {
|
|
13
|
+
if (x[i] !== y[i])
|
|
14
|
+
return x[i] - y[i];
|
|
15
|
+
}
|
|
16
|
+
return x.length - y.length;
|
|
17
|
+
}
|
|
18
|
+
class GraphBuilder {
|
|
19
|
+
steps = new Map();
|
|
20
|
+
add(path, type, portType, props, source) {
|
|
21
|
+
const context = path.startsWith('/ch/') ? 'ch' : 'obj';
|
|
22
|
+
this.steps.set(path, { node: { name: graphName(path), context, type, props, inputs: [],
|
|
23
|
+
outputs: [{ name: 'value', type: portType }] }, dependencies: new Set(), source });
|
|
24
|
+
}
|
|
25
|
+
depend(to, on) {
|
|
26
|
+
this.steps.get(to).dependencies.add(on);
|
|
27
|
+
}
|
|
28
|
+
finish() {
|
|
29
|
+
const paths = [...this.steps.keys()].sort(comparePaths);
|
|
30
|
+
const counts = new Map();
|
|
31
|
+
const readers = new Map();
|
|
32
|
+
for (const path of paths) {
|
|
33
|
+
const { dependencies } = this.steps.get(path);
|
|
34
|
+
counts.set(path, dependencies.size);
|
|
35
|
+
for (const dependency of dependencies) {
|
|
36
|
+
const list = readers.get(dependency) ?? [];
|
|
37
|
+
list.push(path);
|
|
38
|
+
readers.set(dependency, list);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const ready = paths.filter(path => !counts.get(path)).reverse();
|
|
42
|
+
const order = [];
|
|
43
|
+
while (ready.length) {
|
|
44
|
+
const path = ready.pop();
|
|
45
|
+
order.push(path);
|
|
46
|
+
for (const reader of readers.get(path) ?? []) {
|
|
47
|
+
const count = counts.get(reader) - 1;
|
|
48
|
+
counts.set(reader, count);
|
|
49
|
+
if (!count) {
|
|
50
|
+
let left = 0, right = ready.length;
|
|
51
|
+
while (left < right) {
|
|
52
|
+
const mid = (left + right) >>> 1;
|
|
53
|
+
if (comparePaths(ready[mid], reader) > 0)
|
|
54
|
+
left = mid + 1;
|
|
55
|
+
else
|
|
56
|
+
right = mid;
|
|
57
|
+
}
|
|
58
|
+
ready.splice(left, 0, reader);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (order.length !== paths.length) {
|
|
63
|
+
const chain = [];
|
|
64
|
+
const indices = new Map();
|
|
65
|
+
let path = paths.find(path => counts.get(path) > 0);
|
|
66
|
+
while (!indices.has(path)) {
|
|
67
|
+
indices.set(path, chain.length);
|
|
68
|
+
chain.push(path);
|
|
69
|
+
path = [...this.steps.get(path).dependencies].filter(dep => counts.get(dep) > 0).sort(comparePaths)[0];
|
|
70
|
+
}
|
|
71
|
+
const cycle = [...chain.slice(indices.get(path)), path];
|
|
72
|
+
(0, types_js_1.fail)('dependency-cycle', 'Dependency cycle: ' + cycle.join(' -> '), this.steps.get(path).source, cycle);
|
|
73
|
+
}
|
|
74
|
+
const edges = [];
|
|
75
|
+
const nodes = paths.map(path => {
|
|
76
|
+
const { node, dependencies } = this.steps.get(path);
|
|
77
|
+
const inputs = [...dependencies].sort(comparePaths).map(dep => {
|
|
78
|
+
const source = this.steps.get(dep).node;
|
|
79
|
+
const output = source.outputs[0];
|
|
80
|
+
edges.push({ src: { node: source.name, port: output.name }, dst: { node: node.name, port: source.name } });
|
|
81
|
+
return { name: source.name, type: output.type };
|
|
82
|
+
});
|
|
83
|
+
return { ...node, inputs };
|
|
84
|
+
});
|
|
85
|
+
return { order, graph: { name: 'scene', nodes, edges } };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.GraphBuilder = GraphBuilder;
|
package/index.d.ts
CHANGED
|
@@ -1 +1,5 @@
|
|
|
1
1
|
export declare const name = "@vim3d/core";
|
|
2
|
+
export { compileScene } from './compile.js';
|
|
3
|
+
export { evaluateFrame } from './evaluate.js';
|
|
4
|
+
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';
|
package/index.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.name = void 0;
|
|
3
|
+
exports.evaluateFrame = exports.compileScene = exports.name = void 0;
|
|
4
4
|
exports.name = '@vim3d/core';
|
|
5
|
+
var compile_js_1 = require("./compile.js");
|
|
6
|
+
Object.defineProperty(exports, "compileScene", { enumerable: true, get: function () { return compile_js_1.compileScene; } });
|
|
7
|
+
var evaluate_js_1 = require("./evaluate.js");
|
|
8
|
+
Object.defineProperty(exports, "evaluateFrame", { enumerable: true, get: function () { return evaluate_js_1.evaluateFrame; } });
|
package/math.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type Matrix4, type SourceLocation, type Vec3 } from './types.js';
|
|
2
|
+
export declare const identity: readonly [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
|
|
3
|
+
export declare const worldUp: readonly [0, 1, 0];
|
|
4
|
+
export declare const add: (a: Vec3, b: Vec3) => Vec3;
|
|
5
|
+
export declare const sub: (a: Vec3, b: Vec3) => Vec3;
|
|
6
|
+
export declare const scale: (a: Vec3, s: number) => Vec3;
|
|
7
|
+
export declare const dot: (a: Vec3, b: Vec3) => number;
|
|
8
|
+
export declare const cross: (a: Vec3, b: Vec3) => Vec3;
|
|
9
|
+
export declare const length: (a: Vec3) => number;
|
|
10
|
+
export declare const normalize: (a: Vec3) => Vec3;
|
|
11
|
+
export declare const column: (a: Matrix4, i: number) => Vec3;
|
|
12
|
+
type Axes = readonly [Vec3, Vec3, Vec3];
|
|
13
|
+
export declare function euler([x, y, z]: Vec3): Axes;
|
|
14
|
+
export declare function decompose(m: Matrix4, source?: SourceLocation): {
|
|
15
|
+
rotation: Vec3;
|
|
16
|
+
scale: Vec3;
|
|
17
|
+
} | undefined;
|
|
18
|
+
export declare function matrix(position: Vec3, axes: Axes, sizes: Vec3): Matrix4;
|
|
19
|
+
export declare function direction(m: Matrix4, p: Vec3): Vec3;
|
|
20
|
+
export declare function point(m: Matrix4, p: Vec3): Vec3;
|
|
21
|
+
export declare function multiply(a: Matrix4, b: Matrix4): Matrix4;
|
|
22
|
+
export declare function inverseDirection(m: Matrix4, p: Vec3, source?: SourceLocation): Vec3;
|
|
23
|
+
export declare function aim(parent: Matrix4, origin: Vec3, target: Vec3, up: Vec3, forward: 1 | -1, sizes: Vec3, source?: SourceLocation): Axes | undefined;
|
|
24
|
+
export declare function wrap(i: number, count: number): number;
|
|
25
|
+
export {};
|