@shaderfrog/core 0.1.0 → 0.1.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/dist/ast/manipulate.js +328 -0
- package/dist/ast/shader-sections.js +256 -0
- package/dist/context.js +230 -0
- package/dist/engine.js +209 -0
- package/dist/evaluate.js +27 -0
- package/dist/graph-types.js +7 -0
- package/dist/graph.js +381 -0
- package/dist/graph.test.js +168 -0
- package/dist/nodes/code-nodes.js +18 -0
- package/dist/nodes/core-node.js +9 -0
- package/dist/nodes/data-nodes.js +123 -0
- package/dist/nodes/edge.js +1 -0
- package/dist/nodes/engine-node.js +189 -0
- package/dist/parsers.js +213 -0
- package/dist/plugins/babylon/bablyengine.js +582 -0
- package/dist/plugins/babylon/importers.js +64 -0
- package/dist/plugins/babylon/index.js +2 -0
- package/dist/plugins/playcanvas/importers.js +28 -0
- package/dist/plugins/playcanvas/index.js +2 -0
- package/dist/plugins/playcanvas/playengine.js +510 -0
- package/dist/plugins/three/importers.js +15 -0
- package/dist/plugins/three/index.js +2 -0
- package/dist/plugins/three/threngine.js +495 -0
- package/dist/strategy/assignemntTo.js +26 -0
- package/dist/strategy/declarationOf.js +23 -0
- package/dist/strategy/hardCode.js +23 -0
- package/dist/strategy/index.js +38 -0
- package/dist/strategy/inject.js +122 -0
- package/dist/strategy/namedAttribute.js +48 -0
- package/dist/strategy/texture2D.js +83 -0
- package/dist/strategy/uniform.js +190 -0
- package/dist/strategy/variable.js +80 -0
- package/dist/stratgies.test.js +164 -0
- package/dist/util/ast.js +9 -0
- package/dist/util/ensure.js +7 -0
- package/dist/util/id.js +2 -0
- package/package.json +1 -1
package/dist/graph.js
ADDED
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
13
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
14
|
+
if (!m) return o;
|
|
15
|
+
var i = m.call(o), r, ar = [], e;
|
|
16
|
+
try {
|
|
17
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
18
|
+
}
|
|
19
|
+
catch (error) { e = { error: error }; }
|
|
20
|
+
finally {
|
|
21
|
+
try {
|
|
22
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
23
|
+
}
|
|
24
|
+
finally { if (e) throw e.error; }
|
|
25
|
+
}
|
|
26
|
+
return ar;
|
|
27
|
+
};
|
|
28
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
29
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
30
|
+
if (ar || !(i in from)) {
|
|
31
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
32
|
+
ar[i] = from[i];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
36
|
+
};
|
|
37
|
+
import { renameBindings, renameFunctions, } from '@shaderfrog/glsl-parser/parser/utils';
|
|
38
|
+
import { emptyShaderSections, findShaderSections, mergeShaderSections, } from './ast/shader-sections';
|
|
39
|
+
import { makeExpression } from './ast/manipulate';
|
|
40
|
+
import { ensure } from './util/ensure';
|
|
41
|
+
import { SourceType } from './nodes/code-nodes';
|
|
42
|
+
import { nodeInput } from './nodes/core-node';
|
|
43
|
+
import { makeId } from './util/id';
|
|
44
|
+
import { coreParsers } from './parsers';
|
|
45
|
+
import { toGlsl } from './evaluate';
|
|
46
|
+
import { MAGIC_OUTPUT_STMTS, NodeType } from './graph-types';
|
|
47
|
+
var log = function () {
|
|
48
|
+
var _a;
|
|
49
|
+
var args = [];
|
|
50
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
51
|
+
args[_i] = arguments[_i];
|
|
52
|
+
}
|
|
53
|
+
return (_a = console.log).call.apply(_a, __spreadArray([console, '\x1b[31m(core.graph)\x1b[0m'], __read(args), false));
|
|
54
|
+
};
|
|
55
|
+
export var isDataNode = function (node) {
|
|
56
|
+
return 'value' in node;
|
|
57
|
+
};
|
|
58
|
+
export var isSourceNode = function (node) {
|
|
59
|
+
return !isDataNode(node);
|
|
60
|
+
};
|
|
61
|
+
export var findNode = function (graph, id) {
|
|
62
|
+
return ensure(graph.nodes.find(function (node) { return node.id === id; }));
|
|
63
|
+
};
|
|
64
|
+
export var doesLinkThruShader = function (graph, node) {
|
|
65
|
+
var edges = graph.edges.filter(function (edge) { return edge.from === node.id; });
|
|
66
|
+
if (edges.length === 0) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
return edges.reduce(function (foundShader, edge) {
|
|
70
|
+
var upstreamNode = ensure(graph.nodes.find(function (node) { return node.id === edge.to; }));
|
|
71
|
+
return (foundShader ||
|
|
72
|
+
// TODO: LARD this probably will introduce some insidius hard to track
|
|
73
|
+
// down bug, as I try to pull toon and phong up out of core, I need to
|
|
74
|
+
// know if a graph links through a "shader" which now means somehting
|
|
75
|
+
// different... does a config object need isShader? Can we compute it from
|
|
76
|
+
// inputs/ outputs/source?
|
|
77
|
+
(upstreamNode.sourceType !== SourceType.EXPRESSION &&
|
|
78
|
+
upstreamNode.type !== NodeType.OUTPUT) ||
|
|
79
|
+
doesLinkThruShader(graph, upstreamNode));
|
|
80
|
+
}, false);
|
|
81
|
+
};
|
|
82
|
+
export var nodeName = function (node) {
|
|
83
|
+
return 'main_' + node.name.replace(/[^a-zA-Z0-9]/g, ' ').replace(/ +/g, '_');
|
|
84
|
+
};
|
|
85
|
+
export var mangleName = function (name, node) {
|
|
86
|
+
// Mangle names by using the next stage id, if present
|
|
87
|
+
var id = ('nextStageNodeId' in node && node.nextStageNodeId) || node.id;
|
|
88
|
+
return "".concat(name, "_").concat(id);
|
|
89
|
+
};
|
|
90
|
+
export var mangleVar = function (name, engine, node) {
|
|
91
|
+
return engine.preserve.has(name) ? name : mangleName(name, node);
|
|
92
|
+
};
|
|
93
|
+
export var mangleEntireProgram = function (ast, node, engine) {
|
|
94
|
+
renameBindings(ast.scopes[0], function (name, n) {
|
|
95
|
+
return n.doNotDescope ? name : mangleVar(name, engine, node);
|
|
96
|
+
});
|
|
97
|
+
mangleMainFn(ast, node);
|
|
98
|
+
};
|
|
99
|
+
export var mangleMainFn = function (ast, node) {
|
|
100
|
+
renameFunctions(ast.scopes[0], function (name) {
|
|
101
|
+
return name === 'main' ? nodeName(node) : mangleName(name, node);
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* Create the inputs on a node from the properties. This used to be done at
|
|
106
|
+
* context time. Doing it at node creation time lets us auto-bake edges into
|
|
107
|
+
* the node at initial graph creation time.
|
|
108
|
+
*/
|
|
109
|
+
export var prepopulatePropertyInputs = function (node) { return (__assign(__assign({}, node), { inputs: __spreadArray(__spreadArray([], __read(node.inputs), false), __read((node.config.properties || []).map(function (property) {
|
|
110
|
+
return nodeInput(property.displayName, "property_".concat(property.property), 'property', property.type, new Set(['data']), !!property.fillerName, // bakeable
|
|
111
|
+
property.property);
|
|
112
|
+
})), false) })); };
|
|
113
|
+
/**
|
|
114
|
+
* Recursively filter the graph, starting from a specific node, looking for
|
|
115
|
+
* nodes and edges that match predicates. This function returns the inputs for
|
|
116
|
+
* matched edges, not the edges themselves, as a convenience for the only
|
|
117
|
+
* consumer of this function, which is finding input names to use as uniforms.
|
|
118
|
+
*
|
|
119
|
+
* Inputs can only be filtered if the graph context has been computed, since
|
|
120
|
+
* inputs aren't created until then.
|
|
121
|
+
*/
|
|
122
|
+
export var filterGraphFromNode = function (graph, node, predicates, depth) {
|
|
123
|
+
var _a;
|
|
124
|
+
if (depth === void 0) { depth = Infinity; }
|
|
125
|
+
var inputs = node.inputs;
|
|
126
|
+
var inputEdges = graph.edges.filter(function (edge) { return edge.to === node.id; });
|
|
127
|
+
var nodeAcc = __assign({}, (predicates.node && predicates.node(node, inputEdges)
|
|
128
|
+
? (_a = {}, _a[node.id] = node, _a) : {}));
|
|
129
|
+
return inputEdges.reduce(function (acc, inputEdge) {
|
|
130
|
+
var _a;
|
|
131
|
+
var input = inputs.find(function (i) { return i.id === inputEdge.input; });
|
|
132
|
+
var fromNode = inputEdge
|
|
133
|
+
? ensure(graph.nodes.find(function (_a) {
|
|
134
|
+
var id = _a.id;
|
|
135
|
+
return id === inputEdge.from;
|
|
136
|
+
}))
|
|
137
|
+
: undefined;
|
|
138
|
+
var inputAcc = __assign(__assign({}, acc.inputs), (input &&
|
|
139
|
+
predicates.input &&
|
|
140
|
+
predicates.input(input, node, inputEdge, fromNode)
|
|
141
|
+
? (_a = {}, _a[node.id] = __spreadArray(__spreadArray([], __read((acc.inputs[node.id] || [])), false), [input], false), _a) : {}));
|
|
142
|
+
if (inputEdge && fromNode && depth > 1) {
|
|
143
|
+
var result = filterGraphFromNode(graph, fromNode, predicates, depth - 1);
|
|
144
|
+
return {
|
|
145
|
+
nodes: __assign(__assign({}, acc.nodes), result.nodes),
|
|
146
|
+
inputs: __assign(__assign(__assign({}, acc.inputs), inputAcc), result.inputs),
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
return __assign(__assign({}, acc), { inputs: __assign(__assign({}, acc.inputs), inputAcc) });
|
|
150
|
+
}, { inputs: {}, nodes: nodeAcc });
|
|
151
|
+
};
|
|
152
|
+
export var collectConnectedNodes = function (graph, node) {
|
|
153
|
+
return filterGraphFromNode(graph, node, { node: function () { return true; } }).nodes;
|
|
154
|
+
};
|
|
155
|
+
export var filterGraphNodes = function (graph, nodes, filter, depth) {
|
|
156
|
+
if (depth === void 0) { depth = Infinity; }
|
|
157
|
+
return nodes.reduce(function (acc, node) {
|
|
158
|
+
var result = filterGraphFromNode(graph, node, filter, depth);
|
|
159
|
+
return {
|
|
160
|
+
nodes: __assign(__assign({}, acc.nodes), result.nodes),
|
|
161
|
+
inputs: __assign(__assign({}, acc.inputs), result.inputs),
|
|
162
|
+
};
|
|
163
|
+
}, {
|
|
164
|
+
nodes: {},
|
|
165
|
+
inputs: {},
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
// before data inputs were known by the input.category being node or data. I
|
|
169
|
+
// tried updating inputs to have acepts: [code|data] and "baked" now is there a
|
|
170
|
+
// way to know if we're plugging in code or data?
|
|
171
|
+
export var isDataInput = function (input) {
|
|
172
|
+
return (input.type === 'uniform' || input.type === 'property') && !input.baked;
|
|
173
|
+
};
|
|
174
|
+
export var compileNode = function (engine, graph, edges, engineContext, node, activeIds) {
|
|
175
|
+
var _a, _b;
|
|
176
|
+
if (activeIds === void 0) { activeIds = {}; }
|
|
177
|
+
// THIS DUPLICATES OTHER LINE
|
|
178
|
+
var parser = __assign(__assign({}, (coreParsers[node.type] || coreParsers[NodeType.SOURCE])), (engine.parsers[node.type] || {}));
|
|
179
|
+
var inputs = node.inputs;
|
|
180
|
+
if (!parser) {
|
|
181
|
+
console.error(node);
|
|
182
|
+
throw new Error("No parser found for ".concat(node.name, " (").concat(node.type, ", id ").concat(node.id, ")"));
|
|
183
|
+
}
|
|
184
|
+
var nodeContext = isDataNode(node)
|
|
185
|
+
? null
|
|
186
|
+
: ensure(engineContext.nodes[node.id], "No node context found for \"".concat(node.name, "\" (id ").concat(node.id, ")!"));
|
|
187
|
+
var _c = (nodeContext || {}), ast = _c.ast, inputFillers = _c.inputFillers;
|
|
188
|
+
if (!inputs) {
|
|
189
|
+
throw new Error("I'm drunk and I think this case should be impossible");
|
|
190
|
+
}
|
|
191
|
+
var compiledIds = activeIds;
|
|
192
|
+
var inputEdges = edges.filter(function (edge) { return edge.to === node.id; });
|
|
193
|
+
if (inputEdges.length) {
|
|
194
|
+
var continuation_1 = emptyShaderSections();
|
|
195
|
+
inputEdges
|
|
196
|
+
.map(function (edge) { return ({
|
|
197
|
+
edge: edge,
|
|
198
|
+
fromNode: ensure(graph.nodes.find(function (node) { return edge.from === node.id; }), "GraphNode for edge ".concat(edge.from, " not found")),
|
|
199
|
+
input: ensure(inputs.find(function (_a) {
|
|
200
|
+
var id = _a.id;
|
|
201
|
+
return id == edge.input;
|
|
202
|
+
}), "GraphNode \"".concat(node.name, "\" has no input ").concat(edge.input, "!\nAvailable:").concat(inputs.map(function (_a) {
|
|
203
|
+
var id = _a.id;
|
|
204
|
+
return id;
|
|
205
|
+
}).join(', '))),
|
|
206
|
+
}); })
|
|
207
|
+
.filter(function (_a) {
|
|
208
|
+
var input = _a.input;
|
|
209
|
+
return !isDataInput(input);
|
|
210
|
+
})
|
|
211
|
+
.forEach(function (_a) {
|
|
212
|
+
var _b;
|
|
213
|
+
var fromNode = _a.fromNode, edge = _a.edge, input = _a.input;
|
|
214
|
+
var _c = __read(compileNode(engine, graph, edges, engineContext, fromNode, activeIds), 3), inputSections = _c[0], fillerAst = _c[1], childIds = _c[2];
|
|
215
|
+
if (!fillerAst) {
|
|
216
|
+
throw new TypeError("Expected a filler ast from node ID ".concat(fromNode.id, " (").concat(fromNode.type, ") but none was returned"));
|
|
217
|
+
}
|
|
218
|
+
continuation_1 = mergeShaderSections(continuation_1, inputSections);
|
|
219
|
+
compiledIds = __assign(__assign({}, compiledIds), childIds);
|
|
220
|
+
var filler;
|
|
221
|
+
var fillerName;
|
|
222
|
+
if (nodeContext) {
|
|
223
|
+
if (input.property) {
|
|
224
|
+
fillerName = ensure((_b = (node.config.properties || []).find(function (p) { return p.property === input.property; })) === null || _b === void 0 ? void 0 : _b.fillerName, "Node \"".concat(node.name, "\" has no property named \"").concat(input.property, "\" to find the filler for"));
|
|
225
|
+
filler = inputFillers[fillerName];
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
filler = inputFillers[input.id];
|
|
229
|
+
}
|
|
230
|
+
if (!filler) {
|
|
231
|
+
console.error('No filler for property', {
|
|
232
|
+
input: input,
|
|
233
|
+
node: node,
|
|
234
|
+
inputFillers: inputFillers,
|
|
235
|
+
fillerName: fillerName,
|
|
236
|
+
});
|
|
237
|
+
throw new Error("Node \"".concat(node.name, "\" has no filler for input \"").concat(input.displayName, "\" named ").concat(fillerName));
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* +------+ +------+
|
|
241
|
+
* a -- o add o -- o tex |
|
|
242
|
+
* b -- o | +------+
|
|
243
|
+
* +------+
|
|
244
|
+
*
|
|
245
|
+
* This could produce:
|
|
246
|
+
* main_a(v1) + main_b(v2)
|
|
247
|
+
* I guess it has to? or it could produce
|
|
248
|
+
* function add(v1) { return main_a(v1) + main_b(v2); }
|
|
249
|
+
* It can't replace the arg _expression_ in the from shaders, because
|
|
250
|
+
* the expression isn't available there.
|
|
251
|
+
*/
|
|
252
|
+
// TODO: This is a hard coded hack for vUv backfilling. It works in
|
|
253
|
+
// the simple case. Doesn't work for hell (based on world position).
|
|
254
|
+
if (filler.backfillArgs &&
|
|
255
|
+
!Array.isArray(fillerAst) &&
|
|
256
|
+
fillerAst.type === 'function_call') {
|
|
257
|
+
// Object.values(filterGraphFromNode(graph, node, {
|
|
258
|
+
// node: (n) => n.type === 'source'
|
|
259
|
+
// }).nodes).forEach(sourceNode => {
|
|
260
|
+
if (fromNode.type === 'source') {
|
|
261
|
+
// @ts-ignore
|
|
262
|
+
fillerAst.args = filler.backfillArgs;
|
|
263
|
+
// const fc = engineContext.nodes[sourceNode.id];
|
|
264
|
+
var fc = engineContext.nodes[fromNode.id];
|
|
265
|
+
var main = Object.values(fc.ast.scopes[0].functions.main)[0].declaration;
|
|
266
|
+
main.prototype.parameters = [
|
|
267
|
+
'vec2 vv',
|
|
268
|
+
];
|
|
269
|
+
// @ts-ignore
|
|
270
|
+
var scope = fc.ast.scopes[0];
|
|
271
|
+
renameBindings(scope, function (name, node) {
|
|
272
|
+
return node.type !== 'declaration' && name === 'vUv'
|
|
273
|
+
? 'vv'
|
|
274
|
+
: name;
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
// })
|
|
278
|
+
}
|
|
279
|
+
// Fill in the input! The return value is the new AST of the filled in
|
|
280
|
+
// fromNode.
|
|
281
|
+
nodeContext.ast = filler.filler(fillerAst);
|
|
282
|
+
}
|
|
283
|
+
// log(generate(ast.program));
|
|
284
|
+
});
|
|
285
|
+
// Order matters here! *Prepend* the input nodes to this one, because
|
|
286
|
+
// you have to declare functions in order of use in GLSL
|
|
287
|
+
var sections = mergeShaderSections(continuation_1, isDataNode(node) ||
|
|
288
|
+
node.sourceType === SourceType.EXPRESSION ||
|
|
289
|
+
node.sourceType === SourceType.FN_BODY_FRAGMENT
|
|
290
|
+
? emptyShaderSections()
|
|
291
|
+
: findShaderSections(ast));
|
|
292
|
+
var filler = isDataNode(node)
|
|
293
|
+
? makeExpression(toGlsl(node))
|
|
294
|
+
: parser.produceFiller(node, ast);
|
|
295
|
+
return [sections, filler, __assign(__assign({}, compiledIds), (_a = {}, _a[node.id] = node, _a))];
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
// TODO: This duplicates the above branch, and also does this mean we
|
|
299
|
+
// recalculate the shader sections and filler for every edge? Can I move
|
|
300
|
+
// these lines above the loop?
|
|
301
|
+
var sections = isDataNode(node) ||
|
|
302
|
+
node.sourceType === SourceType.EXPRESSION ||
|
|
303
|
+
node.sourceType === SourceType.FN_BODY_FRAGMENT
|
|
304
|
+
? emptyShaderSections()
|
|
305
|
+
: findShaderSections(ast);
|
|
306
|
+
var filler = isDataNode(node)
|
|
307
|
+
? makeExpression(toGlsl(node))
|
|
308
|
+
: parser.produceFiller(node, ast);
|
|
309
|
+
return [sections, filler, __assign(__assign({}, compiledIds), (_b = {}, _b[node.id] = node, _b))];
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
export var compileGraph = function (engineContext, engine, graph) {
|
|
313
|
+
// computeGraphContext(engineContext, engine, graph);
|
|
314
|
+
var outputFrag = graph.nodes.find(function (node) { return node.type === 'output' && node.stage === 'fragment'; });
|
|
315
|
+
if (!outputFrag) {
|
|
316
|
+
throw new Error('No fragment output in graph');
|
|
317
|
+
}
|
|
318
|
+
var _a = __read(compileNode(engine, graph, graph.edges, engineContext, outputFrag), 3), fragment = _a[0], fragmentIds = _a[2];
|
|
319
|
+
var outputVert = graph.nodes.find(function (node) { return node.type === 'output' && node.stage === 'vertex'; });
|
|
320
|
+
if (!outputVert) {
|
|
321
|
+
throw new Error('No vertex output in graph');
|
|
322
|
+
}
|
|
323
|
+
var vertexIds = collectConnectedNodes(graph, outputVert);
|
|
324
|
+
// Some fragment shaders reference vertex shaders which may not have been
|
|
325
|
+
// given edges in the graph. Build invisible edges from these vertex nodes to
|
|
326
|
+
// the hidden "mainStmts" input on the output node, which inlines the function
|
|
327
|
+
// calls to those vertex main() statements and includes them in the output
|
|
328
|
+
var orphanNodes = graph.nodes.filter(function (node) {
|
|
329
|
+
return isSourceNode(node) &&
|
|
330
|
+
node.stage === 'vertex' &&
|
|
331
|
+
node.nextStageNodeId &&
|
|
332
|
+
fragmentIds[node.nextStageNodeId] &&
|
|
333
|
+
!vertexIds[node.id];
|
|
334
|
+
});
|
|
335
|
+
var orphanEdges = orphanNodes.map(function (node) { return ({
|
|
336
|
+
id: makeId(),
|
|
337
|
+
from: node.id,
|
|
338
|
+
to: outputVert.id,
|
|
339
|
+
output: 'main',
|
|
340
|
+
input: "filler_".concat(MAGIC_OUTPUT_STMTS),
|
|
341
|
+
stage: 'vertex',
|
|
342
|
+
category: 'code',
|
|
343
|
+
}); });
|
|
344
|
+
var _b = __read(compileNode(engine, graph, __spreadArray(__spreadArray([], __read(graph.edges), false), __read(orphanEdges), false), engineContext, outputVert), 2), vertex = _b[0];
|
|
345
|
+
// Every compileNode returns the AST so far, as well as the filler for the
|
|
346
|
+
// next node with inputs. On the final step, we discard the filler
|
|
347
|
+
return {
|
|
348
|
+
fragment: fragment,
|
|
349
|
+
vertex: vertex,
|
|
350
|
+
outputFrag: outputFrag,
|
|
351
|
+
outputVert: outputVert,
|
|
352
|
+
orphanNodes: orphanNodes,
|
|
353
|
+
activeNodeIds: new Set(__spreadArray(__spreadArray(__spreadArray([], __read(Object.keys(vertexIds)), false), __read(Object.keys(fragmentIds)), false), __read(orphanNodes.map(function (node) { return node.id; })), false)),
|
|
354
|
+
};
|
|
355
|
+
};
|
|
356
|
+
/**
|
|
357
|
+
* Find engine nodes to set properties on, like find a Physical node so
|
|
358
|
+
* consumers can set physicalNode.myProperty = 123.
|
|
359
|
+
*
|
|
360
|
+
* Finds all active nodes in the graph that have inputs that are properties,
|
|
361
|
+
* which currently means it will find all active engine nodes.
|
|
362
|
+
*/
|
|
363
|
+
export var collectNodeProperties = function (graph) {
|
|
364
|
+
var nodesWithProperties = {
|
|
365
|
+
node: function (node) {
|
|
366
|
+
var _a;
|
|
367
|
+
return 'config' in node &&
|
|
368
|
+
'properties' in node.config &&
|
|
369
|
+
!!((_a = node.config.properties) === null || _a === void 0 ? void 0 : _a.length);
|
|
370
|
+
},
|
|
371
|
+
input: function (input) { return !!input.property; },
|
|
372
|
+
};
|
|
373
|
+
var outputFrag = graph.nodes.find(function (node) { return node.type === 'output' && node.stage === 'fragment'; });
|
|
374
|
+
var outputVert = graph.nodes.find(function (node) { return node.type === 'output' && node.stage === 'vertex'; });
|
|
375
|
+
var fragProperties = filterGraphFromNode(graph, outputFrag, nodesWithProperties);
|
|
376
|
+
var vertProperties = filterGraphFromNode(graph, outputVert, nodesWithProperties);
|
|
377
|
+
return {
|
|
378
|
+
nodes: __assign(__assign({}, fragProperties.nodes), vertProperties.nodes),
|
|
379
|
+
inputs: __assign(__assign({}, fragProperties.inputs), vertProperties.inputs),
|
|
380
|
+
};
|
|
381
|
+
};
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import util from 'util';
|
|
2
|
+
import { parser } from '@shaderfrog/glsl-parser';
|
|
3
|
+
import { generate } from '@shaderfrog/glsl-parser';
|
|
4
|
+
import { shaderSectionsToProgram } from './ast/shader-sections';
|
|
5
|
+
import { addNode } from './nodes/engine-node';
|
|
6
|
+
import { mergeShaderSections, findShaderSections } from './ast/shader-sections';
|
|
7
|
+
import { numberNode } from './nodes/data-nodes';
|
|
8
|
+
import { makeEdge } from './nodes/edge';
|
|
9
|
+
import { evaluateNode } from './evaluate';
|
|
10
|
+
var inspect = function (thing) {
|
|
11
|
+
return console.log(util.inspect(thing, false, null, true));
|
|
12
|
+
};
|
|
13
|
+
var mergeBlocks = function (ast1, ast2) {
|
|
14
|
+
var s1 = findShaderSections(ast1);
|
|
15
|
+
var s2 = findShaderSections(ast2);
|
|
16
|
+
var merged = mergeShaderSections(s1, s2);
|
|
17
|
+
return generate(shaderSectionsToProgram(merged, {
|
|
18
|
+
includePrecisions: true,
|
|
19
|
+
includeVersion: true,
|
|
20
|
+
}));
|
|
21
|
+
};
|
|
22
|
+
var dedupe = function (code) {
|
|
23
|
+
return generate(shaderSectionsToProgram(findShaderSections(parser.parse(code)), {
|
|
24
|
+
includePrecisions: true,
|
|
25
|
+
includeVersion: true,
|
|
26
|
+
}));
|
|
27
|
+
};
|
|
28
|
+
var counter = 0;
|
|
29
|
+
var p = { x: 0, y: 0 };
|
|
30
|
+
var id = function () { return '' + counter++; };
|
|
31
|
+
var constructor = function () { return ({
|
|
32
|
+
config: {
|
|
33
|
+
version: 3,
|
|
34
|
+
preprocess: false,
|
|
35
|
+
strategies: [],
|
|
36
|
+
uniforms: [],
|
|
37
|
+
},
|
|
38
|
+
id: '1',
|
|
39
|
+
name: '1',
|
|
40
|
+
type: '',
|
|
41
|
+
inputs: [],
|
|
42
|
+
outputs: [],
|
|
43
|
+
position: { x: 0, y: 0 },
|
|
44
|
+
source: '',
|
|
45
|
+
stage: undefined,
|
|
46
|
+
groupId: null,
|
|
47
|
+
}); };
|
|
48
|
+
var engine = {
|
|
49
|
+
name: 'three',
|
|
50
|
+
evaluateNode: function (node) {
|
|
51
|
+
if (node.type === 'number') {
|
|
52
|
+
return parseFloat(node.value);
|
|
53
|
+
}
|
|
54
|
+
return node.value;
|
|
55
|
+
},
|
|
56
|
+
constructors: {
|
|
57
|
+
physical: constructor,
|
|
58
|
+
toon: constructor,
|
|
59
|
+
},
|
|
60
|
+
mergeOptions: {
|
|
61
|
+
includePrecisions: true,
|
|
62
|
+
includeVersion: true,
|
|
63
|
+
},
|
|
64
|
+
importers: {},
|
|
65
|
+
preserve: new Set(),
|
|
66
|
+
parsers: {},
|
|
67
|
+
};
|
|
68
|
+
// it('graph compiler arbitrary helper test', () => {
|
|
69
|
+
// const graph: Graph = {
|
|
70
|
+
// nodes: [
|
|
71
|
+
// outputNode('0', 'Output v', p, 'vertex'),
|
|
72
|
+
// outputNode('1', 'Output f', p, 'fragment'),
|
|
73
|
+
// makeSourceNode(
|
|
74
|
+
// '2',
|
|
75
|
+
// `uniform sampler2D image1;
|
|
76
|
+
// uniform sampler2D image2;
|
|
77
|
+
// void main() {
|
|
78
|
+
// vec3 col = texture2D(image1, posTurn - 0.4 * time).rgb + 1.0;
|
|
79
|
+
// vec3 col = texture2D(image2, negTurn - 0.4 * time).rgb + 2.0;
|
|
80
|
+
// }
|
|
81
|
+
// `,
|
|
82
|
+
// 'fragment'
|
|
83
|
+
// ),
|
|
84
|
+
// makeSourceNode(
|
|
85
|
+
// '3',
|
|
86
|
+
// `void main() {
|
|
87
|
+
// return vec4(0.0);
|
|
88
|
+
// }
|
|
89
|
+
// `,
|
|
90
|
+
// 'fragment'
|
|
91
|
+
// ),
|
|
92
|
+
// makeSourceNode(
|
|
93
|
+
// '4',
|
|
94
|
+
// `void main() {
|
|
95
|
+
// return vec4(1.0);
|
|
96
|
+
// }
|
|
97
|
+
// `,
|
|
98
|
+
// 'fragment'
|
|
99
|
+
// ),
|
|
100
|
+
// ],
|
|
101
|
+
// edges: [
|
|
102
|
+
// makeEdge(id(), '2', '1', 'out', 'filler_frogFragOut', 'fragment'),
|
|
103
|
+
// makeEdge(id(), '3', '2', 'out', 'filler_image1', 'fragment'),
|
|
104
|
+
// makeEdge(id(), '4', '2', 'out', 'filler_image2', 'fragment'),
|
|
105
|
+
// ],
|
|
106
|
+
// };
|
|
107
|
+
// const engineContext: EngineContext = {
|
|
108
|
+
// engine: 'three',
|
|
109
|
+
// nodes: {},
|
|
110
|
+
// runtime: {},
|
|
111
|
+
// debuggingNonsense: {},
|
|
112
|
+
// };
|
|
113
|
+
// const result = compileGraph(engineContext, engine, graph);
|
|
114
|
+
// const built = generate(
|
|
115
|
+
// shaderSectionsToProgram(result.fragment, {
|
|
116
|
+
// includePrecisions: true,
|
|
117
|
+
// includeVersion: true,
|
|
118
|
+
// }).program
|
|
119
|
+
// );
|
|
120
|
+
// expect(built).toBe('hi');
|
|
121
|
+
// });
|
|
122
|
+
describe('evaluateNode()', function () {
|
|
123
|
+
it('evaluates binary nodes', function () {
|
|
124
|
+
var finalAdd = addNode(id(), p);
|
|
125
|
+
var add2 = addNode(id(), p);
|
|
126
|
+
var num1 = numberNode(id(), 'number', p, '3');
|
|
127
|
+
var num2 = numberNode(id(), 'number', p, '5');
|
|
128
|
+
var num3 = numberNode(id(), 'number', p, '7');
|
|
129
|
+
var graph = {
|
|
130
|
+
nodes: [num1, num2, num3, finalAdd, add2],
|
|
131
|
+
edges: [
|
|
132
|
+
makeEdge(id(), num1.id, finalAdd.id, 'out', 'a'),
|
|
133
|
+
makeEdge(id(), add2.id, finalAdd.id, 'out', 'b'),
|
|
134
|
+
makeEdge(id(), num2.id, add2.id, 'out', 'a'),
|
|
135
|
+
makeEdge(id(), num3.id, add2.id, 'out', 'b'),
|
|
136
|
+
],
|
|
137
|
+
};
|
|
138
|
+
expect(evaluateNode(engine, graph, finalAdd)).toBe(15);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
it('should merge uniforms with interface blocks', function () {
|
|
142
|
+
var astX = parser.parse("uniform vec2 x;");
|
|
143
|
+
var astY = parser.parse("uniform vec2 y, z;\nuniform vec3 a;");
|
|
144
|
+
expect(mergeBlocks(astX, astY)).toEqual("uniform vec2 x, y, z;\nuniform vec3 a;\n");
|
|
145
|
+
var astL01 = parser.parse("uniform Light0 { vec4 y; } x;", { quiet: true });
|
|
146
|
+
var astL02 = parser.parse("uniform Light0 { vec4 y; } x;", { quiet: true });
|
|
147
|
+
expect(mergeBlocks(astL01, astL02)).toEqual("uniform Light0 { vec4 y; } x;\n");
|
|
148
|
+
var astL001 = parser.parse("uniform Light0 { vec4 y; } x;", {
|
|
149
|
+
quiet: true,
|
|
150
|
+
});
|
|
151
|
+
var astL002 = parser.parse("uniform Light0 x;", { quiet: true });
|
|
152
|
+
expect(mergeBlocks(astL001, astL002)).toEqual("uniform Light0 { vec4 y; } x;\n");
|
|
153
|
+
var astLo01 = parser.parse("uniform Light0 x;", { quiet: true });
|
|
154
|
+
var astLo02 = parser.parse("uniform Light0 { vec4 y; } x;", {
|
|
155
|
+
quiet: true,
|
|
156
|
+
});
|
|
157
|
+
expect(mergeBlocks(astLo01, astLo02)).toEqual("uniform Light0 { vec4 y; } x;\n");
|
|
158
|
+
// This may be a bug, look at how the uniforms are merged. I at least want to
|
|
159
|
+
// note its current behavior in this test
|
|
160
|
+
var vec2Arr1 = parser.parse("uniform vec2 y[5];");
|
|
161
|
+
var vec2Arr2 = parser.parse("uniform vec2 y[10];");
|
|
162
|
+
expect(mergeBlocks(vec2Arr1, vec2Arr2)).toEqual("uniform vec2 y[10];\n");
|
|
163
|
+
var block1 = parser.parse("uniform Scene { mat4 view; };");
|
|
164
|
+
var block2 = parser.parse("uniform Scene { mat4 view; };");
|
|
165
|
+
expect(mergeBlocks(block1, block2)).toEqual("uniform Scene { mat4 view; };\n");
|
|
166
|
+
// Verify these lines are preserved (they go through dedupeUniforms)
|
|
167
|
+
expect(dedupe("layout(std140,column_major) uniform;")).toEqual("layout(std140,column_major) uniform;");
|
|
168
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export var mapInputName = function (node, _a) {
|
|
2
|
+
var _b, _c;
|
|
3
|
+
var id = _a.id, displayName = _a.displayName;
|
|
4
|
+
return ((_c = (_b = node.config) === null || _b === void 0 ? void 0 : _b.inputMapping) === null || _c === void 0 ? void 0 : _c[id]) || displayName;
|
|
5
|
+
};
|
|
6
|
+
export var property = function (displayName, property, type, fillerName, defaultValue) { return ({
|
|
7
|
+
displayName: displayName,
|
|
8
|
+
type: type,
|
|
9
|
+
property: property,
|
|
10
|
+
fillerName: fillerName,
|
|
11
|
+
defaultValue: defaultValue,
|
|
12
|
+
}); };
|
|
13
|
+
export var SourceType;
|
|
14
|
+
(function (SourceType) {
|
|
15
|
+
SourceType["EXPRESSION"] = "Expression";
|
|
16
|
+
SourceType["FN_BODY_FRAGMENT"] = "Function Body Fragment";
|
|
17
|
+
SourceType["SHADER_FRAGMENT"] = "Shader Fragment";
|
|
18
|
+
})(SourceType || (SourceType = {}));
|