@shaderfrog/core 1.0.10 → 1.1.0

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 (45) hide show
  1. package/README.md +7 -0
  2. package/dist/engine.d.ts +8 -5
  3. package/dist/engine.js +1 -1
  4. package/dist/graph/base-node.d.ts +5 -3
  5. package/dist/graph/code-nodes.d.ts +6 -8
  6. package/dist/graph/code-nodes.js +1 -1
  7. package/dist/graph/context.d.ts +7 -5
  8. package/dist/graph/context.js +44 -31
  9. package/dist/graph/data-nodes.d.ts +22 -22
  10. package/dist/graph/data-nodes.js +9 -1
  11. package/dist/graph/edge.d.ts +3 -2
  12. package/dist/graph/edge.js +6 -0
  13. package/dist/graph/{engine-node.d.ts → graph-node.d.ts} +1 -8
  14. package/dist/graph/{engine-node.js → graph-node.js} +11 -65
  15. package/dist/graph/graph-types.d.ts +13 -0
  16. package/dist/graph/graph-types.js +15 -0
  17. package/dist/graph/graph.d.ts +22 -12
  18. package/dist/graph/graph.js +107 -50
  19. package/dist/graph/graph.test.js +3 -2
  20. package/dist/graph/index.d.ts +1 -1
  21. package/dist/graph/index.js +1 -1
  22. package/dist/graph/parsers.d.ts +2 -2
  23. package/dist/graph/parsers.js +7 -7
  24. package/dist/plugins/babylon/bablyengine.d.ts +2 -2
  25. package/dist/plugins/babylon/bablyengine.js +23 -16
  26. package/dist/plugins/playcanvas/playengine.d.ts +2 -2
  27. package/dist/plugins/playcanvas/playengine.js +32 -25
  28. package/dist/plugins/three/threngine.d.ts +5 -5
  29. package/dist/plugins/three/threngine.js +103 -38
  30. package/dist/strategy/assignemntTo.js +2 -2
  31. package/dist/strategy/declarationOf.js +2 -2
  32. package/dist/strategy/hardCode.js +1 -1
  33. package/dist/strategy/index.d.ts +1 -0
  34. package/dist/strategy/index.js +1 -0
  35. package/dist/strategy/inject.js +2 -2
  36. package/dist/strategy/namedAttribute.js +2 -2
  37. package/dist/strategy/strategy.d.ts +2 -2
  38. package/dist/strategy/strategy.js +1 -1
  39. package/dist/strategy/stratgies.test.js +7 -7
  40. package/dist/strategy/texture2D.js +2 -2
  41. package/dist/strategy/uniform.js +3 -3
  42. package/dist/strategy/variable.js +2 -2
  43. package/dist/util/ast.js +0 -1
  44. package/dist/util/id.js +1 -1
  45. package/package.json +4 -5
@@ -71,7 +71,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
71
71
  return to.concat(ar || Array.prototype.slice.call(from));
72
72
  };
73
73
  var _a, _b;
74
- import { ShaderLib, RawShaderMaterial, Vector2, Vector3, Vector4, Color, } from 'three';
74
+ import { ShaderLib, RawShaderMaterial, Vector2, Vector3, Vector4, Color, GLSL3, Light, Texture, MeshPhongMaterial, MeshPhysicalMaterial, MeshToonMaterial, } from 'three';
75
75
  import { NodeType } from '../../graph/graph-types';
76
76
  import { prepopulatePropertyInputs, mangleMainFn } from '../../graph/graph';
77
77
  import importers from './importers';
@@ -88,12 +88,63 @@ var log = function () {
88
88
  }
89
89
  return (_a = console.log).call.apply(_a, __spreadArray([console, '\x1b[35m(three)\x1b[0m'], __read(args), false));
90
90
  };
