@shaderfrog/core 3.0.0 → 3.0.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.
package/engine.d.ts CHANGED
@@ -77,7 +77,9 @@ export declare const extendNodesContext: (context: EngineContext, nodesContext:
77
77
  };
78
78
  };
79
79
  export type EngineImporter = {
80
- convertAst(ast: Program, type?: ShaderStage): void;
80
+ convertAst(ast: Program, options?: Record<string, unknown> & {
81
+ type?: ShaderStage;
82
+ }): void;
81
83
  nodeInputMap: Partial<Record<EngineNodeType, Record<string, string | null>>>;
82
84
  edgeMap: {
83
85
  [oldInput: string]: string;
package/engine.js CHANGED
@@ -71,8 +71,8 @@ export var convertNode = function (node, converter) {
71
71
  define: function () { return true; },
72
72
  },
73
73
  });
74
- var ast = parser.parse(preprocessed);
75
- converter.convertAst(ast, node.stage);
74
+ var ast = parser.parse(preprocessed, { stage: node.stage });
75
+ converter.convertAst(ast, { type: node.stage });
76
76
  var source = generate(ast);
77
77
  return __assign(__assign({}, node), { source: source });
78
78
  };
@@ -1,4 +1,4 @@
1
- import { type GlslSyntaxError } from '@shaderfrog/glsl-parser';
1
+ import { GlslSyntaxError } from '@shaderfrog/glsl-parser';
2
2
  import { AstNode, FunctionNode, Program } from '@shaderfrog/glsl-parser/ast';
3
3
  import { Engine, EngineContext } from '../engine';
4
4
  import { NodeInput } from './base-node';
package/graph/parsers.js CHANGED
@@ -88,7 +88,7 @@ export var coreParsers = (_a = {},
88
88
  version: function () { return true; },
89
89
  },
90
90
  });
