circuit-json-to-gltf 0.0.2 → 0.0.4
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/LICENSE +21 -0
- package/dist/index.js +22 -5
- package/dist/svg-to-png-browser-4BZIU5HP.js +66 -0
- package/package.json +12 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 tscircuit Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.js
CHANGED
|
@@ -520,7 +520,7 @@ async function convertSvgToPng(svgString, resolution, backgroundColor) {
|
|
|
520
520
|
if (typeof window !== "undefined" && typeof document !== "undefined") {
|
|
521
521
|
return convertSvgToCanvasBrowser(svgString, resolution, backgroundColor);
|
|
522
522
|
} else {
|
|
523
|
-
const { svgToPngDataUrl } = await import("./svg-to-png-browser-
|
|
523
|
+
const { svgToPngDataUrl } = await import("./svg-to-png-browser-4BZIU5HP.js");
|
|
524
524
|
return await svgToPngDataUrl(svgString, {
|
|
525
525
|
width: resolution,
|
|
526
526
|
background: backgroundColor
|
|
@@ -1139,7 +1139,11 @@ function createMeshFromOBJ(objMesh) {
|
|
|
1139
1139
|
const baseIndex = targetMesh.positions.length / 3;
|
|
1140
1140
|
for (const vertex of triangle.vertices) {
|
|
1141
1141
|
targetMesh.positions.push(vertex.x, vertex.y, vertex.z);
|
|
1142
|
-
targetMesh.normals.push(
|
|
1142
|
+
targetMesh.normals.push(
|
|
1143
|
+
triangle.normal.x,
|
|
1144
|
+
triangle.normal.y,
|
|
1145
|
+
triangle.normal.z
|
|
1146
|
+
);
|
|
1143
1147
|
targetMesh.texcoords.push(vertex.x, vertex.z);
|
|
1144
1148
|
}
|
|
1145
1149
|
targetMesh.indices.push(baseIndex, baseIndex + 2, baseIndex + 1);
|
|
@@ -1332,7 +1336,12 @@ var GLTFBuilder = class {
|
|
|
1332
1336
|
const alpha = 1 - dissolve;
|
|
1333
1337
|
let baseColor = [0.3, 0.3, 0.3, alpha];
|
|
1334
1338
|
if (objMaterial.color) {
|
|
1335
|
-
const color = typeof objMaterial.color === "string" ? this.parseColorString(objMaterial.color) : [
|
|
1339
|
+
const color = typeof objMaterial.color === "string" ? this.parseColorString(objMaterial.color) : [
|
|
1340
|
+
objMaterial.color[0] / 255,
|
|
1341
|
+
objMaterial.color[1] / 255,
|
|
1342
|
+
objMaterial.color[2] / 255,
|
|
1343
|
+
alpha
|
|
1344
|
+
];
|
|
1336
1345
|
baseColor = [color[0], color[1], color[2], alpha];
|
|
1337
1346
|
}
|
|
1338
1347
|
const gltfMaterialIndex = this.addMaterial({
|
|
@@ -1349,7 +1358,11 @@ var GLTFBuilder = class {
|
|
|
1349
1358
|
}
|
|
1350
1359
|
const primitives = [];
|
|
1351
1360
|
for (const { meshData, materialIndex } of meshDataArray) {
|
|
1352
|
-
const transformedMeshData = transformMesh(
|
|
1361
|
+
const transformedMeshData = transformMesh(
|
|
1362
|
+
meshData,
|
|
1363
|
+
box.center,
|
|
1364
|
+
box.rotation
|
|
1365
|
+
);
|
|
1353
1366
|
const positionAccessorIndex = this.addAccessor(
|
|
1354
1367
|
transformedMeshData.positions,
|
|
1355
1368
|
"VEC3",
|
|
@@ -1464,7 +1477,11 @@ var GLTFBuilder = class {
|
|
|
1464
1477
|
const meshIndex = this.meshes.length;
|
|
1465
1478
|
const primitives = [];
|
|
1466
1479
|
for (const [faceName, faceData] of Object.entries(faceMeshes)) {
|
|
1467
|
-
const transformedFaceData = transformMesh(
|
|
1480
|
+
const transformedFaceData = transformMesh(
|
|
1481
|
+
faceData,
|
|
1482
|
+
box.center,
|
|
1483
|
+
box.rotation
|
|
1484
|
+
);
|
|
1468
1485
|
const positionAccessorIndex = this.addAccessor(
|
|
1469
1486
|
transformedFaceData.positions,
|
|
1470
1487
|
"VEC3",
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// lib/utils/svg-to-png-browser.ts
|
|
2
|
+
import { Resvg, initWasm } from "@resvg/resvg-wasm";
|
|
3
|
+
var wasmInitialized = false;
|
|
4
|
+
async function ensureWasmInitialized() {
|
|
5
|
+
if (!wasmInitialized) {
|
|
6
|
+
try {
|
|
7
|
+
if (typeof process !== "undefined" && process.versions?.node) {
|
|
8
|
+
const { readFileSync } = await import("fs");
|
|
9
|
+
const { fileURLToPath } = await import("url");
|
|
10
|
+
const { dirname, join } = await import("path");
|
|
11
|
+
const currentDir = dirname(fileURLToPath(import.meta.url));
|
|
12
|
+
const wasmPath = join(
|
|
13
|
+
currentDir,
|
|
14
|
+
"../../node_modules/@resvg/resvg-wasm/index_bg.wasm"
|
|
15
|
+
);
|
|
16
|
+
const wasmBuffer = readFileSync(wasmPath);
|
|
17
|
+
await initWasm(wasmBuffer);
|
|
18
|
+
} else {
|
|
19
|
+
try {
|
|
20
|
+
const wasmUrl = await import("@resvg/resvg-wasm/index_bg.wasm?url");
|
|
21
|
+
await initWasm(fetch(wasmUrl.default));
|
|
22
|
+
} catch {
|
|
23
|
+
await initWasm(
|
|
24
|
+
fetch("https://unpkg.com/@resvg/resvg-wasm@2.6.2/index_bg.wasm")
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
wasmInitialized = true;
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.error("Failed to initialize WASM:", error);
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async function svgToPng(svgString, options = {}) {
|
|
36
|
+
await ensureWasmInitialized();
|
|
37
|
+
const opts = {
|
|
38
|
+
background: options.background,
|
|
39
|
+
fitTo: options.width ? {
|
|
40
|
+
mode: "width",
|
|
41
|
+
value: options.width
|
|
42
|
+
} : options.height ? {
|
|
43
|
+
mode: "height",
|
|
44
|
+
value: options.height
|
|
45
|
+
} : void 0
|
|
46
|
+
};
|
|
47
|
+
const resvg = new Resvg(svgString, opts);
|
|
48
|
+
const pngData = resvg.render();
|
|
49
|
+
const pngBuffer = pngData.asPng();
|
|
50
|
+
return pngBuffer;
|
|
51
|
+
}
|
|
52
|
+
async function svgToPngDataUrl(svgString, options = {}) {
|
|
53
|
+
const pngBuffer = await svgToPng(svgString, options);
|
|
54
|
+
let binary = "";
|
|
55
|
+
const bytes = new Uint8Array(pngBuffer);
|
|
56
|
+
const len = bytes.byteLength;
|
|
57
|
+
for (let i = 0; i < len; i++) {
|
|
58
|
+
binary += String.fromCharCode(bytes[i]);
|
|
59
|
+
}
|
|
60
|
+
const base64 = btoa(binary);
|
|
61
|
+
return `data:image/png;base64,${base64}`;
|
|
62
|
+
}
|
|
63
|
+
export {
|
|
64
|
+
svgToPng,
|
|
65
|
+
svgToPngDataUrl
|
|
66
|
+
};
|
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.
|
|
5
|
+
"version": "0.0.4",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "bun test tests/",
|
|
8
8
|
"format": "biome format --write .",
|
|
@@ -35,7 +35,17 @@
|
|
|
35
35
|
"vite": "^7.1.1"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"typescript": "^5"
|
|
38
|
+
"typescript": "^5",
|
|
39
|
+
"@resvg/resvg-wasm": "2",
|
|
40
|
+
"@resvg/resvg-js": "2",
|
|
41
|
+
"circuit-to-svg": "*",
|
|
42
|
+
"@tscircuit/circuit-json-util": "*",
|
|
43
|
+
"circuit-json": "*"
|
|
44
|
+
},
|
|
45
|
+
"peerDependenciesMeta": {
|
|
46
|
+
"@resvg/resvg-wasm": {
|
|
47
|
+
"optional": true
|
|
48
|
+
}
|
|
39
49
|
},
|
|
40
50
|
"dependencies": {}
|
|
41
51
|
}
|