@shaderfrog/core 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/dist/ast/manipulate.js +328 -0
  2. package/dist/ast/shader-sections.js +256 -0
  3. package/dist/context.js +230 -0
  4. package/dist/engine.js +209 -0
  5. package/dist/evaluate.js +27 -0
  6. package/dist/graph-types.js +7 -0
  7. package/dist/graph.js +381 -0
  8. package/dist/graph.test.js +168 -0
  9. package/dist/nodes/code-nodes.js +18 -0
  10. package/dist/nodes/core-node.js +9 -0
  11. package/dist/nodes/data-nodes.js +123 -0
  12. package/dist/nodes/edge.js +1 -0
  13. package/dist/nodes/engine-node.js +189 -0
  14. package/dist/parsers.js +213 -0
  15. package/dist/plugins/babylon/bablyengine.js +582 -0
  16. package/dist/plugins/babylon/importers.js +64 -0
  17. package/dist/plugins/babylon/index.js +2 -0
  18. package/dist/plugins/playcanvas/importers.js +28 -0
  19. package/dist/plugins/playcanvas/index.js +2 -0
  20. package/dist/plugins/playcanvas/playengine.js +510 -0
  21. package/dist/plugins/three/importers.js +15 -0
  22. package/dist/plugins/three/index.js +2 -0
  23. package/dist/plugins/three/threngine.js +495 -0
  24. package/dist/strategy/assignemntTo.js +26 -0
  25. package/dist/strategy/declarationOf.js +23 -0
  26. package/dist/strategy/hardCode.js +23 -0
  27. package/dist/strategy/index.js +38 -0
  28. package/dist/strategy/inject.js +122 -0
  29. package/dist/strategy/namedAttribute.js +48 -0
  30. package/dist/strategy/texture2D.js +83 -0
  31. package/dist/strategy/uniform.js +190 -0
  32. package/dist/strategy/variable.js +80 -0
  33. package/dist/stratgies.test.js +164 -0
  34. package/dist/util/ast.js +9 -0
  35. package/dist/util/ensure.js +7 -0
  36. package/dist/util/id.js +2 -0
  37. package/package.json +1 -1