91
- ast = parser.parse(preprocessed);
91
+ ast = parser.parse(preprocessed, { stage: node.stage });
92
92
  if (node.config.version === 2 && node.stage) {
93
93
  from2To3(ast, node.stage);
94
94
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shaderfrog/core",
3
- "version": "3.0.0",
3
+ "version": "3.0.1",
4
4
  "description": "Shaderfrog core",
5
5
  "type": "module",
6
6
  "files": [
@@ -34,7 +34,7 @@
34
34
  "@babel/core": "^7.21.8",
35
35
  "@babel/preset-env": "^7.21.5",
36
36
  "@babel/preset-typescript": "^7.21.5",
37
- "@shaderfrog/glsl-parser": "^5.3.2",
37
+ "@shaderfrog/glsl-parser": "^6.0.0-beta.7",
38
38
  "@swc/core": "^1.6.7",
39
39
  "@types/lodash.groupby": "^4.6.7",
40
40
  "@types/three": "^0.169.0",
@@ -48,7 +48,7 @@
48
48
  "lodash.groupby": "^4.6.0"
49
49
  },
50
50
  "peerDependencies": {
51
- "@shaderfrog/glsl-parser": "^5.0.0-beta.4",
51
+ "@shaderfrog/glsl-parser": "^6.0.0-beta.7",
52
52
  "babylonjs": ">=4",
53
53
  "playcanvas": "^1.65.3",
54
54
  "three": ">=0.50"
@@ -18,7 +18,7 @@ var importers = {
18
18
  // from the original shader and the right imports added
19
19
  //
20
20
  // Also need to show babylon compile errors in the UI
21
- convertAst: function (ast, type) {
21
+ convertAst: function (ast, options) {
22
22
  // Babylon has no normalmatrix. They do have a normal attribute. So undo any
23
23
  // multiplication by normalMatrix?
24
24
  var seen = {};
@@ -10,7 +10,7 @@ var nodeInputMap = {
10
10
  };
11
11
  var importers = {
12
12
  three: {
13
- convertAst: function (ast, type) {
13
+ convertAst: function (ast, options) {
14
14
  throw new Error('Not implemented');
15
15
  },
16
16
  nodeInputMap: (_a = {},
@@ -1,4 +1,4 @@
1
1
  import { EngineImporters } from '../../engine';
2
- export declare const defaultShadertoyVertex = "\nprecision highp float;\nprecision highp int;\n\nattribute vec3 position;\nattribute vec2 uv;\nvarying vec2 vUv;\n\nvoid main() {\n vUv = uv;\n gl_Position = vec4(position * 2.0, 1.0);\n}\n";
2
+ export declare const defaultShadertoyVertex = "\nprecision highp float;\nprecision highp int;\n\nuniform mat4 modelMatrix;\nuniform mat4 modelViewMatrix;\nuniform mat4 projectionMatrix;\nuniform mat4 viewMatrix;\nuniform mat3 normalMatrix;\n\nattribute vec3 position;\nattribute vec3 normal;\nattribute vec2 uv;\nattribute vec2 uv2;\n\nvarying vec2 vUv;\nvarying vec3 vPosition;\nvarying vec3 vNormal;\n\nvoid main() {\n vUv = uv;\n vPosition = position;\n vNormal = normal;\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n}\n";
3
3
  declare const importers: EngineImporters;
4
4
  export default importers;
@@ -1,20 +1,33 @@
1
1
  import { renameBindings, renameFunction, } from '@shaderfrog/glsl-parser/parser/utils';
2
2
  import { findMainOrThrow, makeStatement } from '../../util/ast';
3
- export var defaultShadertoyVertex = "\nprecision highp float;\nprecision highp int;\n\nattribute vec3 position;\nattribute vec2 uv;\nvarying vec2 vUv;\n\nvoid main() {\n vUv = uv;\n gl_Position = vec4(position * 2.0, 1.0);\n}\n";
3
+ import { range } from '../../util/math';
4
+ export var defaultShadertoyVertex = "\nprecision highp float;\nprecision highp int;\n\nuniform mat4 modelMatrix;\nuniform mat4 modelViewMatrix;\nuniform mat4 projectionMatrix;\nuniform mat4 viewMatrix;\nuniform mat3 normalMatrix;\n\nattribute vec3 position;\nattribute vec3 normal;\nattribute vec2 uv;\nattribute vec2 uv2;\n\nvarying vec2 vUv;\nvarying vec3 vPosition;\nvarying vec3 vNormal;\n\nvoid main() {\n vUv = uv;\n vPosition = position;\n vNormal = normal;\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n}\n";
4
5
  var importers = {
5
6
  shadertoy: {
6
7
  code: {
7
8
  defaultShadertoyVertex: defaultShadertoyVertex,
8
9
  },
9
- convertAst: function (ast, type) {
10
- ast.program.unshift(makeStatement('uniform vec2 renderResolution', '\n')[0]);
10
+ convertAst: function (ast, options) {
11
+ var isUv = (options === null || options === void 0 ? void 0 : options.importType) === 'uv';
12
+ var isScreen = !isUv;
13
+ if (isScreen) {
14
+ ast.program.unshift(makeStatement('uniform vec2 renderResolution', '\n')[0]);
15
+ }
16
+ else {
17
+ ast.program.unshift(makeStatement('varying vec2 vUv', '\n')[0]);
18
+ }
11
19
  // These do not catch variables in preprocessor definitions! See "SAD HACK"
12
- //if (ast.scopes.some((s) => 'iTime' in s.bindings)) {
13
- ast.program.unshift(makeStatement('uniform float time')[0]);
14
- //}
15
- //if (ast.scopes.some((s) => 'iMouse' in s.bindings)) {
16
- ast.program.unshift(makeStatement('uniform vec2 mouse')[0]);
17
- //}
20
+ if (ast.scopes.some(function (s) { return 'iTime' in s.bindings; })) {
21
+ ast.program.unshift(makeStatement('uniform float time')[0]);
22
+ }
23
+ if (ast.scopes.some(function (s) { return 'iMouse' in s.bindings; })) {
24
+ ast.program.unshift(makeStatement('uniform vec2 mouse')[0]);
25
+ }
26
+ range(0, 9).forEach(function (i) {
27
+ if (ast.scopes.some(function (s) { return "iChannel".concat(i) in s.bindings; })) {
28
+ ast.program.unshift(makeStatement("uniform sampler2D iChannel".concat(i))[0]);
29
+ }
30
+ });
18
31
  ast.program.unshift(makeStatement('precision highp int', '\n')[0]);
19
32
  ast.program.unshift(makeStatement('precision highp float')[0]);
20
33
  ast.scopes[0].functions.main = renameFunction(ast.scopes[0].functions.mainImage, 'main');
@@ -33,13 +46,23 @@ var importers = {
33
46
  return 'mouse';
34
47
  }
35
48
  if (name === 'iResolution') {
36
- return 'renderResolution';
49
+ if (isUv) {
50
+ return 'vec2(1.0)';
51
+ }
52
+ else {
53
+ return 'renderResolution';
54
+ }
37
55
  }
38
56
  if (name === 'fragColor') {
39
57
  return 'gl_FragColor';
40
58
  }
41
59
  if (name === 'fragCoord') {
42
- return 'gl_FragCoord.xy';
60
+ if (isUv) {
61
+ return 'vUv';
62
+ }
63
+ else {
64
+ return 'gl_FragCoord.xy';
65
+ }
43
66
  }
44
67
  return name;
45
68
  });
@@ -49,7 +72,7 @@ var importers = {
49
72
  edgeMap: {},
50
73
  },
51
74
  babylon: {
52
- convertAst: function (ast, type) {
75
+ convertAst: function (ast, options) {
53
76
  ast.scopes[0].bindings = renameBindings(ast.scopes[0].bindings, function (name) {
54
77
  return name === 'vMainUV1' ? 'vUv' : name === 'vNormalW' ? 'vNormal' : name;
55
78
  });
@@ -129,7 +129,7 @@ it('threngine shadertoy import', function () { return __awaiter(void 0, void 0,
129
129
  return __generator(this, function (_a) {
130
130
  testImport = "\nvoid mainImage( out vec4 fragColor, in vec2 fragCoord ){\n vec3 rd = normalize(vec3(2.*fragCoord - iResolution.xy, iResolution.y));\n fragColor = vec4(sqrt(clamp(col, 0., 1.)), 1.0 * iTime);\n}\n";
131
131
  p = parser.parse(testImport);
132
- importers.shadertoy.convertAst(p, 'fragment');
132
+ importers.shadertoy.convertAst(p, { type: 'fragment' });
133
133
  expect(generate(p)).toContain("\nprecision highp float;\nprecision highp int;\n\nuniform vec2 mouse;\nuniform float time;\nuniform vec2 renderResolution;\n\nvoid main() {\n vec3 rd = normalize(vec3(2.*gl_FragCoord.xy - renderResolution.xy, renderResolution.y));\n gl_FragColor = vec4(sqrt(clamp(col, 0., 1.)), 1.0 * time);\n}\n");
134
134
  return [2 /*return*/];
135
135
  });
package/util/math.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare const randomBetween: (min: number, max: number) => number;
2
+ export declare const range: (start: number, end: number) => number[];
package/util/math.js ADDED
@@ -0,0 +1,6 @@
1
+ export var randomBetween = function (min, max) {
2
+ return Math.random() * (max - min) + min;
3
+ };
4
+ export var range = function (start, end) {
5
+ return Array.from({ length: end - start }, function (_, i) { return i + start; });
6
+ };