poppygl 0.0.15 → 0.0.17

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
@@ -53,6 +53,12 @@ interface GLTFScene {
53
53
  }
54
54
 
55
55
  declare const DEFAULT_LIGHT_DIR: readonly [-0.4, -0.9, -0.2];
56
+ /**
57
+ * Convert a hex color string to RGB array with values 0-1
58
+ * @param hex - Hex color string like "#ffffff" or "ffffff"
59
+ * @returns RGB array [r, g, b] with values 0-1, or null if invalid
60
+ */
61
+ declare function hexToRgb(hex: string): [number, number, number] | null;
56
62
  interface RenderOptions {
57
63
  width: number;
58
64
  height: number;
@@ -63,7 +69,7 @@ interface RenderOptions {
63
69
  lightDir: readonly [number, number, number];
64
70
  camPos?: readonly [number, number, number] | null;
65
71
  lookAt?: readonly [number, number, number] | null;
66
- backgroundColor?: readonly [number, number, number] | null;
72
+ backgroundColor?: readonly [number, number, number] | string | null;
67
73
  grid?: boolean | GridOptions;
68
74
  }
69
75
  type RenderOptionsInput = Partial<RenderOptions>;
@@ -156,6 +162,7 @@ declare class SoftwareRenderer {
156
162
  */
157
163
  declare function drawInfiniteGrid(software_renderer: SoftwareRenderer, params: {
158
164
  camera: Camera;
165
+ grid_y?: number;
159
166
  cell_size?: number;
160
167
  section_size?: number;
161
168
  fade_distance?: number;
@@ -180,4 +187,4 @@ declare function renderGLTFToPNGBuffer(gltfPathOrString: string, options?: Rende
180
187
 
181
188
  declare function renderSceneFromGLTF(scene: GLTFScene, options?: RenderOptionsInput, imageFactory?: ImageFactory): RenderResult;
182
189
 
183
- export { type BitmapLike, type Camera, DEFAULT_LIGHT_DIR, DEFAULT_RENDER_OPTIONS, 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, isJPG, isPNG, loadGLTFWithResourcesFromURL, pureImageFactory, renderDrawCalls, renderGLTFToPNGBuffer, renderGLTFToPNGBufferFromGLBBuffer, renderGLTFToPNGBufferFromURL, renderSceneFromGLTF, resolveRenderOptions };
190
+ export { type BitmapLike, type Camera, DEFAULT_LIGHT_DIR, DEFAULT_RENDER_OPTIONS, 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 };
package/dist/index.js CHANGED
@@ -134,6 +134,16 @@ var createUint8Bitmap = (width, height) => ({
134
134
 
135
135
  // lib/render/getDefaultRenderOptions.ts
136
136
  var DEFAULT_LIGHT_DIR = [-0.4, -0.9, -0.2];
137
+ function hexToRgb(hex) {
138
+ const cleanHex = hex.replace(/^#/, "");
139
+ if (!/^[0-9A-Fa-f]{6}$/.test(cleanHex)) {
140
+ return null;
141
+ }
142
+ const r = Number.parseInt(cleanHex.substring(0, 2), 16) / 255;
143
+ const g = Number.parseInt(cleanHex.substring(2, 4), 16) / 255;
144
+ const b = Number.parseInt(cleanHex.substring(4, 6), 16) / 255;
145
+ return [r, g, b];
146
+ }
137
147
  var DEFAULT_RENDER_OPTIONS = {
138
148
  width: 800,
139
149
  height: 600,
@@ -630,6 +640,7 @@ function computeGridFadeAlpha(params) {
630
640
  );
631
641
  }
632
642
  function drawInfiniteGrid(software_renderer, params) {
643
+ const grid_y = params.grid_y ?? 0;
633
644
  const cell_size = params.cell_size ?? 1;
634
645
  const section_size = params.section_size ?? 10;
635
646
  const fade_distance = params.fade_distance ?? 100;
@@ -667,13 +678,21 @@ function drawInfiniteGrid(software_renderer, params) {
667
678
  const ray_dir_y = far_point[1] - near_point[1];
668
679
  const ray_dir_z = far_point[2] - near_point[2];
669
680
  if (Math.abs(ray_dir_y) < 1e-6) continue;
670
- const t = -near_point[1] / ray_dir_y;
681
+ const t = (grid_y - near_point[1]) / ray_dir_y;
671
682
  if (t < 0 || t > 1) continue;
672
683
  const world_x = near_point[0] + t * ray_dir_x;
673
684
  const world_z = near_point[2] + t * ray_dir_z;
685
+ const world_pt = vec43.fromValues(world_x, grid_y, world_z, 1);
686
+ const clip_pt = vec43.create();
687
+ vec43.transformMat4(clip_pt, world_pt, vp);
688
+ const ndc_z = clip_pt[2] / clip_pt[3];
689
+ const depth_01 = ndc_z * 0.5 + 0.5;
690
+ const di = y * software_renderer.width + x;
691
+ if (depth_01 >= software_renderer.depth[di]) continue;
674
692
  const dx = world_x - cam_pos_x;
693
+ const dy = grid_y - cam_pos_y;
675
694
  const dz = world_z - cam_pos_z;
676
- const dist = Math.sqrt(dx * dx + dz * dz);
695
+ const dist = Math.sqrt(dx * dx + dy * dy + dz * dz);
677
696
  const alpha = computeGridFadeAlpha({
678
697
  dist,
679
698
  fade_distance,
@@ -756,8 +775,16 @@ function renderDrawCalls(drawCalls, optionsInput = {}, imageFactory = createUint
756
775
  options.height,
757
776
  imageFactory
758
777
  );
778
+ let rgb = null;
759
779
  if (options.backgroundColor) {
760
- const [r, g, b] = options.backgroundColor;
780
+ if (typeof options.backgroundColor === "string") {
781
+ rgb = hexToRgb(options.backgroundColor);
782
+ } else {
783
+ rgb = options.backgroundColor;
784
+ }
785
+ }
786
+ if (rgb) {
787
+ const [r, g, b] = rgb;
761
788
  renderer.clear([
762
789
  Math.round(r * 255),
763
790
  Math.round(g * 255),
@@ -824,9 +851,15 @@ function renderDrawCalls(drawCalls, optionsInput = {}, imageFactory = createUint
824
851
  }
825
852
  }
826
853
  };
854
+ renderGroup(opaqueDraws);
855
+ renderGroup(maskDraws);
827
856
  if (infiniteGridOptions) {
857
+ const aabb = computeWorldAABB(drawCalls);
858
+ const modelCenterY = (aabb.min[1] + aabb.max[1]) / 2;
859
+ const gridY = infiniteGridOptions.offset?.y ?? modelCenterY;
828
860
  drawInfiniteGrid(renderer, {
829
861
  camera,
862
+ grid_y: gridY,
830
863
  cell_size: infiniteGridOptions.cellSize,
831
864
  section_size: infiniteGridOptions.sectionSize,
832
865
  fade_distance: infiniteGridOptions.fadeDistance,
@@ -836,8 +869,6 @@ function renderDrawCalls(drawCalls, optionsInput = {}, imageFactory = createUint
836
869
  gamma_out: options.gamma
837
870
  });
838
871
  }
839
- renderGroup(opaqueDraws);
840
- renderGroup(maskDraws);
841
872
  renderGroup(blendDraws);
842
873
  return { bitmap: renderer.bitmap, camera, options };
843
874
  }
@@ -1590,6 +1621,7 @@ export {
1590
1621
  drawInfiniteGrid,
1591
1622
  encodePNGToBuffer,
1592
1623
  getDefaultRenderOptions,
1624
+ hexToRgb,
1593
1625
  isJPG,
1594
1626
  isPNG,
1595
1627
  loadGLTFWithResourcesFromURL,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "poppygl",
3
3
  "main": "dist/index.js",
4
- "version": "0.0.15",
4
+ "version": "0.0.17",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/tscircuit/poppygl.git"