@expofp/renderer 3.0.0 → 3.0.1

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
@@ -1462,6 +1462,7 @@ class ImageSystem extends RenderableSystem {
1462
1462
  /** Textures memory limit in megabytes */
1463
1463
  __publicField(this, "memoryLimitMb");
1464
1464
  __publicField(this, "packer");
1465
+ __publicField(this, "originalAtlases", /* @__PURE__ */ new Map());
1465
1466
  __publicField(this, "globalTranslationMatrix", new Matrix4());
1466
1467
  __publicField(this, "originTranslationMatrix", new Matrix4());
1467
1468
  __publicField(this, "rotationMatrix", new Matrix4());
@@ -1472,6 +1473,10 @@ class ImageSystem extends RenderableSystem {
1472
1473
  const padding = 1;
1473
1474
  this.packer = new MaxRectsPacker(atlasTextureSize, atlasTextureSize, padding, { pot: false });
1474
1475
  }
1476
+ dispose() {
1477
+ super.dispose();
1478
+ this.originalAtlases.clear();
1479
+ }
1475
1480
  updateLayerImpl(group, layerDef) {
1476
1481
  super.updateLayerImpl(group, layerDef);
1477
1482
  if (this.memoryLimitMb) this.resizeTextures();
@@ -1507,13 +1512,15 @@ class ImageSystem extends RenderableSystem {
1507
1512
  this.registerDefObject(def, batchedMesh, instanceId);
1508
1513
  }
1509
1514
  const nonResizable = rectsWithDef.some(({ def }) => def.source instanceof HTMLCanvasElement);
1510
- batchedMesh.userData["nonResizable"] = nonResizable;
1515
+ if (!nonResizable) this.originalAtlases.set(batchedMesh, texture.image);
1511
1516
  group.add(batchedMesh);
1512
1517
  }
1513
1518
  return group;
1514
1519
  }
1515
1520
  /**
1516
1521
  * Resize textures to fit the memory limit.
1522
+ * Uses stored original atlases so that every call computes a globally proportional
1523
+ * resize factor, regardless of how many layers have been loaded so far.
1517
1524
  */