@@ -0,0 +1,164 @@
1
+ var __read = (this && this.__read) || function (o, n) {
2
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
3
+ if (!m) return o;
4
+ var i = m.call(o), r, ar = [], e;
5
+ try {
6
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
7
+ }
8
+ catch (error) { e = { error: error }; }
9
+ finally {
10
+ try {
11
+ if (r && !r.done && (m = i["return"])) m.call(i);
12
+ }
13
+ finally { if (e) throw e.error; }
14
+ }
15
+ return ar;
16
+ };
17
+ import { parser } from '@shaderfrog/glsl-parser';
18
+ import { generate } from '@shaderfrog/glsl-parser';
19
+ import { applyStrategy, StrategyType } from './strategy';
20
+ import * as graphModule from './graph';
21
+ import { makeExpression } from './ast/manipulate';
22
+ import preprocess from '@shaderfrog/glsl-parser/preprocessor';
23
+ var orig;
24
+ beforeEach(function () {
25
+ orig = graphModule.mangleName;
26
+ // Terrible hack. in the real world, strategies are applied after mangling
27
+ // @ts-ignore
28
+ graphModule.mangleName = function (name) { return name; };
29
+ });
30
+ afterEach(function () {
31
+ // @ts-ignore
32
+ graphModule.mangleName = orig;
33
+ });
34
+ it('named attribute strategy`', function () {
35
+ var source = "\nin vec3 replaceThisAtrribute;\nvoid main() {\n vec2 y = replaceThisAtrribute;\n}\n";
36
+ var ast = parser.parse(source, { quiet: true });
37
+ var fillers = applyStrategy({
38
+ type: StrategyType.NAMED_ATTRIBUTE,
39
+ config: {
40
+ attributeName: 'replaceThisAtrribute',
41
+ },
42
+ }, { source: source }, ast);
43
+ expect(fillers.length).toBe(1);
44
+ fillers[0][1]({
45
+ type: 'literal',
46
+ literal: "myFiller()",
47
+ whitespace: '',
48
+ });
49
+ var result = generate(ast);
50
+ // Should replace the use of the filler, but not the declaration
51
+ expect(result).toBe("\nin vec3 replaceThisAtrribute;\nvoid main() {\n vec2 y = myFiller();\n}\n");
52
+ });
53
+ it('inject strategy after', function () {
54
+ var source = "\nuniform float x;\n// Some comment\nvoid main() {\n/* some comment */\nre(x, y, z);\n// Middle comment\nre(x, y, z);\n// Final comment\n}";
55
+ var ast = parser.parse(source, { quiet: true });
56
+ var fillers = applyStrategy({
57
+ type: StrategyType.INJECT,
58
+ config: {
59
+ find: 're(x, y, z);',
60
+ insert: 'after',
61
+ count: Infinity,
62
+ },
63
+ }, { source: source }, ast);
64
+ expect(fillers.length).toBe(1);
65
+ fillers[0][1]({
66
+ type: 'literal',
67
+ literal: "someOtherCall(x, y, z);\nsomeOtherCall(x, y, z);",
68
+ whitespace: '',
69
+ });
70
+ var result = generate(ast);
71
+ // Should fill references
72
+ expect(result).toBe("\nuniform float x;\n// Some comment\nvoid main() {\n/* some comment */\nre(x, y, z);\nsomeOtherCall(x, y, z);\nsomeOtherCall(x, y, z);\n// Middle comment\nre(x, y, z);\nsomeOtherCall(x, y, z);\nsomeOtherCall(x, y, z);\n// Final comment\n}");
73
+ });
74
+ it('inject strategy before', function () {
75
+ var source = "\nuniform float x;\n// Some comment\nvoid main() {\n/* some comment */\nre(x, y, z);\n// Middle comment\nre(x, y, z);\n// Final comment\n}";
76
+ var ast = parser.parse(source, { quiet: true });
77
+ var fillers = applyStrategy({
78
+ type: StrategyType.INJECT,
79
+ config: {
80
+ find: 're(x, y, z);',
81
+ insert: 'before',
82
+ count: Infinity,
83
+ },
84
+ }, { source: source }, ast);
85
+ expect(fillers.length).toBe(1);
86
+ fillers[0][1]({
87
+ type: 'literal',
88
+ literal: "someOtherCall(x, y, z);\nsomeOtherCall(x, y, z);",
89
+ whitespace: '\n',
90
+ });
91
+ var result = generate(ast);
92
+ // Should fill references
93
+ expect(result).toBe("\nuniform float x;\n// Some comment\nvoid main() {\n/* some comment */\nsomeOtherCall(x, y, z);\nsomeOtherCall(x, y, z);\nre(x, y, z);\n// Middle comment\nsomeOtherCall(x, y, z);\nsomeOtherCall(x, y, z);\nre(x, y, z);\n// Final comment\n}");
94
+ });
95
+ it('correctly fills with uniform strategy', function () {
96
+ var _a, _b, _c;
97
+ var ast = parser.parse("\nlayout(std140,column_major) uniform;\nuniform sampler2D image;\nuniform vec4 input, output, other;\nuniform vec4 zenput;\nuniform Light0 { vec4 y; } x;\nvec3 topLevel = vec3(0.0);\nvoid other(in vec3 param) {}\nvoid main() {\n vec4 computed = texture2D(image, uvPow * 1.0);\n vec4 x = input;\n vec4 y = output;\n vec4 z = zenput;\n}", { quiet: true });
98
+ var fillers = applyStrategy({ type: StrategyType.UNIFORM, config: {} }, {}, ast);
99
+ // It should find uniforms with simple types, excluding sampler2D
100
+ expect(fillers.map(function (_a) {
101
+ var _b = __read(_a, 1), name = _b[0].displayName;
102
+ return name;
103
+ })).toEqual([
104
+ 'image',
105
+ 'input',
106
+ 'output',
107
+ 'other',
108
+ 'zenput',
109
+ ]);
110
+ (_a = fillers.find(function (_a) {
111
+ var _b = __read(_a, 1), name = _b[0].displayName;
112
+ return name === 'input';
113
+ })) === null || _a === void 0 ? void 0 : _a[1](makeExpression('a'));
114
+ (_b = fillers.find(function (_a) {
115
+ var _b = __read(_a, 1), name = _b[0].displayName;
116
+ return name === 'output';
117
+ })) === null || _b === void 0 ? void 0 : _b[1](makeExpression('b'));
118
+ (_c = fillers.find(function (_a) {
119
+ var _b = __read(_a, 1), name = _b[0].displayName;
120
+ return name === 'zenput';
121
+ })) === null || _c === void 0 ? void 0 : _c[1](makeExpression('c'));
122
+ var result = generate(ast);
123
+ // Should fill references
124
+ expect(result).toContain('vec4 x = a;');
125
+ expect(result).toContain('vec4 y = b;');
126
+ expect(result).toContain('vec4 z = c;');
127
+ // Should preserve things it shouldn't touch
128
+ expect(result).toContain('layout(std140,column_major) uniform;');
129
+ expect(result).toContain('uniform sampler2D image;');
130
+ expect(result).toContain('uniform Light0 { vec4 y; } x;');
131
+ // Should remove uniforms from declarator list
132
+ expect(result).toContain('uniform vec4 other;');
133
+ // Should remove uniform lines
134
+ expect(result).not.toContain('uniform vec4 zenput');
135
+ });
136
+ it('uses name without suffix for single call', function () {
137
+ var ast = parser.parse("\nvoid main() {\n vec4 computed = texture2D(noiseImage, uvPow * 1.0);\n}", { quiet: true });
138
+ expect(applyStrategy({ type: StrategyType.TEXTURE_2D, config: {} }, {}, ast).map(function (_a) {
139
+ var _b = __read(_a, 1), name = _b[0].displayName;
140
+ return name;
141
+ })).toEqual(['noiseImage']);
142
+ });
143
+ it('finds multiple texture2D inputs for one uniform', function () {
144
+ var ast = parser.parse("\nvoid main() {\n vec4 computed = texture2D(noiseImage, uvPow * 1.0);\n computed += texture2D(noiseImage, uvPow * 2.0);\n}", { quiet: true });
145
+ expect(applyStrategy({ type: StrategyType.TEXTURE_2D, config: {} }, {}, ast).map(function (_a) {
146
+ var _b = __read(_a, 1), name = _b[0].displayName;
147
+ return name;
148
+ })).toEqual(['noiseImage_0', 'noiseImage_1']);
149
+ });
150
+ it('Make sure texture2D finds preprocessed texture() call', function () {
151
+ // I thought this was a regression, but it wasn't a real bug, but tests seems
152
+ // benign to keep anyway
153
+ var program = "\n#define texture2DBias texture\n\nuniform sampler2D normalMap;\n\nvoid getNormal() {\n vec3 normalMap = unpackNormal(texture2DBias(normalMap, vUv0, textureBias));\n}";
154
+ var pp = preprocess(program, {
155
+ preserve: {
156
+ version: function () { return true; },
157
+ },
158
+ });
159
+ var ast = parser.parse(pp, { quiet: true });
160
+ expect(applyStrategy({ type: StrategyType.TEXTURE_2D, config: {} }, {}, ast).map(function (_a) {
161
+ var _b = __read(_a, 1), name = _b[0].displayName;
162
+ return name;
163
+ })).toEqual(['normalMapx']);
164
+ });
@@ -0,0 +1,9 @@
1
+ import { generate } from '@shaderfrog/glsl-parser';
2
+ export var generateFiller = function (filler) {
3
+ if (!filler) {
4
+ throw new Error('Cannot generate void filler!');
5
+ }
6
+ return Array.isArray(filler)
7
+ ? filler.map(generate).join('')
8
+ : generate(filler);
9
+ };
@@ -0,0 +1,7 @@
1
+ export var ensure = function (argument, message) {
2
+ if (message === void 0) { message = 'This value was promised to be there.'; }
3
+ if (argument === undefined || argument === null) {
4
+ throw new TypeError(message);
5
+ }
6
+ return argument;
7
+ };
@@ -0,0 +1,2 @@
1
+ var counter = 0;
2
+ export var makeId = function () { return '' + counter++; };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shaderfrog/core",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Shaderfrog core",
5
5
  "main": "index.js",
6
6
  "scripts": {