circuit-json-to-gltf 0.0.15 → 0.0.17

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 (2) hide show
  1. package/dist/index.js +96 -48
  2. package/package.json +7 -3
package/dist/index.js CHANGED
@@ -1100,37 +1100,39 @@ async function convertCircuitJsonTo3D(circuitJson, options = {}) {
1100
1100
  } = options;
1101
1101
  const db = cju(circuitJson);
1102
1102
  const boxes = [];
1103
- const pcbBoard = db.pcb_board.list()[0];
1104
- if (!pcbBoard) {
1105
- throw new Error("No pcb_board found in circuit JSON");
1106
- }
1107
- const boardBox = {
1108
- center: {
1109
- x: pcbBoard.center.x,
1110
- y: 0,
1111
- z: pcbBoard.center.y
1112
- },
1113
- size: {
1114
- x: pcbBoard.width,
1115
- y: boardThickness,
1116
- z: pcbBoard.height
1117
- }
1118
- };
1119
- if (shouldRenderTextures && textureResolution > 0) {
1120
- try {
1121
- const textures = await renderBoardTextures(circuitJson, textureResolution);
1122
- boardBox.texture = {
1123
- top: textures.top,
1124
- bottom: textures.bottom
1125
- };
1126
- } catch (error) {
1127
- console.warn("Failed to render board textures:", error);
1103
+ const pcbBoard = db.pcb_board?.list?.()[0];
1104
+ if (pcbBoard) {
1105
+ const boardBox = {
1106
+ center: {
1107
+ x: pcbBoard.center.x,
1108
+ y: 0,
1109
+ z: pcbBoard.center.y
1110
+ },
1111
+ size: {
1112
+ x: pcbBoard.width,
1113
+ y: boardThickness,
1114
+ z: pcbBoard.height
1115
+ }
1116
+ };
1117
+ if (shouldRenderTextures && textureResolution > 0) {
1118
+ try {
1119
+ const textures = await renderBoardTextures(
1120
+ circuitJson,
1121
+ textureResolution
1122
+ );
1123
+ boardBox.texture = {
1124
+ top: textures.top,
1125
+ bottom: textures.bottom
1126
+ };
1127
+ } catch (error) {
1128
+ console.warn("Failed to render board textures:", error);
1129
+ boardBox.color = pcbColor;
1130
+ }
1131
+ } else {
1128
1132
  boardBox.color = pcbColor;
1129
1133
  }
1130
- } else {
1131
- boardBox.color = pcbColor;
1134
+ boxes.push(boardBox);
1132
1135
  }
1133
- boxes.push(boardBox);
1134
1136
  const cadComponents = db.cad_component?.list?.() ?? [];
1135
1137
  const pcbComponentIdsWith3D = /* @__PURE__ */ new Set();
1136
1138
  for (const cad of cadComponents) {
@@ -1239,26 +1241,72 @@ async function convertCircuitJsonTo3D(circuitJson, options = {}) {
1239
1241
  labelColor: "white"
1240
1242
  });
1241
1243
  }
1242
- const boardDiagonal = Math.sqrt(
1243
- pcbBoard.width * pcbBoard.width + pcbBoard.height * pcbBoard.height
1244
- );
1245
- const cameraDistance = boardDiagonal * 1.5;
1246
- const camera = {
1247
- position: {
1248
- x: pcbBoard.center.x + cameraDistance * 0.5,
1249
- y: cameraDistance * 0.7,
1250
- z: pcbBoard.center.y + cameraDistance * 0.5
1251
- },
1252
- target: {
1253
- x: pcbBoard.center.x,
1254
- y: 0,
1255
- z: pcbBoard.center.y
1256
- },
1257
- up: { x: 0, y: 1, z: 0 },
1258
- fov: 50,
1259
- near: 0.1,
1260
- far: cameraDistance * 4
1261
- };
1244
+ let camera;
1245
+ if (pcbBoard) {
1246
+ const boardDiagonal = Math.sqrt(
1247
+ pcbBoard.width * pcbBoard.width + pcbBoard.height * pcbBoard.height
1248
+ );
1249
+ const cameraDistance = boardDiagonal * 1.5;
1250
+ camera = {
1251
+ position: {
1252
+ x: pcbBoard.center.x + cameraDistance * 0.5,
1253
+ y: cameraDistance * 0.7,
1254
+ z: pcbBoard.center.y + cameraDistance * 0.5
1255
+ },
1256
+ target: {
1257
+ x: pcbBoard.center.x,
1258
+ y: 0,
1259
+ z: pcbBoard.center.y
1260
+ },
1261
+ up: { x: 0, y: 1, z: 0 },
1262
+ fov: 50,
1263
+ near: 0.1,
1264
+ far: cameraDistance * 4
1265
+ };
1266
+ } else {
1267
+ const hasBoxes = boxes.length > 0;
1268
+ if (hasBoxes) {
1269
+ let minX = Infinity;
1270
+ let minZ = Infinity;
1271
+ let maxX = -Infinity;
1272
+ let maxZ = -Infinity;
1273
+ for (const box of boxes) {
1274
+ const halfX = (box.size?.x ?? 0) / 2;
1275
+ const halfZ = (box.size?.z ?? 0) / 2;
1276
+ minX = Math.min(minX, box.center.x - halfX);
1277
+ maxX = Math.max(maxX, box.center.x + halfX);
1278
+ minZ = Math.min(minZ, box.center.z - halfZ);
1279
+ maxZ = Math.max(maxZ, box.center.z + halfZ);
1280
+ }
1281
+ const width = Math.max(maxX - minX, 1);
1282
+ const height = Math.max(maxZ - minZ, 1);
1283
+ const diagonal = Math.sqrt(width * width + height * height);
1284
+ const distance = diagonal * 1.5;
1285
+ const centerX = (minX + maxX) / 2;
1286
+ const centerZ = (minZ + maxZ) / 2;
1287
+ camera = {
1288
+ position: {
1289
+ x: centerX + distance * 0.5,
1290
+ y: distance * 0.7,
1291
+ z: centerZ + distance * 0.5
1292
+ },
1293
+ target: { x: centerX, y: 0, z: centerZ },
1294
+ up: { x: 0, y: 1, z: 0 },
1295
+ fov: 50,
1296
+ near: 0.1,
1297
+ far: distance * 4
1298
+ };
1299
+ } else {
1300
+ camera = {
1301
+ position: { x: 30, y: 30, z: 25 },
1302
+ target: { x: 0, y: 0, z: 0 },
1303
+ up: { x: 0, y: 1, z: 0 },
1304
+ fov: 50,
1305
+ near: 0.1,
1306
+ far: 120
1307
+ };
1308
+ }
1309
+ }
1262
1310
  const lights = [
1263
1311
  {
1264
1312
  type: "ambient",
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.15",
5
+ "version": "0.0.17",
6
6
  "scripts": {
7
7
  "test": "bun test tests/",
8
8
  "format": "biome format --write .",
@@ -27,15 +27,16 @@
27
27
  "bun-match-svg": "^0.0.12",
28
28
  "circuit-json": "^0.0.267",
29
29
  "circuit-to-svg": "^0.0.175",
30
+ "graphics-debug": "^0.0.65",
30
31
  "looks-same": "^9.0.1",
31
32
  "poppygl": "^0.0.9",
32
33
  "react": "^19.1.1",
33
34
  "react-cosmos": "^7.0.0",
34
35
  "react-cosmos-plugin-vite": "^7.0.0",
35
36
  "react-dom": "^19.1.1",
37
+ "tscircuit": "^0.0.701",
36
38
  "tsup": "^8.5.0",
37
- "vite": "^7.1.1",
38
- "tscircuit": "^0.0.701"
39
+ "vite": "^7.1.1"
39
40
  },
40
41
  "peerDependencies": {
41
42
  "typescript": "^5",
@@ -48,6 +49,9 @@
48
49
  "peerDependenciesMeta": {
49
50
  "@resvg/resvg-wasm": {
50
51
  "optional": true
52
+ },
53
+ "@resvg/resvg-js": {
54
+ "optional": true
51
55
  }
52
56
  }
53
57
  }