@expofp/renderer 2.3.2 → 2.3.3

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.
Files changed (2) hide show
  1. package/dist/index.js +38 -25
  2. package/package.json +1 -3
package/dist/index.js CHANGED
@@ -1472,6 +1472,7 @@ class ImageSystem extends RenderableSystem {
1472
1472
  /** Textures memory limit in megabytes */
1473
1473
  __publicField(this, "memoryLimitMb");
1474
1474
  __publicField(this, "packer");
1475
+ __publicField(this, "originalAtlases", /* @__PURE__ */ new Map());
1475
1476
  __publicField(this, "globalTranslationMatrix", new Matrix4());
1476
1477
  __publicField(this, "originTranslationMatrix", new Matrix4());
1477
1478
  __publicField(this, "rotationMatrix", new Matrix4());
@@ -1482,6 +1483,10 @@ class ImageSystem extends RenderableSystem {
1482
1483
  const padding = 1;
1483
1484
  this.packer = new MaxRectsPacker(atlasTextureSize, atlasTextureSize, padding, { pot: false });
1484
1485
  }
1486
+ dispose() {
1487
+ super.dispose();
1488
+ this.originalAtlases.clear();
1489
+ }
1485
1490
  updateLayerImpl(group, layerDef) {
1486
1491
  super.updateLayerImpl(group, layerDef);
1487
1492
  if (this.memoryLimitMb) this.resizeTextures();
@@ -1517,13 +1522,15 @@ class ImageSystem extends RenderableSystem {
1517
1522
  this.registerDefObject(def, batchedMesh, instanceId);
1518
1523
  }
1519
1524
  const nonResizable = rectsWithDef.some(({ def }) => def.source instanceof HTMLCanvasElement);
1520
- batchedMesh.userData["nonResizable"] = nonResizable;
1525
+ if (!nonResizable) this.originalAtlases.set(batchedMesh, texture.image);
1521
1526
  group.add(batchedMesh);
1522
1527
  }
1523
1528
  return group;
1524
1529
  }
1525
1530
  /**
1526
1531
  * Resize textures to fit the memory limit.
1532
+ * Uses stored original atlases so that every call computes a globally proportional
1533
+ * resize factor, regardless of how many layers have been loaded so far.
1527
1534
  */
