circuit-json-to-gltf 0.0.145 → 0.0.146

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.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { PcbStiffener, CircuitJson, PcbBoard } from 'circuit-json';
2
+ import { MaterialOptions } from 'jscad-planner';
2
3
  import { mat4 } from 'gl-matrix';
3
4
 
4
5
  /** Circuit JSON bend geometry, board-local +Z up, millimeters. */
@@ -80,6 +81,8 @@ interface Size3 {
80
81
  z: number;
81
82
  }
82
83
  interface Triangle {
84
+ /** Authored JSCAD appearance; color tuples use linear 0–1 RGB. */
85
+ material?: MaterialOptions;
83
86
  /** Original PCB surface identity, retained after folding. */
84
87
  pcbFace?: "top" | "bottom" | "side";
85
88
  /** Flat PCB texture coordinates, retained/interpolated during tessellation. */
package/dist/index.js CHANGED
@@ -7640,7 +7640,7 @@ var require_polyhedron = __commonJS({
7640
7640
  colors: void 0,
7641
7641
  orientation: "outward"
7642
7642
  };
7643
- const { points, faces, colors, orientation } = Object.assign({}, defaults, options);
7643
+ const { points, faces, colors: colors2, orientation } = Object.assign({}, defaults, options);
7644
7644
  if (!(Array.isArray(points) && Array.isArray(faces))) {
7645
7645
  throw new Error("points and faces must be arrays");
7646
7646
  }
@@ -7650,11 +7650,11 @@ var require_polyhedron = __commonJS({
7650
7650
  if (faces.length < 1) {
7651
7651
  throw new Error("one or more faces are required");
7652
7652
  }
7653
- if (colors) {
7654
- if (!Array.isArray(colors)) {
7653
+ if (colors2) {
7654
+ if (!Array.isArray(colors2)) {
7655
7655
  throw new Error("colors must be an array");
7656
7656
  }
7657
- if (colors.length !== faces.length) {
7657
+ if (colors2.length !== faces.length) {
7658
7658
  throw new Error("faces and colors must have the same length");
7659
7659
  }
7660
7660
  }
@@ -7670,7 +7670,7 @@ var require_polyhedron = __commonJS({
7670
7670
  }
7671
7671
  const polygons = faces.map((face, findex) => {
7672
7672
  const polygon3 = poly3.create(face.map((pindex) => points[pindex]));
7673
- if (colors && colors[findex]) polygon3.color = colors[findex];
7673
+ if (colors2 && colors2[findex]) polygon3.color = colors2[findex];
7674
7674
  return polygon3;
7675
7675
  });
7676
7676
  return geom35.create(polygons);
@@ -17398,15 +17398,29 @@ var createBoardOuterLoop = (board, center) => {
17398
17398
  // lib/loaders/jscad-plan.ts
17399
17399
  var JSCAD_PLAN_TRANSFORM = COORDINATE_TRANSFORMS.CIRCUIT_Z_UP_TO_SCENE_Y_UP;
17400
17400
  var loadJscadPlan = (plan) => {
17401
- const zUpGeometry = executeJscadOperations(
17402
- jscadModeling2,
17403
- plan
17404
- );
17405
- const polygons = geom33.toPolygons(zUpGeometry);
17406
- const triangles = transformTriangles(
17407
- geom3ToTriangles(zUpGeometry, polygons),
17408
- JSCAD_PLAN_TRANSFORM
17401
+ const plans = Array.isArray(plan) ? plan.flat(Infinity) : [plan];
17402
+ const zUpGeometry = plans.map(
17403
+ (operation) => executeJscadOperations(jscadModeling2, operation)
17409
17404
  );
17405
+ const geometries = zUpGeometry.flat(Infinity);
17406
+ const sourceTriangles = geometries.flatMap((geometry) => {
17407
+ const polygons = geom33.toPolygons(geometry);
17408
+ return polygons.flatMap((polygon3) => {
17409
+ const color3 = polygon3.color ?? geometry.color;
17410
+ const material = geometry.material || color3 ? {
17411
+ ...color3 ? {
17412
+ color: color3.slice(0, 3),
17413
+ opacity: color3[3] ?? 1
17414
+ } : {},
17415
+ ...geometry.material
17416
+ } : void 0;
17417
+ return geom3ToTriangles(geometry, [polygon3]).map((triangle) => ({
17418
+ ...triangle,
17419
+ material
17420
+ }));
17421
+ });
17422
+ });
17423
+ const triangles = transformTriangles(sourceTriangles, JSCAD_PLAN_TRANSFORM);
17410
17424
  return {
17411
17425
  triangles,
17412
17426
  boundingBox: boundsOfTriangles2(triangles)
@@ -18982,6 +18996,9 @@ async function convertCircuitJsonTo3D(inputCircuitJson, options = {}) {
18982
18996
  return options.showErrors ? withCircuitJsonErrors(scene, inputCircuitJson) : scene;
18983
18997
  }
18984
18998
 
18999
+ // lib/gltf/gltf-builder.ts
19000
+ var import_modeling = __toESM(require_src(), 1);
19001
+
18985
19002
  // lib/gltf/gltf-types.ts
18986
19003
  var COMPONENT_TYPE = {
18987
19004
  BYTE: 5120,
@@ -19178,6 +19195,10 @@ var GLTFBuilder = class {
19178
19195
  await this.addOBJMeshWithMaterials(box, box.mesh);
19179
19196
  return;
19180
19197
  }
19198
+ if (box.mesh?.triangles.some((triangle) => triangle.material)) {
19199
+ this.addJscadMeshWithMaterials(box);
19200
+ return;
19201
+ }
19181
19202
  let meshData;
19182
19203
  if (box.mesh) {
19183
19204
  meshData = createMeshFromSTL(box.mesh);
@@ -19216,6 +19237,98 @@ var GLTFBuilder = class {
19216
19237
  });
19217
19238
  this.gltf.scenes[0].nodes.push(nodeIndex);
19218
19239
  }
19240
+ addJscadMeshWithMaterials(box) {
19241
+ const mesh = box.mesh;
19242
+ const groups = /* @__PURE__ */ new Map();
19243
+ for (const triangle of mesh.triangles) {
19244
+ const authored = triangle.material ?? {};
19245
+ const base = this.jscadColor(authored.color, [0.7, 0.7, 0.7]);
19246
+ const opacity = authored.opacity ?? (box.isTranslucent ? 0.5 : 1);
19247
+ const transparent = authored.transparent ?? (opacity < 1 || !!box.isTranslucent);
19248
+ const strength = authored.emissiveIntensity ?? 1;
19249
+ const index = this.addMaterial({
19250
+ pbrMetallicRoughness: {
19251
+ baseColorFactor: [...base, opacity],
19252
+ metallicFactor: authored.metalness ?? 0.1,
19253
+ roughnessFactor: authored.roughness ?? 0.9
19254
+ },
19255
+ emissiveFactor: this.jscadColor(authored.emissive, [0, 0, 0]),
19256
+ ...strength !== 1 ? {
19257
+ extensions: {
19258
+ KHR_materials_emissive_strength: { emissiveStrength: strength }
19259
+ }
19260
+ } : {},
19261
+ alphaMode: transparent ? "BLEND" : "OPAQUE",
19262
+ doubleSided: transparent || void 0
19263
+ });
19264
+ if (strength !== 1)
19265
+ this.gltf.extensionsUsed = ["KHR_materials_emissive_strength"];
19266
+ const group = groups.get(index) ?? [];
19267
+ group.push(triangle);
19268
+ groups.set(index, group);
19269
+ }
19270
+ const primitives = [...groups].map(([material, triangles]) => ({
19271
+ material,
19272
+ // Match addBox: preserve its scene-frame rotation and canonical export orientation.
19273
+ meshData: convertMeshToGLTFOrientation(
19274
+ transformMesh(
19275
+ createMeshFromSTL({ triangles, boundingBox: mesh.boundingBox }),
19276
+ { x: 0, y: 0, z: 0 },
19277
+ box.rotation
19278
+ )
19279
+ )
19280
+ }));
19281
+ const meshIndex = this.addMultiPrimitiveMesh(
19282
+ primitives,
19283
+ box.label || `JscadMesh${this.meshes.length}`,
19284
+ "jscad"
19285
+ );
19286
+ const nodeIndex = this.nodes.length;
19287
+ this.nodes.push({
19288
+ name: box.label || `Box${nodeIndex}`,
19289
+ mesh: meshIndex,
19290
+ translation: this.toGltfTranslation(box.center),
19291
+ ...this.getPoppyglNodeExtras(box)
19292
+ });
19293
+ this.gltf.scenes[0].nodes.push(nodeIndex);
19294
+ }
19295
+ jscadColor(color3, fallback) {
19296
+ if (color3 === void 0) return fallback;
19297
+ if (Array.isArray(color3)) return [...color3];
19298
+ let rgb;
19299
+ if (typeof color3 === "number")
19300
+ rgb = [
19301
+ (color3 >> 16 & 255) / 255,
19302
+ (color3 >> 8 & 255) / 255,
19303
+ (color3 & 255) / 255
19304
+ ];
19305
+ else {
19306
+ const css = color3.trim().toLowerCase();
19307
+ if (css.startsWith("#")) {
19308
+ const hex = css.slice(1);
19309
+ rgb = import_modeling.colors.hexToRgb(
19310
+ hex.length === 3 || hex.length === 4 ? `#${[...hex].map((digit) => digit + digit).join("")}` : css
19311
+ );
19312
+ } else if (/^rgba?\(/.test(css)) {
19313
+ rgb = css.slice(css.indexOf("(") + 1, -1).split(/[,\s/]+/).slice(0, 3).map(
19314
+ (channel) => parseFloat(channel) / (channel.endsWith("%") ? 100 : 255)
19315
+ );
19316
+ } else if (/^hsla?\(/.test(css)) {
19317
+ const [h, s, l] = css.slice(css.indexOf("(") + 1, -1).split(/[,\s/]+/);
19318
+ rgb = import_modeling.colors.hslToRgb(
19319
+ (parseFloat(h) % 360 + 360) % 360 / 360,
19320
+ parseFloat(s) / 100,
19321
+ parseFloat(l) / 100
19322
+ );
19323
+ } else rgb = import_modeling.colors.colorNameToRgb(css);
19324
+ if (!rgb || rgb.length < 3 || rgb.some((value) => !Number.isFinite(value))) {
19325
+ throw new Error(`Unsupported JSCAD material color: ${color3}`);
19326
+ }
19327
+ }
19328
+ return rgb.slice(0, 3).map(
19329
+ (value) => value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4
19330
+ );
19331
+ }
19219
19332
  async addOBJMeshWithMaterials(box, objMesh) {
19220
19333
  const meshDataArray = createMeshFromOBJ(objMesh);
19221
19334
  const objMaterialIndices = /* @__PURE__ */ new Map();
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "circuit-json-to-gltf",
3
3
  "main": "dist/index.js",
4
4
  "type": "module",
5
- "version": "0.0.145",
5
+ "version": "0.0.146",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/tscircuit/circuit-json-to-gltf.git"
@@ -22,16 +22,15 @@
22
22
  "dependencies": {
23
23
  "@jscad/modeling": "^2.12.6",
24
24
  "@tscircuit/alphabet": "^0.0.25",
25
+ "@tscircuit/cableprinter": "^0.0.4",
25
26
  "earcut": "^3.0.2",
26
27
  "gl-matrix": "^3.4.4",
27
28
  "jscad-electronics": "^0.0.187",
28
- "jscad-planner": "^0.0.14",
29
+ "jscad-planner": "^0.0.19",
29
30
  "jscad-to-gltf": "^0.0.5",
30
- "occt-import-js": "^0.0.23",
31
- "@tscircuit/cableprinter": "^0.0.4"
31
+ "occt-import-js": "^0.0.23"
32
32
  },
33
33
  "devDependencies": {
34
- "@tscircuit/flex-utils": "https://jscdn.tscircuit.com/@tscircuit/flex-utils/0.0.8.tgz",
35
34
  "@biomejs/biome": "^2.1.4",
36
35
  "@google/model-viewer": "^4.1.0",
37
36
  "@napi-rs/canvas": "^1.0.9",
@@ -40,6 +39,7 @@
40
39
  "@resvg/resvg-wasm": "^2.6.2",
41
40
  "@tscircuit/circuit-json-util": "^0.0.114",
42
41
  "@tscircuit/core": "^0.0.1955",
42
+ "@tscircuit/flex-utils": "https://jscdn.tscircuit.com/@tscircuit/flex-utils/0.0.8.tgz",
43
43
  "@tscircuit/props": "^0.0.664",
44
44
  "@types/bun": "latest",
45
45
  "@types/earcut": "^3.0.0",
@@ -51,7 +51,7 @@
51
51
  "circuit-to-svg": "^0.0.429",
52
52
  "graphics-debug": "^0.0.96",
53
53
  "looks-same": "^9.0.1",
54
- "poppygl": "0.0.29",
54
+ "poppygl": "0.0.32",
55
55
  "react": "^19.1.1",
56
56
  "react-cosmos": "^7.0.0",
57
57
  "react-cosmos-plugin-vite": "^7.0.0",