@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/dist/index.d.cts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as THREE from 'three';
2
2
  import { Texture, BufferAttribute, InterleavedBufferAttribute, BufferGeometry, Material, Object3D } from 'three';
3
+ import { KTX2Loader } from 'three/examples/jsm/loaders/KTX2Loader.js';
3
4
  import { Ktx2CompressionOptions } from '@webxr-jp/texture-compression';
4
5
 
5
6
  /**
@@ -421,11 +422,34 @@ interface GLTFWriter {
421
422
  getUID: (attribute: BufferAttribute | InterleavedBufferAttribute, isRelativeCopy?: boolean) => number;
422
423
  }
423
424
 
425
+ /**
426
+ * MToonAtlasLoaderPlugin のオプション
427
+ */
428
+ interface MToonAtlasLoaderPluginOptions {
429
+ /**
430
+ * 使用する KTX2Loader。
431
+ * GLTFLoader 側で設定済みのインスタンスを渡せば、二重生成を避けつつ
432
+ * three 本体とバージョンの揃ったトランスコーダーを共有できる
433
+ */
434
+ ktx2Loader?: KTX2Loader;
435
+ /**
436
+ * トランスコーダーの配信元ディレクトリ(末尾スラッシュ必須)。
437
+ * `ktx2Loader` を渡した場合は無視される
438
+ */
439
+ ktx2TranscoderPath?: string;
440
+ }
424
441
  declare class MToonAtlasLoaderPlugin {
425
442
  readonly name = "XRIFT_mtoon_atlas";
426
443
  private parser;
427
444
  private ktx2SourceByImageIndex;
428
- constructor(parser: GLTFParser);
445
+ private injectedKtx2Loader?;
446
+ private ktx2TranscoderPath?;
447
+ constructor(parser: GLTFParser, options?: MToonAtlasLoaderPluginOptions);
448
+ /**
449
+ * このプラグインが使う KTX2Loader を取得する
450
+ * 注入されたインスタンス > 指定パス > アプリ全体の既定値 の順に解決する
451
+ */
452
+ private getKtx2Loader;
429
453
  /**
430
454
  * 再エクスポート用に元の KTX2 バイナリの複製を取得する(画像単位でメモ化)
431
455
  *
@@ -455,6 +479,60 @@ declare class MToonAtlasLoaderPlugin {
455
479
  private _loadMaterialAsync;
456
480
  }
457
481
 
482
+ /**
483
+ * トランスコーダーパスの既定値
484
+ *
485
+ * 明示的に設定されない限り CDN を使う。バージョンは peerDependencies の
486
+ * 下限 (three >= 0.181) に合わせてある。KTX2Loader のワーカーは three 側の
487
+ * コードなので、極端に古いトランスコーダーだと将来の three で
488
+ * 未実装 API を呼んで壊れうる(例: r181 は ktx2File.isHDR() を参照する)。
489
+ *
490
+ * とはいえ URL に固定バージョンを書く以上、利用側の three とのずれは残る。
491
+ * 確実に揃えたい場合は three が同梱するトランスコーダーを
492
+ * node_modules からコピーして自己ホストし、setKtx2TranscoderPath() で
493
+ * そのパスを指定すること(README 参照)。
494
+ */
495
+ declare const DEFAULT_KTX2_TRANSCODER_PATH = "https://cdn.jsdelivr.net/npm/three@0.181.1/examples/jsm/libs/basis/";
496
+ /**
497
+ * KTX2 トランスコーダーの配信元をアプリ全体で切り替える
498
+ *
499
+ * アプリ起動時に一度呼ぶ想定。`MToonAtlasLoaderPlugin` を複数箇所で
500
+ * 生成している場合に、呼び出しごとにオプションを渡さずに済む。
501
+ * 既に生成済みのローダーには影響しないため、VRM を読み込む前に呼ぶこと。
502
+ *
503
+ * @param path - トランスコーダーを配信するディレクトリ(末尾スラッシュ必須)
504
+ *
505
+ * @example
506
+ * ```typescript
507
+ * // public/basis/ に basis_transcoder.js / .wasm を配置して同一オリジンで配信する
508
+ * setKtx2TranscoderPath('/basis/')
509
+ * ```
510
+ */
511
+ declare function setKtx2TranscoderPath(path: string): void;
512
+ /**
513
+ * 現在のトランスコーダーパスの既定値を取得する
514
+ */
515
+ declare function getKtx2TranscoderPath(): string;
516
+ /**
517
+ * KTX2Loader を取得または生成する
518
+ * ブラウザ環境でのみ動作する(WebGL コンテキストが必要)
519
+ *
520
+ * @param transcoderPath - 使用するパス。省略時は既定値
521
+ * @returns KTX2Loader。ブラウザ環境でない場合や初期化に失敗した場合は null
522
+ */
523
+ declare function resolveKtx2Loader(transcoderPath?: string): KTX2Loader | null;
524
+ /**
525
+ * キャッシュ済みの KTX2Loader を破棄する
526
+ *
527
+ * パスを変えて作り直したい場合に、古いローダーが抱えるワーカープールと
528
+ * WebGL コンテキストを解放するために使う。
529
+ *
530
+ * 注意: VRM の読み込み中に呼ばないこと。`loadVRM()` は取得したローダーを
531
+ * GLTFLoader に渡したまま非同期に読み込むため、途中で dispose すると
532
+ * ワーカープールが落ちて進行中の parse が分かりにくいエラーで失敗する。
533
+ */
534
+ declare function clearKtx2LoaderCache(): void;
535
+
458
536
  /**
459
537
  * テクスチャ圧縮オプション
460
538
  * エクスポート時にアトラステクスチャを KTX2 形式で圧縮する
@@ -651,4 +729,4 @@ declare class MToonAtlasExporterPlugin {
651
729
  */
652
730
  declare function rememberKtx2Source(texture: Texture, data: ArrayBuffer | Uint8Array): void;
653
731
 
654
- export { type AtlasedTextureSet, type DebugMode, MTOON_ATLAS_EXTENSION_NAME, MToonAtlasExporterPlugin, type MToonAtlasExtensionSchema, MToonAtlasLoaderPlugin, MToonAtlasMaterial, type MToonAtlasOptions, type MaterialSlotAttributeConfig, type ParameterSemantic, type ParameterSemanticId, type ParameterTextureDescriptor, type TextureCompressionOptions, rememberKtx2Source };
732
+ export { type AtlasedTextureSet, DEFAULT_KTX2_TRANSCODER_PATH, type DebugMode, MTOON_ATLAS_EXTENSION_NAME, MToonAtlasExporterPlugin, type MToonAtlasExtensionSchema, MToonAtlasLoaderPlugin, type MToonAtlasLoaderPluginOptions, MToonAtlasMaterial, type MToonAtlasOptions, type MaterialSlotAttributeConfig, type ParameterSemantic, type ParameterSemanticId, type ParameterTextureDescriptor, type TextureCompressionOptions, clearKtx2LoaderCache, getKtx2TranscoderPath, rememberKtx2Source, resolveKtx2Loader, setKtx2TranscoderPath };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as THREE from 'three';
2
2
  import { Texture, BufferAttribute, InterleavedBufferAttribute, BufferGeometry, Material, Object3D } from 'three';
3
+ import { KTX2Loader } from 'three/examples/jsm/loaders/KTX2Loader.js';
3
4
  import { Ktx2CompressionOptions } from '@webxr-jp/texture-compression';
4
5
 
5
6
  /**
@@ -421,11 +422,34 @@ interface GLTFWriter {
421
422
  getUID: (attribute: BufferAttribute | InterleavedBufferAttribute, isRelativeCopy?: boolean) => number;
422
423
  }
423
424
 
425
+ /**
426
+ * MToonAtlasLoaderPlugin のオプション
427
+ */
428
+ interface MToonAtlasLoaderPluginOptions {
429
+ /**
430
+ * 使用する KTX2Loader。
431
+ * GLTFLoader 側で設定済みのインスタンスを渡せば、二重生成を避けつつ
432
+ * three 本体とバージョンの揃ったトランスコーダーを共有できる
433
+ */
434
+ ktx2Loader?: KTX2Loader;
435
+ /**
436
+ * トランスコーダーの配信元ディレクトリ(末尾スラッシュ必須)。
437
+ * `ktx2Loader` を渡した場合は無視される
438
+ */
439
+ ktx2TranscoderPath?: string;
440
+ }
424
441
  declare class MToonAtlasLoaderPlugin {
425
442
  readonly name = "XRIFT_mtoon_atlas";
426
443
  private parser;
427
444
  private ktx2SourceByImageIndex;
428
- constructor(parser: GLTFParser);
445
+ private injectedKtx2Loader?;
446
+ private ktx2TranscoderPath?;
447
+ constructor(parser: GLTFParser, options?: MToonAtlasLoaderPluginOptions);
448
+ /**
449
+ * このプラグインが使う KTX2Loader を取得する
450
+ * 注入されたインスタンス > 指定パス > アプリ全体の既定値 の順に解決する
451
+ */
452
+ private getKtx2Loader;
429
453
  /**
430
454
  * 再エクスポート用に元の KTX2 バイナリの複製を取得する(画像単位でメモ化)
431
455
  *
@@ -455,6 +479,60 @@ declare class MToonAtlasLoaderPlugin {
455
479
  private _loadMaterialAsync;
456
480
  }
457
481
 
482
+ /**
483
+ * トランスコーダーパスの既定値
484
+ *
485
+ * 明示的に設定されない限り CDN を使う。バージョンは peerDependencies の
486
+ * 下限 (three >= 0.181) に合わせてある。KTX2Loader のワーカーは three 側の
487
+ * コードなので、極端に古いトランスコーダーだと将来の three で
488
+ * 未実装 API を呼んで壊れうる(例: r181 は ktx2File.isHDR() を参照する)。
489
+ *
490
+ * とはいえ URL に固定バージョンを書く以上、利用側の three とのずれは残る。
491
+ * 確実に揃えたい場合は three が同梱するトランスコーダーを
492
+ * node_modules からコピーして自己ホストし、setKtx2TranscoderPath() で
493
+ * そのパスを指定すること(README 参照)。
494
+ */
495
+ declare const DEFAULT_KTX2_TRANSCODER_PATH = "https://cdn.jsdelivr.net/npm/three@0.181.1/examples/jsm/libs/basis/";
496
+ /**
497
+ * KTX2 トランスコーダーの配信元をアプリ全体で切り替える
498
+ *
499
+ * アプリ起動時に一度呼ぶ想定。`MToonAtlasLoaderPlugin` を複数箇所で
500
+ * 生成している場合に、呼び出しごとにオプションを渡さずに済む。
501
+ * 既に生成済みのローダーには影響しないため、VRM を読み込む前に呼ぶこと。
502
+ *
503
+ * @param path - トランスコーダーを配信するディレクトリ(末尾スラッシュ必須)
504
+ *
505
+ * @example
506
+ * ```typescript
507
+ * // public/basis/ に basis_transcoder.js / .wasm を配置して同一オリジンで配信する
508
+ * setKtx2TranscoderPath('/basis/')
509
+ * ```
510
+ */
511
+ declare function setKtx2TranscoderPath(path: string): void;
512
+ /**
513
+ * 現在のトランスコーダーパスの既定値を取得する
514
+ */
515
+ declare function getKtx2TranscoderPath(): string;
516
+ /**
517
+ * KTX2Loader を取得または生成する
518
+ * ブラウザ環境でのみ動作する(WebGL コンテキストが必要)
519
+ *
520
+ * @param transcoderPath - 使用するパス。省略時は既定値
521
+ * @returns KTX2Loader。ブラウザ環境でない場合や初期化に失敗した場合は null
522
+ */
523
+ declare function resolveKtx2Loader(transcoderPath?: string): KTX2Loader | null;
524
+ /**
525
+ * キャッシュ済みの KTX2Loader を破棄する
526
+ *
527
+ * パスを変えて作り直したい場合に、古いローダーが抱えるワーカープールと
528
+ * WebGL コンテキストを解放するために使う。
529
+ *
530
+ * 注意: VRM の読み込み中に呼ばないこと。`loadVRM()` は取得したローダーを
531
+ * GLTFLoader に渡したまま非同期に読み込むため、途中で dispose すると
532
+ * ワーカープールが落ちて進行中の parse が分かりにくいエラーで失敗する。
533
+ */
534
+ declare function clearKtx2LoaderCache(): void;
535
+
458
536
  /**
459
537
  * テクスチャ圧縮オプション
460
538
  * エクスポート時にアトラステクスチャを KTX2 形式で圧縮する
@@ -651,4 +729,4 @@ declare class MToonAtlasExporterPlugin {
651
729
  */
652
730
  declare function rememberKtx2Source(texture: Texture, data: ArrayBuffer | Uint8Array): void;
653
731
 
654
- export { type AtlasedTextureSet, type DebugMode, MTOON_ATLAS_EXTENSION_NAME, MToonAtlasExporterPlugin, type MToonAtlasExtensionSchema, MToonAtlasLoaderPlugin, MToonAtlasMaterial, type MToonAtlasOptions, type MaterialSlotAttributeConfig, type ParameterSemantic, type ParameterSemanticId, type ParameterTextureDescriptor, type TextureCompressionOptions, rememberKtx2Source };
732
+ export { type AtlasedTextureSet, DEFAULT_KTX2_TRANSCODER_PATH, type DebugMode, MTOON_ATLAS_EXTENSION_NAME, MToonAtlasExporterPlugin, type MToonAtlasExtensionSchema, MToonAtlasLoaderPlugin, type MToonAtlasLoaderPluginOptions, MToonAtlasMaterial, type MToonAtlasOptions, type MaterialSlotAttributeConfig, type ParameterSemantic, type ParameterSemanticId, type ParameterTextureDescriptor, type TextureCompressionOptions, clearKtx2LoaderCache, getKtx2TranscoderPath, rememberKtx2Source, resolveKtx2Loader, setKtx2TranscoderPath };
package/dist/index.js CHANGED
@@ -465,9 +465,8 @@ var MToonAtlasMaterial = class _MToonAtlasMaterial extends THREE.ShaderMaterial
465
465
  };
466
466
 
467
467
  // src/extensions/MToonAtlasLoaderPlugin.ts
468
- import { SRGBColorSpace, NoColorSpace, DoubleSide, FrontSide as FrontSide2, NearestFilter, DataTexture, FloatType, RGBAFormat, LinearFilter, LinearMipmapLinearFilter, RepeatWrapping, WebGLRenderer } from "three";
468
+ import { SRGBColorSpace, NoColorSpace, DoubleSide, FrontSide as FrontSide2, NearestFilter, DataTexture, FloatType, RGBAFormat, LinearFilter, LinearMipmapLinearFilter, RepeatWrapping } from "three";
469
469
  import { decode as decodePng } from "fast-png";
470
- import { KTX2Loader } from "three/examples/jsm/loaders/KTX2Loader.js";
471
470
 
472
471
  // src/extensions/ktx2-source-cache.ts
473
472
  var ktx2SourceByTextureSource = /* @__PURE__ */ new WeakMap();
@@ -488,18 +487,31 @@ function isCompressedTexture(texture) {
488
487
  return texture.isCompressedTexture === true;
489
488
  }
490
489
 
491
- // src/extensions/types.ts
492
- var MTOON_ATLAS_EXTENSION_NAME = "XRIFT_mtoon_atlas";
493
-
494
- // src/extensions/MToonAtlasLoaderPlugin.ts
495
- var ktx2LoaderInstance = null;
496
- function getKTX2Loader() {
497
- if (ktx2LoaderInstance) {
498
- return ktx2LoaderInstance;
490
+ // src/extensions/ktx2-loader.ts
491
+ import { WebGLRenderer } from "three";
492
+ import { KTX2Loader } from "three/examples/jsm/loaders/KTX2Loader.js";
493
+ var DEFAULT_KTX2_TRANSCODER_PATH = "https://cdn.jsdelivr.net/npm/three@0.181.1/examples/jsm/libs/basis/";
494
+ function normalizeTranscoderPath(path) {
495
+ return path.endsWith("/") ? path : `${path}/`;
496
+ }
497
+ var defaultTranscoderPath = DEFAULT_KTX2_TRANSCODER_PATH;
498
+ var loaderCache = /* @__PURE__ */ new Map();
499
+ function setKtx2TranscoderPath(path) {
500
+ defaultTranscoderPath = normalizeTranscoderPath(path);
501
+ }
502
+ function getKtx2TranscoderPath() {
503
+ return defaultTranscoderPath;
504
+ }
505
+ function resolveKtx2Loader(transcoderPath) {
506
+ const path = transcoderPath === void 0 ? defaultTranscoderPath : normalizeTranscoderPath(transcoderPath);
507
+ const cached = loaderCache.get(path);
508
+ if (cached) {
509
+ return cached;
499
510
  }
500
511
  if (typeof document === "undefined" || typeof WebGLRenderingContext === "undefined") {
501
512
  return null;
502
513
  }
514
+ let renderer;
503
515
  try {
504
516
  const canvas = document.createElement("canvas");
505
517
  const gl = canvas.getContext("webgl2");
@@ -507,26 +519,46 @@ function getKTX2Loader() {
507
519
  console.warn("MToonAtlasLoaderPlugin: WebGL2\u304C\u30B5\u30DD\u30FC\u30C8\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
508
520
  return null;
509
521
  }
510
- const renderer = new WebGLRenderer({ canvas, context: gl });
511
- ktx2LoaderInstance = new KTX2Loader();
512
- ktx2LoaderInstance.setTranscoderPath(
513
- "https://cdn.jsdelivr.net/npm/three@0.175.0/examples/jsm/libs/basis/"
514
- );
515
- ktx2LoaderInstance.detectSupport(renderer);
516
- renderer.dispose();
517
- return ktx2LoaderInstance;
522
+ renderer = new WebGLRenderer({ canvas, context: gl });
523
+ const loader = new KTX2Loader();
524
+ loader.setTranscoderPath(path);
525
+ loader.detectSupport(renderer);
526
+ loaderCache.set(path, loader);
527
+ return loader;
518
528
  } catch (error) {
519
529
  console.warn("MToonAtlasLoaderPlugin: KTX2Loader\u306E\u521D\u671F\u5316\u306B\u5931\u6557:", error);
520
530
  return null;
531
+ } finally {
532
+ renderer?.dispose();
521
533
  }
522
534
  }
535
+ function clearKtx2LoaderCache() {
536
+ for (const loader of loaderCache.values()) {
537
+ loader.dispose();
538
+ }
539
+ loaderCache.clear();
540
+ }
541
+
542
+ // src/extensions/types.ts
543
+ var MTOON_ATLAS_EXTENSION_NAME = "XRIFT_mtoon_atlas";
544
+
545
+ // src/extensions/MToonAtlasLoaderPlugin.ts
523
546
  var MToonAtlasLoaderPlugin = class {
524
- constructor(parser) {
547
+ constructor(parser, options = {}) {
525
548
  this.name = MTOON_ATLAS_EXTENSION_NAME;
526
549
  // 画像インデックス -> 元 KTX2 バイナリの複製(再エクスポート用、issue #39)
527
550
  // 同じアトラス画像が複数マテリアルから参照されるため、画像単位でメモ化する
528
551
  this.ktx2SourceByImageIndex = /* @__PURE__ */ new Map();
529
552
  this.parser = parser;
553
+ this.injectedKtx2Loader = options.ktx2Loader;
554
+ this.ktx2TranscoderPath = options.ktx2TranscoderPath;
555
+ }
556
+ /**
557
+ * このプラグインが使う KTX2Loader を取得する
558
+ * 注入されたインスタンス > 指定パス > アプリ全体の既定値 の順に解決する
559
+ */
560
+ getKtx2Loader() {
561
+ return this.injectedKtx2Loader ?? resolveKtx2Loader(this.ktx2TranscoderPath);
530
562
  }
531
563
  /**
532
564
  * 再エクスポート用に元の KTX2 バイナリの複製を取得する(画像単位でメモ化)
@@ -679,7 +711,7 @@ var MToonAtlasLoaderPlugin = class {
679
711
  console.warn("MToonAtlasLoaderPlugin: \u7121\u52B9\u306A\u753B\u50CF\u5B9A\u7FA9\uFF08uri/bufferView\u306A\u3057\uFF09");
680
712
  return null;
681
713
  }
682
- const ktx2Loader = getKTX2Loader();
714
+ const ktx2Loader = this.getKtx2Loader();
683
715
  if (!ktx2Loader) {
684
716
  console.warn("MToonAtlasLoaderPlugin: KTX2Loader\u304C\u5229\u7528\u3067\u304D\u307E\u305B\u3093");
685
717
  return null;
@@ -1742,10 +1774,15 @@ var MToonAtlasExporterPlugin = class {
1742
1774
  }
1743
1775
  };
1744
1776
  export {
1777
+ DEFAULT_KTX2_TRANSCODER_PATH,
1745
1778
  MTOON_ATLAS_EXTENSION_NAME,
1746
1779
  MToonAtlasExporterPlugin,
1747
1780
  MToonAtlasLoaderPlugin,
1748
1781
  MToonAtlasMaterial,
1749
- rememberKtx2Source
1782
+ clearKtx2LoaderCache,
1783
+ getKtx2TranscoderPath,
1784
+ rememberKtx2Source,
1785
+ resolveKtx2Loader,
1786
+ setKtx2TranscoderPath
1750
1787
  };
1751
1788
  //# sourceMappingURL=index.js.map