1528
1535
  resizeTextures() {
1529
1536
  var _a2;
@@ -1532,47 +1539,54 @@ class ImageSystem extends RenderableSystem {
1532
1539
  return;
1533
1540
  }
1534
1541
  logger$9.debug(`Resizing textures to fit memory limit: ${this.memoryLimitMb} MB`);
1535
- const texturesToResize = [];
1536
- let totalResizable = 0;
1542
+ const resizableMeshes = [];
1543
+ let totalOriginal = 0;
1537
1544
  let totalNonResizable = 0;
1538
1545
  for (const mesh of this.getAllObjects()) {
1546
+ const originalCanvas = this.originalAtlases.get(mesh);
1539
1547
  const texture = mesh.material.map;
1540
- const imageBytes = getTextureSizeBytes(texture);
1541
- const nonResizable = mesh.userData["nonResizable"];
1542
- if (nonResizable) {
1543
- totalNonResizable += imageBytes;
1548
+ if (originalCanvas) {
1549
+ totalOriginal += getCanvasSizeBytes(originalCanvas, texture.generateMipmaps);
1550
+ resizableMeshes.push(mesh);
1544
1551
  } else {
1545
- totalResizable += imageBytes;
1546
- texturesToResize.push(mesh);
1552
+ totalNonResizable += getCanvasSizeBytes(texture.image, texture.generateMipmaps);
1547
1553
  }
1548
1554
  }
1555
+ if (resizableMeshes.length === 0) {
1556
+ logger$9.debug("No resizable meshes found, no need to resize textures.");
1557
+ return;
1558
+ }
1549
1559
  const budget = this.memoryLimitMb * 1024 * 1024 - totalNonResizable;
1550
1560
  if (budget < 0) {
1551
1561
  logger$9.debug("Memory limit is too low, unable to resize textures.");
1552
1562
  return;
1553
1563
  }
1554
- const resizeFactor = Math.sqrt(budget / totalResizable);
1564
+ const resizeFactor = Math.sqrt(budget / totalOriginal);
1555
1565
  if (resizeFactor >= 1) {
1556
1566
  logger$9.debug("Textures are already within the memory limit, no need to resize");
1557
1567
  return;
1558
1568
  }
1559
1569
  logger$9.debug(`Resize factor: ${resizeFactor}`);
1560
1570
  let newTotal = totalNonResizable;
1561
- for (const mesh of texturesToResize) {
1571
+ for (const mesh of resizableMeshes) {
1562
1572
  const material = mesh.material;
1563
- const texture = material.map;
1564
- const resizedTexture = resizeTexture(texture, resizeFactor);
1565
- const textureDim = `${texture.image.width}x${texture.image.height}`;
1573
+ const currentTexture = material.map;
1574
+ const originalCanvas = this.originalAtlases.get(mesh);
1575
+ const resizedTexture = resizeCanvas(originalCanvas, resizeFactor);
1576
+ const originalDim = `${originalCanvas.width}x${originalCanvas.height}`;
1566
1577
  const resizedDim = `${resizedTexture.image.width}x${resizedTexture.image.height}`;
1567
- logger$9.debug(`Resized atlas for ${mesh.name || ((_a2 = mesh.parent) == null ? void 0 : _a2.name)}, from ${textureDim} to ${resizedDim}`);
1568
- newTotal += getTextureSizeBytes(resizedTexture);
1578
+ logger$9.debug(`Resized atlas for ${mesh.name || ((_a2 = mesh.parent) == null ? void 0 : _a2.name)}, from ${originalDim} to ${resizedDim}`);
1579
+ newTotal += getCanvasSizeBytes(resizedTexture.image, resizedTexture.generateMipmaps);
1569
1580
  material.map = resizedTexture;
1570
1581
  material.needsUpdate = true;
1571
- texture.dispose();
1572
- mesh.userData["nonResizable"] = true;
1582
+ currentTexture.dispose();
1573
1583
  }
1574
1584
  logger$9.debug(`New memory usage after resizing: ${newTotal} bytes`);
1575
1585
  }
1586
+ disposeObject(object) {
1587
+ this.originalAtlases.delete(object);
1588
+ super.disposeObject(object);
1589
+ }
1576
1590
  updateDefImpl(imageDef, mesh, instanceIds) {
1577
1591
  const instanceId = instanceIds[0];
1578
1592
  const bounds = imageDef.bounds;
@@ -1630,12 +1644,12 @@ function createAtlas(bin) {
1630
1644
  logger$9.debug(`Create atlas took ${(t1 - t0).toFixed(2)} milliseconds.`);
1631
1645
  return createTexture(canvas);
1632
1646
  }
1633
- function resizeTexture(texture, resizeFactor) {
1647
+ function resizeCanvas(source, resizeFactor) {
1634
1648
  const canvas = document.createElement("canvas");
1635
- canvas.width = Math.floor(texture.image.width * resizeFactor);
1636
- canvas.height = Math.floor(texture.image.height * resizeFactor);
1649
+ canvas.width = Math.floor(source.width * resizeFactor);
1650
+ canvas.height = Math.floor(source.height * resizeFactor);
1637
1651
  const ctx = canvas.getContext("2d");
1638
- ctx.drawImage(texture.image, 0, 0, canvas.width, canvas.height);
1652
+ ctx.drawImage(source, 0, 0, canvas.width, canvas.height);
1639
1653
  return createTexture(canvas);
1640
1654
  }
1641
1655
  function createTexture(source) {
@@ -1646,9 +1660,8 @@ function createTexture(source) {
1646
1660
  texture.needsUpdate = true;
1647
1661
  return texture;
1648
1662
  }
1649
- function getTextureSizeBytes(texture) {
1650
- const imageBytes = texture.image.width * texture.image.height * 4 * (texture.generateMipmaps ? 1.33 : 1);
1651
- return Math.ceil(imageBytes);
1663
+ function getCanvasSizeBytes(canvas, useMipmaps) {
1664
+ return Math.ceil(canvas.width * canvas.height * 4 * (useMipmaps ? 1.33 : 1));
1652
1665
  }
1653
1666
  const logger$8 = createLogger("line");
1654
1667
  class LineSystem extends RenderableSystem {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@expofp/renderer",
3
- "version": "2.3.2",
3
+ "version": "2.3.3",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"
@@ -14,8 +14,6 @@
14
14
  "@types/object-hash": "^3.0.6",
15
15
  "@types/three": "^0.174.0",
16
16
  "stats-gl": "^3.6.0",
17
- "typescript": "^5.2.2",
18
- "vite": "^5.2.0",
19
17
  "vite-bundle-analyzer": "^1.3.2",
20
18
  "vite-plugin-dts": "^4.5.4",
21
19
  "vite-plugin-externalize-deps": "^0.9.0"