@shaderfrog/core 1.3.0 → 1.3.2

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 -7
  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
@@ -1,533 +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
- Object.defineProperty(exports, "__esModule", { value: true });
75
- exports.compileSource = exports.collectNodeProperties = exports.compileGraph = exports.compileNode = exports.isDataInput = exports.filterGraphNodes = exports.collectConnectedNodes = exports.filterGraphFromNode = exports.prepopulatePropertyInputs = exports.mergeSearchResults = exports.consSearchResult = exports.findLinkedVertexNodes = exports.findLinkedNode = exports.resetGraphIds = exports.ensureFromNode = exports.mangleMainFn = exports.mangleEntireProgram = exports.mangleVar = exports.mangleName = exports.nodeName = exports.doesLinkThruShader = exports.findNode = exports.isSourceNode = exports.isDataNode = void 0;
76
- var utils_1 = require("@shaderfrog/glsl-parser/parser/utils");
77
- var context_1 = require("./context");
78
- var shader_sections_1 = require("./shader-sections");
79
- var ast_1 = require("../util/ast");
80
- var ensure_1 = require("../util/ensure");
81
- var code_nodes_1 = require("./code-nodes");
82
- var base_node_1 = require("./base-node");
83
- var id_1 = require("../util/id");
84
- var parsers_1 = require("./parsers");
85
- var evaluate_1 = require("./evaluate");
86
- var graph_types_1 = require("./graph-types");
87
- var glsl_parser_1 = require("@shaderfrog/glsl-parser");
88
- var log = function () {
89
- var _a;
90
- var args = [];
91
- for (var _i = 0; _i < arguments.length; _i++) {
92
- args[_i] = arguments[_i];
93
- }
94
- return (_a = console.log).call.apply(_a, __spreadArray([console, '\x1b[31m(core.graph)\x1b[0m'], __read(args), false));
95
- };
96
- var isDataNode = function (node) {
97
- return 'value' in node;
98
- };
99
- exports.isDataNode = isDataNode;
100
- var isSourceNode = function (node) {
101
- return !(0, exports.isDataNode)(node);
102
- };
103
- exports.isSourceNode = isSourceNode;
104
- var findNode = function (graph, id) {
105
- return (0, ensure_1.ensure)(graph.nodes.find(function (node) { return node.id === id; }));
106
- };
107
- exports.findNode = findNode;
108
- var doesLinkThruShader = function (graph, node) {
109
- var edges = graph.edges.filter(function (edge) { return edge.type !== graph_types_1.EdgeLink.NEXT_STAGE && edge.from === node.id; });
110
- if (edges.length === 0) {
111
- return false;
112
- }
113
- return edges.reduce(function (foundShader, edge) {
114
- var upstreamNode = (0, ensure_1.ensure)(graph.nodes.find(function (node) { return node.id === edge.to; }));
115
- return (foundShader ||
116
- // TODO: LARD this probably will introduce some insidius hard to track
117
- // down bug, as I try to pull toon and phong up out of core, I need to
118
- // know if a graph links through a "shader" which now means somehting
119
- // different... does a config object need isShader? Can we compute it from
120
- // inputs/ outputs/source?
121
- (upstreamNode.sourceType !== code_nodes_1.SourceType.EXPRESSION &&
122
- upstreamNode.type !== graph_types_1.NodeType.OUTPUT) ||
123
- (0, exports.doesLinkThruShader)(graph, upstreamNode));
124
- }, false);
125
- };
126
- exports.doesLinkThruShader = doesLinkThruShader;
127
- var nodeName = function (node) {
128
- return 'main_' + node.name.replace(/[^a-zA-Z0-9]/g, ' ').replace(/ +/g, '_');
129
- };
130
- exports.nodeName = nodeName;
131
- var mangleName = function (name, node, nextSibling) {
132
- // Mangle a name to its next stage node, so the vertex suffix becomes the
133
- // fragment id, but not the other way around.
134
- var id = (nextSibling === null || nextSibling === void 0 ? void 0 : nextSibling.stage) === 'fragment'
135
- ? nextSibling === null || nextSibling === void 0 ? void 0 : nextSibling.id
136
- : node.id;
137
- return "".concat(name, "_").concat(id);
138
- };
139
- exports.mangleName = mangleName;
140
- var mangleVar = function (name, engine, node, sibling) { return (engine.preserve.has(name) ? name : (0, exports.mangleName)(name, node, sibling)); };
141
- exports.mangleVar = mangleVar;
142
- var mangleEntireProgram = function (engine, ast, node, sibling) {
143
- (0, utils_1.renameBindings)(ast.scopes[0], function (name, n) {
144
- return n.doNotDescope ? name : (0, exports.mangleVar)(name, engine, node, sibling);
145
- });
146
- (0, exports.mangleMainFn)(ast, node, sibling);
147
- };
148
- exports.mangleEntireProgram = mangleEntireProgram;
149
- var mangleMainFn = function (ast, node, sibling) {
150
- (0, utils_1.renameFunctions)(ast.scopes[0], function (name) {
151
- return name === 'main' ? (0, exports.nodeName)(node) : (0, exports.mangleName)(name, node, sibling);
152
- });
153
- };
154
- exports.mangleMainFn = mangleMainFn;
155
- var ensureFromNode = function (graph, inputEdge) {
156
- return (0, ensure_1.ensure)(graph.nodes.find(function (_a) {
157
- var id = _a.id;
158
- return id === inputEdge.from;
159
- }), "Orphaned edge! There is an edge fro \"".concat(inputEdge.from, "\" to \"").concat(inputEdge.to, "\", but from node ").concat(inputEdge.from, " does not exist in the graph."));
160
- };
161
- exports.ensureFromNode = ensureFromNode;
162
- var resetGraphIds = function (graph) {
163
- var idMap = {};
164
- var map = function (id) {
165
- idMap[id] = idMap[id] || (0, id_1.makeId)();
166
- return idMap[id];
167
- };
168
- return {
169
- nodes: graph.nodes.map(function (n) { return (__assign(__assign(__assign({}, n), { id: map(n.id) }), (n.parentId ? { parentId: map(n.parentId) } : {}))); }),
170
- edges: graph.edges.map(function (e) { return (__assign(__assign({}, e), { id: map(e.id), from: map(e.from), to: map(e.to) })); }),
171
- };
172
- };
173
- exports.resetGraphIds = resetGraphIds;
174
- var findLinkedNode = function (graph, id) {
175
- var edgeLink = graph.edges.find(function (e) { return e.type === graph_types_1.EdgeLink.NEXT_STAGE && (e.from === id || e.to === id); });
176
- var otherId = (edgeLink === null || edgeLink === void 0 ? void 0 : edgeLink.from) === id ? edgeLink === null || edgeLink === void 0 ? void 0 : edgeLink.to : edgeLink === null || edgeLink === void 0 ? void 0 : edgeLink.from;
177
- return graph.nodes.find(function (node) { return node.id === otherId; });
178
- };
179
- exports.findLinkedNode = findLinkedNode;
180
- /**
181
- * Find any unconnected vertex nodes linked to collected fragment nodes
182
- */
183
- var findLinkedVertexNodes = function (graph, existingIds) {
184
- if (existingIds === void 0) { existingIds = {}; }
185
- // Group edges by where they point
186
- var edgeLinks = graph.edges
187
- .filter(function (e) { return e.type === graph_types_1.EdgeLink.NEXT_STAGE; })
188
- .reduce(function (edges, edge) {
189
- var _a;
190
- return (__assign(__assign({}, edges), (_a = {}, _a[edge.to] = edge, _a[edge.from] = edge, _a)));
191
- }, {});
192
- return graph.nodes.filter(function (node) {
193
- // If this is a vertex node
194
- return (0, exports.isSourceNode)(node) &&
195
- node.stage === 'vertex' &&
196
- // That's linked
197
- node.id in edgeLinks &&
198
- // And not already captured (this should probably just be a set)
199
- !existingIds[node.id];
200
- });
201
- };
202
- exports.findLinkedVertexNodes = findLinkedVertexNodes;
203
- var consSearchResult = function () { return ({
204
- nodes: {},
205
- inputs: {},
206
- edges: [],
207
- }); };
208
- exports.consSearchResult = consSearchResult;
209
- var mergeSearchResults = function (a, b) { return ({
210
- nodes: __assign(__assign({}, a.nodes), b.nodes),
211
- inputs: __assign(__assign({}, a.inputs), b.inputs),
212
- edges: __spreadArray(__spreadArray([], __read(a.edges), false), __read(b.edges), false),
213
- }); };
214
- exports.mergeSearchResults = mergeSearchResults;
215
- /**
216
- * Create the inputs on a node from the properties. This used to be done at
217
- * context time. Doing it at node creation time lets us auto-bake edges into
218
- * the node at initial graph creation time.
219
- */
220
- var prepopulatePropertyInputs = function (node) { return (__assign(__assign({}, node), { inputs: __spreadArray(__spreadArray([], __read(node.inputs), false), __read((node.config.properties || []).map(function (property) {
221
- return (0, base_node_1.nodeInput)(property.displayName, "property_".concat(property.property), 'property', property.type, ['data'], !!property.fillerName, // bakeable
222
- property.property);
223
- })), false) })); };
224
- exports.prepopulatePropertyInputs = prepopulatePropertyInputs;
225
- /**
226
- * Recursively filter the graph, starting from a specific node, looking for
227
- * nodes and edges that match predicates.
228
- *
229
- * Inputs can only be filtered if the graph context has been computed, since
230
- * inputs aren't created until then.
231
- */
232
- var filterGraphFromNode = function (graph, node, predicates, depth, lastResult) {
233
- var _a;
234
- if (depth === void 0) { depth = Infinity; }
235
- if (lastResult === void 0) { lastResult = (0, exports.consSearchResult)(); }
236
- var inputs = node.inputs;
237
- var inputEdges = graph.edges.filter(function (edge) { return edge.to === node.id; });
238
- var nodeAcc = __assign({}, (predicates.node && predicates.node(node, inputEdges, lastResult)
239
- ? (_a = {}, _a[node.id] = node, _a) : {}));
240
- var accumulatedResult = __assign(__assign({}, lastResult), { nodes: __assign(__assign({}, lastResult.nodes), nodeAcc) });
241
- return inputEdges.reduce(function (acc, inputEdge) {
242
- var _a;
243
- var input = inputs.find(function (i) { return i.id === inputEdge.input; });
244
- var fromNode = inputEdge ? (0, exports.ensureFromNode)(graph, inputEdge) : undefined;
245
- var inputAcc = __assign(__assign({}, acc.inputs), (input &&
246
- predicates.input &&
247
- predicates.input(input, node, inputEdge, fromNode, lastResult)
248
- ? (_a = {}, _a[node.id] = __spreadArray(__spreadArray([], __read((acc.inputs[node.id] || [])), false), [input], false), _a) : {}));
249
- var edgeAcc = __spreadArray(__spreadArray([], __read(acc.edges), false), __read((predicates.edge &&
250
- predicates.edge(input, node, inputEdge, fromNode, lastResult)
251
- ? [inputEdge]
252
- : [])), false);
253
- // Add in the latest result of edges and inputs so that when we recurse into
254
- // the next node, it has the latest accumulator
255
- var intermediateAcc = __assign(__assign({}, acc), { inputs: inputAcc, edges: edgeAcc });
256
- if (inputEdge && fromNode && depth > 1) {
257
- var result = (0, exports.filterGraphFromNode)(graph, fromNode, predicates, depth - 1, intermediateAcc);
258
- return (0, exports.mergeSearchResults)(intermediateAcc, result);
259
- }
260
- else {
261
- return intermediateAcc;
262
- }
263
- }, accumulatedResult);
264
- };
265
- exports.filterGraphFromNode = filterGraphFromNode;
266
- var collectConnectedNodes = function (graph, node) {
267
- return (0, exports.filterGraphFromNode)(graph, node, { node: function () { return true; } }).nodes;
268
- };
269
- exports.collectConnectedNodes = collectConnectedNodes;
270
- var filterGraphNodes = function (graph, nodes, filter, depth) {
271
- if (depth === void 0) { depth = Infinity; }
272
- return nodes.reduce(function (acc, node) {
273
- var result = (0, exports.filterGraphFromNode)(graph, node, filter, depth);
274
- return {
275
- nodes: __assign(__assign({}, acc.nodes), result.nodes),
276
- inputs: __assign(__assign({}, acc.inputs), result.inputs),
277
- edges: __assign(__assign({}, acc.edges), result.edges),
278
- };
279
- }, (0, exports.consSearchResult)());
280
- };
281
- exports.filterGraphNodes = filterGraphNodes;
282
- // before data inputs were known by the input.category being node or data. I
283
- // tried updating inputs to have acepts: [code|data] and "baked" now is there a
284
- // way to know if we're plugging in code or data?
285
- var isDataInput = function (input) {
286
- return (input.type === 'uniform' || input.type === 'property') && !input.baked;
287
- };
288
- exports.isDataInput = isDataInput;
289
- var compileNode = function (engine, graph, edges, engineContext, node, activeIds) {
290
- var _a, _b;
291
- if (activeIds === void 0) { activeIds = {}; }
292
- // THIS DUPLICATES OTHER LINE
293
- var parser = __assign(__assign({}, (parsers_1.coreParsers[node.type] || parsers_1.coreParsers[graph_types_1.NodeType.SOURCE])), (engine.parsers[node.type] || {}));
294
- var inputs = node.inputs;
295
- if (!parser) {
296
- console.error(node);
297
- throw new Error("No parser found for ".concat(node.name, " (").concat(node.type, ", id ").concat(node.id, ")"));
298
- }
299
- var nodeContext = (0, exports.isDataNode)(node)
300
- ? null
301
- : (0, ensure_1.ensure)(engineContext.nodes[node.id], "No node context found for \"".concat(node.name, "\" (id ").concat(node.id, ")!"));
302
- var _c = (nodeContext || {}), ast = _c.ast, inputFillers = _c.inputFillers;
303
- if (!inputs) {
304
- throw new Error("I'm drunk and I think this case should be impossible");
305
- }
306
- var compiledIds = activeIds;
307
- var inputEdges = edges.filter(function (edge) { return edge.to === node.id; });
308
- if (inputEdges.length) {
309
- var continuation_1 = (0, shader_sections_1.emptyShaderSections)();
310
- inputEdges
311
- .filter(function (edge) { return edge.type !== graph_types_1.EdgeLink.NEXT_STAGE; })
312
- .map(function (edge) { return ({
313
- edge: edge,
314
- fromNode: (0, ensure_1.ensure)(graph.nodes.find(function (node) { return edge.from === node.id; }), "GraphNode for edge ".concat(edge.from, " not found")),
315
- input: (0, ensure_1.ensure)(inputs.find(function (_a) {
316
- var id = _a.id;
317
- return id == edge.input;
318
- }), "GraphNode \"".concat(node.name, "\"").concat(node.stage ? " (".concat(node.stage, ")") : '', " has no input ").concat(edge.input, "!\nAvailable:").concat(inputs
319
- .map(function (_a) {
320
- var id = _a.id;
321
- return id;
322
- })
323
- .join(', '))),
324
- }); })
325
- .filter(function (_a) {
326
- var input = _a.input;
327
- return !(0, exports.isDataInput)(input);
328
- })
329
- .forEach(function (_a) {
330
- var _b;
331
- var fromNode = _a.fromNode, edge = _a.edge, input = _a.input;
332
- var _c = __read((0, exports.compileNode)(engine, graph, edges, engineContext, fromNode, activeIds), 3), inputSections = _c[0], fillerAst = _c[1], childIds = _c[2];
333
- if (!fillerAst) {
334
- throw new TypeError("Expected a filler ast from node ID ".concat(fromNode.id, " (").concat(fromNode.type, ") but none was returned"));
335
- }
336
- continuation_1 = (0, shader_sections_1.mergeShaderSections)(continuation_1, inputSections);
337
- compiledIds = __assign(__assign({}, compiledIds), childIds);
338
- var filler;
339
- var fillerName;
340
- if (nodeContext) {
341
- if (input.property) {
342
- fillerName = (0, ensure_1.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"));
343
- filler = inputFillers[fillerName];
344
- }
345
- else {
346
- filler = inputFillers[input.id];
347
- }
348
- if (!filler) {
349
- console.error('No filler for property', {
350
- input: input,
351
- node: node,
352
- inputFillers: inputFillers,
353
- fillerName: fillerName,
354
- });
355
- throw new Error("Node \"".concat(node.name, "\"").concat(node.stage
356
- ? " (".concat(node.stage, ")")
357
- : '', " has no filler for input \"").concat(input.displayName, "\" named ").concat(fillerName));
358
- }
359
- /**
360
- * +------+ +------+
361
- * a -- o add o -- o tex |
362
- * b -- o | +------+
363
- * +------+
364
- *
365
- * This could produce:
366
- * main_a(v1) + main_b(v2)
367
- * I guess it has to? or it could produce
368
- * function add(v1) { return main_a(v1) + main_b(v2); }
369
- * It can't replace the arg _expression_ in the from shaders, because
370
- * the expression isn't available there.
371
- */
372
- // TODO: This is a hard coded hack for vUv backfilling. It works in
373
- // the simple case. Doesn't work for hell (based on world position).
374
- if (filler.backfillArgs &&
375
- !Array.isArray(fillerAst) &&
376
- fillerAst.type === 'function_call') {
377
- // Object.values(filterGraphFromNode(graph, node, {
378
- // node: (n) => n.type === 'source'
379
- // }).nodes).forEach(sourceNode => {
380
- if (fromNode.type === 'source') {
381
- // @ts-ignore
382
- fillerAst.args = filler.backfillArgs;
383
- // const fc = engineContext.nodes[sourceNode.id];
384
- var fc = engineContext.nodes[fromNode.id];
385
- var main = Object.values(fc.ast.scopes[0].functions.main)[0].declaration;
386
- main.prototype.parameters = [
387
- 'vec2 vv',
388
- ];
389
- // @ts-ignore
390
- var scope = fc.ast.scopes[0];
391
- (0, utils_1.renameBindings)(scope, function (name, node) {
392
- return node.type !== 'declaration' && name === 'vUv'
393
- ? 'vv'
394
- : name;
395
- });
396
- }
397
- // })
398
- }
399
- // Fill in the input! The return value is the new AST of the filled in
400
- // fromNode.
401
- nodeContext.ast = filler.filler(fillerAst);
402
- }
403
- // log(generate(ast.program));
404
- });
405
- // Order matters here! *Prepend* the input nodes to this one, because
406
- // you have to declare functions in order of use in GLSL
407
- var sections = (0, shader_sections_1.mergeShaderSections)(continuation_1, (0, exports.isDataNode)(node) ||
408
- node.sourceType === code_nodes_1.SourceType.EXPRESSION ||
409
- node.sourceType === code_nodes_1.SourceType.FN_BODY_FRAGMENT
410
- ? (0, shader_sections_1.emptyShaderSections)()
411
- : (0, shader_sections_1.findShaderSections)(ast));
412
- var filler = (0, exports.isDataNode)(node)
413
- ? (0, ast_1.makeExpression)((0, evaluate_1.toGlsl)(node))
414
- : parser.produceFiller(node, ast);
415
- return [sections, filler, __assign(__assign({}, compiledIds), (_a = {}, _a[node.id] = node, _a))];
416
- }
417
- else {
418
- // TODO: This duplicates the above branch, and also does this mean we
419
- // recalculate the shader sections and filler for every edge? Can I move
420
- // these lines above the loop?
421
- var sections = (0, exports.isDataNode)(node) ||
422
- node.sourceType === code_nodes_1.SourceType.EXPRESSION ||
423
- node.sourceType === code_nodes_1.SourceType.FN_BODY_FRAGMENT
424
- ? (0, shader_sections_1.emptyShaderSections)()
425
- : (0, shader_sections_1.findShaderSections)(ast);
426
- var filler = (0, exports.isDataNode)(node)
427
- ? (0, ast_1.makeExpression)((0, evaluate_1.toGlsl)(node))
428
- : parser.produceFiller(node, ast);
429
- return [sections, filler, __assign(__assign({}, compiledIds), (_b = {}, _b[node.id] = node, _b))];
430
- }
431
- };
432
- exports.compileNode = compileNode;
433
- var compileGraph = function (engineContext, engine, graph) {
434
- // computeGraphContext(engineContext, engine, graph);
435
- var outputFrag = graph.nodes.find(function (node) { return node.type === 'output' && node.stage === 'fragment'; });
436
- if (!outputFrag) {
437
- throw new Error('No fragment output in graph');
438
- }
439
- var _a = __read((0, exports.compileNode)(engine, graph, graph.edges, engineContext, outputFrag), 3), fragment = _a[0], fragmentIds = _a[2];
440
- var outputVert = graph.nodes.find(function (node) { return node.type === 'output' && node.stage === 'vertex'; });
441
- if (!outputVert) {
442
- throw new Error('No vertex output in graph');
443
- }
444
- var vertexIds = (0, exports.collectConnectedNodes)(graph, outputVert);
445
- // Some fragment shaders reference vertex shaders which may not have been
446
- // given edges in the graph. Build invisible edges from these vertex nodes to
447
- // the hidden "mainStmts" input on the output node, which inlines the function
448
- // calls to those vertex main() statements and includes them in the output
449
- var orphanNodes = (0, exports.findLinkedVertexNodes)(graph, vertexIds);
450
- var orphanEdges = orphanNodes.map(function (node) { return ({
451
- id: (0, id_1.makeId)(),
452
- from: node.id,
453
- to: outputVert.id,
454
- output: 'main',
455
- input: "filler_".concat(graph_types_1.MAGIC_OUTPUT_STMTS),
456
- stage: 'vertex',
457
- category: 'code',
458
- }); });
459
- var _b = __read((0, exports.compileNode)(engine, graph, __spreadArray(__spreadArray([], __read(graph.edges), false), __read(orphanEdges), false), engineContext, outputVert), 2), vertex = _b[0];
460
- // Every compileNode returns the AST so far, as well as the filler for the
461
- // next node with inputs. On the final step, we discard the filler
462
- return {
463
- fragment: fragment,
464
- vertex: vertex,
465
- outputFrag: outputFrag,
466
- outputVert: outputVert,
467
- orphanNodes: orphanNodes,
468
- 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)),
469
- };
470
- };
471
- exports.compileGraph = compileGraph;
472
- /**
473
- * Find engine nodes to set properties on, like find a Physical node so
474
- * consumers can set physicalNode.myProperty = 123.
475
- *
476
- * Finds all active nodes in the graph that have inputs that are properties,
477
- * which currently means it will find all active engine nodes.
478
- */
479
- var collectNodeProperties = function (graph) {
480
- var nodesWithProperties = {
481
- node: function (node) {
482
- var _a;
483
- return 'config' in node &&
484
- 'properties' in node.config &&
485
- !!((_a = node.config.properties) === null || _a === void 0 ? void 0 : _a.length);
486
- },
487
- input: function (input) { return !!input.property; },
488
- };
489
- var outputFrag = graph.nodes.find(function (node) { return node.type === 'output' && node.stage === 'fragment'; });
490
- var outputVert = graph.nodes.find(function (node) { return node.type === 'output' && node.stage === 'vertex'; });
491
- var fragProperties = (0, exports.filterGraphFromNode)(graph, outputFrag, nodesWithProperties);
492
- var vertProperties = (0, exports.filterGraphFromNode)(graph, outputVert, nodesWithProperties);
493
- return {
494
- nodes: __assign(__assign({}, fragProperties.nodes), vertProperties.nodes),
495
- inputs: __assign(__assign({}, fragProperties.inputs), vertProperties.inputs),
496
- edges: __assign(__assign({}, fragProperties.edges), vertProperties.edges),
497
- };
498
- };
499
- exports.collectNodeProperties = collectNodeProperties;
500
- var compileSource = function (graph, engine, ctx) { return __awaiter(void 0, void 0, void 0, function () {
501
- var result, compileResult, fragmentResult, vertexResult, dataInputs, dataNodes;
502
- return __generator(this, function (_a) {
503
- switch (_a.label) {
504
- case 0: return [4 /*yield*/, (0, context_1.computeGraphContext)(ctx, engine, graph)];
505
- case 1:
506
- result = _a.sent();
507
- if ((0, context_1.isError)(result)) {
508
- return [2 /*return*/, result];
509
- }
510
- compileResult = (0, exports.compileGraph)(ctx, engine, graph);
511
- fragmentResult = (0, glsl_parser_1.generate)((0, shader_sections_1.shaderSectionsToProgram)(compileResult.fragment, engine.mergeOptions).program);
512
- vertexResult = (0, glsl_parser_1.generate)((0, shader_sections_1.shaderSectionsToProgram)(compileResult.vertex, engine.mergeOptions).program);
513
- dataInputs = (0, exports.filterGraphNodes)(graph, [compileResult.outputFrag, compileResult.outputVert], { input: exports.isDataInput }).inputs;
514
- dataNodes = Object.entries(dataInputs).reduce(function (acc, _a) {
515
- var _b = __read(_a, 2), nodeId = _b[0], inputs = _b[1];
516
- return inputs.reduce(function (iAcc, input) {
517
- var fromEdge = graph.edges.find(function (edge) { return edge.to === nodeId && edge.input === input.id; });
518
- var fromNode = fromEdge && graph.nodes.find(function (node) { return node.id === fromEdge.from; });
519
- return fromNode
520
- ? __assign(__assign({}, iAcc), (0, exports.collectConnectedNodes)(graph, fromNode)) : iAcc;
521
- }, acc);
522
- }, {});
523
- return [2 /*return*/, {
524
- compileResult: compileResult,
525
- fragmentResult: fragmentResult,
526
- vertexResult: vertexResult,
527
- dataNodes: dataNodes,
528
- dataInputs: dataInputs,
529
- }];
530
- }
531
- });
532
- }); };
533
- exports.compileSource = compileSource;