@shaderfrog/core 1.3.1 → 1.3.3

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 (155) hide show
  1. package/package.json +10 -8
  2. package/cjs/engine.js +0 -218
  3. package/cjs/graph/base-node.js +0 -13
  4. package/cjs/graph/code-nodes.js +0 -23
  5. package/cjs/graph/context.js +0 -253
  6. package/cjs/graph/data-nodes.js +0 -146
  7. package/cjs/graph/edge.js +0 -12
  8. package/cjs/graph/evaluate.js +0 -32
  9. package/cjs/graph/graph-node.js +0 -143
  10. package/cjs/graph/graph-types.js +0 -25
  11. package/cjs/graph/graph.js +0 -533
  12. package/cjs/graph/graph.test.js +0 -173
  13. package/cjs/graph/index.js +0 -27
  14. package/cjs/graph/parsers.js +0 -219
  15. package/cjs/graph/shader-sections.js +0 -268
  16. package/cjs/index.js +0 -19
  17. package/cjs/package.json +0 -1
  18. package/cjs/plugins/babylon/bablyengine.js +0 -597
  19. package/cjs/plugins/babylon/importers.js +0 -66
  20. package/cjs/plugins/babylon/index.js +0 -5
  21. package/cjs/plugins/playcanvas/importers.js +0 -30
  22. package/cjs/plugins/playcanvas/index.js +0 -5
  23. package/cjs/plugins/playcanvas/playengine.js +0 -549
  24. package/cjs/plugins/three/importers.js +0 -17
  25. package/cjs/plugins/three/index.js +0 -6
  26. package/cjs/plugins/three/threngine.js +0 -619
  27. package/cjs/strategy/assignemntTo.js +0 -31
  28. package/cjs/strategy/declarationOf.js +0 -28
  29. package/cjs/strategy/hardCode.js +0 -28
  30. package/cjs/strategy/index.js +0 -25
  31. package/cjs/strategy/inject.js +0 -127
  32. package/cjs/strategy/namedAttribute.js +0 -53
  33. package/cjs/strategy/strategy.js +0 -35
  34. package/cjs/strategy/stratgies.test.js +0 -192
  35. package/cjs/strategy/texture2D.js +0 -88
  36. package/cjs/strategy/uniform.js +0 -195
  37. package/cjs/strategy/variable.js +0 -85
  38. package/cjs/util/ast.js +0 -351
  39. package/cjs/util/ensure.js +0 -11
  40. package/cjs/util/id.js +0 -6
  41. package/esm/engine.d.ts +0 -69
  42. package/esm/graph/base-node.d.ts +0 -36
  43. package/esm/graph/code-nodes.d.ts +0 -44
  44. package/esm/graph/context.d.ts +0 -37
  45. package/esm/graph/data-nodes.d.ts +0 -83
  46. package/esm/graph/edge.d.ts +0 -13
  47. package/esm/graph/evaluate.d.ts +0 -9
  48. package/esm/graph/graph-node.d.ts +0 -8
  49. package/esm/graph/graph-types.d.ts +0 -38
  50. package/esm/graph/graph.d.ts +0 -89
  51. package/esm/graph/graph.test.d.ts +0 -1
  52. package/esm/graph/index.d.ts +0 -11
  53. package/esm/graph/parsers.d.ts +0 -39
  54. package/esm/graph/shader-sections.d.ts +0 -47
  55. package/esm/index.d.ts +0 -3
  56. package/esm/package.json +0 -1
  57. package/esm/plugins/babylon/bablyengine.d.ts +0 -28
  58. package/esm/plugins/babylon/importers.d.ts +0 -3
  59. package/esm/plugins/babylon/index.d.ts +0 -2
  60. package/esm/plugins/playcanvas/importers.d.ts +0 -3
  61. package/esm/plugins/playcanvas/index.d.ts +0 -2
  62. package/esm/plugins/playcanvas/playengine.d.ts +0 -39
  63. package/esm/plugins/three/importers.d.ts +0 -3
  64. package/esm/plugins/three/index.d.ts +0 -2
  65. package/esm/plugins/three/threngine.d.ts +0 -34
  66. package/esm/strategy/assignemntTo.d.ts +0 -9
  67. package/esm/strategy/declarationOf.d.ts +0 -9
  68. package/esm/strategy/hardCode.d.ts +0 -15
  69. package/esm/strategy/index.d.ts +0 -9
  70. package/esm/strategy/inject.d.ts +0 -15
  71. package/esm/strategy/namedAttribute.d.ts +0 -9
  72. package/esm/strategy/strategy.d.ts +0 -28
  73. package/esm/strategy/stratgies.test.d.ts +0 -1
  74. package/esm/strategy/texture2D.d.ts +0 -6
  75. package/esm/strategy/uniform.d.ts +0 -6
  76. package/esm/strategy/variable.d.ts +0 -6
  77. package/esm/util/ast.d.ts +0 -30
  78. package/esm/util/ensure.d.ts +0 -1
  79. package/esm/util/id.d.ts +0 -1
  80. /package/{cjs/engine.d.ts → engine.d.ts} +0 -0
  81. /package/{esm/engine.js → engine.js} +0 -0
  82. /package/{cjs/graph → graph}/base-node.d.ts +0 -0
  83. /package/{esm/graph → graph}/base-node.js +0 -0
  84. /package/{cjs/graph → graph}/code-nodes.d.ts +0 -0
  85. /package/{esm/graph → graph}/code-nodes.js +0 -0
  86. /package/{cjs/graph → graph}/context.d.ts +0 -0
  87. /package/{esm/graph → graph}/context.js +0 -0
  88. /package/{cjs/graph → graph}/data-nodes.d.ts +0 -0
  89. /package/{esm/graph → graph}/data-nodes.js +0 -0
  90. /package/{cjs/graph → graph}/edge.d.ts +0 -0
  91. /package/{esm/graph → graph}/edge.js +0 -0
  92. /package/{cjs/graph → graph}/evaluate.d.ts +0 -0
  93. /package/{esm/graph → graph}/evaluate.js +0 -0
  94. /package/{cjs/graph → graph}/graph-node.d.ts +0 -0
  95. /package/{esm/graph → graph}/graph-node.js +0 -0
  96. /package/{cjs/graph → graph}/graph-types.d.ts +0 -0
  97. /package/{esm/graph → graph}/graph-types.js +0 -0
  98. /package/{cjs/graph → graph}/graph.d.ts +0 -0
  99. /package/{esm/graph → graph}/graph.js +0 -0
  100. /package/{cjs/graph → graph}/graph.test.d.ts +0 -0
  101. /package/{esm/graph → graph}/graph.test.js +0 -0
  102. /package/{cjs/graph → graph}/index.d.ts +0 -0
  103. /package/{esm/graph → graph}/index.js +0 -0
  104. /package/{cjs/graph → graph}/parsers.d.ts +0 -0
  105. /package/{esm/graph → graph}/parsers.js +0 -0
  106. /package/{cjs/graph → graph}/shader-sections.d.ts +0 -0
  107. /package/{esm/graph → graph}/shader-sections.js +0 -0
  108. /package/{cjs/index.d.ts → index.d.ts} +0 -0
  109. /package/{esm/index.js → index.js} +0 -0
  110. /package/{cjs/plugins → plugins}/babylon/bablyengine.d.ts +0 -0
  111. /package/{esm/plugins → plugins}/babylon/bablyengine.js +0 -0
  112. /package/{cjs/plugins → plugins}/babylon/importers.d.ts +0 -0
  113. /package/{esm/plugins → plugins}/babylon/importers.js +0 -0
  114. /package/{cjs/plugins → plugins}/babylon/index.d.ts +0 -0
  115. /package/{esm/plugins → plugins}/babylon/index.js +0 -0
  116. /package/{cjs/plugins → plugins}/playcanvas/importers.d.ts +0 -0
  117. /package/{esm/plugins → plugins}/playcanvas/importers.js +0 -0
  118. /package/{cjs/plugins → plugins}/playcanvas/index.d.ts +0 -0
  119. /package/{esm/plugins → plugins}/playcanvas/index.js +0 -0
  120. /package/{cjs/plugins → plugins}/playcanvas/playengine.d.ts +0 -0
  121. /package/{esm/plugins → plugins}/playcanvas/playengine.js +0 -0
  122. /package/{cjs/plugins → plugins}/three/importers.d.ts +0 -0
  123. /package/{esm/plugins → plugins}/three/importers.js +0 -0
  124. /package/{cjs/plugins → plugins}/three/index.d.ts +0 -0
  125. /package/{esm/plugins → plugins}/three/index.js +0 -0
  126. /package/{cjs/plugins → plugins}/three/threngine.d.ts +0 -0
  127. /package/{esm/plugins → plugins}/three/threngine.js +0 -0
  128. /package/{cjs/strategy → strategy}/assignemntTo.d.ts +0 -0
  129. /package/{esm/strategy → strategy}/assignemntTo.js +0 -0
  130. /package/{cjs/strategy → strategy}/declarationOf.d.ts +0 -0
  131. /package/{esm/strategy → strategy}/declarationOf.js +0 -0
  132. /package/{cjs/strategy → strategy}/hardCode.d.ts +0 -0
  133. /package/{esm/strategy → strategy}/hardCode.js +0 -0
  134. /package/{cjs/strategy → strategy}/index.d.ts +0 -0
  135. /package/{esm/strategy → strategy}/index.js +0 -0
  136. /package/{cjs/strategy → strategy}/inject.d.ts +0 -0
  137. /package/{esm/strategy → strategy}/inject.js +0 -0
  138. /package/{cjs/strategy → strategy}/namedAttribute.d.ts +0 -0
  139. /package/{esm/strategy → strategy}/namedAttribute.js +0 -0
  140. /package/{cjs/strategy → strategy}/strategy.d.ts +0 -0
  141. /package/{esm/strategy → strategy}/strategy.js +0 -0
  142. /package/{cjs/strategy → strategy}/stratgies.test.d.ts +0 -0
  143. /package/{esm/strategy → strategy}/stratgies.test.js +0 -0
  144. /package/{cjs/strategy → strategy}/texture2D.d.ts +0 -0
  145. /package/{esm/strategy → strategy}/texture2D.js +0 -0
  146. /package/{cjs/strategy → strategy}/uniform.d.ts +0 -0
  147. /package/{esm/strategy → strategy}/uniform.js +0 -0
  148. /package/{cjs/strategy → strategy}/variable.d.ts +0 -0
  149. /package/{esm/strategy → strategy}/variable.js +0 -0
  150. /package/{cjs/util → util}/ast.d.ts +0 -0
  151. /package/{esm/util → util}/ast.js +0 -0
  152. /package/{cjs/util → util}/ensure.d.ts +0 -0
  153. /package/{esm/util → util}/ensure.js +0 -0
  154. /package/{cjs/util → util}/id.d.ts +0 -0
  155. /package/{esm/util → util}/id.js +0 -0
