poppygl 0.0.21 → 0.0.22
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 +4 -2
- package/dist/index.js +78 -45
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -204,8 +204,10 @@ declare function renderGLTFToPNGBufferFromURL(url: string, options?: RenderGLTFT
|
|
|
204
204
|
type RenderGLTFToPNGBufferFromGLBBufferOptions = RenderOptionsInput;
|
|
205
205
|
declare function renderGLTFToPNGBufferFromGLBBuffer(glb: ArrayBuffer | Uint8Array, options?: RenderGLTFToPNGBufferFromGLBBufferOptions): Promise<Buffer>;
|
|
206
206
|
|
|
207
|
-
|
|
207
|
+
interface RenderGLTFToPNGBufferOptions extends RenderOptionsInput, LoadGLTFWithResourcesFromURLOptions {
|
|
208
|
+
}
|
|
209
|
+
declare function renderGLTFToPNGBuffer(gltfOrJson: string | any, options?: RenderGLTFToPNGBufferOptions, resources?: GLTFResources): Promise<Buffer>;
|
|
208
210
|
|
|
209
211
|
declare function renderSceneFromGLTF(scene: GLTFScene, options?: RenderOptionsInput, imageFactory?: ImageFactory): RenderResult;
|
|
210
212
|
|
|
211
|
-
export { type BitmapLike, type Camera, type CameraRotation, type CameraUp, DEFAULT_LIGHT_DIR, DEFAULT_RENDER_OPTIONS, type DebugPoint, type DrawCall, type FetchLike, type GLTFResources, type GLTFScene, type GridOptions, 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, createGrid, createInfiniteGrid, createSceneFromGLTF, createUint8Bitmap, decodeImageFromBuffer, drawInfiniteGrid, encodePNGToBuffer, getDefaultRenderOptions, hexToRgb, isJPG, isPNG, loadGLTFWithResourcesFromURL, pureImageFactory, renderDrawCalls, renderGLTFToPNGBuffer, renderGLTFToPNGBufferFromGLBBuffer, renderGLTFToPNGBufferFromURL, renderSceneFromGLTF, resolveRenderOptions };
|
|
213
|
+
export { type BitmapLike, type Camera, type CameraRotation, type CameraUp, DEFAULT_LIGHT_DIR, DEFAULT_RENDER_OPTIONS, type DebugPoint, type DrawCall, type FetchLike, type GLTFResources, type GLTFScene, type GridOptions, type ImageFactory, type LightSettings, type LoadGLTFWithResourcesFromURLOptions, type Material, type MutableRGBA, type RGBA, type RenderGLTFToPNGBufferFromGLBBufferOptions, type RenderGLTFToPNGBufferFromURLOptions, type RenderGLTFToPNGBufferOptions, type RenderOptions, type RenderOptionsInput, type RenderResult, SoftwareRenderer, bufferFromDataURI, buildCamera, computeSmoothNormals, computeWorldAABB, createGrid, createInfiniteGrid, createSceneFromGLTF, createUint8Bitmap, decodeImageFromBuffer, drawInfiniteGrid, encodePNGToBuffer, getDefaultRenderOptions, hexToRgb, isJPG, isPNG, loadGLTFWithResourcesFromURL, pureImageFactory, renderDrawCalls, renderGLTFToPNGBuffer, renderGLTFToPNGBufferFromGLBBuffer, renderGLTFToPNGBufferFromURL, renderSceneFromGLTF, resolveRenderOptions };
|
package/dist/index.js
CHANGED
|
@@ -1811,8 +1811,8 @@ async function encodePNGToBuffer(image) {
|
|
|
1811
1811
|
passThrough.on("data", (chunk) => {
|
|
1812
1812
|
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
1813
1813
|
});
|
|
1814
|
-
const resultPromise = new Promise((
|
|
1815
|
-
passThrough.on("end", () =>
|
|
1814
|
+
const resultPromise = new Promise((resolve, reject) => {
|
|
1815
|
+
passThrough.on("end", () => resolve(Buffer.concat(chunks)));
|
|
1816
1816
|
passThrough.on("error", reject);
|
|
1817
1817
|
});
|
|
1818
1818
|
await PImage3.encodePNGToStream(image, passThrough);
|
|
@@ -1908,62 +1908,59 @@ async function renderGLTFToPNGBufferFromGLBBuffer(glb, options = {}) {
|
|
|
1908
1908
|
return encodePNGToBuffer(bitmap);
|
|
1909
1909
|
}
|
|
1910
1910
|
|
|
1911
|
-
//
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1911
|
+
// lib/gltf/loadGLTFWithResourcesFromPath.ts
|
|
1912
|
+
async function loadGLTFWithResourcesFromPath(gltfPath) {
|
|
1913
|
+
const fs = await import("fs/promises");
|
|
1914
|
+
const path = await import("path");
|
|
1915
1915
|
const baseDir = path.dirname(gltfPath);
|
|
1916
|
-
const gltf = JSON.parse(await fs.
|
|
1916
|
+
const gltf = JSON.parse(await fs.readFile(gltfPath, "utf8"));
|
|
1917
1917
|
const buffers = await Promise.all(
|
|
1918
|
-
(gltf.buffers || []).map(async (
|
|
1919
|
-
if (
|
|
1920
|
-
return bufferFromDataURI(
|
|
1918
|
+
(gltf.buffers || []).map(async (buffer) => {
|
|
1919
|
+
if (buffer.uri?.startsWith("data:")) {
|
|
1920
|
+
return bufferFromDataURI(buffer.uri);
|
|
1921
1921
|
}
|
|
1922
|
-
if (
|
|
1923
|
-
const resolved = path.resolve(baseDir, decodeURIComponent(
|
|
1924
|
-
return fs.
|
|
1922
|
+
if (buffer.uri) {
|
|
1923
|
+
const resolved = path.resolve(baseDir, decodeURIComponent(buffer.uri));
|
|
1924
|
+
return fs.readFile(resolved);
|
|
1925
1925
|
}
|
|
1926
1926
|
throw new Error("Buffer without uri not supported in this loader.");
|
|
1927
1927
|
})
|
|
1928
1928
|
);
|
|
1929
1929
|
const images = await Promise.all(
|
|
1930
|
-
(gltf.images || []).map(async (
|
|
1931
|
-
if (
|
|
1932
|
-
if (
|
|
1933
|
-
const
|
|
1934
|
-
return decodeImageFromBuffer(
|
|
1930
|
+
(gltf.images || []).map(async (image) => {
|
|
1931
|
+
if (image.uri) {
|
|
1932
|
+
if (image.uri.startsWith("data:")) {
|
|
1933
|
+
const bytes = bufferFromDataURI(image.uri);
|
|
1934
|
+
return decodeImageFromBuffer(bytes, image.mimeType);
|
|
1935
1935
|
}
|
|
1936
|
-
const filePath = path.resolve(baseDir, decodeURIComponent(
|
|
1937
|
-
const
|
|
1938
|
-
const hintedMime = isPNG(
|
|
1939
|
-
return decodeImageFromBuffer(
|
|
1936
|
+
const filePath = path.resolve(baseDir, decodeURIComponent(image.uri));
|
|
1937
|
+
const fileBytes = await fs.readFile(filePath);
|
|
1938
|
+
const hintedMime = isPNG(image.uri) ? "image/png" : isJPG(image.uri) ? "image/jpeg" : image.mimeType;
|
|
1939
|
+
return decodeImageFromBuffer(fileBytes, hintedMime);
|
|
1940
1940
|
}
|
|
1941
|
-
if (typeof
|
|
1942
|
-
const bufferView = gltf.bufferViews?.[
|
|
1941
|
+
if (typeof image.bufferView === "number") {
|
|
1942
|
+
const bufferView = gltf.bufferViews?.[image.bufferView];
|
|
1943
1943
|
if (!bufferView) {
|
|
1944
|
-
throw new Error(`Invalid image bufferView index ${
|
|
1944
|
+
throw new Error(`Invalid image bufferView index ${image.bufferView}`);
|
|
1945
1945
|
}
|
|
1946
1946
|
const buffer = buffers[bufferView.buffer];
|
|
1947
|
-
if (!buffer)
|
|
1947
|
+
if (!buffer) {
|
|
1948
1948
|
throw new Error(
|
|
1949
|
-
`Missing buffer for image bufferView ${
|
|
1949
|
+
`Missing buffer for image bufferView ${image.bufferView}`
|
|
1950
1950
|
);
|
|
1951
|
+
}
|
|
1951
1952
|
const byteOffset = bufferView.byteOffset ?? 0;
|
|
1952
1953
|
const byteLength = bufferView.byteLength;
|
|
1953
1954
|
if (typeof byteLength !== "number") {
|
|
1954
1955
|
throw new Error(
|
|
1955
|
-
`bufferView ${
|
|
1956
|
+
`bufferView ${image.bufferView} missing byteLength for image.`
|
|
1956
1957
|
);
|
|
1957
1958
|
}
|
|
1958
|
-
const slice =
|
|
1959
|
-
|
|
1960
|
-
buffer.byteOffset + byteOffset,
|
|
1961
|
-
byteLength
|
|
1962
|
-
);
|
|
1963
|
-
return decodeImageFromBuffer(slice, img.mimeType);
|
|
1959
|
+
const slice = buffer.subarray(byteOffset, byteOffset + byteLength);
|
|
1960
|
+
return decodeImageFromBuffer(slice, image.mimeType);
|
|
1964
1961
|
}
|
|
1965
1962
|
throw new Error(
|
|
1966
|
-
"images[*] entry missing uri or bufferView; unsupported in this
|
|
1963
|
+
"images[*] entry missing uri or bufferView; unsupported in this loader."
|
|
1967
1964
|
);
|
|
1968
1965
|
})
|
|
1969
1966
|
);
|
|
@@ -1976,22 +1973,58 @@ async function loadGLTFWithResources(gltfPath) {
|
|
|
1976
1973
|
};
|
|
1977
1974
|
}
|
|
1978
1975
|
|
|
1979
|
-
//
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1976
|
+
// lib/gltf/resolveGLTFInput.ts
|
|
1977
|
+
function isNodeRuntime() {
|
|
1978
|
+
const runtimeProcess = globalThis.process;
|
|
1979
|
+
return !!runtimeProcess?.versions?.node;
|
|
1980
|
+
}
|
|
1981
|
+
function isFetchableURLInNode(source) {
|
|
1983
1982
|
try {
|
|
1984
|
-
|
|
1985
|
-
|
|
1983
|
+
const url = new URL(source);
|
|
1984
|
+
return ["http:", "https:", "data:", "blob:"].includes(url.protocol);
|
|
1986
1985
|
} catch {
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1986
|
+
return false;
|
|
1987
|
+
}
|
|
1988
|
+
}
|
|
1989
|
+
function parseGLTFJSON(source) {
|
|
1990
|
+
try {
|
|
1991
|
+
return JSON.parse(source);
|
|
1992
|
+
} catch (error) {
|
|
1993
|
+
if (error instanceof SyntaxError) return null;
|
|
1994
|
+
throw error;
|
|
1995
|
+
}
|
|
1996
|
+
}
|
|
1997
|
+
async function resolveGLTFInput(source, options = {}) {
|
|
1998
|
+
const gltf = parseGLTFJSON(source);
|
|
1999
|
+
if (gltf !== null) {
|
|
2000
|
+
return {
|
|
2001
|
+
gltf,
|
|
2002
|
+
resources: { buffers: [], images: [] }
|
|
2003
|
+
};
|
|
1990
2004
|
}
|
|
2005
|
+
if (!isNodeRuntime()) {
|
|
2006
|
+
return loadGLTFWithResourcesFromURL(source, options);
|
|
2007
|
+
}
|
|
2008
|
+
if (isFetchableURLInNode(source)) {
|
|
2009
|
+
return loadGLTFWithResourcesFromURL(source, options);
|
|
2010
|
+
}
|
|
2011
|
+
return loadGLTFWithResourcesFromPath(source);
|
|
2012
|
+
}
|
|
2013
|
+
|
|
2014
|
+
// lib/render/renderGLTFToPNGBuffer.ts
|
|
2015
|
+
function renderFromGLTF(gltf, options, resources) {
|
|
1991
2016
|
const scene = createSceneFromGLTF(gltf, resources);
|
|
1992
|
-
const { bitmap } =
|
|
2017
|
+
const { bitmap } = renderDrawCalls(scene.drawCalls, options, pureImageFactory);
|
|
1993
2018
|
return encodePNGToBuffer(bitmap);
|
|
1994
2019
|
}
|
|
2020
|
+
async function renderGLTFToPNGBuffer(gltfOrJson, options = {}, resources = { buffers: [], images: [] }) {
|
|
2021
|
+
const { fetchImpl, ...renderOptions } = options;
|
|
2022
|
+
if (typeof gltfOrJson !== "string") {
|
|
2023
|
+
return renderFromGLTF(gltfOrJson, renderOptions, resources);
|
|
2024
|
+
}
|
|
2025
|
+
const loaded = await resolveGLTFInput(gltfOrJson, { fetchImpl });
|
|
2026
|
+
return renderFromGLTF(loaded.gltf, renderOptions, loaded.resources);
|
|
2027
|
+
}
|
|
1995
2028
|
|
|
1996
2029
|
// lib/index.ts
|
|
1997
2030
|
function renderSceneFromGLTF(scene, options, imageFactory) {
|