@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,195 +0,0 @@
1
- "use strict";
2
- var __read = (this && this.__read) || function (o, n) {
3
- var m = typeof Symbol === "function" && o[Symbol.iterator];
4
- if (!m) return o;
5
- var i = m.call(o), r, ar = [], e;
6
- try {
7
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
8
- }
9
- catch (error) { e = { error: error }; }
10
- finally {
11
- try {
12
- if (r && !r.done && (m = i["return"])) m.call(i);
13
- }
14
- finally { if (e) throw e.error; }
15
- }
16
- return ar;
17
- };
18
- Object.defineProperty(exports, "__esModule", { value: true });
19
- exports.applyUniformStrategy = exports.uniformStrategy = void 0;
20
- var graph_1 = require("../graph/graph");
21
- var base_node_1 = require("../graph/base-node");
22
- var _1 = require(".");
23
- var ast_1 = require("../util/ast");
24
- var uniformStrategy = function () { return ({
25
- type: _1.StrategyType.UNIFORM,
26
- config: {},
27
- }); };
28
- exports.uniformStrategy = uniformStrategy;
29
- var DATA_TYPE_MAP = [
30
- ['vector2', new Set(['bvec2', 'dvec2', 'ivec2', 'uvec2', 'vec2'])],
31
- ['number', new Set(['float', 'double', 'int', 'uint', 'atomic_uint'])],
32
- ['vector3', new Set(['bvec3', 'dvec3', 'ivec3', 'uvec3', 'vec3'])],
33
- ['vector4', new Set(['bvec4', 'dvec4', 'ivec4', 'uvec4', 'vec4'])],
34
- ['texture', new Set(['sampler2D'])],
35
- ['mat2', new Set(['mat2', 'dmat2'])],
36
- ['mat3', new Set(['mat3', 'dmat3'])],
37
- ['mat4', new Set(['mat4', 'dmat4'])],
38
- ['mat2x2', new Set(['mat2x2', 'dmat2x2'])],
39
- ['mat2x3', new Set(['mat2x3', 'dmat2x3'])],
40
- ['mat2x4', new Set(['mat2x4', 'dmat2x4'])],
41
- ['mat3x2', new Set(['mat3x2', 'dmat3x2'])],
42
- ['mat3x3', new Set(['mat3x3', 'dmat3x3'])],
43
- ['mat3x4', new Set(['mat3x4', 'dmat3x4'])],
44
- ['mat4x2', new Set(['mat4x2', 'dmat4x2'])],
45
- ['mat4x3', new Set(['mat4x3', 'dmat4x3'])],
46
- ['mat4x4', new Set(['mat4x4', 'dmat4x4'])],
47
- ];
48
- /**
49
- * Uncategorized:
50
- *
51
- "sampler1D"
52
- "sampler3D"
53
- "samplerCube"
54
- "sampler1DShadow"
55
- "sampler2DShadow"
56
- "samplerCubeShadow"
57
- "sampler1DArray"
58
- "sampler2DArray"
59
- "sampler1DArrayShadow"
60
- "sampler2DArrayshadow"
61
- "isampler1D"
62
- "isampler2D"
63
- "isampler3D"
64
- "isamplerCube"
65
- "isampler1Darray"
66
- "isampler2DArray"
67
- "usampler1D"
68
- "usampler2D"
69
- "usampler3D"
70
- "usamplerCube"
71
- "usampler1DArray"
72
- "usampler2DArray"
73
- "sampler2DRect"
74
- "sampler2DRectshadow"
75
- "isampler2DRect"
76
- "usampler2DRect"
77
- "samplerBuffer"
78
- "isamplerBuffer"
79
- "usamplerBuffer"
80
- "samplerCubeArray"
81
- "samplerCubeArrayShadow"
82
- "isamplerCubeArray"
83
- "usamplerCubeArray"
84
- "sampler2DMS"
85
- "isampler2DMS"
86
- "usampler2DMS"
87
- "sampler2DMSArray"
88
- "isampler2DMSArray"
89
- "usampler2DMSArray"
90
- "image1D"
91
- "iimage1D"
92
- "uimage1D"
93
- "image2D"
94
- "iimage2D"
95
- "uimage2D"
96
- "image3D"
97
- "iimage3D"
98
- "uimage3D"
99
- "image2DRect"
100
- "iimage2DRect"
101
- "uimage2DRect"
102
- "imageCube"
103
- "iimageCube"
104
- "uimageCube"
105
- "imageBuffer"
106
- "iimageBuffer"
107
- "uimageBuffer"
108
- "image1DArray"
109
- "iimage1DArray"
110
- "uimage1DArray"
111
- "image2DArray"
112
- "iimage2DArray"
113
- "uimage2DArray"
114
- "imageCubeArray"
115
- "iimageCubeArray"
116
- "uimageCubeArray"
117
- "image2DMS"
118
- "iimage2DMS"
119
- "uimage2DMS"
120
- "image2DMArray"
121
- "iimage2DMSArray"
122
- "uimage2DMSArray"
123
- "struct"
124
- */
125
- var mapUniformType = function (type) {
126
- var found = DATA_TYPE_MAP.find(function (_a) {
127
- var _b = __read(_a, 2), _ = _b[0], set = _b[1];
128
- return set.has(type);
129
- });
130
- if (found) {
131
- return found[0];
132
- }
133
- // console.log(`Unknown uniform type, can't map to graph: ${type}`);
134
- };
135
- var applyUniformStrategy = function (strategy, ast, graphNode, siblingNode) {
136
- var program = ast;
137
- return (program.program || []).flatMap(function (node) {
138
- var _a, _b, _c, _d, _e, _f, _g;
139
- // The uniform declaration type, like vec4
140
- var uniformType = (_d = (_c = (_b = (_a = node.declaration) === null || _a === void 0 ? void 0 : _a.specified_type) === null || _b === void 0 ? void 0 : _b.specifier) === null || _c === void 0 ? void 0 : _c.specifier) === null || _d === void 0 ? void 0 : _d.token;
141
- var graphDataType = mapUniformType(uniformType);
142
- // If this is a uniform declaration line
143
- if (node.type === 'declaration_statement' &&
144
- node.declaration.type === 'declarator_list' &&
145
- ((_g = (_f = (_e = node.declaration) === null || _e === void 0 ? void 0 : _e.specified_type) === null || _f === void 0 ? void 0 : _f.qualifiers) === null || _g === void 0 ? void 0 : _g.find(function (n) { return n.token === 'uniform'; }))
146
- // commented this out to allow for sampler2D uniforms to appear as inputs
147
- // && uniformType !== 'sampler2D'
148
- ) {
149
- // Capture all the declared names, removing mangling suffix
150
- var declarations_1 = node.declaration.declarations;
151
- var names = declarations_1.map(function (d) { return d.identifier.identifier; });
152
- // Tricky code warning: The flow of preparing a node for the graph is:
153
- // 1. Produce/mangle the AST (with unmangled names)
154
- // 2. findInputs() (with unmangled names)
155
- // 3. The AST is *then* mangled in graph.ts
156
- // 4. Later, the inputs are filled in, and now, we have an input with
157
- // the name "x" but the ast now has the mangled name "x_1". So
158
- // here, we look for the *mangled* name in the strategy runner
159
- return names.map(function (name) { return [
160
- (0, base_node_1.nodeInput)(name, "uniform_".concat(name), 'uniform', graphDataType, ['code', 'data'], true),
161
- function (filler) {
162
- var mangledName = (0, graph_1.mangleName)(name, graphNode, siblingNode);
163
- // Remove the declaration line, or the declared uniform
164
- if (declarations_1.length === 1) {
165
- program.program.splice(program.program.indexOf(node), 1);
166
- }
167
- else {
168
- var decl = node.declaration;
169
- decl.declarations = decl.declarations.filter(function (d) { return d.identifier.identifier !== mangledName; });
170
- }
171
- // And rename all the references to said uniform
172
- program.scopes[0].bindings[name].references.forEach(function (ref) {
173
- if (ref.type === 'identifier' && ref.identifier === mangledName) {
174
- ref.identifier = (0, ast_1.generateFiller)(filler);
175
- }
176
- else if (ref.type === 'parameter_declaration' &&
177
- 'identifier' in ref &&
178
- ref.identifier.identifier === mangledName) {
179
- ref.identifier.identifier = (0, ast_1.generateFiller)(filler);
180
- }
181
- else if ('identifier' in ref) {
182
- ref.identifier = (0, ast_1.generateFiller)(filler);
183
- }
184
- else {
185
- console.warn('Unknown uniform reference for', graphNode.name, 'ref');
186
- }
187
- });
188
- return ast;
189
- },
190
- ]; });
191
- }
192
- return [];
193
- });
194
- };
195
- exports.applyUniformStrategy = applyUniformStrategy;
@@ -1,85 +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
- Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.applyVariableStrategy = exports.variableStrategy = void 0;
40
- var base_node_1 = require("../graph/base-node");
41
- var _1 = require(".");
42
- var ast_1 = require("../util/ast");
43
- var variableStrategy = function () { return ({
44
- type: _1.StrategyType.VARIABLE,
45
- config: {},
46
- }); };
47
- exports.variableStrategy = variableStrategy;
48
- var applyVariableStrategy = function (strategy, ast, graphNode, siblingNode) {
49
- var program = ast;
50
- return Object.values(program.scopes.reduce(function (acc, scope) { return (__assign(__assign({}, acc), scope.bindings)); }, {})).flatMap(function (binding) {
51
- return binding.references.reduce(function (acc, ref) {
52
- var identifier, replacer;
53
- if (ref.type === 'declaration') {
54
- identifier = ref.identifier.identifier;
55
- replacer = function (fillerAst) {
56
- ref.identifier.identifier = (0, ast_1.generateFiller)(fillerAst);
57
- return ast;
58
- };
59
- }
60
- else if (ref.type === 'identifier') {
61
- identifier = ref.identifier;
62
- replacer = function (fillerAst) {
63
- ref.identifier = (0, ast_1.generateFiller)(fillerAst);
64
- return ast;
65
- };
66
- // } else if (ref.type === 'parameter_declaration') {
67
- // identifier = ref.declaration.identifier.identifier;
68
- // replacer = (fillerAst: AstNode) => {
69
- // ref.declaration.identifier.identifier = generate(fillerAst);
70
- // };
71
- }
72
- else {
73
- return acc;
74
- }
75
- return __spreadArray(__spreadArray([], __read(acc), false), [
76
- [
77
- (0, base_node_1.nodeInput)(identifier, "filler_".concat(identifier), 'filler', undefined, // Data type for what plugs into this filler
78
- ['code', 'data'], false),
79
- replacer,
80
- ],
81
- ], false);
82
- }, []);
83
- });
84
- };
85
- exports.applyVariableStrategy = applyVariableStrategy;
package/cjs/util/ast.js DELETED
@@ -1,351 +0,0 @@
1
- "use strict";
2
- var __read = (this && this.__read) || function (o, n) {
3
- var m = typeof Symbol === "function" && o[Symbol.iterator];
4
- if (!m) return o;
5
- var i = m.call(o), r, ar = [], e;
6
- try {
7
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
8
- }
9
- catch (error) { e = { error: error }; }
10
- finally {
11
- try {
12
- if (r && !r.done && (m = i["return"])) m.call(i);
13
- }
14
- finally { if (e) throw e.error; }
15
- }
16
- return ar;
17
- };
18
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
19
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
20
- if (ar || !(i in from)) {
21
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
22
- ar[i] = from[i];
23
- }
24
- }
25
- return to.concat(ar || Array.prototype.slice.call(from));
26
- };
27
- Object.defineProperty(exports, "__esModule", { value: true });
28
- exports.generateFiller = exports.convert300MainToReturn = exports.returnGlPositionVec3Right = exports.returnGlPositionHardCoded = exports.returnGlPosition = exports.findFn = exports.makeFnBodyStatementWithScopes = exports.makeExpressionWithScopes = exports.makeExpression = exports.makeFnStatement = exports.makeStatement = exports.outDeclaration = exports.from2To3 = exports.findDeclarationOf = exports.findAssignmentTo = exports.findVec4Constructor = void 0;
29
- var glsl_parser_1 = require("@shaderfrog/glsl-parser");
30
- var ast_1 = require("@shaderfrog/glsl-parser/ast");
31
- var log = function () {
32
- var _a;
33
- var args = [];
34
- for (var _i = 0; _i < arguments.length; _i++) {
35
- args[_i] = arguments[_i];
36
- }
37
- return (_a = console.log).call.apply(_a, __spreadArray([console, '\x1b[31m(core.manipulate)\x1b[0m'], __read(args), false));
38
- };
39
- var findVec4Constructor = function (ast) {
40
- var parent;
41
- var visitors = {
42
- function_call: {
43
- enter: function (path) {
44
- var _a, _b;
45
- if (((_a = path.node.identifier) === null || _a === void 0 ? void 0 : _a.specifier).token === 'vec4') {
46
- parent = (_b = path.findParent(function (p) { return 'right' in p.node; })) === null || _b === void 0 ? void 0 : _b.node;
47
- path.skip();
48
- }
49
- },
50
- },
51
- };
52
- (0, ast_1.visit)(ast, visitors);
53
- return parent;
54
- };
55
- exports.findVec4Constructor = findVec4Constructor;
56
- var findAssignmentTo = function (ast, assignTo) {
57
- var assign;
58
- var visitors = {
59
- expression_statement: {
60
- enter: function (path) {
61
- var _a, _b;
62
- if (((_b = (_a = path.node.expression) === null || _a === void 0 ? void 0 : _a.left) === null || _b === void 0 ? void 0 : _b.identifier) === assignTo) {
63
- assign = path.node;
64
- }
65
- path.skip();
66
- },
67
- },
68
- };
69
- (0, ast_1.visit)(ast, visitors);
70
- return assign;
71
- };
72
- exports.findAssignmentTo = findAssignmentTo;
73
- var findDeclarationOf = function (ast, declarationOf) {
74
- var declaration;
75
- var visitors = {
76
- declaration_statement: {
77
- enter: function (path) {
78
- var _a, _b;
79
- var foundDecl = (_b = (_a = path.node.declaration) === null || _a === void 0 ? void 0 : _a.declarations) === null || _b === void 0 ? void 0 : _b.find(function (decl) { var _a; return ((_a = decl === null || decl === void 0 ? void 0 : decl.identifier) === null || _a === void 0 ? void 0 : _a.identifier) === declarationOf; });
80
- if (foundDecl) {
81
- declaration = foundDecl;
82
- }
83
- path.skip();
84
- },
85
- },
86
- };
87
- (0, ast_1.visit)(ast, visitors);
88
- return declaration;
89
- };
90
- exports.findDeclarationOf = findDeclarationOf;
91
- var from2To3 = function (ast, stage) {
92
- var glOut = 'fragmentColor';
93
- // TODO: add this back in when there's only one after the merge
94
- // ast.program.unshift({
95
- // type: 'preprocessor',
96
- // line: '#version 300 es',
97
- // _: '\n',
98
- // });
99
- if (stage === 'fragment') {
100
- ast.program.unshift((0, exports.makeStatement)("out vec4 ".concat(glOut)));
101
- }
102
- (0, ast_1.visit)(ast, {
103
- function_call: {
104
- enter: function (path) {
105
- var identifier = path.node.identifier;
106
- if (identifier.type === 'identifier' &&
107
- identifier.identifier === 'texture2D') {
108
- identifier.identifier = 'texture';
109
- }
110
- },
111
- },
112
- identifier: {
113
- enter: function (path) {
114
- if (path.node.identifier === 'gl_FragColor') {
115
- path.node.identifier = glOut;
116
- }
117
- },
118
- },
119
- keyword: {
120
- enter: function (path) {
121
- if ((path.node.token === 'attribute' || path.node.token === 'varying') &&
122
- path.findParent(function (path) { return path.node.type === 'declaration_statement'; })) {
123
- path.node.token =
124
- stage === 'vertex' && path.node.token === 'varying' ? 'out' : 'in';
125
- }
126
- },
127
- },
128
- });
129
- };
130
- exports.from2To3 = from2To3;
131
- var outDeclaration = function (name) { return ({
132
- type: 'declaration_statement',
133
- declaration: {
134
- type: 'declarator_list',
135
- specified_type: {
136
- type: 'fully_specified_type',
137
- qualifiers: [{ type: 'keyword', token: 'out', whitespace: ' ' }],
138
- specifier: {
139
- type: 'type_specifier',
140
- specifier: { type: 'keyword', token: 'vec4', whitespace: ' ' },
141
- quantifier: null,
142
- },
143
- },
144
- declarations: [
145
- {
146
- type: 'declaration',
147
- identifier: {
148
- type: 'identifier',
149
- identifier: name,
150
- whitespace: undefined,
151
- },
152
- quantifier: null,
153
- operator: undefined,
154
- initializer: undefined,
155
- },
156
- ],
157
- commas: [],
158
- },
159
- semi: { type: 'literal', literal: ';', whitespace: '\n ' },
160
- }); };
161
- exports.outDeclaration = outDeclaration;
162
- var makeStatement = function (stmt) {
163
- // log(`Parsing "${stmt}"`);
164
- var ast;
165
- try {
166
- ast = glsl_parser_1.parser.parse("".concat(stmt, ";\n"), { quiet: true });
167
- }
168
- catch (error) {
169
- console.error({ stmt: stmt, error: error });
170
- throw new Error("Error parsing stmt \"".concat(stmt, "\": ").concat(error === null || error === void 0 ? void 0 : error.message));
171
- }
172
- // log(util.inspect(ast, false, null, true));
173
- return ast.program[0];
174
- };
175
- exports.makeStatement = makeStatement;
176
- var makeFnStatement = function (fnStmt) {
177
- var ast;
178
- try {
179
- ast = glsl_parser_1.parser.parse("\n void main() {\n ".concat(fnStmt, ";\n }"), { quiet: true });
180
- }
181
- catch (error) {
182
- console.error({ fnStmt: fnStmt, error: error });
183
- throw new Error("Error parsing fnStmt \"".concat(fnStmt, "\": ").concat(error === null || error === void 0 ? void 0 : error.message));
184
- }
185
- // log(util.inspect(ast, false, null, true));
186
- return ast.program[0].body.statements[0];
187
- };
188
- exports.makeFnStatement = makeFnStatement;
189
- var makeExpression = function (expr) {
190
- var ast;
191
- try {
192
- ast = glsl_parser_1.parser.parse("void main() {\n a = ".concat(expr, ";\n }"), { quiet: true });
193
- }
194
- catch (error) {
195
- console.error({ expr: expr, error: error });
196
- throw new Error("Error parsing expr \"".concat(expr, "\": ").concat(error === null || error === void 0 ? void 0 : error.message));
197
- }
198
- return ast.program[0].body
199
- .statements[0].expression.right;
200
- };
201
- exports.makeExpression = makeExpression;
202
- var makeExpressionWithScopes = function (expr) {
203
- var ast;
204
- try {
205
- ast = glsl_parser_1.parser.parse("void main() {\n ".concat(expr, ";\n }"), { quiet: true });
206
- }
207
- catch (error) {
208
- console.error({ expr: expr, error: error });
209
- throw new Error("Error parsing expr \"".concat(expr, "\": ").concat(error === null || error === void 0 ? void 0 : error.message));
210
- }
211
- // log(util.inspect(ast, false, null, true));
212
- return {
213
- scope: ast.scopes[1],
214
- expression: ast.program[0].body
215
- .statements[0].expression,
216
- };
217
- };
218
- exports.makeExpressionWithScopes = makeExpressionWithScopes;
219
- var makeFnBodyStatementWithScopes = function (body) {
220
- var ast;
221
- try {
222
- ast = glsl_parser_1.parser.parse("void main() {\n".concat(body, "\n }"), { quiet: true });
223
- }
224
- catch (error) {
225
- console.error({ body: body, error: error });
226
- throw new Error("Error parsing body \"".concat(body, "\": ").concat(error === null || error === void 0 ? void 0 : error.message));
227
- }
228
- // log(util.inspect(ast, false, null, true));
229
- return {
230
- scope: ast.scopes[1],
231
- statements: ast.program[0].body.statements,
232
- };
233
- };
234
- exports.makeFnBodyStatementWithScopes = makeFnBodyStatementWithScopes;
235
- var findFn = function (ast, name) {
236
- return ast.program.find(function (stmt) {
237
- return stmt.type === 'function' && stmt.prototype.header.name.identifier === name;
238
- });
239
- };
240
- exports.findFn = findFn;
241
- var returnGlPosition = function (fnName, ast) {
242
- return convertVertexMain(fnName, ast, 'vec4', function (assign) { return assign.expression.right; });
243
- };
244
- exports.returnGlPosition = returnGlPosition;
245
- var returnGlPositionHardCoded = function (fnName, ast, returnType, hardCodedReturn) {
246
- return convertVertexMain(fnName, ast, returnType, function () {
247
- return (0, exports.makeExpression)(hardCodedReturn);
248
- });
249
- };
250
- exports.returnGlPositionHardCoded = returnGlPositionHardCoded;
251
- var returnGlPositionVec3Right = function (fnName, ast) {
252
- return convertVertexMain(fnName, ast, 'vec3', function (assign) {
253
- var found;
254
- (0, ast_1.visit)(assign, {
255
- function_call: {
256
- enter: function (path) {
257
- var _a, _b, _c, _d, _e;
258
- var node = path.node;
259
- if (((_b = (_a = node === null || node === void 0 ? void 0 : node.identifier) === null || _a === void 0 ? void 0 : _a.specifier) === null || _b === void 0 ? void 0 : _b.token) === 'vec4' &&
260
- ((_e = (_d = (_c = node === null || node === void 0 ? void 0 : node.args) === null || _c === void 0 ? void 0 : _c[2]) === null || _d === void 0 ? void 0 : _d.token) === null || _e === void 0 ? void 0 : _e.includes('1.'))) {
261
- found = node.args[0];
262
- }
263
- },
264
- },
265
- });
266
- if (!found) {
267
- console.error((0, glsl_parser_1.generate)(ast));
268
- throw new Error('Could not find position assignment to convert to return!');
269
- }
270
- return found;
271
- });
272
- };
273
- exports.returnGlPositionVec3Right = returnGlPositionVec3Right;
274
- var convertVertexMain = function (fnName, ast, returnType, generateRight) {
275
- var mainReturnVar = "frogOut";
276
- var main = (0, exports.findFn)(ast, fnName);
277
- if (!main) {
278
- throw new Error("No ".concat(fnName, " fn found!"));
279
- }
280
- // Convert the main function to one that returns
281
- main.prototype.header.returnType.specifier.specifier.token =
282
- returnType;
283
- // Find the gl_position assignment line
284
- var assign = main.body.statements.find(function (stmt) {
285
- var _a;
286
- return stmt.type === 'expression_statement' &&
287
- ((_a = stmt.expression.left) === null || _a === void 0 ? void 0 : _a.identifier) === 'gl_Position';
288
- });
289
- if (!assign) {
290
- throw new Error("No gl position assign found in main fn!");
291
- }
292
- var rtnStmt = (0, exports.makeFnStatement)("".concat(returnType, " ").concat(mainReturnVar, " = 1.0"));
293
- rtnStmt.declaration.declarations[0].initializer =
294
- generateRight(assign);
295
- main.body.statements.splice(main.body.statements.indexOf(assign), 1, rtnStmt);
296
- main.body.statements.push((0, exports.makeFnStatement)("return ".concat(mainReturnVar)));
297
- };
298
- var convert300MainToReturn = function (suffix, ast) {
299
- var mainReturnVar = "frogOut_".concat(suffix);
300
- // Find the output variable, as in "pc_fragColor" from "out highp vec4 pc_fragColor;"
301
- var outName;
302
- ast.program.find(function (line, index) {
303
- var _a, _b;
304
- var declaration = line === null || line === void 0 ? void 0 : line.declaration;
305
- if (
306
- // line.type === 'declaration_statement' &&
307
- ((_b = (_a = declaration === null || declaration === void 0 ? void 0 : declaration.specified_type) === null || _a === void 0 ? void 0 : _a.qualifiers) === null || _b === void 0 ? void 0 : _b.find(function (n) { return n.token === 'out'; })) &&
308
- declaration.specified_type.specifier.specifier.token ===
309
- 'vec4') {
310
- // Remove the out declaration
311
- ast.program.splice(index, 1);
312
- outName = declaration.declarations[0].identifier.identifier;
313
- return true;
314
- }
315
- });
316
- if (!outName) {
317
- console.error((0, glsl_parser_1.generate)(ast));
318
- throw new Error('No "out vec4" line found in the fragment shader');
319
- }
320
- ast.program.unshift((0, exports.makeStatement)("vec4 ".concat(mainReturnVar)));
321
- (0, ast_1.visit)(ast, {
322
- identifier: {
323
- enter: function (path) {
324
- if (path.node.identifier === outName) {
325
- path.node.identifier = mainReturnVar;
326
- // @ts-ignore
327
- path.node.doNotDescope = true; // hack because this var is in the scope which gets renamed later
328
- }
329
- },
330
- },
331
- function: {
332
- enter: function (path) {
333
- if (path.node.prototype.header.name.identifier === 'main') {
334
- path.node.prototype.header.returnType.specifier
335
- .specifier.token = 'vec4';
336
- path.node.body.statements.push((0, exports.makeFnStatement)("return ".concat(mainReturnVar)));
337
- }
338
- },
339
- },
340
- });
341
- };
342
- exports.convert300MainToReturn = convert300MainToReturn;
343
- var generateFiller = function (filler) {
344
- if (!filler) {
345
- throw new Error('Cannot generate void filler!');
346
- }
347
- return Array.isArray(filler)
348
- ? filler.map(glsl_parser_1.generate).join('')
349
- : (0, glsl_parser_1.generate)(filler);
350
- };
351
- exports.generateFiller = generateFiller;
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ensure = void 0;
4
- var ensure = function (argument, message) {
5
- if (message === void 0) { message = 'This value was promised to be there.'; }
6
- if (argument === undefined || argument === null) {
7
- throw new TypeError(message);
8
- }
9
- return argument;
10
- };
11
- exports.ensure = ensure;
package/cjs/util/id.js DELETED
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.makeId = void 0;
4
- var counter = 0;
5
- var makeId = function () { return "".concat(Date.now(), "_").concat(counter++); };
6
- exports.makeId = makeId;