@genart-dev/core 0.1.2 → 0.2.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.
package/dist/index.cjs CHANGED
@@ -22,6 +22,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
22
22
  var index_exports = {};
23
23
  __export(index_exports, {
24
24
  COLOR_SKILLS: () => COLOR_SKILLS,
25
+ COMPONENT_REGISTRY: () => import_components.COMPONENT_REGISTRY,
25
26
  COMPOSITION_SKILLS: () => COMPOSITION_SKILLS,
26
27
  Canvas2DRendererAdapter: () => Canvas2DRendererAdapter,
27
28
  GLSLRendererAdapter: () => GLSLRendererAdapter,
@@ -32,10 +33,27 @@ __export(index_exports, {
32
33
  ThreeRendererAdapter: () => ThreeRendererAdapter,
33
34
  createDefaultRegistry: () => createDefaultRegistry,
34
35
  createDefaultSkillRegistry: () => createDefaultSkillRegistry,
35
- hexToVec3: () => hexToVec3
36
+ hexToVec3: () => hexToVec3,
37
+ resolveComponents: () => import_components.resolveComponents
36
38
  });
37
39
  module.exports = __toCommonJS(index_exports);
38
40
  __reExport(index_exports, require("@genart-dev/format"), module.exports);
41
+ var import_components = require("@genart-dev/components");
42
+
43
+ // src/sketch/adapters/component-utils.ts
44
+ function extractComponentCode(components) {
45
+ if (!components) return "";
46
+ const blocks = [];
47
+ for (const [name, value] of Object.entries(components)) {
48
+ if (typeof value === "string") continue;
49
+ if (value.code) {
50
+ const ver = value.version ? ` v${value.version}` : "";
51
+ blocks.push(`// --- ${name}${ver} ---
52
+ ${value.code}`);
53
+ }
54
+ }
55
+ return blocks.join("\n\n");
56
+ }
39
57
 
40
58
  // src/sketch/adapters/p5.ts
41
59
  var P5_CDN_VERSION = "1.11.3";
@@ -61,15 +79,20 @@ var P5RendererAdapter = class {
61
79
  }
62
80
  return { valid: errors.length === 0, errors };
63
81
  }
64
- async compile(algorithm) {
82
+ async compile(algorithm, components) {
65
83
  const validation = this.validate(algorithm);
66
84
  if (!validation.valid) {
67
85
  throw new Error(
68
86
  `p5 compilation failed: ${validation.errors.join("; ")}`
69
87
  );
70
88
  }
89
+ const componentCode = components?.map(
90
+ (c) => `// --- ${c.name} v${c.version} ---
91
+ ${c.code}`
92
+ ).join("\n\n") ?? "";
71
93
  const wrappedSource = `
72
94
  return (function() {
95
+ ${componentCode}
73
96
  ${algorithm}
74
97
  return sketch;
75
98
  })();
@@ -223,6 +246,7 @@ var P5RendererAdapter = class {
223
246
  state.PARAMS = state.params;
224
247
  state.COLORS = ${colorsJson};
225
248
 
249
+ ${extractComponentCode(sketch.components)}
226
250
  ${sketch.algorithm}
227
251
 
228
252
  new p5(function(p) {
@@ -278,15 +302,20 @@ var Canvas2DRendererAdapter = class {
278
302
  }
279
303
  return { valid: errors.length === 0, errors };
280
304
  }
281
- async compile(algorithm) {
305
+ async compile(algorithm, components) {
282
306
  const validation = this.validate(algorithm);
283
307
  if (!validation.valid) {
284
308
  throw new Error(
285
309
  `Canvas 2D compilation failed: ${validation.errors.join("; ")}`
286
310
  );
287
311
  }
312
+ const componentCode = components?.map(
313
+ (c) => `// --- ${c.name} v${c.version} ---
314
+ ${c.code}`
315
+ ).join("\n\n") ?? "";
288
316
  const wrappedSource = `
289
317
  return (function() {
318
+ ${componentCode}
290
319
  ${algorithm}
291
320
  return sketch;
292
321
  })();
@@ -446,6 +475,7 @@ var Canvas2DRendererAdapter = class {
446
475
  const state = ${stateJson};
447
476
  state.canvas = { width: ${width}, height: ${height}, pixelDensity: ${pixelDensity} };
448
477
 
478
+ ${extractComponentCode(sketch.components)}
449
479
  ${sketch.algorithm}
450
480
 
451
481
  const canvas = document.getElementById('canvas');
@@ -505,15 +535,20 @@ var ThreeRendererAdapter = class {
505
535
  }
506
536
  return { valid: true, errors: [] };
507
537
  }
508
- async compile(algorithm) {
538
+ async compile(algorithm, components) {
509
539
  const validation = this.validate(algorithm);
510
540
  if (!validation.valid) {
511
541
  throw new Error(
512
542
  `Three.js compilation failed: ${validation.errors.join("; ")}`
513
543
  );
514
544
  }
545
+ const componentCode = components?.map(
546
+ (c) => `// --- ${c.name} v${c.version} ---
547
+ ${c.code}`
548
+ ).join("\n\n") ?? "";
515
549
  const wrappedSource = `
516
550
  return (function() {
551
+ ${componentCode}
517
552
  ${algorithm}
518
553
  return sketch;
519
554
  })();
@@ -656,6 +691,7 @@ var ThreeRendererAdapter = class {
656
691
  const state = ${stateJson};
657
692
  state.canvas = { width: ${width}, height: ${height}, pixelDensity: ${pixelDensity} };
658
693
 
694
+ ${extractComponentCode(sketch.components)}
659
695
  ${sketch.algorithm}
660
696
 
661
697
  sketch(THREE, state, document.getElementById('canvas-container'));
@@ -760,6 +796,41 @@ function extractUniforms(source) {
760
796
  }
761
797
  return { params, colors };
762
798
  }
799
+ function injectGLSLSource(algorithm, codeToInject) {
800
+ if (!codeToInject) return algorithm;
801
+ const trimmed = algorithm.trimStart();
802
+ const versionMatch = trimmed.match(/^(#version\s+\S+[^\n]*\n)/);
803
+ const versionLine = versionMatch?.[1] ?? "";
804
+ let shaderBody = versionMatch ? trimmed.slice(versionLine.length) : trimmed;
805
+ const precisionLines = [];
806
+ let precisionMatch;
807
+ while ((precisionMatch = shaderBody.match(/^\s*(precision\s+\w+\s+\w+;\s*\n)/)) !== null) {
808
+ precisionLines.push(precisionMatch[1]);
809
+ shaderBody = shaderBody.slice(precisionMatch[0].length);
810
+ }
811
+ return versionLine + precisionLines.join("") + "\n" + codeToInject + "\n\n" + shaderBody;
812
+ }
813
+ function injectGLSLComponents(algorithm, components) {
814
+ if (!components || components.length === 0) return algorithm;
815
+ const componentCode = components.map(
816
+ (c) => `// --- ${c.name} v${c.version} ---
817
+ ${c.code}`
818
+ ).join("\n\n");
819
+ return injectGLSLSource(algorithm, componentCode);
820
+ }
821
+ function extractGLSLComponentCode(components) {
822
+ if (!components) return "";
823
+ const blocks = [];
824
+ for (const [name, value] of Object.entries(components)) {
825
+ if (typeof value === "string") continue;
826
+ if (value.code) {
827
+ const ver = value.version ? ` v${value.version}` : "";
828
+ blocks.push(`// --- ${name}${ver} ---
829
+ ${value.code}`);
830
+ }
831
+ }
832
+ return blocks.join("\n\n");
833
+ }
763
834
  var GLSLRendererAdapter = class {
764
835
  type = "glsl";
765
836
  displayName = "GLSL Shader";
@@ -783,16 +854,17 @@ var GLSLRendererAdapter = class {
783
854
  }
784
855
  return { valid: errors.length === 0, errors };
785
856
  }
786
- async compile(algorithm) {
857
+ async compile(algorithm, components) {
787
858
  const validation = this.validate(algorithm);
788
859
  if (!validation.valid) {
789
860
  throw new Error(
790
861
  `GLSL compilation failed: ${validation.errors.join("; ")}`
791
862
  );
792
863
  }
793
- const uniforms = extractUniforms(algorithm);
864
+ const fragmentSource = injectGLSLComponents(algorithm, components);
865
+ const uniforms = extractUniforms(fragmentSource);
794
866
  const compiled = {
795
- fragmentSource: algorithm,
867
+ fragmentSource,
796
868
  vertexSource: FULLSCREEN_QUAD_VERTEX,
797
869
  uniformNames: uniforms
798
870
  };
@@ -1038,7 +1110,9 @@ var GLSLRendererAdapter = class {
1038
1110
  const { width, height } = sketch.canvas;
1039
1111
  const pixelDensity = sketch.canvas.pixelDensity ?? 1;
1040
1112
  const stateJson = JSON.stringify(sketch.state, null, 2);
1041
- const uniforms = extractUniforms(sketch.algorithm);
1113
+ const glslComponentCode = extractGLSLComponentCode(sketch.components);
1114
+ const fullAlgorithm = glslComponentCode ? injectGLSLSource(sketch.algorithm, glslComponentCode) : sketch.algorithm;
1115
+ const uniforms = extractUniforms(fullAlgorithm);
1042
1116
  const paramBindings = uniforms.params.map((u) => {
1043
1117
  const key = u.substring(2);
1044
1118
  return ` { const loc = gl.getUniformLocation(program, "${u}"); if (loc && state.params["${key}"] !== undefined) gl.uniform1f(loc, state.params["${key}"]); }`;
@@ -1072,7 +1146,7 @@ var GLSLRendererAdapter = class {
1072
1146
  const vertSrc = \`${FULLSCREEN_QUAD_VERTEX}\`;
1073
1147
 
1074
1148
  // Fragment shader
1075
- const fragSrc = \`${sketch.algorithm.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$/g, "\\$")}\`;
1149
+ const fragSrc = \`${fullAlgorithm.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$/g, "\\$")}\`;
1076
1150
 
1077
1151
  function createShader(type, src) {
1078
1152
  const s = gl.createShader(type);
@@ -1169,15 +1243,20 @@ var SVGRendererAdapter = class {
1169
1243
  }
1170
1244
  return { valid: true, errors: [] };
1171
1245
  }
1172
- async compile(algorithm) {
1246
+ async compile(algorithm, components) {
1173
1247
  const validation = this.validate(algorithm);
1174
1248
  if (!validation.valid) {
1175
1249
  throw new Error(
1176
1250
  `SVG compilation failed: ${validation.errors.join("; ")}`
1177
1251
  );
1178
1252
  }
1253
+ const componentCode = components?.map(
1254
+ (c) => `// --- ${c.name} v${c.version} ---
1255
+ ${c.code}`
1256
+ ).join("\n\n") ?? "";
1179
1257
  const wrappedSource = `
1180
1258
  return (function() {
1259
+ ${componentCode}
1181
1260
  ${algorithm}
1182
1261
  return sketch;
1183
1262
  })();
@@ -1307,6 +1386,7 @@ var SVGRendererAdapter = class {
1307
1386
  const state = ${stateJson};
1308
1387
  state.canvas = { width: ${width}, height: ${height} };
1309
1388
 
1389
+ ${extractComponentCode(sketch.components)}
1310
1390
  ${sketch.algorithm}
1311
1391
 
1312
1392
  const module = sketch(state);
@@ -1841,6 +1921,7 @@ function createDefaultSkillRegistry() {
1841
1921
  // Annotate the CommonJS export names for ESM import in node:
1842
1922
  0 && (module.exports = {
1843
1923
  COLOR_SKILLS,
1924
+ COMPONENT_REGISTRY,
1844
1925
  COMPOSITION_SKILLS,
1845
1926
  Canvas2DRendererAdapter,
1846
1927
  GLSLRendererAdapter,
@@ -1852,6 +1933,7 @@ function createDefaultSkillRegistry() {
1852
1933
  createDefaultRegistry,
1853
1934
  createDefaultSkillRegistry,
1854
1935
  hexToVec3,
1936
+ resolveComponents,
1855
1937
  ...require("@genart-dev/format")
1856
1938
  });
1857
1939
  //# sourceMappingURL=index.cjs.map