circuit-json-to-gltf 0.0.1

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 ADDED
@@ -0,0 +1,96 @@
1
+ # circuit-json-to-gltf
2
+
3
+ Converts circuit JSON to 3D GLTF files. Used for exporting circuits as 3D models.
4
+
5
+ ## Features
6
+
7
+ - Convert circuit JSON to GLTF 2.0 format (JSON or binary)
8
+ - Render PCB board with accurate dimensions and textures
9
+ - Support for STL and OBJ model loading for components
10
+ - High-quality board texture rendering using circuit-to-svg and resvg
11
+ - Automatic component positioning and generic 3D representations
12
+ - Customizable camera, lighting, and material settings
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ bun install circuit-json-to-gltf
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ```typescript
23
+ import { convertCircuitJsonToGltf } from "circuit-json-to-gltf"
24
+
25
+ // Your circuit JSON data
26
+ const circuitJson = {
27
+ elements: [
28
+ {
29
+ type: "pcb_board",
30
+ pcb_board_id: "board1",
31
+ center: { x: 0, y: 0 },
32
+ width: 80,
33
+ height: 60,
34
+ thickness: 1.6
35
+ },
36
+ // ... components, traces, etc.
37
+ ]
38
+ }
39
+
40
+ // Convert to GLTF
41
+ const gltf = await convertCircuitJsonToGltf(circuitJson, {
42
+ format: "gltf", // or "glb" for binary
43
+ boardTextureResolution: 2048
44
+ })
45
+
46
+ // Save the result
47
+ fs.writeFileSync("circuit.gltf", JSON.stringify(gltf))
48
+ ```
49
+
50
+ ## API
51
+
52
+ ### Main Function
53
+
54
+ ```typescript
55
+ convertCircuitJsonToGltf(circuitJson: CircuitJson, options?: ConversionOptions): Promise<ArrayBuffer | object>
56
+ ```
57
+
58
+ ### Options
59
+
60
+ - `format`: "gltf" (JSON) or "glb" (binary) - default: "gltf"
61
+ - `boardTextureResolution`: Resolution for board texture rendering - default: 1024
62
+ - `includeModels`: Whether to load external 3D models - default: true
63
+ - `modelCache`: Map for caching loaded models
64
+ - `backgroundColor`: Background color for board rendering
65
+ - `showBoundingBoxes`: Show bounding boxes for debugging
66
+
67
+ ## Architecture
68
+
69
+ The converter uses a modular architecture:
70
+
71
+ 1. **Circuit to 3D Converter**: Parses circuit JSON and creates a 3D scene representation
72
+ 2. **Board Renderer**: Renders PCB layers as textures using circuit-to-svg and resvg
73
+ 3. **Model Loaders**: Load STL and OBJ files for component 3D models
74
+ 4. **GLTF Builder**: Constructs the final GLTF using Three.js
75
+
76
+ ## Development
77
+
78
+ ```bash
79
+ # Install dependencies
80
+ bun install
81
+
82
+ # Run tests
83
+ bun test
84
+
85
+ # Run example
86
+ bun run examples/basic-conversion.ts
87
+ ```
88
+
89
+ ## Implementation Details
90
+
91
+ - Uses `circuit-to-svg` to render the top/bottom layers of the board to SVG
92
+ - Uses `@resvg/resvg-js` to convert SVG to PNG textures
93
+ - Includes built-in STL and OBJ parsers for 3D model loading
94
+ - Pure GLTF 2.0 implementation without external 3D library dependencies
95
+ - Supports both JSON (.gltf) and binary (.glb) formats
96
+ - Embeds all assets (textures, buffers) directly in the output
@@ -0,0 +1,167 @@
1
+ import { CircuitJson } from 'circuit-json';
2
+
3
+ interface ConversionOptions {
4
+ format?: "gltf" | "glb";
5
+ boardTextureResolution?: number;
6
+ includeModels?: boolean;
7
+ modelCache?: Map<string, STLMesh | OBJMesh>;
8
+ backgroundColor?: string;
9
+ showBoundingBoxes?: boolean;
10
+ coordinateTransform?: CoordinateTransformConfig;
11
+ }
12
+ interface CoordinateTransformConfig {
13
+ flipX?: number;
14
+ flipY?: number;
15
+ flipZ?: number;
16
+ axisMapping?: {
17
+ x?: "x" | "y" | "z" | "-x" | "-y" | "-z";
18
+ y?: "x" | "y" | "z" | "-x" | "-y" | "-z";
19
+ z?: "x" | "y" | "z" | "-x" | "-y" | "-z";
20
+ };
21
+ rotation?: {
22
+ x?: number;
23
+ y?: number;
24
+ z?: number;
25
+ };
26
+ }
27
+ interface Point3 {
28
+ x: number;
29
+ y: number;
30
+ z: number;
31
+ }
32
+ interface Size3 {
33
+ x: number;
34
+ y: number;
35
+ z: number;
36
+ }
37
+ interface Triangle {
38
+ vertices: [Point3, Point3, Point3];
39
+ normal: Point3;
40
+ color?: Color;
41
+ materialIndex?: number;
42
+ }
43
+ interface BoundingBox {
44
+ min: Point3;
45
+ max: Point3;
46
+ }
47
+ interface STLMesh {
48
+ triangles: Triangle[];
49
+ boundingBox: BoundingBox;
50
+ }
51
+ interface OBJMesh extends STLMesh {
52
+ materials?: Map<string, OBJMaterial>;
53
+ }
54
+ interface OBJMaterial {
55
+ name: string;
56
+ color?: Color;
57
+ ambient?: Color;
58
+ specular?: Color;
59
+ shininess?: number;
60
+ dissolve?: number;
61
+ }
62
+ type Color = string | [number, number, number, number];
63
+ interface Box3D {
64
+ center: Point3;
65
+ size: Size3;
66
+ rotation?: Point3;
67
+ color?: Color;
68
+ texture?: {
69
+ top?: string;
70
+ bottom?: string;
71
+ front?: string;
72
+ back?: string;
73
+ left?: string;
74
+ right?: string;
75
+ };
76
+ mesh?: STLMesh | OBJMesh;
77
+ meshUrl?: string;
78
+ meshType?: "stl" | "obj";
79
+ label?: string;
80
+ labelColor?: Color;
81
+ }
82
+ interface Scene3D {
83
+ boxes: Box3D[];
84
+ camera?: Camera3D;
85
+ lights?: Light3D[];
86
+ }
87
+ interface Camera3D {
88
+ position: Point3;
89
+ target: Point3;
90
+ up?: Point3;
91
+ fov?: number;
92
+ near?: number;
93
+ far?: number;
94
+ }
95
+ interface Light3D {
96
+ type: "ambient" | "directional" | "point";
97
+ color?: Color;
98
+ intensity?: number;
99
+ position?: Point3;
100
+ direction?: Point3;
101
+ }
102
+ interface GLTFExportOptions {
103
+ binary?: boolean;
104
+ trs?: boolean;
105
+ onlyVisible?: boolean;
106
+ truncateDrawRange?: boolean;
107
+ embedImages?: boolean;
108
+ animations?: any[];
109
+ forceIndices?: boolean;
110
+ includeCustomExtensions?: boolean;
111
+ }
112
+ interface CircuitTo3DOptions {
113
+ pcbColor?: Color;
114
+ componentColor?: Color;
115
+ boardThickness?: number;
116
+ defaultComponentHeight?: number;
117
+ renderBoardTextures?: boolean;
118
+ textureResolution?: number;
119
+ coordinateTransform?: CoordinateTransformConfig;
120
+ }
121
+ interface BoardRenderOptions {
122
+ layer: "top" | "bottom";
123
+ resolution?: number;
124
+ backgroundColor?: string;
125
+ copperColor?: string;
126
+ silkscreenColor?: string;
127
+ padColor?: string;
128
+ drillColor?: string;
129
+ }
130
+
131
+ declare function loadSTL(url: string, transform?: CoordinateTransformConfig): Promise<STLMesh>;
132
+ declare function clearSTLCache(): void;
133
+
134
+ declare function loadOBJ(url: string, transform?: CoordinateTransformConfig): Promise<OBJMesh>;
135
+ declare function clearOBJCache(): void;
136
+
137
+ declare function convertCircuitJsonTo3D(circuitJson: CircuitJson, options?: CircuitTo3DOptions): Promise<Scene3D>;
138
+
139
+ declare function convertSceneToGLTF(scene: Scene3D, options?: GLTFExportOptions): Promise<ArrayBuffer | object>;
140
+
141
+ declare function renderBoardLayer(circuitJson: CircuitJson, options: BoardRenderOptions): Promise<string>;
142
+ declare function renderBoardTextures(circuitJson: CircuitJson, resolution?: number): Promise<{
143
+ top: string;
144
+ bottom: string;
145
+ }>;
146
+
147
+ declare function applyCoordinateTransform(point: Point3, config: CoordinateTransformConfig): Point3;
148
+ declare function transformTriangles(triangles: Triangle[], config: CoordinateTransformConfig): Triangle[];
149
+ declare const COORDINATE_TRANSFORMS: {
150
+ readonly Z_UP_TO_Y_UP: CoordinateTransformConfig;
151
+ readonly Z_OUT_OF_TOP: CoordinateTransformConfig;
152
+ readonly USB_PORT_FIX: CoordinateTransformConfig;
153
+ readonly Z_UP_TO_Y_UP_USB_FIX: CoordinateTransformConfig;
154
+ readonly IDENTITY: CoordinateTransformConfig;
155
+ readonly TEST_ROTATE_X_90: CoordinateTransformConfig;
156
+ readonly TEST_ROTATE_X_270: CoordinateTransformConfig;
157
+ readonly TEST_ROTATE_Y_90: CoordinateTransformConfig;
158
+ readonly TEST_ROTATE_Y_270: CoordinateTransformConfig;
159
+ readonly TEST_ROTATE_Z_90: CoordinateTransformConfig;
160
+ readonly TEST_ROTATE_Z_270: CoordinateTransformConfig;
161
+ readonly TEST_FLIP_X: CoordinateTransformConfig;
162
+ readonly TEST_FLIP_Z: CoordinateTransformConfig;
163
+ };
164
+
165
+ declare function convertCircuitJsonToGltf(circuitJson: CircuitJson, options?: ConversionOptions): Promise<ArrayBuffer | object>;
166
+
167
+ export { type BoardRenderOptions, type BoundingBox, type Box3D, COORDINATE_TRANSFORMS, type Camera3D, type CircuitTo3DOptions, type Color, type ConversionOptions, type CoordinateTransformConfig, type GLTFExportOptions, type Light3D, type OBJMaterial, type OBJMesh, type Point3, type STLMesh, type Scene3D, type Size3, type Triangle, applyCoordinateTransform, clearOBJCache, clearSTLCache, convertCircuitJsonTo3D, convertCircuitJsonToGltf, convertSceneToGLTF, loadOBJ, loadSTL, renderBoardLayer, renderBoardTextures, transformTriangles };