@shaderfrog/core 0.0.2 → 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.
Files changed (56) hide show
  1. package/README.md +184 -1
  2. package/dist/ast/manipulate.js +328 -0
  3. package/dist/ast/shader-sections.js +256 -0
  4. package/dist/context.js +230 -0
  5. package/dist/engine.js +209 -0
  6. package/dist/evaluate.js +27 -0
  7. package/dist/graph-types.js +7 -0
  8. package/dist/graph.js +381 -0
  9. package/dist/graph.test.js +168 -0
  10. package/dist/nodes/code-nodes.js +18 -0
  11. package/dist/nodes/core-node.js +9 -0
  12. package/dist/nodes/data-nodes.js +123 -0
  13. package/dist/nodes/edge.js +1 -0
  14. package/dist/nodes/engine-node.js +189 -0
  15. package/dist/parsers.js +213 -0
  16. package/dist/plugins/babylon/bablyengine.js +582 -0
  17. package/dist/plugins/babylon/importers.js +64 -0
  18. package/{src/plugins/babylon/index.ts → dist/plugins/babylon/index.js} +0 -1
  19. package/dist/plugins/playcanvas/importers.js +28 -0
  20. package/dist/plugins/playcanvas/index.js +2 -0
  21. package/dist/plugins/playcanvas/playengine.js +510 -0
  22. package/dist/plugins/three/importers.js +15 -0
  23. package/{src/plugins/three/index.ts → dist/plugins/three/index.js} +0 -1
  24. package/dist/plugins/three/threngine.js +495 -0
  25. package/dist/strategy/assignemntTo.js +26 -0
  26. package/dist/strategy/declarationOf.js +23 -0
  27. package/dist/strategy/hardCode.js +23 -0
  28. package/dist/strategy/index.js +38 -0
  29. package/dist/strategy/inject.js +122 -0
  30. package/dist/strategy/namedAttribute.js +48 -0
  31. package/dist/strategy/texture2D.js +83 -0
  32. package/dist/strategy/uniform.js +190 -0
  33. package/dist/strategy/variable.js +80 -0
  34. package/dist/stratgies.test.js +164 -0
  35. package/dist/util/ast.js +9 -0
  36. package/dist/util/ensure.js +7 -0
  37. package/dist/util/id.js +2 -0
  38. package/package.json +12 -4
  39. package/src/ast/manipulate.ts +0 -392
  40. package/src/ast/shader-sections.ts +0 -323
  41. package/src/core/engine.ts +0 -214
  42. package/src/core/file.js +0 -53
  43. package/src/core/graph.ts +0 -1007
  44. package/src/core/nodes/code-nodes.ts +0 -66
  45. package/src/core/nodes/core-node.ts +0 -48
  46. package/src/core/nodes/data-nodes.ts +0 -344
  47. package/src/core/nodes/edge.ts +0 -23
  48. package/src/core/nodes/engine-node.ts +0 -266
  49. package/src/core/strategy.ts +0 -520
  50. package/src/core.test.ts +0 -312
  51. package/src/plugins/babylon/bablyengine.ts +0 -670
  52. package/src/plugins/babylon/importers.ts +0 -69
  53. package/src/plugins/three/importers.ts +0 -18
  54. package/src/plugins/three/threngine.tsx +0 -571
  55. package/src/util/ensure.ts +0 -10
  56. package/src/util/id.ts +0 -2
