poppygl 0.0.10 → 0.0.11
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 +3 -1
- package/dist/index.js +79 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -124,6 +124,8 @@ declare function renderGLTFToPNGBufferFromURL(url: string, options?: RenderGLTFT
|
|
|
124
124
|
type RenderGLTFToPNGBufferFromGLBBufferOptions = RenderOptionsInput;
|
|
125
125
|
declare function renderGLTFToPNGBufferFromGLBBuffer(glb: ArrayBuffer | Uint8Array, options?: RenderGLTFToPNGBufferFromGLBBufferOptions): Promise<Buffer>;
|
|
126
126
|
|
|
127
|
+
declare function renderGLTFToPNGBuffer(gltfPath: string, options?: RenderOptionsInput): Promise<Buffer>;
|
|
128
|
+
|
|
127
129
|
declare function renderSceneFromGLTF(scene: GLTFScene, options?: RenderOptionsInput, imageFactory?: ImageFactory): RenderResult;
|
|
128
130
|
|
|
129
|
-
export { type BitmapLike, type Camera, DEFAULT_LIGHT_DIR, DEFAULT_RENDER_OPTIONS, type DrawCall, type FetchLike, type GLTFResources, type GLTFScene, type ImageFactory, type LightSettings, type LoadGLTFWithResourcesFromURLOptions, type Material, type MutableRGBA, type RGBA, type RenderGLTFToPNGBufferFromGLBBufferOptions, type RenderGLTFToPNGBufferFromURLOptions, type RenderOptions, type RenderOptionsInput, type RenderResult, SoftwareRenderer, bufferFromDataURI, buildCamera, computeSmoothNormals, computeWorldAABB, createSceneFromGLTF, createUint8Bitmap, decodeImageFromBuffer, encodePNGToBuffer, getDefaultRenderOptions, isJPG, isPNG, loadGLTFWithResourcesFromURL, pureImageFactory, renderDrawCalls, renderGLTFToPNGBufferFromGLBBuffer, renderGLTFToPNGBufferFromURL, renderSceneFromGLTF, resolveRenderOptions };
|
|
131
|
+
export { type BitmapLike, type Camera, DEFAULT_LIGHT_DIR, DEFAULT_RENDER_OPTIONS, type DrawCall, type FetchLike, type GLTFResources, type GLTFScene, type ImageFactory, type LightSettings, type LoadGLTFWithResourcesFromURLOptions, type Material, type MutableRGBA, type RGBA, type RenderGLTFToPNGBufferFromGLBBufferOptions, type RenderGLTFToPNGBufferFromURLOptions, type RenderOptions, type RenderOptionsInput, type RenderResult, SoftwareRenderer, bufferFromDataURI, buildCamera, computeSmoothNormals, computeWorldAABB, createSceneFromGLTF, createUint8Bitmap, decodeImageFromBuffer, encodePNGToBuffer, getDefaultRenderOptions, isJPG, isPNG, loadGLTFWithResourcesFromURL, pureImageFactory, renderDrawCalls, renderGLTFToPNGBuffer, renderGLTFToPNGBufferFromGLBBuffer, renderGLTFToPNGBufferFromURL, renderSceneFromGLTF, resolveRenderOptions };
|
package/dist/index.js
CHANGED
|
@@ -969,8 +969,8 @@ async function encodePNGToBuffer(image) {
|
|
|
969
969
|
passThrough.on("data", (chunk) => {
|
|
970
970
|
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
971
971
|
});
|
|
972
|
-
const resultPromise = new Promise((
|
|
973
|
-
passThrough.on("end", () =>
|
|
972
|
+
const resultPromise = new Promise((resolve2, reject) => {
|
|
973
|
+
passThrough.on("end", () => resolve2(Buffer.concat(chunks)));
|
|
974
974
|
passThrough.on("error", reject);
|
|
975
975
|
});
|
|
976
976
|
await PImage3.encodePNGToStream(image, passThrough);
|
|
@@ -1066,6 +1066,82 @@ async function renderGLTFToPNGBufferFromGLBBuffer(glb, options = {}) {
|
|
|
1066
1066
|
return encodePNGToBuffer(bitmap);
|
|
1067
1067
|
}
|
|
1068
1068
|
|
|
1069
|
+
// cli/loadGLTFWithResources.ts
|
|
1070
|
+
import * as fs from "fs";
|
|
1071
|
+
import * as path from "path";
|
|
1072
|
+
async function loadGLTFWithResources(gltfPath) {
|
|
1073
|
+
const baseDir = path.dirname(gltfPath);
|
|
1074
|
+
const gltf = JSON.parse(await fs.promises.readFile(gltfPath, "utf8"));
|
|
1075
|
+
const buffers = await Promise.all(
|
|
1076
|
+
(gltf.buffers || []).map(async (b) => {
|
|
1077
|
+
if (b.uri && b.uri.startsWith("data:")) {
|
|
1078
|
+
return bufferFromDataURI(b.uri);
|
|
1079
|
+
}
|
|
1080
|
+
if (b.uri) {
|
|
1081
|
+
const resolved = path.resolve(baseDir, decodeURIComponent(b.uri));
|
|
1082
|
+
return fs.promises.readFile(resolved);
|
|
1083
|
+
}
|
|
1084
|
+
throw new Error("Buffer without uri not supported in this loader.");
|
|
1085
|
+
})
|
|
1086
|
+
);
|
|
1087
|
+
const images = await Promise.all(
|
|
1088
|
+
(gltf.images || []).map(async (img) => {
|
|
1089
|
+
if (img.uri) {
|
|
1090
|
+
if (img.uri.startsWith("data:")) {
|
|
1091
|
+
const buf = bufferFromDataURI(img.uri);
|
|
1092
|
+
return decodeImageFromBuffer(buf, img.mimeType);
|
|
1093
|
+
}
|
|
1094
|
+
const filePath = path.resolve(baseDir, decodeURIComponent(img.uri));
|
|
1095
|
+
const fileBuf = await fs.promises.readFile(filePath);
|
|
1096
|
+
const hintedMime = isPNG(img.uri) ? "image/png" : isJPG(img.uri) ? "image/jpeg" : img.mimeType;
|
|
1097
|
+
return decodeImageFromBuffer(fileBuf, hintedMime);
|
|
1098
|
+
}
|
|
1099
|
+
if (typeof img.bufferView === "number") {
|
|
1100
|
+
const bufferView = gltf.bufferViews?.[img.bufferView];
|
|
1101
|
+
if (!bufferView) {
|
|
1102
|
+
throw new Error(`Invalid image bufferView index ${img.bufferView}`);
|
|
1103
|
+
}
|
|
1104
|
+
const buffer = buffers[bufferView.buffer];
|
|
1105
|
+
if (!buffer)
|
|
1106
|
+
throw new Error(
|
|
1107
|
+
`Missing buffer for image bufferView ${img.bufferView}`
|
|
1108
|
+
);
|
|
1109
|
+
const byteOffset = bufferView.byteOffset ?? 0;
|
|
1110
|
+
const byteLength = bufferView.byteLength;
|
|
1111
|
+
if (typeof byteLength !== "number") {
|
|
1112
|
+
throw new Error(
|
|
1113
|
+
`bufferView ${img.bufferView} missing byteLength for image.`
|
|
1114
|
+
);
|
|
1115
|
+
}
|
|
1116
|
+
const slice = Buffer.from(
|
|
1117
|
+
buffer.buffer,
|
|
1118
|
+
buffer.byteOffset + byteOffset,
|
|
1119
|
+
byteLength
|
|
1120
|
+
);
|
|
1121
|
+
return decodeImageFromBuffer(slice, img.mimeType);
|
|
1122
|
+
}
|
|
1123
|
+
throw new Error(
|
|
1124
|
+
"images[*] entry missing uri or bufferView; unsupported in this lightweight loader."
|
|
1125
|
+
);
|
|
1126
|
+
})
|
|
1127
|
+
);
|
|
1128
|
+
return {
|
|
1129
|
+
gltf,
|
|
1130
|
+
resources: {
|
|
1131
|
+
buffers,
|
|
1132
|
+
images
|
|
1133
|
+
}
|
|
1134
|
+
};
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
// cli/renderGLTFToPNGBuffer.ts
|
|
1138
|
+
async function renderGLTFToPNGBuffer(gltfPath, options = {}) {
|
|
1139
|
+
const { gltf, resources } = await loadGLTFWithResources(gltfPath);
|
|
1140
|
+
const scene = createSceneFromGLTF(gltf, resources);
|
|
1141
|
+
const { bitmap } = renderSceneFromGLTF(scene, options, pureImageFactory);
|
|
1142
|
+
return encodePNGToBuffer(bitmap);
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1069
1145
|
// lib/index.ts
|
|
1070
1146
|
function renderSceneFromGLTF(scene, options, imageFactory) {
|
|
1071
1147
|
return renderDrawCalls(scene.drawCalls, options, imageFactory);
|
|
@@ -1088,6 +1164,7 @@ export {
|
|
|
1088
1164
|
loadGLTFWithResourcesFromURL,
|
|
1089
1165
|
pureImageFactory,
|
|
1090
1166
|
renderDrawCalls,
|
|
1167
|
+
renderGLTFToPNGBuffer,
|
|
1091
1168
|
renderGLTFToPNGBufferFromGLBBuffer,
|
|
1092
1169
|
renderGLTFToPNGBufferFromURL,
|
|
1093
1170
|
renderSceneFromGLTF,
|