package/package.json CHANGED
@@ -1,15 +1,17 @@
1
1
  {
2
2
  "name": "@shaderfrog/core",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "description": "Shaderfrog core",
5
5
  "type": "module",
6
- "exports": {
7
- "require": "./cjs/index.js",
8
- "import": "./esm/index.js"
9
- },
10
6
  "files": [
11
- "cjs",
12
- "esm"
7
+ "engine.d.ts",
8
+ "engine.js",
9
+ "graph",
10
+ "index.d.ts",
11
+ "index.js",
12
+ "plugins",
13
+ "strategy",
14
+ "util"
13
15
  ],
14
16
  "scripts": {
15
17
  "prepare": "npm run build && ./prepublish.sh",
@@ -46,7 +48,7 @@
46
48
  "lodash.groupby": "^4.6.0"
47
49
  },
48
50
  "peerDependencies": {
49
- "@shaderfrog/glsl-parser": "^2.1.5",
51
+ "@shaderfrog/glsl-parser": "^3.0.0",
50
52
  "babylonjs": ">=4",
51
53
  "playcanvas": "^1.65.3",
52
54
  "three": ">=0.50"
package/cjs/engine.js DELETED
@@ -1,218 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __read = (this && this.__read) || function (o, n) {
14
- var m = typeof Symbol === "function" && o[Symbol.iterator];
15
- if (!m) return o;
16
- var i = m.call(o), r, ar = [], e;
17
- try {
18
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
- }
20
- catch (error) { e = { error: error }; }
21
- finally {
22
- try {
23
- if (r && !r.done && (m = i["return"])) m.call(i);
24
- }
25
- finally { if (e) throw e.error; }
26
- }
27
- return ar;
28
- };
29
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
30
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
31
- if (ar || !(i in from)) {
32
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
33
- ar[i] = from[i];
34
- }
35
- }
36
- return to.concat(ar || Array.prototype.slice.call(from));
37
- };
38
- var __importDefault = (this && this.__importDefault) || function (mod) {
39
- return (mod && mod.__esModule) ? mod : { "default": mod };
40
- };
41
- Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.collectInitialEvaluatedGraphProperties = exports.convertToEngine = exports.convertNode = exports.EngineNodeType = void 0;
43
- var preprocessor_1 = __importDefault(require("@shaderfrog/glsl-parser/preprocessor"));
44
- var glsl_parser_1 = require("@shaderfrog/glsl-parser");
45
- var graph_types_1 = require("./graph/graph-types");
46
- var lodash_groupby_1 = __importDefault(require("lodash.groupby"));
47
- var graph_1 = require("./graph/graph");
48
- var evaluate_1 = require("./graph/evaluate");
49
- var log = function () {
50
- var _a;
51
- var args = [];
52
- for (var _i = 0; _i < arguments.length; _i++) {
53
- args[_i] = arguments[_i];
54
- }
55
- return (_a = console.log).call.apply(_a, __spreadArray([console, '\x1b[32m(core)\x1b[0m'], __read(args), false));
56
- };
57
- var EngineNodeType;
58
- (function (EngineNodeType) {
59
- EngineNodeType["toon"] = "toon";
60
- EngineNodeType["phong"] = "phong";
61
- EngineNodeType["physical"] = "physical";
62
- EngineNodeType["shader"] = "shader";
63
- EngineNodeType["binary"] = "binary";
64
- })(EngineNodeType = exports.EngineNodeType || (exports.EngineNodeType = {}));
65
- // type EdgeUpdates = { [edgeId: string]: { oldInput: string; newInput: string } };
66
- var convertNode = function (node, converter) {
67
- log("Converting ".concat(node.name, " (").concat(node.id, ")"));
68
- var preprocessed = (0, preprocessor_1.default)(node.source, {
69
- preserveComments: true,
70
- preserve: {
71
- version: function () { return true; },
72
- define: function () { return true; },
73
- },
74
- });
75
- var ast = glsl_parser_1.parser.parse(preprocessed);
76
- converter.convertAst(ast, node.stage);
77
- var source = (0, glsl_parser_1.generate)(ast);
78
- return __assign(__assign({}, node), { source: source });
79
- };
80
- exports.convertNode = convertNode;
81
- var convertToEngine = function (oldEngine, newEngine, graph) {
82
- var converter = newEngine.importers[oldEngine.name];
83
- if (!converter) {
84
- throw new Error("The engine ".concat(newEngine.name, " has no importer for ").concat(oldEngine.name));
85
- }
86
- log("Attempting to convert from ".concat(newEngine.name, " to ").concat(oldEngine.name));
87
- // const edgeUpdates: EdgeUpdates = {};
88
- var edgesByNodeId = (0, lodash_groupby_1.default)(graph.edges, 'to');
89
- var edgeUpdates = {};
90
- var nodeUpdates = {};
91
- graph.nodes.forEach(function (node) {
92
- // Convert engine nodes
93
- if (node.type in EngineNodeType) {
94
- if (node.type in newEngine.constructors) {
95
- var source = node;
96
- nodeUpdates[source.id] = // @ts-ignore
97
- newEngine.constructors[source.type](source.id, source.name, source.position, source.config.uniforms, source.stage);
98
- // Bail if no conversion
99
- }
100
- else {
101
- throw new Error("Can't convert ".concat(oldEngine.name, " to ").concat(newEngine.name, " because ").concat(newEngine.name, " does not have a \"").concat(node.type, "\" constructor"));
102
- }
103
- }
104
- else if (graph_types_1.NodeType.SOURCE === node.type) {
105
- nodeUpdates[node.id] = (0, exports.convertNode)(node, converter);
106
- }
107
- // Then update input edges. We only care about engine nodes
108
- if (node.type in converter.nodeInputMap) {
109
- var map_1 = converter.nodeInputMap[node.type];
110
- (edgesByNodeId[node.id] || []).forEach(function (edge) {
111
- if (edge.input in map_1) {
112
- var mapped = map_1[edge.input];
113
- log('Converting edge', edge.input, 'to', map_1[edge.input]);
114
- edgeUpdates[edge.id] = __assign(__assign({}, edge), { input: mapped });
115
- }
116
- else {
117
- log('Discarding', edge.input, 'as there is no edge mapping in the', newEngine.name, 'importer');
118
- edgeUpdates[edge.id] = null;
119
- }
120
- });
121
- }
122
- });
123
- graph.edges = graph.edges.reduce(function (edges, edge) {
124
- if (edge.id in edgeUpdates) {
125
- var res = edgeUpdates[edge.id];
126
- if (res === null) {
127
- return edges;
128
- }
129
- else {
130
- return __spreadArray(__spreadArray([], __read(edges), false), [res], false);
131
- }
132
- }
133
- return __spreadArray(__spreadArray([], __read(edges), false), [edge], false);
134
- }, []);
135
- graph.nodes = graph.nodes.reduce(function (nodes, node) {
136
- if (node.id in nodeUpdates) {
137
- var res = nodeUpdates[node.id];
138
- if (res === null) {
139
- return nodes;
140
- }
141
- else {
142
- return __spreadArray(__spreadArray([], __read(nodes), false), [res], false);
143
- }
144
- }
145
- return __spreadArray(__spreadArray([], __read(nodes), false), [node], false);
146
- }, []);
147
- log('Created converted graph', graph);
148
- return graph;
149
- };
150
- exports.convertToEngine = convertToEngine;
151
- /**
152
- * Create the initial engine node properties for a plugin to create its initial
153
- * material with. This finds all engine nodes in the graph, finds all their
154
- * properties, evalutes them, and returns an object with initial properties to
155
- * set on the new plugin material, like a three.RawShaderMaterial().
156
- *
157
- * Currently only PlayCanvas uses this. It's at odds with the compileResult.dataInputs
158
- * code path. That path uses isDataNode() to check for inputs, which excludes
159
- * baked inputs. PlayCanvas requires (at least diffusesMap?) baked input properties
160
- * to be set to a pc.Texture() at runtime, otherwise there's an error about
161
- * vertex_texCoord0.
162
- */
163
- var collectInitialEvaluatedGraphProperties = function (engine, graph, defaultPropertySetting) {
164
- var graphProperties = {};
165
- // Get all the nodes with properties, meaning engine nodes, and the inputs
166
- // for each property (property is like "diffuseMap").
167
- var _a = (0, graph_1.collectNodeProperties)(graph), nodes = _a.nodes, inputs = _a.inputs;
168
- Object.entries(inputs).forEach(function (_a) {
169
- var _b = __read(_a, 2), nodeId = _b[0], nodeInputs = _b[1];
170
- // For every node with properties... There might be mulitple if there are
171
- // uniforms plugged into both frag and vertex engine nodes, which
172
- var node = nodes[nodeId];
173
- nodeInputs.forEach(function (i) {
174
- // Cast this to an input with a property specified on it, which the
175
- // predicate search enforces
176
- var input = i;
177
- var edge = graph.edges.find(function (_a) {
178
- var to = _a.to, i = _a.input;
179
- return to === node.id && i === input.id;
180
- });
181
- // In the case where a node has been deleted from the graph,
182
- // dataInputs won't have been udpated until a recompile completes
183
- var fromNode = edge && graph.nodes.find(function (_a) {
184
- var id = _a.id;
185
- return id === edge.from;
186
- });
187
- if (fromNode) {
188
- // If this is a baked input, we need to set the engine property to force
189
- // whatever we're baking to generate.
190
- if (input.baked) {
191
- // Find the corresponding property on the node and get the default
192
- // setting
193
- var property = (node.config.properties || []).find(function (p) { return p.property === input.property; });
194
- if (property) {
195
- graphProperties[input.property] = defaultPropertySetting(property);
196
- }
197
- else {
198
- console.error('Property not found on input node', node, input);
199
- throw new Error('Property not found on input node');
200
- }
201
- // Other inputs should(?) be data if not baked
202
- }
203
- else {
204
- try {
205
- graphProperties[input.property] = (0, evaluate_1.evaluateNode)(engine, graph, fromNode);
206
- }
207
- catch (err) {
208
- console.error('Tried to evaluate a non-data node!', {
209
- err: err,
210
- });
211
- }
212
- }
213
- }
214
- });
215
- });
216
- return graphProperties;
217
- };
218
- exports.collectInitialEvaluatedGraphProperties = collectInitialEvaluatedGraphProperties;
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.nodeInput = void 0;
4
- var nodeInput = function (displayName, id, type, dataType, accepts, bakeable, property) { return ({
5
- displayName: displayName,
6
- id: id,
7
- type: type,
8
- dataType: dataType,
9
- accepts: accepts,
10
- bakeable: bakeable,
11
- property: property,
12
- }); };
13
- exports.nodeInput = nodeInput;
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SourceType = exports.property = exports.mapInputName = void 0;
4
- var mapInputName = function (node, _a) {
5
- var _b, _c;
6
- var id = _a.id, displayName = _a.displayName;
7
- return ((_c = (_b = node.config) === null || _b === void 0 ? void 0 : _b.inputMapping) === null || _c === void 0 ? void 0 : _c[id]) || displayName;
8
- };
9
- exports.mapInputName = mapInputName;
10
- var property = function (displayName, property, type, fillerName, defaultValue) { return ({
11
- displayName: displayName,
12
- type: type,
13
- property: property,
14
- fillerName: fillerName,
15
- defaultValue: defaultValue,
16
- }); };
17
- exports.property = property;
18
- var SourceType;
19
- (function (SourceType) {
20
- SourceType["SHADER_PROGRAM"] = "Shader Program";
21
- SourceType["EXPRESSION"] = "Expression";
22
- SourceType["FN_BODY_FRAGMENT"] = "Function Body Fragment";
23
- })(SourceType = exports.SourceType || (exports.SourceType = {}));
@@ -1,253 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
- var __read = (this && this.__read) || function (o, n) {
50
- var m = typeof Symbol === "function" && o[Symbol.iterator];
51
- if (!m) return o;
52
- var i = m.call(o), r, ar = [], e;
53
- try {
54
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
55
- }
56
- catch (error) { e = { error: error }; }
57
- finally {
58
- try {
59
- if (r && !r.done && (m = i["return"])) m.call(i);
60
- }
61
- finally { if (e) throw e.error; }
62
- }
63
- return ar;
64
- };
65
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
66
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
67
- if (ar || !(i in from)) {
68
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
69
- ar[i] = from[i];
70
- }
71
- }
72
- return to.concat(ar || Array.prototype.slice.call(from));
73
- };
74
- var __importDefault = (this && this.__importDefault) || function (mod) {
75
- return (mod && mod.__esModule) ? mod : { "default": mod };
76
- };
77
- Object.defineProperty(exports, "__esModule", { value: true });
78
- exports.computeGraphContext = exports.computeAllContexts = exports.computeContextForNodes = exports.isError = void 0;
79
- var lodash_groupby_1 = __importDefault(require("lodash.groupby"));
80
- var code_nodes_1 = require("./code-nodes");
81
- var graph_types_1 = require("./graph-types");
82
- var graph_1 = require("./graph");
83
- var parsers_1 = require("./parsers");
84
- var makeError = function (nodeId) {
85
- var errors = [];
86
- for (var _i = 1; _i < arguments.length; _i++) {
87
- errors[_i - 1] = arguments[_i];
88
- }
89
- return ({
90
- type: 'errors',
91
- nodeId: nodeId,
92
- errors: errors,
93
- });
94
- };
95
- var isError = function (test) {
96
- return (test === null || test === void 0 ? void 0 : test.type) === 'errors';
97
- };
98
- exports.isError = isError;
99
- // Merge existing node inputs, and inputs based on properties, with new ones
100
- // found from the source code, using the *id* as the uniqueness key. Any filler input gets
101
- // merged into property inputs with the same id. This preserves the
102
- // "baked" property on node inputs which is toggle-able in the graph
103
- var collapseNodeInputs = function (node, updatedInputs) {
104
- return Object.values((0, lodash_groupby_1.default)(__spreadArray(__spreadArray([], __read(updatedInputs), false), __read(node.inputs), false), function (i) { return i.id; })).map(function (dupes) { return dupes.reduce(function (node, dupe) { return (__assign(__assign({}, node), dupe)); }); });
105
- };
106
- var computeNodeContext = function (engineContext, engine, graph, node) { return __awaiter(void 0, void 0, void 0, function () {
107
- var parser, sibling, onBeforeCompile, manipulateAst, inputEdges, ast, dataInputs, computedInputs, nodeContext;
108
- return __generator(this, function (_a) {
109
- switch (_a.label) {
110
- case 0:
111
- parser = __assign(__assign({}, (parsers_1.coreParsers[node.type] || parsers_1.coreParsers[graph_types_1.NodeType.SOURCE])), (engine.parsers[node.type] || {}));
112
- sibling = (0, graph_1.findLinkedNode)(graph, node.id);
113
- onBeforeCompile = parser.onBeforeCompile, manipulateAst = parser.manipulateAst;
114
- if (!onBeforeCompile) return [3 /*break*/, 2];
115
- return [4 /*yield*/, onBeforeCompile(graph, engineContext, node, sibling)];
116
- case 1:
117
- _a.sent();
118
- _a.label = 2;
119
- case 2:
120
- inputEdges = graph.edges.filter(function (edge) { return edge.to === node.id; });
121
- try {
122
- ast = parser.produceAst(engineContext, engine, graph, node, inputEdges);
123
- if (manipulateAst) {
124
- ast = manipulateAst(engineContext, engine, graph, ast, inputEdges, node, sibling);
125
- }
126
- }
127
- catch (error) {
128
- console.error('Error parsing source code!', { error: error, node: node });
129
- return [2 /*return*/, makeError(node.id, error)];
130
- }
131
- dataInputs = (0, lodash_groupby_1.default)((0, graph_1.filterGraphFromNode)(graph, node, {
132
- input: function (input, b, c, fromNode) {
133
- return input.bakeable && (fromNode === null || fromNode === void 0 ? void 0 : fromNode.type) === 'source';
134
- },
135
- }, 1).inputs[node.id] || [], 'id');
136
- computedInputs = parser.findInputs(engineContext, ast, inputEdges, node, sibling);
137
- node.inputs = collapseNodeInputs(node, computedInputs.map(function (_a) {
138
- var _b = __read(_a, 1), i = _b[0];
139
- return (__assign(__assign({}, i), { displayName: (0, code_nodes_1.mapInputName)(node, i) }));
140
- })).map(function (input) { return (__assign(__assign({}, input), (input.id in dataInputs ? { baked: true } : {}))); });
141
- nodeContext = {
142
- ast: ast,
143
- id: node.id,
144
- inputFillers: computedInputs.reduce(function (acc, _a) {
145
- var _b;
146
- var _c = __read(_a, 3), input = _c[0], filler = _c[1], args = _c[2];
147
- return (__assign(__assign({}, acc), (_b = {}, _b[input.id] = {
148
- filler: filler,
149
- args: args,
150
- }, _b)));
151
- }, {}),
152
- };
153
- // Skip mangling if the node tells us to, which probably means it's an engine
154
- // node where we don't care about renaming all the variables, or if it's
155
- // an expression, where we want to be in the context of other variables
156
- // TODO: Use global undefined engine variables here?
157
- if (node.config.mangle !== false &&
158
- node.sourceType !== code_nodes_1.SourceType.EXPRESSION &&
159
- node.sourceType !== code_nodes_1.SourceType.FN_BODY_FRAGMENT) {
160
- (0, graph_1.mangleEntireProgram)(engine, ast, node, (0, graph_1.findLinkedNode)(graph, node.id));
161
- }
162
- return [2 /*return*/, nodeContext];
163
- }
164
- });
165
- }); };
166
- var computeContextForNodes = function (engineContext, engine, graph, nodes) { return __awaiter(void 0, void 0, void 0, function () {
167
- return __generator(this, function (_a) {
168
- return [2 /*return*/, nodes
169
- .filter(graph_1.isSourceNode)
170
- .reduce(function (ctx, node) { return __awaiter(void 0, void 0, void 0, function () {
171
- var context, nodeContextOrError;
172
- return __generator(this, function (_a) {
173
- switch (_a.label) {
174
- case 0: return [4 /*yield*/, ctx];
175
- case 1:
176
- context = _a.sent();
177
- if ((0, exports.isError)(context)) {
178
- return [2 /*return*/, context];
179
- }
180
- return [4 /*yield*/, computeNodeContext(engineContext, engine, graph, node)];
181
- case 2:
182
- nodeContextOrError = _a.sent();
183
- if ((0, exports.isError)(nodeContextOrError)) {
184
- return [2 /*return*/, nodeContextOrError];
185
- }
186
- context[node.id] = __assign(__assign({}, (context[node.id] || {})), nodeContextOrError);
187
- return [2 /*return*/, context];
188
- }
189
- });
190
- }); }, Promise.resolve(engineContext.nodes))];
191
- });
192
- }); };
193
- exports.computeContextForNodes = computeContextForNodes;
194
- /**
195
- * Compute the context for every node in the graph, done on initial graph load
196
- * to compute the inputs/outputs for every node
197
- */
198
- var computeAllContexts = function (engineContext, engine, graph) { return __awaiter(void 0, void 0, void 0, function () {
199
- var result;
200
- return __generator(this, function (_a) {
201
- switch (_a.label) {
202
- case 0: return [4 /*yield*/, (0, exports.computeContextForNodes)(engineContext, engine, graph, graph.nodes)];
203
- case 1:
204
- result = _a.sent();
205
- if ((0, exports.isError)(result)) {
206
- return [2 /*return*/, result];
207
- }
208
- return [2 /*return*/];
209
- }
210
- });
211
- }); };
212
- exports.computeAllContexts = computeAllContexts;
213
- /**
214
- * Compute the contexts for nodes starting from the outputs, working backwards.
215
- * Used to only (re)-compute context for any actively used nodes
216
- */
217
- var computeGraphContext = function (engineContext, engine, graph) { return __awaiter(void 0, void 0, void 0, function () {
218
- var outputFrag, outputVert, vertexIds, fragmentIds, unlinkedNodes, vertNodesOrError, fragNodesOrError;
219
- return __generator(this, function (_a) {
220
- switch (_a.label) {
221
- case 0:
222
- outputFrag = graph.nodes.find(function (node) { return node.type === 'output' && node.stage === 'fragment'; });
223
- if (!outputFrag) {
224
- throw new Error('No fragment output in graph');
225
- }
226
- outputVert = graph.nodes.find(function (node) { return node.type === 'output' && node.stage === 'vertex'; });
227
- if (!outputVert) {
228
- throw new Error('No vertex output in graph');
229
- }
230
- vertexIds = (0, graph_1.collectConnectedNodes)(graph, outputVert);
231
- fragmentIds = (0, graph_1.collectConnectedNodes)(graph, outputFrag);
232
- unlinkedNodes = (0, graph_1.findLinkedVertexNodes)(graph, vertexIds);
233
- return [4 /*yield*/, (0, exports.computeContextForNodes)(engineContext, engine, graph, __spreadArray(__spreadArray([
234
- outputVert
235
- ], __read(Object.values(vertexIds).filter(function (node) { return node.id !== outputVert.id; })), false), __read(unlinkedNodes), false))];
236
- case 1:
237
- vertNodesOrError = _a.sent();
238
- if ((0, exports.isError)(vertNodesOrError)) {
239
- return [2 /*return*/, vertNodesOrError];
240
- }
241
- return [4 /*yield*/, (0, exports.computeContextForNodes)(engineContext, engine, graph, __spreadArray([
242
- outputFrag
243
- ], __read(Object.values(fragmentIds).filter(function (node) { return node.id !== outputFrag.id; })), false))];
244
- case 2:
245
- fragNodesOrError = _a.sent();
246
- if ((0, exports.isError)(fragNodesOrError)) {
247
- return [2 /*return*/, fragNodesOrError];
248
- }
249
- return [2 /*return*/];
250
- }
251
- });
252
- }); };
253
- exports.computeGraphContext = computeGraphContext;