@webxr-jp/mtoon-atlas 0.1.6 → 0.3.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/README.md CHANGED
@@ -156,6 +156,60 @@ void main() {
156
156
  }
157
157
  ```
158
158
 
159
+ ## KTX2 トランスコーダーの配信元
160
+
161
+ KTX2 テクスチャの読み込みには Basis Universal のトランスコーダー
162
+ (`basis_transcoder.js` / `.wasm`) が必要です。既定では jsdelivr の CDN から
163
+ 取得しますが、以下の理由で差し替えたいことがあります。
164
+
165
+ - CDN 障害や CORS 事故の影響を受けたくない(自己ホストしたい)
166
+ - アプリ側がインストールしている three とバージョンを揃えたい
167
+
168
+ ### アプリ全体で切り替える(推奨)
169
+
170
+ 起動時に一度呼びます。`MToonAtlasLoaderPlugin` を複数箇所で生成している場合でも、
171
+ 呼び出しごとにオプションを渡す必要がありません。
172
+
173
+ ```typescript
174
+ import { setKtx2TranscoderPath } from '@webxr-jp/mtoon-atlas'
175
+
176
+ // public/basis/ に basis_transcoder.js / .wasm を置いて同一オリジンで配信する
177
+ setKtx2TranscoderPath('/basis/')
178
+ ```
179
+
180
+ VRM を読み込む前に呼んでください。生成済みのローダーには影響しません。
181
+
182
+ トランスコーダーの実体は three が同梱しています。ビルド時に
183
+ `node_modules/three/examples/jsm/libs/basis/` からコピーすれば、
184
+ three を更新してもバージョンがずれません。
185
+
186
+ ### KTX2Loader を共有する
187
+
188
+ GLTFLoader 側で設定済みのインスタンスを渡すと、二重生成(初期化用の
189
+ 一時 WebGLRenderer が 2 つ作られる)も避けられます。
190
+
191
+ ```typescript
192
+ import { MToonAtlasLoaderPlugin } from '@webxr-jp/mtoon-atlas'
193
+
194
+ loader.register((parser) => new MToonAtlasLoaderPlugin(parser, { ktx2Loader }))
195
+ ```
196
+
197
+ ### プラグイン単位でパスを指定する
198
+
199
+ ```typescript
200
+ loader.register(
201
+ (parser) => new MToonAtlasLoaderPlugin(parser, { ktx2TranscoderPath: '/basis/' }),
202
+ )
203
+ ```
204
+
205
+ 優先順位は `ktx2Loader` > `ktx2TranscoderPath` > `setKtx2TranscoderPath()` の既定値 > CDN です。
206
+
207
+ `@webxr-jp/avatar-optimizer` の `loadVRM()` も同じオプションを受け取ります。
208
+
209
+ ```typescript
210
+ const result = await loadVRM(file, { ktx2TranscoderPath: '/basis/' })
211
+ ```
212
+
159
213
  ## API Reference
160
214
 
161
215
  ### MToonAtlasMaterial
package/dist/index.cjs CHANGED
@@ -30,11 +30,17 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
+ DEFAULT_KTX2_TRANSCODER_PATH: () => DEFAULT_KTX2_TRANSCODER_PATH,
33
34
  MTOON_ATLAS_EXTENSION_NAME: () => MTOON_ATLAS_EXTENSION_NAME,
34
35
  MToonAtlasExporterPlugin: () => MToonAtlasExporterPlugin,
35
36
  MToonAtlasLoaderPlugin: () => MToonAtlasLoaderPlugin,
36
37
  MToonAtlasMaterial: () => MToonAtlasMaterial,
37
- rememberKtx2Source: () => rememberKtx2Source
38
+ applyAtlasTextureColorSpace: () => applyAtlasTextureColorSpace,
39
+ clearKtx2LoaderCache: () => clearKtx2LoaderCache,
40
+ getKtx2TranscoderPath: () => getKtx2TranscoderPath,
41
+ rememberKtx2Source: () => rememberKtx2Source,
42
+ resolveKtx2Loader: () => resolveKtx2Loader,
43
+ setKtx2TranscoderPath: () => setKtx2TranscoderPath
38
44
  });
39
45
  module.exports = __toCommonJS(index_exports);
40
46
 
@@ -505,9 +511,8 @@ var MToonAtlasMaterial = class _MToonAtlasMaterial extends THREE.ShaderMaterial
505
511
  };
506
512
 
507
513
  // src/extensions/MToonAtlasLoaderPlugin.ts
508
- var import_three = require("three");
514
+ var import_three2 = require("three");
509
515
  var import_fast_png = require("fast-png");
510
- var import_KTX2Loader = require("three/examples/jsm/loaders/KTX2Loader.js");
511
516
 
512
517
  // src/extensions/ktx2-source-cache.ts
513
518
  var ktx2SourceByTextureSource = /* @__PURE__ */ new WeakMap();
@@ -528,18 +533,31 @@ function isCompressedTexture(texture) {
528
533
  return texture.isCompressedTexture === true;
529
534
  }
530
535
 
531
- // src/extensions/types.ts
532
- var MTOON_ATLAS_EXTENSION_NAME = "XRIFT_mtoon_atlas";
533
-
534
- // src/extensions/MToonAtlasLoaderPlugin.ts
535
- var ktx2LoaderInstance = null;
536
- function getKTX2Loader() {
537
- if (ktx2LoaderInstance) {
538
- return ktx2LoaderInstance;
536
+ // src/extensions/ktx2-loader.ts
537
+ var import_three = require("three");
538
+ var import_KTX2Loader = require("three/examples/jsm/loaders/KTX2Loader.js");
539
+ var DEFAULT_KTX2_TRANSCODER_PATH = "https://cdn.jsdelivr.net/npm/three@0.181.1/examples/jsm/libs/basis/";
540
+ function normalizeTranscoderPath(path) {
541
+ return path.endsWith("/") ? path : `${path}/`;
542
+ }
543
+ var defaultTranscoderPath = DEFAULT_KTX2_TRANSCODER_PATH;
544
+ var loaderCache = /* @__PURE__ */ new Map();
545
+ function setKtx2TranscoderPath(path) {
546
+ defaultTranscoderPath = normalizeTranscoderPath(path);
547
+ }
548
+ function getKtx2TranscoderPath() {
549
+ return defaultTranscoderPath;
550
+ }
551
+ function resolveKtx2Loader(transcoderPath) {
552
+ const path = transcoderPath === void 0 ? defaultTranscoderPath : normalizeTranscoderPath(transcoderPath);
553
+ const cached = loaderCache.get(path);
554
+ if (cached) {
555
+ return cached;
539
556
  }
540
557
  if (typeof document === "undefined" || typeof WebGLRenderingContext === "undefined") {
541
558
  return null;
542
559
  }
560
+ let renderer;
543
561
  try {
544
562
  const canvas = document.createElement("canvas");
545
563
  const gl = canvas.getContext("webgl2");
@@ -547,26 +565,53 @@ function getKTX2Loader() {
547
565
  console.warn("MToonAtlasLoaderPlugin: WebGL2\u304C\u30B5\u30DD\u30FC\u30C8\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
548
566
  return null;
549
567
  }
550
- const renderer = new import_three.WebGLRenderer({ canvas, context: gl });
551
- ktx2LoaderInstance = new import_KTX2Loader.KTX2Loader();
552
- ktx2LoaderInstance.setTranscoderPath(
553
- "https://cdn.jsdelivr.net/npm/three@0.175.0/examples/jsm/libs/basis/"
554
- );
555
- ktx2LoaderInstance.detectSupport(renderer);
556
- renderer.dispose();
557
- return ktx2LoaderInstance;
568
+ renderer = new import_three.WebGLRenderer({ canvas, context: gl });
569
+ const loader = new import_KTX2Loader.KTX2Loader();
570
+ loader.setTranscoderPath(path);
571
+ loader.detectSupport(renderer);
572
+ loaderCache.set(path, loader);
573
+ return loader;
558
574
  } catch (error) {
559
575
  console.warn("MToonAtlasLoaderPlugin: KTX2Loader\u306E\u521D\u671F\u5316\u306B\u5931\u6557:", error);
560
576
  return null;
577
+ } finally {
578
+ renderer?.dispose();
579
+ }
580
+ }
581
+ function clearKtx2LoaderCache() {
582
+ for (const loader of loaderCache.values()) {
583
+ loader.dispose();
561
584
  }
585
+ loaderCache.clear();
586
+ }
587
+
588
+ // src/extensions/types.ts
589
+ var MTOON_ATLAS_EXTENSION_NAME = "XRIFT_mtoon_atlas";
590
+
591
+ // src/extensions/MToonAtlasLoaderPlugin.ts
592
+ var SRGB_ATLAS_SLOTS = ["baseColor", "shade", "emissive", "matcap", "rim"];
593
+ function applyAtlasTextureColorSpace(texture, slot, isKtx2) {
594
+ if (isKtx2) {
595
+ return;
596
+ }
597
+ texture.colorSpace = SRGB_ATLAS_SLOTS.includes(slot) ? import_three2.SRGBColorSpace : import_three2.NoColorSpace;
562
598
  }
563
599
  var MToonAtlasLoaderPlugin = class {
564
- constructor(parser) {
600
+ constructor(parser, options = {}) {
565
601
  this.name = MTOON_ATLAS_EXTENSION_NAME;
566
602
  // 画像インデックス -> 元 KTX2 バイナリの複製(再エクスポート用、issue #39)
567
603
  // 同じアトラス画像が複数マテリアルから参照されるため、画像単位でメモ化する
568
604
  this.ktx2SourceByImageIndex = /* @__PURE__ */ new Map();
569
605
  this.parser = parser;
606
+ this.injectedKtx2Loader = options.ktx2Loader;
607
+ this.ktx2TranscoderPath = options.ktx2TranscoderPath;
608
+ }
609
+ /**
610
+ * このプラグインが使う KTX2Loader を取得する
611
+ * 注入されたインスタンス > 指定パス > アプリ全体の既定値 の順に解決する
612
+ */
613
+ getKtx2Loader() {
614
+ return this.injectedKtx2Loader ?? resolveKtx2Loader(this.ktx2TranscoderPath);
570
615
  }
571
616
  /**
572
617
  * 再エクスポート用に元の KTX2 バイナリの複製を取得する(画像単位でメモ化)
@@ -672,11 +717,11 @@ var MToonAtlasLoaderPlugin = class {
672
717
  }
673
718
  }
674
719
  }
675
- const texture = new import_three.DataTexture(floatData, width, height, import_three.RGBAFormat, import_three.FloatType);
720
+ const texture = new import_three2.DataTexture(floatData, width, height, import_three2.RGBAFormat, import_three2.FloatType);
676
721
  texture.flipY = false;
677
- texture.colorSpace = import_three.NoColorSpace;
678
- texture.minFilter = import_three.NearestFilter;
679
- texture.magFilter = import_three.NearestFilter;
722
+ texture.colorSpace = import_three2.NoColorSpace;
723
+ texture.minFilter = import_three2.NearestFilter;
724
+ texture.magFilter = import_three2.NearestFilter;
680
725
  texture.needsUpdate = true;
681
726
  return texture;
682
727
  }
@@ -719,7 +764,7 @@ var MToonAtlasLoaderPlugin = class {
719
764
  console.warn("MToonAtlasLoaderPlugin: \u7121\u52B9\u306A\u753B\u50CF\u5B9A\u7FA9\uFF08uri/bufferView\u306A\u3057\uFF09");
720
765
  return null;
721
766
  }
722
- const ktx2Loader = getKTX2Loader();
767
+ const ktx2Loader = this.getKtx2Loader();
723
768
  if (!ktx2Loader) {
724
769
  console.warn("MToonAtlasLoaderPlugin: KTX2Loader\u304C\u5229\u7528\u3067\u304D\u307E\u305B\u3093");
725
770
  return null;
@@ -730,10 +775,10 @@ var MToonAtlasLoaderPlugin = class {
730
775
  ktx2Data,
731
776
  (texture) => {
732
777
  texture.flipY = false;
733
- texture.minFilter = import_three.LinearMipmapLinearFilter;
734
- texture.magFilter = import_three.LinearFilter;
735
- texture.wrapS = import_three.RepeatWrapping;
736
- texture.wrapT = import_three.RepeatWrapping;
778
+ texture.minFilter = import_three2.LinearMipmapLinearFilter;
779
+ texture.magFilter = import_three2.LinearFilter;
780
+ texture.wrapS = import_three2.RepeatWrapping;
781
+ texture.wrapT = import_three2.RepeatWrapping;
737
782
  texture.needsUpdate = true;
738
783
  if (ktx2SourceCopy) {
739
784
  rememberKtx2Source(texture, ktx2SourceCopy);
@@ -775,12 +820,7 @@ var MToonAtlasLoaderPlugin = class {
775
820
  texture.source = loadedTexture.source;
776
821
  }
777
822
  texture.flipY = false;
778
- const srgbTextures = ["baseColor", "shade", "emissive", "matcap", "rim"];
779
- if (srgbTextures.includes(key)) {
780
- texture.colorSpace = import_three.SRGBColorSpace;
781
- } else {
782
- texture.colorSpace = import_three.NoColorSpace;
783
- }
823
+ applyAtlasTextureColorSpace(texture, key, isKtx2);
784
824
  atlasedTextures[key] = texture;
785
825
  } catch (error) {
786
826
  console.warn(`MToonAtlasLoaderPlugin: \u30C6\u30AF\u30B9\u30C1\u30E3 ${key} (index: ${textureInfo.index}) \u306E\u8AAD\u307F\u8FBC\u307F\u4E2D\u306B\u30A8\u30E9\u30FC\u304C\u767A\u751F\u3057\u307E\u3057\u305F:`, error);
@@ -803,11 +843,11 @@ var MToonAtlasLoaderPlugin = class {
803
843
  await Promise.all(pending);
804
844
  if (!parameterTexture) {
805
845
  const dummyData = new Float32Array(4).fill(0);
806
- parameterTexture = new import_three.DataTexture(dummyData, 1, 1, import_three.RGBAFormat, import_three.FloatType);
846
+ parameterTexture = new import_three2.DataTexture(dummyData, 1, 1, import_three2.RGBAFormat, import_three2.FloatType);
807
847
  parameterTexture.flipY = false;
808
- parameterTexture.colorSpace = import_three.NoColorSpace;
809
- parameterTexture.minFilter = import_three.NearestFilter;
810
- parameterTexture.magFilter = import_three.NearestFilter;
848
+ parameterTexture.colorSpace = import_three2.NoColorSpace;
849
+ parameterTexture.minFilter = import_three2.NearestFilter;
850
+ parameterTexture.magFilter = import_three2.NearestFilter;
811
851
  parameterTexture.needsUpdate = true;
812
852
  }
813
853
  const slotAttributeName = extension.slotAttributeName.toLowerCase();
@@ -824,9 +864,9 @@ var MToonAtlasLoaderPlugin = class {
824
864
  name: materialDef.name
825
865
  });
826
866
  if (materialDef.doubleSided) {
827
- material.side = import_three.DoubleSide;
867
+ material.side = import_three2.DoubleSide;
828
868
  } else {
829
- material.side = import_three.FrontSide;
869
+ material.side = import_three2.FrontSide;
830
870
  }
831
871
  if (materialDef.alphaMode === "BLEND") {
832
872
  material.transparent = true;
@@ -851,7 +891,7 @@ var MToonAtlasLoaderPlugin = class {
851
891
  };
852
892
 
853
893
  // src/extensions/MToonAtlasExporterPlugin.ts
854
- var import_three2 = require("three");
894
+ var import_three3 = require("three");
855
895
  var import_fast_png2 = require("fast-png");
856
896
  var import_texture_compression = require("@webxr-jp/texture-compression");
857
897
  var MToonAtlasExporterPlugin = class {
@@ -891,7 +931,7 @@ var MToonAtlasExporterPlugin = class {
891
931
  const roots = Array.isArray(input) ? input : [input];
892
932
  for (const root of roots) {
893
933
  root.traverse((obj) => {
894
- if (obj instanceof import_three2.Mesh) {
934
+ if (obj instanceof import_three3.Mesh) {
895
935
  const materials = Array.isArray(obj.material) ? obj.material : [obj.material];
896
936
  const mtoonMaterials = [];
897
937
  for (const material of materials) {
@@ -1342,7 +1382,7 @@ var MToonAtlasExporterPlugin = class {
1342
1382
  compressionLevel: this.textureCompressionOptions?.compressionLevel,
1343
1383
  generateMipmaps: this.textureCompressionOptions?.generateMipmaps,
1344
1384
  supercompression: this.textureCompressionOptions?.supercompression,
1345
- srgb: texture.colorSpace === import_three2.SRGBColorSpace
1385
+ srgb: texture.colorSpace === import_three3.SRGBColorSpace
1346
1386
  };
1347
1387
  const result = await (0, import_texture_compression.compressToKtx2)(
1348
1388
  rgbaData,
@@ -1783,10 +1823,16 @@ var MToonAtlasExporterPlugin = class {
1783
1823
  };
1784
1824
  // Annotate the CommonJS export names for ESM import in node:
1785
1825
  0 && (module.exports = {
1826
+ DEFAULT_KTX2_TRANSCODER_PATH,
1786
1827
  MTOON_ATLAS_EXTENSION_NAME,
1787
1828
  MToonAtlasExporterPlugin,
1788
1829
  MToonAtlasLoaderPlugin,
1789
1830
  MToonAtlasMaterial,
1790
- rememberKtx2Source
1831
+ applyAtlasTextureColorSpace,
1832
+ clearKtx2LoaderCache,
1833
+ getKtx2TranscoderPath,
1834
+ rememberKtx2Source,
1835
+ resolveKtx2Loader,
1836
+ setKtx2TranscoderPath
1791
1837
  });
1792
1838
  //# sourceMappingURL=index.cjs.map