@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,123 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ export var numberNode = function (id, name, position, value, optionals) { return ({
13
+ type: 'number',
14
+ id: id,
15
+ name: name,
16
+ position: position,
17
+ value: value,
18
+ inputs: (optionals === null || optionals === void 0 ? void 0 : optionals.inputs) || [],
19
+ outputs: (optionals === null || optionals === void 0 ? void 0 : optionals.outputs) || [
20
+ {
21
+ name: 'float',
22
+ id: '1',
23
+ category: 'data',
24
+ },
25
+ ],
26
+ range: optionals === null || optionals === void 0 ? void 0 : optionals.range,
27
+ stepper: optionals === null || optionals === void 0 ? void 0 : optionals.stepper,
28
+ }); };
29
+ export var numberUniformData = function (name, value, range, stepper) { return ({
30
+ type: 'number',
31
+ name: name,
32
+ value: value,
33
+ range: range,
34
+ stepper: stepper,
35
+ }); };
36
+ export var textureNode = function (id, name, position, value) { return ({
37
+ type: 'texture',
38
+ id: id,
39
+ name: name,
40
+ position: position,
41
+ value: value,
42
+ inputs: [],
43
+ outputs: [
44
+ {
45
+ name: 'texture',
46
+ id: '1',
47
+ category: 'data',
48
+ },
49
+ ],
50
+ }); };
51
+ export var textureUniformData = function (name, value) { return ({ type: 'texture', name: name, value: value }); };
52
+ export var samplerCubeNode = function (id, name, position, value) { return ({
53
+ type: 'samplerCube',
54
+ id: id,
55
+ name: name,
56
+ position: position,
57
+ value: value,
58
+ inputs: [],
59
+ outputs: [
60
+ {
61
+ name: 'samplerCube',
62
+ id: '1',
63
+ category: 'data',
64
+ },
65
+ ],
66
+ }); };
67
+ export var samplerCubeUniformData = function (name, value) { return ({ type: 'samplerCube', name: name, value: value }); };
68
+ export function arrayNode(id, name, position, value) {
69
+ return {
70
+ id: id,
71
+ name: name,
72
+ position: position,
73
+ inputs: [],
74
+ outputs: [
75
+ {
76
+ name: 'array',
77
+ id: '1',
78
+ category: 'data',
79
+ },
80
+ ],
81
+ value: value,
82
+ dimensions: value.length,
83
+ type: 'array',
84
+ };
85
+ }
86
+ export function vectorNode(id, name, position, value) {
87
+ return __assign({ id: id, name: name, position: position, inputs: [], outputs: [
88
+ {
89
+ name: "vector".concat(value.length),
90
+ id: '1',
91
+ category: 'data',
92
+ },
93
+ ] }, (value.length === 2
94
+ ? { value: value, dimensions: 2, type: 'vector2' }
95
+ : value.length === 3
96
+ ? { value: value, dimensions: 3, type: 'vector3' }
97
+ : { value: value, dimensions: 4, type: 'vector4' }));
98
+ }
99
+ export var arrayUniformData = function (name, value) { return ({
100
+ name: name,
101
+ value: value,
102
+ dimensions: value.length,
103
+ type: 'array',
104
+ }); };
105
+ export var vectorUniformData = function (name, value) { return (__assign({ name: name }, (value.length === 2
106
+ ? { value: value, dimensions: 2, type: 'vector2' }
107
+ : value.length === 3
108
+ ? { value: value, dimensions: 3, type: 'vector3' }
109
+ : { value: value, dimensions: 4, type: 'vector4' }))); };
110
+ export function colorNode(id, name, position, value) {
111
+ return __assign({ id: id, name: name, position: position, inputs: [], outputs: [
112
+ {
113
+ name: value.length === 3 ? 'rgb' : 'rgba',
114
+ id: '1',
115
+ category: 'data',
116
+ },
117
+ ] }, (value.length === 3
118
+ ? { value: value, dimensions: 3, type: 'rgb' }
119
+ : { value: value, dimensions: 4, type: 'rgba' }));
120
+ }
121
+ export var colorUniformData = function (name, value) { return (__assign({ name: name }, (value.length === 3
122
+ ? { value: value, dimensions: 3, type: 'rgb' }
123
+ : { value: value, dimensions: 4, type: 'rgba' }))); };
@@ -0,0 +1 @@
1
+ export var makeEdge = function (id, from, to, output, input, type) { return ({ id: id, from: from, to: to, output: output, input: input, type: type }); };
@@ -0,0 +1,189 @@
1
+ import { EngineNodeType } from '../engine';
2
+ import { prepopulatePropertyInputs } from '../graph';
3
+ import { NodeType } from '../graph-types';
4
+ import { assignemntToStrategy, namedAttributeStrategy, texture2DStrategy, uniformStrategy, variableStrategy, } from '../strategy';
5
+ import { SourceType, property, } from './code-nodes';
6
+ /**
7
+ * TODO: These definitions should live outside of core since I'm trying to
8
+ * refactor out this core folder to only know about nodes with config config,
9
+ * where nodes like output/phong/physical are all configured at the
10
+ * implementation level. "phong" shouldn't be in the core
11
+ */
12
+ export var sourceNode = function (id, name, position, config, source, stage, originalEngine, nextStageNodeId) { return ({
13
+ id: id,
14
+ name: name,
15
+ groupId: undefined,
16
+ type: NodeType.SOURCE,
17
+ config: config,
18
+ position: position,
19
+ inputs: [],
20
+ outputs: [
21
+ {
22
+ name: 'vector4',
23
+ category: 'data',
24
+ id: '1',
25
+ },
26
+ ],
27
+ source: source,
28
+ stage: stage,
29
+ originalEngine: originalEngine,
30
+ nextStageNodeId: nextStageNodeId,
31
+ }); };
32
+ export var outputNode = function (id, name, position, stage) { return ({
33
+ id: id,
34
+ name: name,
35
+ position: position,
36
+ groupId: undefined,
37
+ type: NodeType.OUTPUT,
38
+ config: {
39
+ version: 3,
40
+ mangle: false,
41
+ preprocess: false,
42
+ uniforms: [],
43
+ inputMapping: stage === 'fragment'
44
+ ? {
45
+ filler_frogFragOut: 'Color',
46
+ }
47
+ : {
48
+ filler_gl_Position: 'Position',
49
+ },
50
+ strategies: [
51
+ assignemntToStrategy(stage === 'fragment' ? 'frogFragOut' : 'gl_Position'),
52
+ ],
53
+ },
54
+ inputs: [],
55
+ outputs: [],
56
+ // Consumed by findVec4Constructo4
57
+ source: stage === 'fragment'
58
+ ? "\n#version 300 es\nprecision highp float;\n\nout vec4 frogFragOut;\nvoid main() {\n frogFragOut = vec4(1.0);\n}\n"
59
+ : // gl_Position isn't "out"-able apparently https://stackoverflow.com/a/24425436/743464
60
+ "\n#version 300 es\nprecision highp float;\n\nvoid main() {\n gl_Position = vec4(1.0);\n}\n",
61
+ stage: stage,
62
+ }); };
63
+ export var expressionNode = function (id, name, position, source) { return ({
64
+ id: id,
65
+ name: name,
66
+ position: position,
67
+ type: NodeType.SOURCE,
68
+ sourceType: SourceType.EXPRESSION,
69
+ groupId: undefined,
70
+ stage: undefined,
71
+ config: {
72
+ uniforms: [],
73
+ version: 3,
74
+ preprocess: false,
75
+ inputMapping: {},
76
+ strategies: [variableStrategy()],
77
+ },
78
+ inputs: [],
79
+ outputs: [
80
+ {
81
+ name: 'expression',
82
+ category: 'data',
83
+ id: '1',
84
+ },
85
+ ],
86
+ source: source,
87
+ }); };
88
+ export var phongNode = function (id, name, groupId, position, stage, nextStageNodeId) {
89
+ return prepopulatePropertyInputs({
90
+ id: id,
91
+ name: name,
92
+ groupId: groupId,
93
+ position: position,
94
+ type: EngineNodeType.phong,
95
+ config: {
96
+ version: 3,
97
+ uniforms: [],
98
+ preprocess: true,
99
+ mangle: false,
100
+ properties: [
101
+ property('Color', 'color', 'rgb', 'uniform_diffuse'),
102
+ property('Emissive', 'emissive', 'rgb', 'uniform_emissive'),
103
+ property('Emissive Map', 'emissiveMap', 'texture', 'filler_emissiveMap'),
104
+ property('Emissive Intensity', 'emissiveIntensity', 'number', 'uniform_emissive'),
105
+ property('Texture', 'map', 'texture', 'filler_map'),
106
+ property('Normal Map', 'normalMap', 'texture', 'filler_normalMap'),
107
+ property('Normal Scale', 'normalScale', 'vector2'),
108
+ property('Shininess', 'shininess', 'number'),
109
+ property('Reflectivity', 'reflectivity', 'number'),
110
+ property('Refraction Ratio', 'refractionRatio', 'number'),
111
+ property('Specular', 'specular', 'rgb', 'uniform_specular'),
112
+ property('Specular Map', 'specularMap', 'texture', 'filler_specularMap'),
113
+ property('Displacement Map', 'displacementMap', 'texture'),
114
+ property('Env Map', 'envMap', 'samplerCube'),
115
+ ],
116
+ strategies: [
117
+ uniformStrategy(),
118
+ stage === 'fragment'
119
+ ? texture2DStrategy()
120
+ : namedAttributeStrategy('position'),
121
+ ],
122
+ },
123
+ inputs: [],
124
+ outputs: [
125
+ {
126
+ name: 'vector4',
127
+ category: 'data',
128
+ id: '1',
129
+ },
130
+ ],
131
+ source: '',
132
+ stage: stage,
133
+ nextStageNodeId: nextStageNodeId,
134
+ });
135
+ };
136
+ export var addNode = function (id, position) { return ({
137
+ id: id,
138
+ name: 'add',
139
+ position: position,
140
+ type: NodeType.BINARY,
141
+ groupId: undefined,
142
+ stage: undefined,
143
+ config: {
144
+ mangle: false,
145
+ version: 3,
146
+ preprocess: true,
147
+ strategies: [],
148
+ uniforms: [],
149
+ },
150
+ inputs: [],
151
+ outputs: [
152
+ {
153
+ name: 'sum',
154
+ category: 'data',
155
+ id: '1',
156
+ },
157
+ ],
158
+ source: "a + b",
159
+ operator: '+',
160
+ sourceType: SourceType.EXPRESSION,
161
+ biStage: true,
162
+ }); };
163
+ export var multiplyNode = function (id, position) { return ({
164
+ id: id,
165
+ name: 'multiply',
166
+ type: NodeType.BINARY,
167
+ groupId: undefined,
168
+ stage: undefined,
169
+ position: position,
170
+ config: {
171
+ version: 3,
172
+ uniforms: [],
173
+ mangle: false,
174
+ preprocess: true,
175
+ strategies: [],
176
+ },
177
+ inputs: [],
178
+ outputs: [
179
+ {
180
+ name: 'product',
181
+ category: 'data',
182
+ id: '1',
183
+ },
184
+ ],
185
+ source: "a * b",
186
+ operator: '*',
187
+ sourceType: SourceType.EXPRESSION,
188
+ biStage: true,
189
+ }); };
@@ -0,0 +1,213 @@
1
+ var __read = (this && this.__read) || function (o, n) {
2
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
3
+ if (!m) return o;
4
+ var i = m.call(o), r, ar = [], e;
5
+ try {
6
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
7
+ }
8
+ catch (error) { e = { error: error }; }
9
+ finally {
10
+ try {
11
+ if (r && !r.done && (m = i["return"])) m.call(i);
12
+ }
13
+ finally { if (e) throw e.error; }
14
+ }
15
+ return ar;
16
+ };
17
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
18
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
19
+ if (ar || !(i in from)) {
20
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
21
+ ar[i] = from[i];
22
+ }
23
+ }
24
+ return to.concat(ar || Array.prototype.slice.call(from));
25
+ };
26
+ var _a;
27
+ import { generate, parser } from '@shaderfrog/glsl-parser';
28
+ import { visit, } from '@shaderfrog/glsl-parser/ast';
29
+ import preprocess from '@shaderfrog/glsl-parser/preprocessor';
30
+ import { convert300MainToReturn, from2To3, makeExpression, makeExpressionWithScopes, makeFnBodyStatementWithScopes, makeFnStatement, } from './ast/manipulate';
31
+ import { applyStrategy } from './strategy';
32
+ import { SourceType } from './nodes/code-nodes';
33
+ import { nodeInput } from './nodes/core-node';
34
+ import { MAGIC_OUTPUT_STMTS, NodeType } from './graph-types';
35
+ import { nodeName } from './graph';
36
+ import { generateFiller } from './util/ast';
37
+ /*
38
+ * Core graph parsers, which is the plumbing/interface the graph and context
39
+ * calls into, to parse, find inputs, etc, and define this per-node type.
40
+ */
41
+ var log = function () {
42
+ var _a;
43
+ var args = [];
44
+ for (var _i = 0; _i < arguments.length; _i++) {
45
+ args[_i] = arguments[_i];
46
+ }
47
+ return (_a = console.log).call.apply(_a, __spreadArray([console, '\x1b[31m(core.parsers)\x1b[0m'], __read(args), false));
48
+ };
49
+ export var alphabet = 'abcdefghijklmnopqrstuvwxyz';
50
+ export var coreParsers = (_a = {},
51
+ _a[NodeType.SOURCE] = {
52
+ produceAst: function (engineContext, engine, graph, node, inputEdges) {
53
+ var ast;
54
+ // @ts-ignore
55
+ if (node.expressionOnly) {
56
+ node.sourceType = SourceType.EXPRESSION;
57
+ // @ts-ignore
58
+ delete node.expressionOnly;
59
+ }
60
+ if (node.sourceType === SourceType.FN_BODY_FRAGMENT) {
61
+ var _a = makeFnBodyStatementWithScopes(node.source), statements = _a.statements, scope = _a.scope;
62
+ ast = {
63
+ type: 'program',
64
+ scopes: [scope],
65
+ // @ts-ignore
66
+ program: statements,
67
+ };
68
+ }
69
+ else if (node.sourceType === SourceType.EXPRESSION) {
70
+ var _b = makeExpressionWithScopes(node.source), expression = _b.expression, scope = _b.scope;
71
+ ast = {
72
+ type: 'program',
73
+ scopes: [scope],
74
+ // @ts-ignore
75
+ program: [expression],
76
+ };
77
+ }
78
+ else {
79
+ var preprocessed = node.config.preprocess === false
80
+ ? node.source
81
+ : preprocess(node.source, {
82
+ preserve: {
83
+ version: function () { return true; },
84
+ },
85
+ });
86
+ ast = parser.parse(preprocessed);
87
+ if (node.config.version === 2 && node.stage) {
88
+ from2To3(ast, node.stage);
89
+ log('converted ', node, 'to version 3', {
90
+ code: generate(ast),
91
+ });
92
+ }
93
+ // This assumes that expressionOnly nodes don't have a stage and that all
94
+ // fragment source code shades have main function, which is probably wrong
95
+ if (node.stage === 'fragment') {
96
+ convert300MainToReturn(node.id, ast);
97
+ }
98
+ }
99
+ return ast;
100
+ },
101
+ findInputs: function (engineContext, node, ast) {
102
+ var seen = new Set();
103
+ return node.config.strategies
104
+ .flatMap(function (strategy) { return applyStrategy(strategy, node, ast); })
105
+ .filter(function (_a) {
106
+ var _b = __read(_a, 2), input = _b[0], _ = _b[1];
107
+ if (!seen.has(input.id)) {
108
+ seen.add(input.id);
109
+ return true;
110
+ }
111
+ return false;
112
+ });
113
+ },
114
+ produceFiller: function (node, ast) {
115
+ return node.sourceType === SourceType.EXPRESSION
116
+ ? ast.program[0]
117
+ : node.sourceType === SourceType.FN_BODY_FRAGMENT
118
+ ? ast.program
119
+ : makeExpression("".concat(nodeName(node), "()"));
120
+ },
121
+ },
122
+ // TODO: Output node assumes strategies are still passed in on node creation,
123
+ // which might be a little awkward for graph creators?
124
+ _a[NodeType.OUTPUT] = {
125
+ produceAst: function (engineContext, engine, graph, node, inputEdges) {
126
+ return parser.parse(node.source);
127
+ },
128
+ findInputs: function (engineContext, node, ast) {
129
+ return __spreadArray(__spreadArray([], __read(node.config.strategies.flatMap(function (strategy) {
130
+ return applyStrategy(strategy, node, ast);
131
+ })), false), [
132
+ [
133
+ nodeInput(MAGIC_OUTPUT_STMTS, "filler_".concat(MAGIC_OUTPUT_STMTS), 'filler', 'rgba', new Set(['code']), false),
134
+ function (fillerAst) {
135
+ var fn = ast.program.find(function (stmt) { return stmt.type === 'function'; });
136
+ fn === null || fn === void 0 ? void 0 : fn.body.statements.unshift(makeFnStatement(generateFiller(fillerAst)));
137
+ return ast;
138
+ },
139
+ ],
140
+ ], false);
141
+ },
142
+ produceFiller: function (node, ast) {
143
+ return makeExpression('impossible_call()');
144
+ },
145
+ },
146
+ _a[NodeType.BINARY] = {
147
+ produceAst: function (engineContext, engine, graph, iNode, inputEdges) {
148
+ var node = iNode;
149
+ return makeExpression('(' +
150
+ (inputEdges.length
151
+ ? inputEdges
152
+ .map(function (_, index) { return alphabet.charAt(index); })
153
+ .join(" ".concat(node.operator, " "))
154
+ : "a ".concat(node.operator, " b")) +
155
+ ')');
156
+ },
157
+ findInputs: function (engineContext, node, ast, inputEdges) {
158
+ return new Array(Math.max(inputEdges.length + 1, 2))
159
+ .fill(0)
160
+ .map(function (_, index) {
161
+ var letter = alphabet.charAt(index);
162
+ return [
163
+ nodeInput(letter, letter, 'filler', undefined, new Set(['data', 'code']), false),
164
+ function (fillerAst) {
165
+ var foundPath;
166
+ var visitors = {
167
+ identifier: {
168
+ enter: function (path) {
169
+ if (path.node.identifier === letter) {
170
+ foundPath = path;
171
+ }
172
+ },
173
+ },
174
+ };
175
+ visit(ast, visitors);
176
+ if (!foundPath) {
177
+ throw new Error("Im drunk and I think this case is impossible, no \"".concat(letter, "\" found in binary node?"));
178
+ }
179
+ if (foundPath.parent && foundPath.key) {
180
+ // @ts-ignore
181
+ foundPath.parent[foundPath.key] = fillerAst;
182
+ return ast;
183
+ }
184
+ else {
185
+ return fillerAst;
186
+ }
187
+ },
188
+ ];
189
+ });
190
+ },
191
+ produceFiller: function (node, ast) {
192
+ return ast;
193
+ },
194
+ evaluate: function (node, inputEdges, inputNodes, evaluateNode) {
195
+ var operator = node.operator;
196
+ return inputNodes.map(evaluateNode).reduce(function (num, next) {
197
+ if (operator === '+') {
198
+ return num + next;
199
+ }
200
+ else if (operator === '*') {
201
+ return num * next;
202
+ }
203
+ else if (operator === '-') {
204
+ return num - next;
205
+ }
206
+ else if (operator === '/') {
207
+ return num / next;
208
+ }
209
+ throw new Error("Don't know how to evaluate ".concat(operator, " for node ").concat(node.name, " (").concat(node.id, ")"));
210
+ });
211
+ },
212
+ },
213
+ _a);