@webxr-jp/mtoon-atlas 0.1.6 → 0.2.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,16 @@ 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
+ clearKtx2LoaderCache: () => clearKtx2LoaderCache,
39
+ getKtx2TranscoderPath: () => getKtx2TranscoderPath,
40
+ rememberKtx2Source: () => rememberKtx2Source,
41
+ resolveKtx2Loader: () => resolveKtx2Loader,
42
+ setKtx2TranscoderPath: () => setKtx2TranscoderPath
38
43
  });
39
44
  module.exports = __toCommonJS(index_exports);
40
45
 
@@ -505,9 +510,8 @@ var MToonAtlasMaterial = class _MToonAtlasMaterial extends THREE.ShaderMaterial
505
510
  };
506
511
 
507
512
  // src/extensions/MToonAtlasLoaderPlugin.ts
508
- var import_three = require("three");
513
+ var import_three2 = require("three");
509
514
  var import_fast_png = require("fast-png");
510
- var import_KTX2Loader = require("three/examples/jsm/loaders/KTX2Loader.js");
511
515
 
512
516
  // src/extensions/ktx2-source-cache.ts
513
517
  var ktx2SourceByTextureSource = /* @__PURE__ */ new WeakMap();
@@ -528,18 +532,31 @@ function isCompressedTexture(texture) {
528
532
  return texture.isCompressedTexture === true;
529
533
  }
