circuit-json-to-gltf 0.0.145 → 0.0.147

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);
@@ -16308,9 +16308,31 @@ function getBoundingBoxCenter(bounds) {
16308
16308
 
16309
16309
  // lib/loaders/cable.ts
16310
16310
  function loadCable(cable) {
16311
+ const pin1Side = (position, end) => {
16312
+ if (!position || cable.path.length < 2) return void 0;
16313
+ const tip = end === "from" ? cable.path[0] : cable.path.at(-1);
16314
+ const next = end === "from" ? cable.path[1] : cable.path.at(-2);
16315
+ const axis = vec3.normalize(vec3.create(), [
16316
+ next.x - tip.x,
16317
+ next.y - tip.y,
16318
+ next.z - tip.z
16319
+ ]);
16320
+ const side = vec3.fromValues(
16321
+ position.x - tip.x,
16322
+ position.y - tip.y,
16323
+ position.z - tip.z
16324
+ );
16325
+ vec3.scaleAndAdd(side, side, axis, -vec3.dot(side, axis));
16326
+ if (vec3.length(side) < 1e-6) return void 0;
16327
+ vec3.normalize(side, side);
16328
+ return [side[0], side[1], side[2]];
16329
+ };
16311
16330
  const meshes = createCableMeshes({
16312
16331
  definition: parseCableString(cable.cableprinter_string),
16313
- path: cable.path.map(({ x, y, z: z3 }) => [x, y, z3])
16332
+ path: cable.path.map(({ x, y, z: z3 }) => [x, y, z3]),
16333
+ // Orient in circuit Z-up before the paired mesh transform to scene Y-up.
16334
+ startPin1Side: pin1Side(cable.from_connector_pin1_position, "from"),
16335
+ endPin1Side: pin1Side(cable.to_connector_pin1_position, "to")
16314
16336
  });
16315
16337
  return meshes.map((mesh) => {
16316
16338
  const circuitTriangles = [];
@@ -17398,15 +17420,29 @@ var createBoardOuterLoop = (board, center) => {
17398
17420
  // lib/loaders/jscad-plan.ts
17399
17421
  var JSCAD_PLAN_TRANSFORM = COORDINATE_TRANSFORMS.CIRCUIT_Z_UP_TO_SCENE_Y_UP;
17400
17422
  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
17423
+ const plans = Array.isArray(plan) ? plan.flat(Infinity) : [plan];
17424
+ const zUpGeometry = plans.map(
17425
+ (operation) => executeJscadOperations(jscadModeling2, operation)
17409
17426
  );
17427
+ const geometries = zUpGeometry.flat(Infinity);
17428
+ const sourceTriangles = geometries.flatMap((geometry) => {
17429
+ const polygons = geom33.toPolygons(geometry);
17430
+ return polygons.flatMap((polygon3) => {
17431
+ const color3 = polygon3.color ?? geometry.color;
17432
+ const material = geometry.material || color3 ? {
17433
+ ...color3 ? {
17434
+ color: color3.slice(0, 3),
17435
+ opacity: color3[3] ?? 1
17436
+ } : {},
17437
+ ...geometry.material
17438
+ } : void 0;
17439
+ return geom3ToTriangles(geometry, [polygon3]).map((triangle) => ({
17440
+ ...triangle,
17441
+ material
17442
+ }));
17443
+ });
17444
+ });
17445
+ const triangles = transformTriangles(sourceTriangles, JSCAD_PLAN_TRANSFORM);
17410
17446
  return {
17411
17447
  triangles,
17412
17448
  boundingBox: boundsOfTriangles2(triangles)
@@ -18982,6 +19018,9 @@ async function convertCircuitJsonTo3D(inputCircuitJson, options = {}) {
18982
19018
  return options.showErrors ? withCircuitJsonErrors(scene, inputCircuitJson) : scene;
18983
19019
  }
18984
19020
 
19021
+ // lib/gltf/gltf-builder.ts
19022
+ var import_modeling = __toESM(require_src(), 1);
19023
+
18985
19024
  // lib/gltf/gltf-types.ts
18986
19025
  var COMPONENT_TYPE = {
18987
19026
  BYTE: 5120,
@@ -19178,6 +19217,10 @@ var GLTFBuilder = class {
19178
19217
  await this.addOBJMeshWithMaterials(box, box.mesh);
19179
19218
  return;
19180
19219
  }
19220
+ if (box.mesh?.triangles.some((triangle) => triangle.material)) {
19221
+ this.addJscadMeshWithMaterials(box);
19222
+ return;
19223
+ }
19181
19224
  let meshData;
19182
19225
  if (box.mesh) {
19183
19226
  meshData = createMeshFromSTL(box.mesh);
@@ -19216,6 +19259,98 @@ var GLTFBuilder = class {
19216
19259
  });
19217
19260
  this.gltf.scenes[0].nodes.push(nodeIndex);
19218
19261
  }
19262
+ addJscadMeshWithMaterials(box) {
19263
+ const mesh = box.mesh;
19264
+ const groups = /* @__PURE__ */ new Map();
19265
+ for (const triangle of mesh.triangles) {
19266
+ const authored = triangle.material ?? {};
19267
+ const base = this.jscadColor(authored.color, [0.7, 0.7, 0.7]);
19268
+ const opacity = authored.opacity ?? (box.isTranslucent ? 0.5 : 1);
19269
+ const transparent = authored.transparent ?? (opacity < 1 || !!box.isTranslucent);
19270
+ const strength = authored.emissiveIntensity ?? 1;
19271
+ const index = this.addMaterial({
19272
+ pbrMetallicRoughness: {
19273
+ baseColorFactor: [...base, opacity],
19274
+ metallicFactor: authored.metalness ?? 0.1,
19275
+ roughnessFactor: authored.roughness ?? 0.9
19276
+ },
19277
+ emissiveFactor: this.jscadColor(authored.emissive, [0, 0, 0]),
19278
+ ...strength !== 1 ? {
19279
+ extensions: {
19280
+ KHR_materials_emissive_strength: { emissiveStrength: strength }
19281
+ }
19282
+ } : {},
19283
+ alphaMode: transparent ? "BLEND" : "OPAQUE",
19284
+ doubleSided: transparent || void 0
19285
+ });
19286
+ if (strength !== 1)
19287
+ this.gltf.extensionsUsed = ["KHR_materials_emissive_strength"];
19288
+ const group = groups.get(index) ?? [];
19289
+ group.push(triangle);
19290
+ groups.set(index, group);
19291
+ }
19292
+ const primitives = [...groups].map(([material, triangles]) => ({
19293
+ material,
19294
+ // Match addBox: preserve its scene-frame rotation and canonical export orientation.
19295
+ meshData: convertMeshToGLTFOrientation(
19296
+ transformMesh(
19297
+ createMeshFromSTL({ triangles, boundingBox: mesh.boundingBox }),
19298
+ { x: 0, y: 0, z: 0 },
19299
+ box.rotation
19300
+ )
19301
+ )
19302
+ }));
19303
+ const meshIndex = this.addMultiPrimitiveMesh(
19304
+ primitives,
19305
+ box.label || `JscadMesh${this.meshes.length}`,
19306
+ "jscad"
19307
+ );
19308
+ const nodeIndex = this.nodes.length;
19309
+ this.nodes.push({
19310
+ name: box.label || `Box${nodeIndex}`,
19311
+ mesh: meshIndex,
19312
+ translation: this.toGltfTranslation(box.center),
19313
+ ...this.getPoppyglNodeExtras(box)
19314
+ });
19315
+ this.gltf.scenes[0].nodes.push(nodeIndex);
19316
+ }
19317
+ jscadColor(color3, fallback) {
19318
+ if (color3 === void 0) return fallback;
19319
+ if (Array.isArray(color3)) return [...color3];
19320
+ let rgb;
19321
+ if (typeof color3 === "number")
19322
+ rgb = [
19323
+ (color3 >> 16 & 255) / 255,
19324
+ (color3 >> 8 & 255) / 255,
19325
+ (color3 & 255) / 255
19326
+ ];
19327
+ else {
19328
+ const css = color3.trim().toLowerCase();
19329
+ if (css.startsWith("#")) {
19330
+ const hex = css.slice(1);
19331
+ rgb = import_modeling.colors.hexToRgb(
19332
+ hex.length === 3 || hex.length === 4 ? `#${[...hex].map((digit) => digit + digit).join("")}` : css
19333
+ );
19334
+ } else if (/^rgba?\(/.test(css)) {
19335
+ rgb = css.slice(css.indexOf("(") + 1, -1).split(/[,\s/]+/).slice(0, 3).map(
19336
+ (channel) => parseFloat(channel) / (channel.endsWith("%") ? 100 : 255)
19337
+ );
19338
+ } else if (/^hsla?\(/.test(css)) {
19339
+ const [h, s, l] = css.slice(css.indexOf("(") + 1, -1).split(/[,\s/]+/);
19340
+ rgb = import_modeling.colors.hslToRgb(
19341
+ (parseFloat(h) % 360 + 360) % 360 / 360,
19342
+ parseFloat(s) / 100,
19343
+ parseFloat(l) / 100
19344
+ );
19345
+ } else rgb = import_modeling.colors.colorNameToRgb(css);
19346
+ if (!rgb || rgb.length < 3 || rgb.some((value) => !Number.isFinite(value))) {
19347
+ throw new Error(`Unsupported JSCAD material color: ${color3}`);
19348
+ }
19349
+ }
19350
+ return rgb.slice(0, 3).map(
19351
+ (value) => value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4
19352
+ );
19353
+ }
19219
19354
  async addOBJMeshWithMaterials(box, objMesh) {
19220
19355
  const meshDataArray = createMeshFromOBJ(objMesh);
19221
19356
  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.147",
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
- "jscad-electronics": "^0.0.187",
28
- "jscad-planner": "^0.0.14",
28
+ "jscad-electronics": "^0.0.193",
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",
@@ -47,11 +47,11 @@
47
47
  "@types/react-dom": "^19.1.7",
48
48
  "@vitejs/plugin-react": "^5.0.0",
49
49
  "bun-match-svg": "^0.0.12",
50
- "circuit-json": "^0.0.516",
50
+ "circuit-json": "^0.0.519",
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",
@@ -78,6 +78,6 @@
78
78
  },
79
79
  "overrides": {
80
80
  "@tscircuit/core": "^0.0.1955",
81
- "circuit-json": "^0.0.516"
81
+ "circuit-json": "^0.0.519"
82
82
  }
83
83
  }