@scenography/engine 0.3.1 → 0.4.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Javier López Úbeda
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs CHANGED
@@ -479,8 +479,126 @@ const EDGES = "edges";
479
479
  */
480
480
  const EYE_HEIGHT = "eyeHeight";
481
481
  //#endregion
482
+ //#region src/builders/textureCache.ts
483
+ /** Two surfaces can share a texture only at the same tiling and color space. */
484
+ const textureKey = (src, repeat, srgb) => `${src}|${repeat[0]}x${repeat[1]}|${srgb ? "srgb" : "linear"}`;
485
+ /** Tiling and color space of a texture; the image arrives later from the loader. */
486
+ const configureTexture = (texture, repeat, srgb) => {
487
+ texture.wrapS = three.RepeatWrapping;
488
+ texture.wrapT = three.RepeatWrapping;
489
+ texture.repeat.set(repeat[0], repeat[1]);
490
+ texture.colorSpace = srgb ? three.SRGBColorSpace : three.NoColorSpace;
491
+ };
492
+ /**
493
+ * Shares textures between materials: the same source at the same tiling and
494
+ * color space becomes one texture, loaded once and uploaded to the GPU once.
495
+ * Reference counted, so releasing a material keeps the texture alive while
496
+ * another one still samples it.
497
+ */
498
+ var TextureCache = class {
499
+ source;
500
+ entries = /* @__PURE__ */ new Map();
501
+ owners = /* @__PURE__ */ new Map();
502
+ constructor(source) {
503
+ this.source = source;
504
+ }
505
+ /** The shared texture for a source, created on first use. */
506
+ acquire(src, repeat, srgb) {
507
+ const key = textureKey(src, repeat, srgb);
508
+ const existing = this.entries.get(key);
509
+ if (existing) {
510
+ existing.refs += 1;
511
+ return existing.texture;
512
+ }
513
+ const texture = this.source(src);
514
+ configureTexture(texture, repeat, srgb);
515
+ const entry = {
516
+ key,
517
+ texture,
518
+ refs: 1
519
+ };
520
+ this.entries.set(key, entry);
521
+ this.owners.set(texture, entry);
522
+ return texture;
523
+ }
524
+ /** Drop one reference; the texture is disposed when the last one goes. */
525
+ release(texture) {
526
+ const entry = this.owners.get(texture);
527
+ if (!entry) {
528
+ texture.dispose();
529
+ return;
530
+ }
531
+ entry.refs -= 1;
532
+ if (entry.refs > 0) return;
533
+ this.entries.delete(entry.key);
534
+ this.owners.delete(texture);
535
+ texture.dispose();
536
+ }
537
+ /** Textures currently shared. */
538
+ get size() {
539
+ return this.entries.size;
540
+ }
541
+ };
542
+ //#endregion
543
+ //#region src/builders/texture.ts
544
+ const textureLoader = new three.TextureLoader();
545
+ const cache = new TextureCache((src) => textureLoader.load(src));
546
+ /**
547
+ * Load one map into `material[slot]`. Textures are shared: every material that
548
+ * samples the same source at the same tiling reuses one texture object. `.exr`
549
+ * maps go through EXRLoader, imported on demand, and are not shared because
550
+ * they resolve asynchronously.
551
+ */
552
+ const loadMap = (material, slot, src, repeat, srgb) => {
553
+ if (src.toLowerCase().endsWith(".exr")) {
554
+ import("three/examples/jsm/loaders/EXRLoader.js").then(({ EXRLoader }) => {
555
+ new EXRLoader().load(src, (texture) => {
556
+ configureTexture(texture, repeat, srgb);
557
+ texture.needsUpdate = true;
558
+ material[slot] = texture;
559
+ material.needsUpdate = true;
560
+ });
561
+ });
562
+ return;
563
+ }
564
+ material[slot] = cache.acquire(src, repeat, srgb);
565
+ };
566
+ /**
567
+ * Apply a `SurfaceTexture` to a standard material. Returns whether a second UV
568
+ * set (`uv1`) is needed — three samples `aoMap` from it, so the geometry must
569
+ * provide one.
570
+ */
571
+ const applyTexture = (material, texture) => {
572
+ const repeat = texture.repeat ?? [1, 1];
573
+ loadMap(material, "map", texture.src, repeat, true);
574
+ if (texture.normal) {
575
+ loadMap(material, "normalMap", texture.normal, repeat, false);
576
+ const scale = texture.normalScale ?? 1;
577
+ material.normalScale = new three.Vector2(scale, scale);
578
+ }
579
+ if (texture.roughness) loadMap(material, "roughnessMap", texture.roughness, repeat, false);
580
+ if (texture.ao) loadMap(material, "aoMap", texture.ao, repeat, false);
581
+ material.needsUpdate = true;
582
+ return texture.ao !== void 0;
583
+ };
584
+ /**
585
+ * Release one reference to a texture assigned by `applyTexture`. Shared
586
+ * textures stay alive while another material still samples them.
587
+ */
588
+ const releaseTexture = (texture) => {
589
+ cache.release(texture);
590
+ };
591
+ //#endregion
482
592
  //#region src/SceneManager.ts
