poppygl 0.0.13 → 0.0.14

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
@@ -13,6 +13,8 @@ declare const createUint8Bitmap: ImageFactory;
13
13
  interface Material {
14
14
  baseColorFactor: [number, number, number, number];
15
15
  baseColorTexture: BitmapLike | null;
16
+ alphaMode?: "OPAQUE" | "MASK" | "BLEND";
17
+ alphaCutoff?: number;
16
18
  }
17
19
  interface DrawCall {
18
20
  positions: Float32Array;
package/dist/index.js CHANGED
@@ -544,6 +544,13 @@ var SoftwareRenderer = class {
544
544
  let g = baseColor[1] * lit;
545
545
  let b = baseColor[2] * lit;
546
546
  let a = baseColor[3];
547
+ const alphaMode = material.alphaMode ?? "OPAQUE";
548
+ const alphaCutoff = material.alphaCutoff ?? 0.5;
549
+ if (alphaMode === "MASK" && a < alphaCutoff) continue;
550
+ const shouldWriteDepth = alphaMode !== "BLEND";
551
+ if (shouldWriteDepth) {
552
+ depth[di] = z01;
553
+ }
547
554
  if (gammaOut) {
548
555
  r = srgbEncodeLinear01(clamp(r, 0, 1));
549
556
  g = srgbEncodeLinear01(clamp(g, 0, 1));
@@ -553,13 +560,29 @@ var SoftwareRenderer = class {
553
560
  g = clamp(g, 0, 1);
554
561
  b = clamp(b, 0, 1);
555
562
  }
563
+ const dstIdx = (y * this.width + x) * 4;
564
+ const dstR = (this.buffer[dstIdx + 0] ?? 0) / 255;
565
+ const dstG = (this.buffer[dstIdx + 1] ?? 0) / 255;
566
+ const dstB = (this.buffer[dstIdx + 2] ?? 0) / 255;
567
+ const dstA = (this.buffer[dstIdx + 3] ?? 0) / 255;
568
+ let outR = r;
569
+ let outG = g;
570
+ let outB = b;
571
+ let outA = a;
572
+ if (alphaMode === "BLEND" && a < 1) {
573
+ const oneMinusA = 1 - a;
574
+ outR = r * a + dstR * oneMinusA;
575
+ outG = g * a + dstG * oneMinusA;
576
+ outB = b * a + dstB * oneMinusA;
577
+ outA = a + dstA * oneMinusA;
578
+ }
556
579
  this.setPixel(
557
580
  x,
558
581
  y,
559
- r * 255 | 0,
560
- g * 255 | 0,
561
- b * 255 | 0,
562
- clamp(a, 0, 1) * 255 | 0
582
+ clamp(outR, 0, 1) * 255 | 0,
583
+ clamp(outG, 0, 1) * 255 | 0,
584
+ clamp(outB, 0, 1) * 255 | 0,
585
+ clamp(outA, 0, 1) * 255 | 0
563
586
  );
564
587
  }
565
588
  }
@@ -635,20 +658,34 @@ function renderDrawCalls(drawCalls, optionsInput = {}, imageFactory = createUint
635
658
  };
636
659
  allDrawCalls.push(createGrid(finalGridOptions));
637
660
  }
638
- for (const dc of allDrawCalls) {
639
- if (dc.mode === 1) {
640
- renderer.drawLines(dc, camera, options.gamma);
641
- } else {
642
- renderer.drawMesh(
643
- dc,
644
- camera,
645
- { dir: options.lightDir, ambient: options.ambient },
646
- dc.material,
647
- options.cull,
648
- options.gamma
649
- );
661
+ const opaqueDraws = allDrawCalls.filter(
662
+ (dc) => (dc.material.alphaMode ?? "OPAQUE") === "OPAQUE"
663
+ );
664
+ const maskDraws = allDrawCalls.filter(
665
+ (dc) => (dc.material.alphaMode ?? "OPAQUE") === "MASK"
666
+ );
667
+ const blendDraws = allDrawCalls.filter(
668
+ (dc) => (dc.material.alphaMode ?? "OPAQUE") === "BLEND"
669
+ );
670
+ const renderGroup = (dcs) => {
671
+ for (const dc of dcs) {
672
+ if (dc.mode === 1) {
673
+ renderer.drawLines(dc, camera, options.gamma);
674
+ } else {
675
+ renderer.drawMesh(
676
+ dc,
677
+ camera,
678
+ { dir: options.lightDir, ambient: options.ambient },
679
+ dc.material,
680
+ options.cull,
681
+ options.gamma
682
+ );
683
+ }
650
684
  }
651
- }
685
+ };
686
+ renderGroup(opaqueDraws);
687
+ renderGroup(maskDraws);
688
+ renderGroup(blendDraws);
652
689
  return { bitmap: renderer.bitmap, camera, options };
653
690
  }
654
691
 
@@ -877,7 +914,14 @@ function getMaterial(gltf, textures, images, materialIndex) {
877
914
  texImg = images[imageIndex] || null;
878
915
  }
879
916
  }
880
- return { baseColorFactor: factor, baseColorTexture: texImg };
917
+ const alphaMode = typeof material.alphaMode === "string" ? material.alphaMode : "OPAQUE";
918
+ const alphaCutoff = typeof material.alphaCutoff === "number" ? material.alphaCutoff : 0.5;
919
+ return {
920
+ baseColorFactor: factor,
921
+ baseColorTexture: texImg,
922
+ alphaMode,
923
+ alphaCutoff
924
+ };
881
925
  }
882
926
  function createSceneFromGLTF(gltf, resources) {
883
927
  const ctx = { gltf, buffers: resources.buffers };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "poppygl",
3
3
  "main": "dist/index.js",
4
- "version": "0.0.13",
4
+ "version": "0.0.14",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/tscircuit/poppygl.git"