circuit-json-to-gltf 0.0.153 → 0.0.155
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/README.md +23 -4
- package/dist/index.d.ts +7 -0
- package/dist/index.js +65 -40
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,16 +37,16 @@ const circuitJson = {
|
|
|
37
37
|
center: { x: 0, y: 0 },
|
|
38
38
|
width: 80,
|
|
39
39
|
height: 60,
|
|
40
|
-
thickness: 1.6
|
|
40
|
+
thickness: 1.6,
|
|
41
41
|
},
|
|
42
42
|
// ... components, traces, etc.
|
|
43
|
-
]
|
|
43
|
+
],
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
// Convert to GLTF
|
|
47
47
|
const gltf = await convertCircuitJsonToGltf(circuitJson, {
|
|
48
48
|
format: "gltf", // or "glb" for binary
|
|
49
|
-
boardTextureResolution: 2048
|
|
49
|
+
boardTextureResolution: 2048,
|
|
50
50
|
})
|
|
51
51
|
|
|
52
52
|
// Save the result
|
|
@@ -88,7 +88,6 @@ silkscreen colors. Explicit conversion options take precedence.
|
|
|
88
88
|
## Architecture
|
|
89
89
|
|
|
90
90
|
The converter uses a modular architecture:
|
|
91
|
-
|
|
92
91
|
1. **Circuit to 3D Converter**: Parses circuit JSON and creates a 3D scene representation
|
|
93
92
|
2. **Board Renderer**: Renders PCB layers as textures using circuit-to-svg and resvg
|
|
94
93
|
3. **Model Loaders**: Load STL and OBJ files for component 3D models
|
|
@@ -115,3 +114,23 @@ bun run examples/basic-conversion.ts
|
|
|
115
114
|
- Pure GLTF 2.0 implementation without external 3D library dependencies
|
|
116
115
|
- Supports both JSON (.gltf) and binary (.glb) formats
|
|
117
116
|
- Embeds all assets (textures, buffers) directly in the output
|
|
117
|
+
|
|
118
|
+
### Component metadata
|
|
119
|
+
|
|
120
|
+
Both glTF and GLB exports preserve component names as node `name` values and
|
|
121
|
+
include lightweight identity in component node `extras.tscircuit`:
|
|
122
|
+
|
|
123
|
+
```json
|
|
124
|
+
{
|
|
125
|
+
"cad_component_id": "cad_component_1",
|
|
126
|
+
"source_component_id": "source_component_1",
|
|
127
|
+
"pcb_component_id": "pcb_component_1",
|
|
128
|
+
"componentName": "U1"
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Only available fields are serialized. Each CAD instance keeps its own identity,
|
|
133
|
+
including when instances share a mesh or source component. This allows a viewer
|
|
134
|
+
to show names, hide components, and navigate to a schematic from a pre-rendered
|
|
135
|
+
GLB without embedding the complete Circuit JSON. Existing extras, such as
|
|
136
|
+
`poppygl.showHiddenEdges`, are preserved alongside the metadata.
|
package/dist/index.d.ts
CHANGED
|
@@ -135,6 +135,13 @@ interface OBJMaterial {
|
|
|
135
135
|
}
|
|
136
136
|
type Color = string | [number, number, number, number];
|
|
137
137
|
interface Box3D {
|
|
138
|
+
/** Lightweight identity exported as glTF node extras.tscircuit (no geometry). */
|
|
139
|
+
componentMetadata?: {
|
|
140
|
+
cad_component_id?: string;
|
|
141
|
+
source_component_id?: string;
|
|
142
|
+
pcb_component_id?: string;
|
|
143
|
+
componentName?: string;
|
|
144
|
+
};
|
|
138
145
|
center: Point3;
|
|
139
146
|
size: Size3;
|
|
140
147
|
rotation?: Point3;
|
package/dist/index.js
CHANGED
|
@@ -17285,36 +17285,51 @@ function getAccessorData(accessor, bufferViews, binaryBuffer) {
|
|
|
17285
17285
|
const type = accessor.type;
|
|
17286
17286
|
const componentsPerElement = type === "SCALAR" ? 1 : type === "VEC2" ? 2 : type === "VEC3" ? 3 : type === "VEC4" ? 4 : 1;
|
|
17287
17287
|
const totalComponents = count * componentsPerElement;
|
|
17288
|
-
|
|
17288
|
+
const componentSize = componentType === 5120 || componentType === 5121 ? 1 : componentType === 5122 || componentType === 5123 ? 2 : 4;
|
|
17289
|
+
const elementSize = componentSize * componentsPerElement;
|
|
17290
|
+
const stride = bufferView.byteStride ?? elementSize;
|
|
17291
|
+
if (componentType === 5126 && stride === elementSize) {
|
|
17289
17292
|
return new Float32Array(binaryBuffer, byteOffset, totalComponents);
|
|
17290
|
-
}
|
|
17291
|
-
|
|
17292
|
-
|
|
17293
|
-
|
|
17294
|
-
|
|
17295
|
-
|
|
17296
|
-
|
|
17297
|
-
|
|
17298
|
-
|
|
17299
|
-
|
|
17300
|
-
|
|
17301
|
-
|
|
17302
|
-
|
|
17303
|
-
|
|
17304
|
-
|
|
17305
|
-
|
|
17306
|
-
|
|
17307
|
-
|
|
17308
|
-
|
|
17309
|
-
|
|
17310
|
-
|
|
17311
|
-
|
|
17312
|
-
|
|
17313
|
-
|
|
17314
|
-
|
|
17315
|
-
|
|
17316
|
-
|
|
17317
|
-
|
|
17293
|
+
}
|
|
17294
|
+
const view = new DataView(binaryBuffer);
|
|
17295
|
+
const result = new Float32Array(totalComponents);
|
|
17296
|
+
for (let element = 0; element < count; element++) {
|
|
17297
|
+
for (let component = 0; component < componentsPerElement; component++) {
|
|
17298
|
+
const offset = byteOffset + element * stride + component * componentSize;
|
|
17299
|
+
let value;
|
|
17300
|
+
switch (componentType) {
|
|
17301
|
+
case 5120:
|
|
17302
|
+
value = view.getInt8(offset);
|
|
17303
|
+
if (accessor.normalized) value = Math.max(value / 127, -1);
|
|
17304
|
+
break;
|
|
17305
|
+
case 5121:
|
|
17306
|
+
value = view.getUint8(offset);
|
|
17307
|
+
if (accessor.normalized) value /= 255;
|
|
17308
|
+
break;
|
|
17309
|
+
case 5122:
|
|
17310
|
+
value = view.getInt16(offset, true);
|
|
17311
|
+
if (accessor.normalized) value = Math.max(value / 32767, -1);
|
|
17312
|
+
break;
|
|
17313
|
+
case 5123:
|
|
17314
|
+
value = view.getUint16(offset, true);
|
|
17315
|
+
if (accessor.normalized) value /= 65535;
|
|
17316
|
+
break;
|
|
17317
|
+
case 5124:
|
|
17318
|
+
value = view.getInt32(offset, true);
|
|
17319
|
+
break;
|
|
17320
|
+
case 5125:
|
|
17321
|
+
value = view.getUint32(offset, true);
|
|
17322
|
+
break;
|
|
17323
|
+
case 5126:
|
|
17324
|
+
value = view.getFloat32(offset, true);
|
|
17325
|
+
break;
|
|
17326
|
+
default:
|
|
17327
|
+
throw new Error(`Unsupported component type: ${componentType}`);
|
|
17328
|
+
}
|
|
17329
|
+
result[element * componentsPerElement + component] = value;
|
|
17330
|
+
}
|
|
17331
|
+
}
|
|
17332
|
+
return result;
|
|
17318
17333
|
}
|
|
17319
17334
|
function computeNormal(v0, v1, v2) {
|
|
17320
17335
|
const edge1 = {
|
|
@@ -18829,7 +18844,13 @@ async function convertCircuitJsonTo3D(inputCircuitJson, options = {}) {
|
|
|
18829
18844
|
size,
|
|
18830
18845
|
isTranslucent: cad.show_as_translucent_model,
|
|
18831
18846
|
showHiddenEdges: cad.show_hidden_edges,
|
|
18832
|
-
label: sourceComponent?.name
|
|
18847
|
+
label: sourceComponent?.name,
|
|
18848
|
+
componentMetadata: {
|
|
18849
|
+
cad_component_id: cad.cad_component_id,
|
|
18850
|
+
source_component_id: cad.source_component_id,
|
|
18851
|
+
pcb_component_id: cad.pcb_component_id,
|
|
18852
|
+
componentName: sourceComponent?.name
|
|
18853
|
+
}
|
|
18833
18854
|
};
|
|
18834
18855
|
if (model_stl_url || model_obj_url || model_glb_url || model_gltf_url || model_step_url) {
|
|
18835
18856
|
box.meshUrl = model_stl_url || model_obj_url || model_glb_url || model_gltf_url || model_step_url;
|
|
@@ -18998,6 +19019,11 @@ async function convertCircuitJsonTo3D(inputCircuitJson, options = {}) {
|
|
|
18998
19019
|
},
|
|
18999
19020
|
color: componentColor,
|
|
19000
19021
|
label: sourceComponent?.name ?? "?",
|
|
19022
|
+
componentMetadata: {
|
|
19023
|
+
source_component_id: component.source_component_id,
|
|
19024
|
+
pcb_component_id: component.pcb_component_id,
|
|
19025
|
+
componentName: sourceComponent?.name
|
|
19026
|
+
},
|
|
19001
19027
|
labelColor: "white"
|
|
19002
19028
|
};
|
|
19003
19029
|
const result = fold ? tryFoldRigidBox(box, fold, pcbBoard.center, component.center) : void 0;
|
|
@@ -19391,7 +19417,7 @@ var GLTFBuilder = class {
|
|
|
19391
19417
|
name: box.label || `Box${nodeIndex}`,
|
|
19392
19418
|
mesh: meshIndex,
|
|
19393
19419
|
translation: this.toGltfTranslation(box.center),
|
|
19394
|
-
...this.
|
|
19420
|
+
...this.getNodeExtras(box)
|
|
19395
19421
|
});
|
|
19396
19422
|
this.gltf.scenes[0].nodes.push(nodeIndex);
|
|
19397
19423
|
}
|
|
@@ -19449,7 +19475,7 @@ var GLTFBuilder = class {
|
|
|
19449
19475
|
name: box.label || `Box${nodeIndex}`,
|
|
19450
19476
|
mesh: meshIndex,
|
|
19451
19477
|
translation: this.toGltfTranslation(box.center),
|
|
19452
|
-
...this.
|
|
19478
|
+
...this.getNodeExtras(box)
|
|
19453
19479
|
});
|
|
19454
19480
|
this.gltf.scenes[0].nodes.push(nodeIndex);
|
|
19455
19481
|
}
|
|
@@ -19550,7 +19576,7 @@ var GLTFBuilder = class {
|
|
|
19550
19576
|
name: box.label || `OBJBox${nodeIndex}`,
|
|
19551
19577
|
mesh: meshIndex,
|
|
19552
19578
|
translation: this.toGltfTranslation(box.center),
|
|
19553
|
-
...this.
|
|
19579
|
+
...this.getNodeExtras(box)
|
|
19554
19580
|
});
|
|
19555
19581
|
this.gltf.scenes[0].nodes.push(nodeIndex);
|
|
19556
19582
|
}
|
|
@@ -19710,7 +19736,7 @@ var GLTFBuilder = class {
|
|
|
19710
19736
|
name: box.label || `Box${nodeIndex}`,
|
|
19711
19737
|
mesh: meshIndex,
|
|
19712
19738
|
translation: this.toGltfTranslation(box.center),
|
|
19713
|
-
...this.
|
|
19739
|
+
...this.getNodeExtras(box)
|
|
19714
19740
|
});
|
|
19715
19741
|
this.gltf.scenes[0].nodes.push(nodeIndex);
|
|
19716
19742
|
}
|
|
@@ -19816,7 +19842,7 @@ var GLTFBuilder = class {
|
|
|
19816
19842
|
name: box.label || `Box${nodeIndex}`,
|
|
19817
19843
|
mesh: meshIndex,
|
|
19818
19844
|
translation: this.toGltfTranslation(box.center),
|
|
19819
|
-
...this.
|
|
19845
|
+
...this.getNodeExtras(box)
|
|
19820
19846
|
});
|
|
19821
19847
|
this.gltf.scenes[0].nodes.push(nodeIndex);
|
|
19822
19848
|
}
|
|
@@ -19872,13 +19898,12 @@ var GLTFBuilder = class {
|
|
|
19872
19898
|
toGltfTranslation(center) {
|
|
19873
19899
|
return [-center.x, center.y, center.z];
|
|
19874
19900
|
}
|
|
19875
|
-
|
|
19876
|
-
if (!box.showHiddenEdges) return {};
|
|
19901
|
+
getNodeExtras(box) {
|
|
19902
|
+
if (!box.showHiddenEdges && !box.componentMetadata) return {};
|
|
19877
19903
|
return {
|
|
19878
19904
|
extras: {
|
|
19879
|
-
poppygl: {
|
|
19880
|
-
|
|
19881
|
-
}
|
|
19905
|
+
...box.showHiddenEdges ? { poppygl: { showHiddenEdges: true } } : {},
|
|
19906
|
+
...box.componentMetadata ? { tscircuit: box.componentMetadata } : {}
|
|
19882
19907
|
}
|
|
19883
19908
|
};
|
|
19884
19909
|
}
|