530
534
 
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;
535
+ // src/extensions/ktx2-loader.ts
536
+ var import_three = require("three");
537
+ var import_KTX2Loader = require("three/examples/jsm/loaders/KTX2Loader.js");
538
+ var DEFAULT_KTX2_TRANSCODER_PATH = "https://cdn.jsdelivr.net/npm/three@0.181.1/examples/jsm/libs/basis/";
539
+ function normalizeTranscoderPath(path) {
540
+ return path.endsWith("/") ? path : `${path}/`;
541
+ }
542
+ var defaultTranscoderPath = DEFAULT_KTX2_TRANSCODER_PATH;
543
+ var loaderCache = /* @__PURE__ */ new Map();
544
+ function setKtx2TranscoderPath(path) {
545
+ defaultTranscoderPath = normalizeTranscoderPath(path);
546
+ }
547
+ function getKtx2TranscoderPath() {
548
+ return defaultTranscoderPath;
549
+ }
550
+ function resolveKtx2Loader(transcoderPath) {
551
+ const path = transcoderPath === void 0 ? defaultTranscoderPath : normalizeTranscoderPath(transcoderPath);
552
+ const cached = loaderCache.get(path);
553
+ if (cached) {
554
+ return cached;
539
555
  }
540
556
  if (typeof document === "undefined" || typeof WebGLRenderingContext === "undefined") {
541
557
  return null;
542
558
  }
559
+ let renderer;
543
560
  try {
544
561
  const canvas = document.createElement("canvas");
545
562
  const gl = canvas.getContext("webgl2");
@@ -547,26 +564,46 @@ function getKTX2Loader() {
547
564
  console.warn("MToonAtlasLoaderPlugin: WebGL2\u304C\u30B5\u30DD\u30FC\u30C8\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
548
565
  return null;
549
566
  }
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;
567
+ renderer = new import_three.WebGLRenderer({ canvas, context: gl });
568
+ const loader = new import_KTX2Loader.KTX2Loader();
569
+ loader.setTranscoderPath(path);
570
+ loader.detectSupport(renderer);
571
+ loaderCache.set(path, loader);
572
+ return loader;
558
573
  } catch (error) {
559
574
  console.warn("MToonAtlasLoaderPlugin: KTX2Loader\u306E\u521D\u671F\u5316\u306B\u5931\u6557:", error);
560
575
  return null;
576
+ } finally {
577
+ renderer?.dispose();
578
+ }
579
+ }
580
+ function clearKtx2LoaderCache() {
581
+ for (const loader of loaderCache.values()) {
582
+ loader.dispose();
561
583
  }
584
+ loaderCache.clear();
562
585
  }
586
+
587
+ // src/extensions/types.ts
588
+ var MTOON_ATLAS_EXTENSION_NAME = "XRIFT_mtoon_atlas";
589
+
590
+ // src/extensions/MToonAtlasLoaderPlugin.ts
563
591
  var MToonAtlasLoaderPlugin = class {
564
- constructor(parser) {
592
+ constructor(parser, options = {}) {
565
593
  this.name = MTOON_ATLAS_EXTENSION_NAME;
566
594
  // 画像インデックス -> 元 KTX2 バイナリの複製(再エクスポート用、issue #39)
567
595
  // 同じアトラス画像が複数マテリアルから参照されるため、画像単位でメモ化する
568
596
  this.ktx2SourceByImageIndex = /* @__PURE__ */ new Map();
569
597
  this.parser = parser;
598
+ this.injectedKtx2Loader = options.ktx2Loader;
599
+ this.ktx2TranscoderPath = options.ktx2TranscoderPath;
600
+ }
601
+ /**
602
+ * このプラグインが使う KTX2Loader を取得する
603
+ * 注入されたインスタンス > 指定パス > アプリ全体の既定値 の順に解決する
604
+ */
605
+ getKtx2Loader() {
606
+ return this.injectedKtx2Loader ?? resolveKtx2Loader(this.ktx2TranscoderPath);
570
607
  }
571
608
  /**
572
609
  * 再エクスポート用に元の KTX2 バイナリの複製を取得する(画像単位でメモ化)
@@ -672,11 +709,11 @@ var MToonAtlasLoaderPlugin = class {
672
709
  }
673
710
  }
674
711
  }
675
- const texture = new import_three.DataTexture(floatData, width, height, import_three.RGBAFormat, import_three.FloatType);
712
+ const texture = new import_three2.DataTexture(floatData, width, height, import_three2.RGBAFormat, import_three2.FloatType);
676
713
  texture.flipY = false;
677
- texture.colorSpace = import_three.NoColorSpace;
678
- texture.minFilter = import_three.NearestFilter;
679
- texture.magFilter = import_three.NearestFilter;
714
+ texture.colorSpace = import_three2.NoColorSpace;
715
+ texture.minFilter = import_three2.NearestFilter;
716
+ texture.magFilter = import_three2.NearestFilter;
680
717
  texture.needsUpdate = true;
681
718
  return texture;
682
719
  }
@@ -719,7 +756,7 @@ var MToonAtlasLoaderPlugin = class {
719
756
  console.warn("MToonAtlasLoaderPlugin: \u7121\u52B9\u306A\u753B\u50CF\u5B9A\u7FA9\uFF08uri/bufferView\u306A\u3057\uFF09");
720
757
  return null;
721
758
  }
722
- const ktx2Loader = getKTX2Loader();
759
+ const ktx2Loader = this.getKtx2Loader();
723
760
  if (!ktx2Loader) {
724
761
  console.warn("MToonAtlasLoaderPlugin: KTX2Loader\u304C\u5229\u7528\u3067\u304D\u307E\u305B\u3093");
725
762
  return null;
@@ -730,10 +767,10 @@ var MToonAtlasLoaderPlugin = class {
730
767
  ktx2Data,
731
768
  (texture) => {
732
769
  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;
770
+ texture.minFilter = import_three2.LinearMipmapLinearFilter;
771
+ texture.magFilter = import_three2.LinearFilter;
772
+ texture.wrapS = import_three2.RepeatWrapping;
773
+ texture.wrapT = import_three2.RepeatWrapping;
737
774
  texture.needsUpdate = true;
738
775
  if (ktx2SourceCopy) {
739
776
  rememberKtx2Source(texture, ktx2SourceCopy);
@@ -777,9 +814,9 @@ var MToonAtlasLoaderPlugin = class {
777
814
  texture.flipY = false;
778
815
  const srgbTextures = ["baseColor", "shade", "emissive", "matcap", "rim"];
779
816
  if (srgbTextures.includes(key)) {
780
- texture.colorSpace = import_three.SRGBColorSpace;
817
+ texture.colorSpace = import_three2.SRGBColorSpace;
781
818
  } else {
782
- texture.colorSpace = import_three.NoColorSpace;
819
+ texture.colorSpace = import_three2.NoColorSpace;
783
820
  }
784
821
  atlasedTextures[key] = texture;
785
822
  } catch (error) {
@@ -803,11 +840,11 @@ var MToonAtlasLoaderPlugin = class {
803
840
  await Promise.all(pending);
804
841
  if (!parameterTexture) {
805
842
  const dummyData = new Float32Array(4).fill(0);
806
- parameterTexture = new import_three.DataTexture(dummyData, 1, 1, import_three.RGBAFormat, import_three.FloatType);
843
+ parameterTexture = new import_three2.DataTexture(dummyData, 1, 1, import_three2.RGBAFormat, import_three2.FloatType);
807
844
  parameterTexture.flipY = false;
808
- parameterTexture.colorSpace = import_three.NoColorSpace;
809
- parameterTexture.minFilter = import_three.NearestFilter;
810
- parameterTexture.magFilter = import_three.NearestFilter;
845
+ parameterTexture.colorSpace = import_three2.NoColorSpace;
846
+ parameterTexture.minFilter = import_three2.NearestFilter;
847
+ parameterTexture.magFilter = import_three2.NearestFilter;
811
848
  parameterTexture.needsUpdate = true;
812
849
  }
813
850
  const slotAttributeName = extension.slotAttributeName.toLowerCase();
@@ -824,9 +861,9 @@ var MToonAtlasLoaderPlugin = class {
824
861
  name: materialDef.name
825
862
  });
826
863
  if (materialDef.doubleSided) {
827
- material.side = import_three.DoubleSide;
864
+ material.side = import_three2.DoubleSide;
828
865
  } else {
829
- material.side = import_three.FrontSide;
866
+ material.side = import_three2.FrontSide;
830
867
  }
831
868
  if (materialDef.alphaMode === "BLEND") {
832
869
  material.transparent = true;
@@ -851,7 +888,7 @@ var MToonAtlasLoaderPlugin = class {
851
888
  };
852
889
 
853
890
  // src/extensions/MToonAtlasExporterPlugin.ts
854
- var import_three2 = require("three");
891
+ var import_three3 = require("three");
855
892
  var import_fast_png2 = require("fast-png");
856
893
  var import_texture_compression = require("@webxr-jp/texture-compression");
857
894
  var MToonAtlasExporterPlugin = class {
@@ -891,7 +928,7 @@ var MToonAtlasExporterPlugin = class {
891
928
  const roots = Array.isArray(input) ? input : [input];
892
929
  for (const root of roots) {
893
930
  root.traverse((obj) => {
894
- if (obj instanceof import_three2.Mesh) {
931
+ if (obj instanceof import_three3.Mesh) {
895
932
  const materials = Array.isArray(obj.material) ? obj.material : [obj.material];
896
933
  const mtoonMaterials = [];
897
934
  for (const material of materials) {
@@ -1342,7 +1379,7 @@ var MToonAtlasExporterPlugin = class {
1342
1379
  compressionLevel: this.textureCompressionOptions?.compressionLevel,
1343
1380
  generateMipmaps: this.textureCompressionOptions?.generateMipmaps,
1344
1381
  supercompression: this.textureCompressionOptions?.supercompression,
1345
- srgb: texture.colorSpace === import_three2.SRGBColorSpace
1382
+ srgb: texture.colorSpace === import_three3.SRGBColorSpace
1346
1383
  };
1347
1384
  const result = await (0, import_texture_compression.compressToKtx2)(
1348
1385
  rgbaData,
@@ -1783,10 +1820,15 @@ var MToonAtlasExporterPlugin = class {
1783
1820
  };
1784
1821
  // Annotate the CommonJS export names for ESM import in node:
1785
1822
  0 && (module.exports = {
1823
+ DEFAULT_KTX2_TRANSCODER_PATH,
1786
1824
  MTOON_ATLAS_EXTENSION_NAME,
1787
1825
  MToonAtlasExporterPlugin,
1788
1826
  MToonAtlasLoaderPlugin,
1789
1827
  MToonAtlasMaterial,
1790
- rememberKtx2Source
1828
+ clearKtx2LoaderCache,
1829
+ getKtx2TranscoderPath,
1830
+ rememberKtx2Source,
1831
+ resolveKtx2Loader,
1832
+ setKtx2TranscoderPath
1791
1833
  });
1792
1834
  //# sourceMappingURL=index.cjs.map