@@ -0,0 +1,213 @@
1
+ var __read = (this && this.__read) || function (o, n) {
2
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
3
+ if (!m) return o;
4
+ var i = m.call(o), r, ar = [], e;
5
+ try {
6
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
7
+ }
8
+ catch (error) { e = { error: error }; }
9
+ finally {
10
+ try {
11
+ if (r && !r.done && (m = i["return"])) m.call(i);
12
+ }
13
+ finally { if (e) throw e.error; }
14
+ }
15
+ return ar;
16
+ };
17
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
18
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
19
+ if (ar || !(i in from)) {
20
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
21
+ ar[i] = from[i];
22
+ }
23
+ }
24
+ return to.concat(ar || Array.prototype.slice.call(from));
25
+ };
26
+ var _a;
27
+ import { generate, parser } from '@shaderfrog/glsl-parser';
28
+ import { visit, } from '@shaderfrog/glsl-parser/ast';
29
+ import preprocess from '@shaderfrog/glsl-parser/preprocessor';
30
+ import { convert300MainToReturn, from2To3, makeExpression, makeExpressionWithScopes, makeFnBodyStatementWithScopes, makeFnStatement, } from './ast/manipulate';
31
+ import { applyStrategy } from './strategy';
32
+ import { SourceType } from './nodes/code-nodes';
33
+ import { nodeInput } from './nodes/core-node';
34
+ import { MAGIC_OUTPUT_STMTS, NodeType } from './graph-types';
35
+ import { nodeName } from './graph';
36
+ import { generateFiller } from './util/ast';
37
+ /*
38
+ * Core graph parsers, which is the plumbing/interface the graph and context
39
+ * calls into, to parse, find inputs, etc, and define this per-node type.
40
+ */
41
+ var log = function () {
42
+ var _a;
43
+ var args = [];
44
+ for (var _i = 0; _i < arguments.length; _i++) {
45
+ args[_i] = arguments[_i];
46
+ }
47
+ return (_a = console.log).call.apply(_a, __spreadArray([console, '\x1b[31m(core.parsers)\x1b[0m'], __read(args), false));
48
+ };
49
+ export var alphabet = 'abcdefghijklmnopqrstuvwxyz';
50
+ export var coreParsers = (_a = {},
51
+ _a[NodeType.SOURCE] = {
52
+ produceAst: function (engineContext, engine, graph, node, inputEdges) {
53
+ var ast;
54
+ // @ts-ignore
55
+ if (node.expressionOnly) {
56
+ node.sourceType = SourceType.EXPRESSION;
57
+ // @ts-ignore
58
+ delete node.expressionOnly;
59
+ }
60
+ if (node.sourceType === SourceType.FN_BODY_FRAGMENT) {
61
+ var _a = makeFnBodyStatementWithScopes(node.source), statements = _a.statements, scope = _a.scope;
62
+ ast = {
63
+ type: 'program',
64
+ scopes: [scope],
65
+ // @ts-ignore
66
+ program: statements,
67
+ };
68
+ }
69
+ else if (node.sourceType === SourceType.EXPRESSION) {
70
+ var _b = makeExpressionWithScopes(node.source), expression = _b.expression, scope = _b.scope;
71
+ ast = {
72
+ type: 'program',
73
+ scopes: [scope],
74
+ // @ts-ignore
75
+ program: [expression],
76
+ };
77
+ }
78
+ else {
79
+ var preprocessed = node.config.preprocess === false
80
+ ? node.source
81
+ : preprocess(node.source, {
82
+ preserve: {
83
+ version: function () { return true; },
84
+ },
85
+ });
86
+ ast = parser.parse(preprocessed);
87
+ if (node.config.version === 2 && node.stage) {
88
+ from2To3(ast, node.stage);
89
+ log('converted ', node, 'to version 3', {
90
+ code: generate(ast),
91
+ });
92
+ }
93
+ // This assumes that expressionOnly nodes don't have a stage and that all
94
+ // fragment source code shades have main function, which is probably wrong
95
+ if (node.stage === 'fragment') {
96
+ convert300MainToReturn(node.id, ast);
97
+ }
98
+ }
99
+ return ast;
100
+ },
101
+ findInputs: function (engineContext, node, ast) {
102
+ var seen = new Set();
103
+ return node.config.strategies
104
+ .flatMap(function (strategy) { return applyStrategy(strategy, node, ast); })
105
+ .filter(function (_a) {
106
+ var _b = __read(_a, 2), input = _b[0], _ = _b[1];
107
+ if (!seen.has(input.id)) {
108
+ seen.add(input.id);
109
+ return true;
110
+ }
111
+ return false;
112
+ });
113
+ },
114
+ produceFiller: function (node, ast) {
115
+ return node.sourceType === SourceType.EXPRESSION
116
+ ? ast.program[0]
117
+ : node.sourceType === SourceType.FN_BODY_FRAGMENT
118
+ ? ast.program
119
+ : makeExpression("".concat(nodeName(node), "()"));
120
+ },
121
+ },
122
+ // TODO: Output node assumes strategies are still passed in on node creation,
123
+ // which might be a little awkward for graph creators?
124
+ _a[NodeType.OUTPUT] = {
125
+ produceAst: function (engineContext, engine, graph, node, inputEdges) {
126
+ return parser.parse(node.source);
127
+ },
128
+ findInputs: function (engineContext, node, ast) {
129
+ return __spreadArray(__spreadArray([], __read(node.config.strategies.flatMap(function (strategy) {
130
+ return applyStrategy(strategy, node, ast);
131
+ })), false), [
132
+ [
133
+ nodeInput(MAGIC_OUTPUT_STMTS, "filler_".concat(MAGIC_OUTPUT_STMTS), 'filler', 'rgba', new Set(['code']), false),
134
+ function (fillerAst) {
135
+ var fn = ast.program.find(function (stmt) { return stmt.type === 'function'; });
136
+ fn === null || fn === void 0 ? void 0 : fn.body.statements.unshift(makeFnStatement(generateFiller(fillerAst)));
137
+ return ast;
138
+ },
139
+ ],
140
+ ], false);
141
+ },
142
+ produceFiller: function (node, ast) {
143
+ return makeExpression('impossible_call()');
144
+ },
145
+ },
146
+ _a[NodeType.BINARY] = {
147
+ produceAst: function (engineContext, engine, graph, iNode, inputEdges) {
148
+ var node = iNode;
149
+ return makeExpression('(' +
150
+ (inputEdges.length
151
+ ? inputEdges
152
+ .map(function (_, index) { return alphabet.charAt(index); })
153
+ .join(" ".concat(node.operator, " "))
154
+ : "a ".concat(node.operator, " b")) +
155
+ ')');
156
+ },
157
+ findInputs: function (engineContext, node, ast, inputEdges) {
158
+ return new Array(Math.max(inputEdges.length + 1, 2))
159
+ .fill(0)
160
+ .map(function (_, index) {
161
+ var letter = alphabet.charAt(index);
162
+ return [
163
+ nodeInput(letter, letter, 'filler', undefined, new Set(['data', 'code']), false),
164
+ function (fillerAst) {
165
+ var foundPath;
166
+ var visitors = {
167
+ identifier: {
168
+ enter: function (path) {
169
+ if (path.node.identifier === letter) {
170
+ foundPath = path;
171
+ }
172
+ },
173
+ },
174
+ };
175
+ visit(ast, visitors);
176
+ if (!foundPath) {
177
+ throw new Error("Im drunk and I think this case is impossible, no \"".concat(letter, "\" found in binary node?"));
178
+ }
179
+ if (foundPath.parent && foundPath.key) {
180
+ // @ts-ignore
181
+ foundPath.parent[foundPath.key] = fillerAst;
182
+ return ast;
183
+ }
184
+ else {
185
+ return fillerAst;
186
+ }
187
+ },
188
+ ];
189
+ });
190
+ },
191
+ produceFiller: function (node, ast) {
192
+ return ast;
193
+ },
194
+ evaluate: function (node, inputEdges, inputNodes, evaluateNode) {
195
+ var operator = node.operator;
196
+ return inputNodes.map(evaluateNode).reduce(function (num, next) {
197
+ if (operator === '+') {
198
+ return num + next;
199
+ }
200
+ else if (operator === '*') {
201
+ return num * next;
202
+ }
203
+ else if (operator === '-') {
204
+ return num - next;
205
+ }
206
+ else if (operator === '/') {
207
+ return num / next;
208
+ }
209
+ throw new Error("Don't know how to evaluate ".concat(operator, " for node ").concat(node.name, " (").concat(node.id, ")"));
210
+ });
211
+ },
212
+ },
213
+ _a);