593
+ const TEXTURE_SLOTS = [
594
+ "map",
595
+ "normalMap",
596
+ "roughnessMap",
597
+ "aoMap",
598
+ "metalnessMap"
599
+ ];
483
600
  const disposeObject = (object) => {
601
+ const materials = /* @__PURE__ */ new Set();
484
602
  object.traverse((child) => {
485
603
  const video = child.userData?.video;
486
604
  if (video) {
@@ -490,21 +608,18 @@ const disposeObject = (object) => {
490
608
  }
491
609
  const mesh = child;
492
610
  mesh.geometry?.dispose?.();
493
- const dispose = (m) => {
494
- const material = m;
495
- for (const slot of [
496
- "map",
497
- "normalMap",
498
- "roughnessMap",
499
- "aoMap",
500
- "metalnessMap"
501
- ]) (material?.[slot])?.dispose?.();
502
- material?.dispose?.();
503
- };
504
611
  const material = mesh.material;
505
- if (Array.isArray(material)) material.forEach(dispose);
506
- else dispose(material);
612
+ if (Array.isArray(material)) for (const entry of material) materials.add(entry);
613
+ else if (material) materials.add(material);
507
614
  });
615
+ for (const entry of materials) {
616
+ const material = entry;
617
+ for (const slot of TEXTURE_SLOTS) {
618
+ const texture = material[slot];
619
+ if (texture) releaseTexture(texture);
620
+ }
621
+ material.dispose?.();
622
+ }
508
623
  };
509
624
  /**
510
625
  * Owns the live three.js scene graph and keeps it in sync with a `Scene`
@@ -954,50 +1069,6 @@ function createGallery(scene, options) {
954
1069
  };
955
1070
  }
956
1071
  //#endregion
957
- //#region src/builders/texture.ts
958
- const textureLoader = new three.TextureLoader();
959
- /** Configure a loaded texture: tiling + color space (sRGB only for color maps). */
960
- const configure = (tex, repeat, srgb) => {
961
- tex.wrapS = three.RepeatWrapping;
962
- tex.wrapT = three.RepeatWrapping;
963
- tex.repeat.set(repeat[0], repeat[1]);
964
- tex.colorSpace = srgb ? three.SRGBColorSpace : three.NoColorSpace;
965
- tex.needsUpdate = true;
966
- };
967
- /**
968
- * Load one map and assign it to `material[slot]` once ready. `.exr` maps go
969
- * through EXRLoader, imported on demand so it only ships in the bundle when a
970
- * scene actually uses an EXR map; everything else uses the image loader.
971
- */
972
- const loadMap = (material, slot, src, repeat, srgb) => {
973
- const assign = (tex) => {
974
- configure(tex, repeat, srgb);
975
- material[slot] = tex;
976
- material.needsUpdate = true;
977
- };
978
- if (src.toLowerCase().endsWith(".exr")) import("three/examples/jsm/loaders/EXRLoader.js").then(({ EXRLoader }) => {
979
- new EXRLoader().load(src, assign);
980
- });
981
- else textureLoader.load(src, assign);
982
- };
983
- /**
984
- * Apply a `SurfaceTexture` to a standard material. Returns whether a second UV
985
- * set (`uv1`) is needed — three samples `aoMap` from it, so the geometry must
986
- * provide one.
987
- */
988
- const applyTexture = (material, texture) => {
989
- const repeat = texture.repeat ?? [1, 1];
990
- loadMap(material, "map", texture.src, repeat, true);
991
- if (texture.normal) {
992
- loadMap(material, "normalMap", texture.normal, repeat, false);
993
- const s = texture.normalScale ?? 1;
994
- material.normalScale = new three.Vector2(s, s);
995
- }
996
- if (texture.roughness) loadMap(material, "roughnessMap", texture.roughness, repeat, false);
997
- if (texture.ao) loadMap(material, "aoMap", texture.ao, repeat, false);
998
- return texture.ao !== void 0;
999
- };
1000
- //#endregion
1001
1072
  exports.COLLIDERS = COLLIDERS;
1002
1073
  exports.CapabilityRegistry = CapabilityRegistry;
1003
1074
  exports.ControlSettings = ControlSettings;