91
- export var physicalNode = function (id, name, groupId, position, uniforms, stage, nextStageNodeId) {
91
+ export var phongNode = function (id, name, position, uniforms, stage) {
92
92
  return prepopulatePropertyInputs({
93
93
  id: id,
94
- name: name,
95
- groupId: groupId,
94
+ name: 'MeshPhongMaterial',
96
95
  position: position,
96
+ engine: true,
97
+ type: EngineNodeType.phong,
98
+ config: {
99
+ version: 3,
100
+ uniforms: uniforms,
101
+ preprocess: true,
102
+ mangle: false,
103
+ properties: [
104
+ property('Color', 'color', 'rgb', 'uniform_diffuse'),
105
+ property('Emissive', 'emissive', 'rgb', 'uniform_emissive'),
106
+ property('Emissive Map', 'emissiveMap', 'texture', 'filler_emissiveMap'),
107
+ property('Emissive Intensity', 'emissiveIntensity', 'number', 'uniform_emissive'),
108
+ property('Texture', 'map', 'texture', 'filler_map'),
109
+ property('Normal Map', 'normalMap', 'texture', 'filler_normalMap'),
110
+ property('Normal Scale', 'normalScale', 'vector2'),
111
+ property('AO Map', 'aoMap', 'texture', 'filler_aoMap'),
112
+ property('AO Intensity', 'aoMapIntensity', 'number', 'filler_aoMapIntensity'),
113
+ property('Shininess', 'shininess', 'number'),
114
+ property('Reflectivity', 'reflectivity', 'number'),
115
+ property('Refraction Ratio', 'refractionRatio', 'number'),
116
+ property('Specular', 'specular', 'rgb', 'uniform_specular'),
117
+ property('Specular Map', 'specularMap', 'texture', 'filler_specularMap'),
118
+ property('Displacement Map', 'displacementMap', 'texture', 'filler_displacementMap'),
119
+ property('Displacement Scale', 'displacementScale', 'number'),
120
+ property('Env Map', 'envMap', 'samplerCube'),
121
+ ],
122
+ strategies: [
123
+ uniformStrategy(),
124
+ stage === 'fragment'
125
+ ? texture2DStrategy()
126
+ : namedAttributeStrategy('position'),
127
+ ],
128
+ },
129
+ inputs: [],
130
+ outputs: [
131
+ {
132
+ name: 'vector4',
133
+ dataType: 'vector4',
134
+ category: 'data',
135
+ id: '1',
136
+ },
137
+ ],
138
+ source: '',
139
+ stage: stage,
140
+ });
141
+ };
142
+ export var physicalNode = function (id, name, position, uniforms, stage) {
143
+ return prepopulatePropertyInputs({
144
+ id: id,
145
+ name: 'MeshPhysicalMaterial',
146
+ position: position,
147
+ engine: true,
97
148
  type: EngineNodeType.physical,
98
149
  config: {
99
150
  uniforms: uniforms,
@@ -103,12 +154,16 @@ export var physicalNode = function (id, name, groupId, position, uniforms, stage
103
154
  properties: [
104
155
  property('Color', 'color', 'rgb', 'uniform_diffuse'),
105
156
  property('Texture', 'map', 'texture', 'filler_map'),
157
+ property('Opacity', 'opacity', 'number'),
106
158
  property('Normal Map', 'normalMap', 'texture', 'filler_normalMap'),
107
159
  property('Normal Scale', 'normalScale', 'vector2'),
160
+ property('AO Map', 'aoMap', 'texture', 'filler_aoMap'),
161
+ property('AO Intensity', 'aoMapIntensity', 'number', 'filler_aoMapIntensity'),
108
162
  property('Metalness', 'metalness', 'number', 'uniform_metalness'),
109
163
  property('Roughness', 'roughness', 'number', 'uniform_roughness'),
110
164
  property('Roughness Map', 'roughnessMap', 'texture', 'filler_roughnessMap'),
111
- property('Displacement Map', 'displacementMap', 'texture'),
165
+ property('Displacement Map', 'displacementMap', 'texture', 'filler_displacementMap'),
166
+ property('Displacement Scale', 'displacementScale', 'number'),
112
167
  // MeshPhysicalMaterial gets envMap from the scene. MeshStandardMaterial
113
168
  // gets it from the material
114
169
  // property('Env Map', 'envMap', 'samplerCube'),
@@ -116,7 +171,9 @@ export var physicalNode = function (id, name, groupId, position, uniforms, stage
116
171
  property('Transmission Map', 'transmissionMap', 'texture', 'filler_transmissionMap'),
117
172
  property('Thickness', 'thickness', 'number'),
118
173
  property('Index of Refraction', 'ior', 'number'),
119
- property('Sheen', 'sheen', 'number'),
174
+ // Sheen only works with directional lights?
175
+ // https://discourse.threejs.org/t/meshphysicalmaterial-s-sheen/31901/6
176
+ // property('Sheen', 'sheen', 'number'),
120
177
  property('Reflectivity', 'reflectivity', 'number'),
121
178
  property('Clearcoat', 'clearcoat', 'number'),
122
179
  property('Iridescence', 'iridescence', 'number'),
@@ -138,13 +195,13 @@ export var physicalNode = function (id, name, groupId, position, uniforms, stage
138
195
  outputs: [
139
196
  {
140
197
  name: 'vector4',
198
+ dataType: 'vector4',
141
199
  category: 'data',
142
200
  id: '1',
143
201
  },
144
202
  ],
145
203
  source: '',
146
204
  stage: stage,
147
- nextStageNodeId: nextStageNodeId,
148
205
  });
149
206
  };
150
207
  var cacher = function (engineContext, graph, node, sibling, newValue) {
@@ -161,8 +218,12 @@ var cacher = function (engineContext, graph, node, sibling, newValue) {
161
218
  // TODO: We mutate the nodes here, can we avoid that later?
162
219
  node.source =
163
220
  node.stage === 'fragment' ? materialData.fragment : materialData.vertex;
164
- sibling.source =
165
- sibling.stage === 'fragment' ? materialData.fragment : materialData.vertex;
221
+ if (sibling) {
222
+ sibling.source =
223
+ sibling.stage === 'fragment'
224
+ ? materialData.fragment
225
+ : materialData.vertex;
226
+ }
166
227
  };
167
228
  var onBeforeCompileMegaShader = function (engineContext, newMat) {
168
229
  log('compiling three megashader!');
@@ -199,7 +260,7 @@ var onBeforeCompileMegaShader = function (engineContext, newMat) {
199
260
  vertex: vertex,
200
261
  };
201
262
  };
202
- var megaShaderMainpulateAst = function (engineContext, engine, graph, node, ast, inputEdges) {
263
+ var megaShaderMainpulateAst = function (engineContext, engine, graph, ast, inputEdges, node, sibling) {
203
264
  var programAst = ast;
204
265
  var mainName = 'main' || nodeName(node);
205
266
  if (node.stage === 'vertex') {
@@ -212,7 +273,7 @@ var megaShaderMainpulateAst = function (engineContext, engine, graph, node, ast,
212
273
  }
213
274
  // We specify engine nodes are mangle: false, which is the graph step that
214
275
  // handles renaming the main fn, so we have to do it ourselves
215
- mangleMainFn(programAst, node);
276
+ mangleMainFn(programAst, node, sibling);
216
277
  return programAst;
217
278
  };
218
279
  var nodeCacheKey = function (graph, node) {
@@ -234,14 +295,15 @@ var programCacheKey = function (engineContext, graph, node, sibling) {
234
295
  // The megashader source is dependent on scene information, like the number
235
296
  // and type of lights in the scene. This kinda sucks - it's duplicating
236
297
  // three's material cache key, and is coupled to how three builds shaders
237
- var _a = engineContext.runtime, three = _a.three, scene = _a.scene;
298
+ var scene = engineContext.runtime.scene;
238
299
  var lights = [];
239
300
  scene.traverse(function (obj) {
240
- if (obj instanceof three.Light) {
301
+ if (obj instanceof Light) {
241
302
  lights.push(obj.type);
242
303
  }
243
304
  });
244
305
  return ([node, sibling]
306
+ .filter(function (n) { return !!n; })
245
307
  .sort(function (a, b) { return a.id.localeCompare(b.id); })
246
308
  .map(function (n) { return nodeCacheKey(graph, n); })
247
309
  .join('-') +
@@ -250,21 +312,21 @@ var programCacheKey = function (engineContext, graph, node, sibling) {
250
312
  '|Envtex:' +
251
313
  scene.environmentTexture);
252
314
  };
253
- export var defaultPropertySetting = function (three, property) {
315
+ export var defaultPropertySetting = function (property) {
254
316
  if (property.type === 'texture') {
255
- return new three.Texture();
317
+ return new Texture();
256
318
  }
257
319
  else if (property.type === 'number') {
258
320
  return 0.5;
259
321
  }
260
322
  else if (property.type === 'rgb') {
261
- return new three.Color(1, 1, 1);
323
+ return new Color(1, 1, 1);
262
324
  }
263
325
  else if (property.type === 'rgba') {
264
- return new three.Color(1, 1, 1, 1);
326
+ return new Vector4(1, 1, 1, 1);
265
327
  }
266
328
  };
267
- var threeMaterialProperties = function (three, graph, node, sibling) {
329
+ var threeMaterialProperties = function (graph, node, sibling) {
268
330
  // Find inputs to this node that are dependent on a property of the material
269
331
  var propertyInputs = node.inputs
270
332
  .filter(function (i) { return i.property; })
@@ -282,7 +344,7 @@ var threeMaterialProperties = function (three, graph, node, sibling) {
282
344
  // Find the property itself
283
345
  var property_1 = (node.config.properties || []).find(function (p) { return p.property === propertyInput.property; });
284
346
  // Initialize the property on the material
285
- acc[property_1.property] = defaultPropertySetting(three, property_1);
347
+ acc[property_1.property] = defaultPropertySetting(property_1);
286
348
  }
287
349
  return acc;
288
350
  }, {});
@@ -310,12 +372,12 @@ var evaluateNode = function (node) {
310
372
  return node.value;
311
373
  }
312
374
  };
313
- export var toonNode = function (id, name, groupId, position, uniforms, stage, nextStageNodeId) {
375
+ export var toonNode = function (id, name, position, uniforms, stage) {
314
376
  return prepopulatePropertyInputs({
315
377
  id: id,
316
- name: name,
317
- groupId: groupId,
378
+ name: 'MeshToonMaterial',
318
379
  position: position,
380
+ engine: true,
319
381
  type: EngineNodeType.toon,
320
382
  config: {
321
383
  uniforms: uniforms,
@@ -328,7 +390,10 @@ export var toonNode = function (id, name, groupId, position, uniforms, stage, ne
328
390
  property('Gradient Map', 'gradientMap', 'texture', 'filler_gradientMap'),
329
391
  property('Normal Map', 'normalMap', 'texture', 'filler_normalMap'),
330
392
  property('Normal Scale', 'normalScale', 'vector2'),
331
- property('Displacement Map', 'displacementMap', 'texture'),
393
+ property('AO Map', 'aoMap', 'texture', 'filler_aoMap'),
394
+ property('AO Intensity', 'aoMapIntensity', 'number', 'filler_aoMapIntensity'),
395
+ property('Displacement Map', 'displacementMap', 'texture', 'filler_displacementMap'),
396
+ property('Displacement Scale', 'displacementScale', 'number'),
332
397
  property('Env Map', 'envMap', 'samplerCube'),
333
398
  ],
334
399
  strategies: [
@@ -342,17 +407,18 @@ export var toonNode = function (id, name, groupId, position, uniforms, stage, ne
342
407
  outputs: [
343
408
  {
344
409
  name: 'vector4',
410
+ dataType: 'vector4',
345
411
  category: 'data',
346
412
  id: '1',
347
413
  },
348
414
  ],
349
415
  source: '',
350
416
  stage: stage,
351
- nextStageNodeId: nextStageNodeId,
352
417
  });
353
418
  };
354
419
  export var threngine = {
355
420
  name: 'three',
421
+ displayName: 'Three.js',
356
422
  importers: importers,
357
423
  mergeOptions: {
358
424
  includePrecisions: true,
@@ -360,6 +426,7 @@ export var threngine = {
360
426
  },
361
427
  evaluateNode: evaluateNode,
362
428
  constructors: (_a = {},
429
+ _a[EngineNodeType.phong] = phongNode,
363
430
  _a[EngineNodeType.physical] = physicalNode,
364
431
  _a[EngineNodeType.toon] = toonNode,
365
432
  _a),
@@ -376,6 +443,7 @@ export var threngine = {
376
443
  'normal',
377
444
  'uv',
378
445
  'uv2',
446
+ 'tangent',
379
447
  // Varyings
380
448
  'vUv',
381
449
  'vUv2',
@@ -437,7 +505,7 @@ export var threngine = {
437
505
  ]),
438
506
  parsers: (_b = {},
439
507
  _b[NodeType.SOURCE] = {
440
- manipulateAst: function (engineContext, engine, graph, node, ast, inputEdges) {
508
+ manipulateAst: function (engineContext, engine, graph, ast, inputEdges, node, sibling) {
441
509
  var programAst = ast;
442
510
  var mainName = 'main' || nodeName(node);
443
511
  // This hinges on the vertex shader calling vec3(p)
@@ -454,11 +522,11 @@ export var threngine = {
454
522
  },
455
523
  _b[EngineNodeType.phong] = {
456
524
  onBeforeCompile: function (graph, engineContext, node, sibling) { return __awaiter(void 0, void 0, void 0, function () {
457
- var three;
458
525
  return __generator(this, function (_a) {
459
- three = engineContext.runtime.three;
460
526
  cacher(engineContext, graph, node, sibling, function () {
461
- return onBeforeCompileMegaShader(engineContext, new three.MeshPhongMaterial(__assign({ isMeshPhongMaterial: true }, threeMaterialProperties(three, graph, node, sibling))));
527
+ return onBeforeCompileMegaShader(engineContext, new MeshPhongMaterial(__assign({
528
+ // @ts-ignore
529
+ isMeshPhongMaterial: true }, threeMaterialProperties(graph, node, sibling))));
462
530
  });
463
531
  return [2 /*return*/];
464
532
  });
@@ -467,11 +535,9 @@ export var threngine = {
467
535
  },
468
536
  _b[EngineNodeType.physical] = {
469
537
  onBeforeCompile: function (graph, engineContext, node, sibling) { return __awaiter(void 0, void 0, void 0, function () {
470
- var three;
471
538
  return __generator(this, function (_a) {
472
- three = engineContext.runtime.three;
473
539
  cacher(engineContext, graph, node, sibling, function () {
474
- return onBeforeCompileMegaShader(engineContext, new three.MeshPhysicalMaterial(__assign(__assign(__assign({}, node.config.hardCodedProperties), threeMaterialProperties(three, graph, node, sibling)), { iridescence: 1.0, iridescenceIOR: 2.0 })));
540
+ return onBeforeCompileMegaShader(engineContext, new MeshPhysicalMaterial(__assign(__assign({}, node.config.hardCodedProperties), threeMaterialProperties(graph, node, sibling))));
475
541
  });
476
542
  return [2 /*return*/];
477
543
  });
@@ -480,11 +546,11 @@ export var threngine = {
480
546
  },
481
547
  _b[EngineNodeType.toon] = {
482
548
  onBeforeCompile: function (graph, engineContext, node, sibling) { return __awaiter(void 0, void 0, void 0, function () {
483
- var three;
484
549
  return __generator(this, function (_a) {
485
- three = engineContext.runtime.three;
486
550
  cacher(engineContext, graph, node, sibling, function () {
487
- return onBeforeCompileMegaShader(engineContext, new three.MeshToonMaterial(__assign({ gradientMap: new three.Texture(), isMeshToonMaterial: true }, threeMaterialProperties(three, graph, node, sibling))));
551
+ return onBeforeCompileMegaShader(engineContext, new MeshToonMaterial(__assign({ gradientMap: new Texture(),
552
+ // @ts-ignore
553
+ isMeshToonMaterial: true }, threeMaterialProperties(graph, node, sibling))));
488
554
  });
489
555
  return [2 /*return*/];
490
556
  });
@@ -495,15 +561,14 @@ export var threngine = {
495
561
  };
496
562
  export var createMaterial = function (compileResult, ctx) {
497
563
  var engineMaterial = ctx.runtime.engineMaterial;
498
- var finalUniforms = __assign(__assign(__assign(__assign({}, ShaderLib.phong.uniforms), ShaderLib.toon.uniforms), ShaderLib.physical.uniforms), { time: { value: 0 } });
564
+ var finalUniforms = __assign(__assign(__assign(__assign({}, ShaderLib.phong.uniforms), ShaderLib.toon.uniforms), ShaderLib.physical.uniforms), { time: { value: 0 }, cameraPosition: { value: new Vector3(1.0) } });
565
+ // Also the ThreeComponent's sceneConfig properties modify the material
499
566
  var initialProperties = {
500
567
  name: 'ShaderFrog Material',
501
568
  lights: true,
502
569
  uniforms: __assign({}, finalUniforms),
503
- transparent: true,
504
- opacity: 1.0,
505
570
  // See https://github.com/mrdoob/three.js/pull/26809
506
- glslVersion: '300 es',
571
+ glslVersion: GLSL3,
507
572
  vertexShader: compileResult === null || compileResult === void 0 ? void 0 : compileResult.vertexResult.replace('#version 300 es', ''),
508
573
  fragmentShader: compileResult === null || compileResult === void 0 ? void 0 : compileResult.fragmentResult.replace('#version 300 es', ''),
509
574
  };
@@ -7,14 +7,14 @@ export var assignemntToStrategy = function (assignTo) { return ({
7
7
  config: { assignTo: assignTo },
8
8
  }); };
9
9
  // Apply the strategy
10
- export var applyAssignmentToStrategy = function (node, ast, strategy) {
10
+ export var applyAssignmentToStrategy = function (strategy, ast, graphNode, siblingNode) {
11
11
  var assignNode = findAssignmentTo(ast, strategy.config.assignTo);
12
12
  var name = strategy.config.assignTo;
13
13
  return assignNode
14
14
  ? [
15
15
  [
16
16
  nodeInput(name, "filler_".concat(name), 'filler', undefined, // Data type for what plugs into this filler
17
- new Set(['code', 'data']), false),
17
+ ['code', 'data'], false),
18
18
  function (fillerAst) {
19
19
  assignNode.expression.right =
20
20
  fillerAst;
@@ -5,14 +5,14 @@ export var declarationOfStrategy = function (declarationOf) { return ({
5
5
  type: StrategyType.DECLARATION_OF,
6
6
  config: { declarationOf: declarationOf },
7
7
  }); };
8
- export var constApplyDeclarationOf = function (node, ast, strategy) {
8
+ export var constApplyDeclarationOf = function (strategy, ast, graphNode, siblingNode) {
9
9
  var declaration = findDeclarationOf(ast, strategy.config.declarationOf);
10
10
  var name = strategy.config.declarationOf;
11
11
  return declaration
12
12
  ? [
13
13
  [
14
14
  nodeInput(name, "filler_".concat(name), 'filler', undefined, // Data type for what plugs into this filler
15
- new Set(['code', 'data']), false),
15
+ ['code', 'data'], false),
16
16
  function (fillerAst) {
17
17
  declaration.initializer = fillerAst;
18
18
  return ast;
@@ -3,7 +3,7 @@ export var hardCodeStrategy = function (inputs) { return ({
3
3
  type: StrategyType.HARD_CODE_INPUTS,
4
4
  config: { inputs: inputs },
5
5
  }); };
6
- export var applyHardCodeStrategy = function (graphNode, ast, strategy) {
6
+ export var applyHardCodeStrategy = function (strategy, ast, graphNode, siblingNode) {
7
7
  return strategy.config.inputs.map(function (input) {
8
8
  // Broken out into two lines to enforce type checking on the array.
9
9
  var ci = [
@@ -1,6 +1,7 @@
1
1
  export * from './hardCode';
2
2
  export * from './uniform';
3
3
  export * from './assignemntTo';
4
+ export * from './inject';
4
5
  export * from './declarationOf';
5
6
  export * from './texture2D';
6
7
  export * from './namedAttribute';
@@ -1,6 +1,7 @@
1
1
  export * from './hardCode';
2
2
  export * from './uniform';
3
3
  export * from './assignemntTo';
4
+ export * from './inject';
4
5
  export * from './declarationOf';
5
6
  export * from './texture2D';
6
7
  export * from './namedAttribute';
@@ -65,7 +65,7 @@ var transferWhitespace = function (to, from) {
65
65
  ]
66
66
  : [to, from];
67
67
  };
68
- export var applyInjectStrategy = function (graphNode, ast, strategy) {
68
+ export var applyInjectStrategy = function (strategy, ast, graphNode, siblingNode) {
69
69
  var program = ast.program;
70
70
  var _a = strategy.config, find = _a.find, count = _a.count, insert = _a.insert;
71
71
  // Total hack for now. Search each function body for statements that when
@@ -91,7 +91,7 @@ export var applyInjectStrategy = function (graphNode, ast, strategy) {
91
91
  return [
92
92
  [
93
93
  nodeInput(name, "filler_".concat(name), 'filler', undefined, // Data type for what plugs into this filler
94
- new Set(['code', 'data']), false),
94
+ ['code', 'data'], false),
95
95
  function (fillerAst) {
96
96
  var toInsert = Array.isArray(fillerAst)
97
97
  ? fillerAst
@@ -21,13 +21,13 @@ export var namedAttributeStrategy = function (attributeName) { return ({
21
21
  type: StrategyType.NAMED_ATTRIBUTE,
22
22
  config: { attributeName: attributeName },
23
23
  }); };
24
- export var applyNamedAttributeStrategy = function (node, ast, strategy) {
24
+ export var applyNamedAttributeStrategy = function (strategy, ast, graphNode, siblingNode) {
25
25
  var program = ast;
26
26
  var attributeName = strategy.config.attributeName;
27
27
  return [
28
28
  [
29
29
  nodeInput(attributeName, "filler_".concat(attributeName), 'filler', undefined, // Data type for what plugs into this filler
30
- new Set(['code', 'data']), true),
30
+ ['code', 'data'], true),
31
31
  function (fillerAst) {
32
32
  Object.entries(program.scopes[0].bindings).forEach(function (_a) {
33
33
  var _b = __read(_a, 2), name = _b[0], binding = _b[1];
@@ -24,5 +24,5 @@ export interface BaseStrategy {
24
24
  config: Object;
25
25
  }
26
26
  export type Strategy = UniformStrategy | AssignemntToStrategy | Texture2DStrategy | NamedAttributeStrategy | VariableStrategy | HardCodeStrategy | InjectStrategy | DeclarationOfStrategy;
27
- export type ApplyStrategy<T> = (node: SourceNode, ast: AstNode | Program, strategy: T) => ComputedInput[];
28
- export declare const applyStrategy: (strategy: Strategy, node: SourceNode, ast: AstNode | Program) => ComputedInput[];
27
+ export type ApplyStrategy<T> = (strategy: T, ast: AstNode | Program, node: SourceNode, sibling: SourceNode) => ComputedInput[];
28
+ export declare const applyStrategy: (strategy: Strategy, ast: AstNode | Program, node: SourceNode, sibling: SourceNode) => ComputedInput[];
@@ -28,4 +28,4 @@ var strategyRunners = (_a = {},
28
28
  _a[StrategyType.NAMED_ATTRIBUTE] = applyNamedAttributeStrategy,
29
29
  _a[StrategyType.VARIABLE] = applyVariableStrategy,
30
30
  _a);
31
- export var applyStrategy = function (strategy, node, ast) { return strategyRunners[strategy.type](node, ast, strategy); };
31
+ export var applyStrategy = function (strategy, ast, node, sibling) { return strategyRunners[strategy.type](strategy, ast, node, sibling); };
@@ -39,7 +39,7 @@ it('named attribute strategy`', function () {
39
39
  config: {
40
40
  attributeName: 'replaceThisAtrribute',
41
41
  },
42
- }, { source: source }, ast);
42
+ }, ast, { source: source }, {});
43
43
  expect(fillers.length).toBe(1);
44
44
  fillers[0][1]({
45
45
  type: 'literal',
@@ -60,7 +60,7 @@ it('inject strategy after', function () {
60
60
  insert: 'after',
61
61
  count: Infinity,
62
62
  },
63
- }, { source: source }, ast);
63
+ }, ast, { source: source }, {});
64
64
  expect(fillers.length).toBe(1);
65
65
  fillers[0][1]({
66
66
  type: 'literal',
@@ -81,7 +81,7 @@ it('inject strategy before', function () {
81
81
  insert: 'before',
82
82
  count: Infinity,
83
83
  },
84
- }, { source: source }, ast);
84
+ }, ast, { source: source }, {});
85
85
  expect(fillers.length).toBe(1);
86
86
  fillers[0][1]({
87
87
  type: 'literal',
@@ -95,7 +95,7 @@ it('inject strategy before', function () {
95
95
  it('correctly fills with uniform strategy', function () {
96
96
  var _a, _b, _c;
97
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);
98
+ var fillers = applyStrategy({ type: StrategyType.UNIFORM, config: {} }, ast, {}, {});
99
99
  // It should find uniforms with simple types, excluding sampler2D
100
100
  expect(fillers.map(function (_a) {
101
101
  var _b = __read(_a, 1), name = _b[0].displayName;
@@ -135,14 +135,14 @@ it('correctly fills with uniform strategy', function () {
135
135
  });
136
136
  it('uses name without suffix for single call', function () {
137
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) {
138
+ expect(applyStrategy({ type: StrategyType.TEXTURE_2D, config: {} }, ast, {}, {}).map(function (_a) {
139
139
  var _b = __read(_a, 1), name = _b[0].displayName;
140
140
  return name;
141
141
  })).toEqual(['noiseImage']);
142
142
  });
143
143
  it('finds multiple texture2D inputs for one uniform', function () {
144
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) {
145
+ expect(applyStrategy({ type: StrategyType.TEXTURE_2D, config: {} }, ast, {}, {}).map(function (_a) {
146
146
  var _b = __read(_a, 1), name = _b[0].displayName;
147
147
  return name;
148
148
  })).toEqual(['noiseImage_0', 'noiseImage_1']);
@@ -157,7 +157,7 @@ it('Make sure texture2D finds preprocessed texture() call', function () {
157
157
  },
158
158
  });
159
159
  var ast = parser.parse(pp, { quiet: true });
160
- expect(applyStrategy({ type: StrategyType.TEXTURE_2D, config: {} }, {}, ast).map(function (_a) {
160
+ expect(applyStrategy({ type: StrategyType.TEXTURE_2D, config: {} }, ast, {}, {}).map(function (_a) {
161
161
  var _b = __read(_a, 1), name = _b[0].displayName;
162
162
  return name;
163
163
  })).toEqual(['normalMapx']);
@@ -31,7 +31,7 @@ export var texture2DStrategy = function () { return ({
31
31
  type: StrategyType.TEXTURE_2D,
32
32
  config: {},
33
33
  }); };
34
- export var applyTexture2DStrategy = function (node, ast, strategy) {
34
+ export var applyTexture2DStrategy = function (strategy, ast, graphNode, siblingNode) {
35
35
  var texture2Dcalls = [];
36
36
  var seen = {};
37
37
  var visitors = {
@@ -70,7 +70,7 @@ export var applyTexture2DStrategy = function (node, ast, strategy) {
70
70
  var iName = names.has(name) ? "".concat(name, "_").concat(index) : name;
71
71
  return [
72
72
  nodeInput(iName, "filler_".concat(iName), 'filler', 'vector4', // Data type for what plugs into this filler
73
- new Set(['code', 'data']), false),
73
+ ['code', 'data'], false),
74
74
  function (fillerAst) {
75
75
  // @ts-ignore
76
76
  parent[key] = fillerAst;
@@ -128,7 +128,7 @@ var mapUniformType = function (type) {
128
128
  }
129
129
  // console.log(`Unknown uniform type, can't map to graph: ${type}`);
130
130
  };
131
- export var applyUniformStrategy = function (graphNode, ast, strategy) {
131
+ export var applyUniformStrategy = function (strategy, ast, graphNode, siblingNode) {
132
132
  var program = ast;
133
133
  return (program.program || []).flatMap(function (node) {
134
134
  var _a, _b, _c, _d, _e, _f, _g;
@@ -153,9 +153,9 @@ export var applyUniformStrategy = function (graphNode, ast, strategy) {
153
153
  // the name "x" but the ast now has the mangled name "x_1". So
154
154
  // here, we look for the *mangled* name in the strategy runner
155
155
  return names.map(function (name) { return [
156
- nodeInput(name, "uniform_".concat(name), 'uniform', graphDataType, new Set(['code', 'data']), true),
156
+ nodeInput(name, "uniform_".concat(name), 'uniform', graphDataType, ['code', 'data'], true),
157
157
  function (filler) {
158
- var mangledName = mangleName(name, graphNode);
158
+ var mangledName = mangleName(name, graphNode, siblingNode);
159
159
  // Remove the declaration line, or the declared uniform
160
160
  if (declarations_1.length === 1) {
161
161
  program.program.splice(program.program.indexOf(node), 1);
@@ -41,7 +41,7 @@ export var variableStrategy = function () { return ({
41
41
  type: StrategyType.VARIABLE,
42
42
  config: {},
43
43
  }); };
44
- export var applyVariableStrategy = function (node, ast, strategy) {
44
+ export var applyVariableStrategy = function (strategy, ast, graphNode, siblingNode) {
45
45
  var program = ast;
46
46
  return Object.values(program.scopes.reduce(function (acc, scope) { return (__assign(__assign({}, acc), scope.bindings)); }, {})).flatMap(function (binding) {
47
47
  return binding.references.reduce(function (acc, ref) {
@@ -71,7 +71,7 @@ export var applyVariableStrategy = function (node, ast, strategy) {
71
71
  return __spreadArray(__spreadArray([], __read(acc), false), [
72
72
  [
73
73
  nodeInput(identifier, "filler_".concat(identifier), 'filler', undefined, // Data type for what plugs into this filler
74
- new Set(['code', 'data']), false),
74
+ ['code', 'data'], false),
75
75
  replacer,
76
76
  ],
77
77
  ], false);
package/dist/util/ast.js CHANGED
@@ -239,7 +239,6 @@ export var returnGlPositionVec3Right = function (fnName, ast) {
239
239
  function_call: {
240
240
  enter: function (path) {
241
241
  var _a, _b, _c, _d, _e;
242
- log('returnGlPositionVec3Right', path.node);
243
242
  var node = path.node;
244
243
  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' &&
245
244
  ((_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.'))) {
package/dist/util/id.js CHANGED
@@ -1,2 +1,2 @@
1
1
  var counter = 0;
2
- export var makeId = function () { return '' + counter++; };
2
+ export var makeId = function () { return "".concat(Date.now(), "_").concat(counter++); };
package/package.json CHANGED
@@ -1,10 +1,9 @@
1
1
  {
2
2
  "name": "@shaderfrog/core",
3
- "version": "1.0.10",
3
+ "version": "1.1.0",
4
4
  "description": "Shaderfrog core",
5
- "module": "./dist/index.js",
6
5
  "scripts": {
7
- "prepare": "npm run build",
6
+ "prepare": "npm run build && ./prepublish.sh",
8
7
  "build": "./build.sh",
9
8
  "watch-test": "jest --watch",
10
9
  "test": "jest --colors"
@@ -28,7 +27,7 @@
28
27
  "@babel/preset-typescript": "^7.21.5",
29
28
  "@types/jest": "^29.5.1",
30
29
  "@types/lodash.groupby": "^4.6.7",
31
- "@types/three": "^0.156.1",
30
+ "@types/three": "^0.156.0",
32
31
  "babel-jest": "^29.5.0",
33
32
  "babylonjs": "^6.2.0",
34
33
  "jest": "^29.5.0",
@@ -40,7 +39,7 @@
40
39
  "lodash.groupby": "^4.6.0"
41
40
  },
42
41
  "peerDependencies": {
43
- "@shaderfrog/glsl-parser": "^2.0.0",
42
+ "@shaderfrog/glsl-parser": "^2.1.5",
44
43
  "babylonjs": ">=4",
45
44
  "playcanvas": "^1.65.3",
46
45
  "three": ">=0.50"