poppygl 0.0.9 → 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 CHANGED
@@ -21,6 +21,7 @@ interface RenderOptions {
21
21
  lightDir: readonly [number, number, number];
22
22
  camPos?: readonly [number, number, number] | null;
23
23
  lookAt?: readonly [number, number, number] | null;
24
+ backgroundColor?: readonly [number, number, number] | null;
24
25
  }
25
26
  type RenderOptionsInput = Partial<RenderOptions>;
26
27
  declare const DEFAULT_RENDER_OPTIONS: RenderOptions;
@@ -123,6 +124,8 @@ declare function renderGLTFToPNGBufferFromURL(url: string, options?: RenderGLTFT
123
124
  type RenderGLTFToPNGBufferFromGLBBufferOptions = RenderOptionsInput;
124
125
  declare function renderGLTFToPNGBufferFromGLBBuffer(glb: ArrayBuffer | Uint8Array, options?: RenderGLTFToPNGBufferFromGLBBufferOptions): Promise<Buffer>;
125
126
 
127
+ declare function renderGLTFToPNGBuffer(gltfPath: string, options?: RenderOptionsInput): Promise<Buffer>;
128
+
126
129
  declare function renderSceneFromGLTF(scene: GLTFScene, options?: RenderOptionsInput, imageFactory?: ImageFactory): RenderResult;
127
130
 
128
- 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
@@ -94,7 +94,8 @@ var DEFAULT_RENDER_OPTIONS = {
94
94
  ambient: 0.15,
95
95
  lightDir: DEFAULT_LIGHT_DIR,
96
96
  camPos: null,
97
- lookAt: null
97
+ lookAt: null,
98
+ backgroundColor: null
98
99
  };
99
100
  function getDefaultRenderOptions() {
100
101
  return { ...DEFAULT_RENDER_OPTIONS };
@@ -194,7 +195,6 @@ var SoftwareRenderer = class {
194
195
  this.height = height;
195
196
  this.bitmap = imageFactory(width, height);
196
197
  this.depth = new Float32Array(width * height);
197
- this.clear([0, 0, 0, 255]);
198
198
  }
199
199
  get buffer() {
200
200
  return this.bitmap.data;
@@ -457,6 +457,17 @@ function renderDrawCalls(drawCalls, optionsInput = {}, imageFactory = createUint
457
457
  options.height,
458
458
  imageFactory
459
459
  );
460
+ if (options.backgroundColor) {
461
+ const [r, g, b] = options.backgroundColor;
462
+ renderer.clear([
463
+ Math.round(r * 255),
464
+ Math.round(g * 255),
465
+ Math.round(b * 255),
466
+ 255
467
+ ]);
468
+ } else {
469
+ renderer.clear([0, 0, 0, 0]);
470
+ }
460
471
  for (const dc of drawCalls) {
461
472
  renderer.drawMesh(
462
473
  dc,
@@ -958,8 +969,8 @@ async function encodePNGToBuffer(image) {
958
969
  passThrough.on("data", (chunk) => {
959
970
  chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
960
971
  });
961
- const resultPromise = new Promise((resolve, reject) => {
962
- passThrough.on("end", () => resolve(Buffer.concat(chunks)));
972
+ const resultPromise = new Promise((resolve2, reject) => {
973
+ passThrough.on("end", () => resolve2(Buffer.concat(chunks)));
963
974
  passThrough.on("error", reject);
964
975
  });
965
976
  await PImage3.encodePNGToStream(image, passThrough);
@@ -1055,6 +1066,82 @@ async function renderGLTFToPNGBufferFromGLBBuffer(glb, options = {}) {
1055
1066
  return encodePNGToBuffer(bitmap);
1056
1067
  }
1057
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
+
1058
1145
  // lib/index.ts
1059
1146
  function renderSceneFromGLTF(scene, options, imageFactory) {
1060
1147
  return renderDrawCalls(scene.drawCalls, options, imageFactory);
@@ -1077,6 +1164,7 @@ export {
1077
1164
  loadGLTFWithResourcesFromURL,
1078
1165
  pureImageFactory,
1079
1166
  renderDrawCalls,
1167
+ renderGLTFToPNGBuffer,
1080
1168
  renderGLTFToPNGBufferFromGLBBuffer,
1081
1169
  renderGLTFToPNGBufferFromURL,
1082
1170
  renderSceneFromGLTF,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "poppygl",
3
3
  "main": "dist/index.js",
4
- "version": "0.0.9",
4
+ "version": "0.0.11",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/tscircuit/poppygl.git"