1518
1525
  resizeTextures() {
1519
1526
  var _a2;
@@ -1522,47 +1529,54 @@ class ImageSystem extends RenderableSystem {
1522
1529
  return;
1523
1530
  }
1524
1531
  logger$c.debug(`Resizing textures to fit memory limit: ${this.memoryLimitMb} MB`);
1525
- const texturesToResize = [];
1526
- let totalResizable = 0;
1532
+ const resizableMeshes = [];
1533
+ let totalOriginal = 0;
1527
1534
  let totalNonResizable = 0;
1528
1535
  for (const mesh of this.getAllObjects()) {
1536
+ const originalCanvas = this.originalAtlases.get(mesh);
1529
1537
  const texture = mesh.material.map;
1530
- const imageBytes = getTextureSizeBytes(texture);
1531
- const nonResizable = mesh.userData["nonResizable"];
1532
- if (nonResizable) {
1533
- totalNonResizable += imageBytes;
1538
+ if (originalCanvas) {
1539
+ totalOriginal += getCanvasSizeBytes(originalCanvas, texture.generateMipmaps);
1540
+ resizableMeshes.push(mesh);
1534
1541
  } else {
1535
- totalResizable += imageBytes;
1536
- texturesToResize.push(mesh);
1542
+ totalNonResizable += getCanvasSizeBytes(texture.image, texture.generateMipmaps);
1537
1543
  }
1538
1544
  }
1545
+ if (resizableMeshes.length === 0) {
1546
+ logger$c.debug("No resizable meshes found, no need to resize textures.");
1547
+ return;
1548
+ }
1539
1549
  const budget = this.memoryLimitMb * 1024 * 1024 - totalNonResizable;
1540
1550
  if (budget < 0) {
1541
1551
  logger$c.debug("Memory limit is too low, unable to resize textures.");
1542
1552
  return;
1543
1553
  }
1544
- const resizeFactor = Math.sqrt(budget / totalResizable);
1554
+ const resizeFactor = Math.sqrt(budget / totalOriginal);
1545
1555
  if (resizeFactor >= 1) {
1546
1556
  logger$c.debug("Textures are already within the memory limit, no need to resize");
1547
1557
  return;
1548
1558
  }
1549
1559
  logger$c.debug(`Resize factor: ${resizeFactor}`);
1550
1560
  let newTotal = totalNonResizable;
1551
- for (const mesh of texturesToResize) {
1561
+ for (const mesh of resizableMeshes) {
1552
1562
  const material = mesh.material;
1553
- const texture = material.map;
1554
- const resizedTexture = resizeTexture(texture, resizeFactor);
1555
- const textureDim = `${texture.image.width}x${texture.image.height}`;
1563
+ const currentTexture = material.map;
1564
+ const originalCanvas = this.originalAtlases.get(mesh);
1565
+ const resizedTexture = resizeCanvas(originalCanvas, resizeFactor);
1566
+ const originalDim = `${originalCanvas.width}x${originalCanvas.height}`;
1556
1567
  const resizedDim = `${resizedTexture.image.width}x${resizedTexture.image.height}`;
1557
- logger$c.debug(`Resized atlas for ${mesh.name || ((_a2 = mesh.parent) == null ? void 0 : _a2.name)}, from ${textureDim} to ${resizedDim}`);
1558
- newTotal += getTextureSizeBytes(resizedTexture);
1568
+ logger$c.debug(`Resized atlas for ${mesh.name || ((_a2 = mesh.parent) == null ? void 0 : _a2.name)}, from ${originalDim} to ${resizedDim}`);
1569
+ newTotal += getCanvasSizeBytes(resizedTexture.image, resizedTexture.generateMipmaps);
1559
1570
  material.map = resizedTexture;
1560
1571
  material.needsUpdate = true;
1561
- texture.dispose();
1562
- mesh.userData["nonResizable"] = true;
1572
+ currentTexture.dispose();
1563
1573
  }
1564
1574
  logger$c.debug(`New memory usage after resizing: ${newTotal} bytes`);
1565
1575
  }
1576
+ disposeObject(object) {
1577
+ this.originalAtlases.delete(object);
1578
+ super.disposeObject(object);
1579
+ }
1566
1580
  updateDefImpl(imageDef, mesh, instanceIds) {
1567
1581
  const instanceId = instanceIds[0];
1568
1582
  const bounds = imageDef.bounds;
@@ -1620,12 +1634,12 @@ function createAtlas(bin) {
1620
1634
  logger$c.debug(`Create atlas took ${(t1 - t0).toFixed(2)} milliseconds.`);
1621
1635
  return createTexture(canvas);
1622
1636
  }
1623
- function resizeTexture(texture, resizeFactor) {
1637
+ function resizeCanvas(source, resizeFactor) {
1624
1638
  const canvas = document.createElement("canvas");
1625
- canvas.width = Math.floor(texture.image.width * resizeFactor);
1626
- canvas.height = Math.floor(texture.image.height * resizeFactor);
1639
+ canvas.width = Math.floor(source.width * resizeFactor);
1640
+ canvas.height = Math.floor(source.height * resizeFactor);
1627
1641
  const ctx = canvas.getContext("2d");
1628
- ctx.drawImage(texture.image, 0, 0, canvas.width, canvas.height);
1642
+ ctx.drawImage(source, 0, 0, canvas.width, canvas.height);
1629
1643
  return createTexture(canvas);
1630
1644
  }
1631
1645
  function createTexture(source) {
@@ -1636,9 +1650,8 @@ function createTexture(source) {
1636
1650
  texture.needsUpdate = true;
1637
1651
  return texture;
1638
1652
  }
1639
- function getTextureSizeBytes(texture) {
1640
- const imageBytes = texture.image.width * texture.image.height * 4 * (texture.generateMipmaps ? 1.33 : 1);
1641
- return Math.ceil(imageBytes);
1653
+ function getCanvasSizeBytes(canvas, useMipmaps) {
1654
+ return Math.ceil(canvas.width * canvas.height * 4 * (useMipmaps ? 1.33 : 1));
1642
1655
  }
1643
1656
  const logger$b = createLogger("line");
1644
1657
  class LineSystem extends RenderableSystem {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@expofp/renderer",
3
- "version": "3.0.0",
3
+ "version": "3.0.1",
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"