@webxr-jp/mtoon-atlas 0.5.0 → 0.5.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.
package/dist/index.cjs CHANGED
@@ -756,11 +756,14 @@ var MTOON_ATLAS_EXTENSION_NAME = "XRIFT_mtoon_atlas";
756
756
 
757
757
  // src/extensions/MToonAtlasLoaderPlugin.ts
758
758
  var SRGB_ATLAS_SLOTS = ["baseColor", "shade", "emissive", "matcap", "rim"];
759
+ function resolveAtlasTextureColorSpace(slot) {
760
+ return SRGB_ATLAS_SLOTS.includes(slot) ? import_three2.SRGBColorSpace : import_three2.NoColorSpace;
761
+ }
759
762
  function applyAtlasTextureColorSpace(texture, slot, isKtx2) {
760
763
  if (isKtx2) {
761
764
  return;
762
765
  }
763
- texture.colorSpace = SRGB_ATLAS_SLOTS.includes(slot) ? import_three2.SRGBColorSpace : import_three2.NoColorSpace;
766
+ texture.colorSpace = resolveAtlasTextureColorSpace(slot);
764
767
  }
765
768
  var MToonAtlasLoaderPlugin = class {
766
769
  constructor(parser, options = {}) {
@@ -768,6 +771,13 @@ var MToonAtlasLoaderPlugin = class {
768
771
  // 画像インデックス -> 元 KTX2 バイナリの複製(再エクスポート用、issue #39)
769
772
  // 同じアトラス画像が複数マテリアルから参照されるため、画像単位でメモ化する
770
773
  this.ktx2SourceByImageIndex = /* @__PURE__ */ new Map();
774
+ // WebGPURenderer は THREE.Texture インスタンス単位で GPU リソースを管理するため、
775
+ // マテリアルごとに clone() すると同一アトラステクスチャがマテリアル数倍に増殖する。
776
+ // ロード単位で Promise をキャッシュし、同一条件のテクスチャ/パラメータを全マテリアルで共有する
777
+ this.atlasTextureCache = /* @__PURE__ */ new Map();
778
+ this.ktx2TextureCache = /* @__PURE__ */ new Map();
779
+ this.parameterDataCache = /* @__PURE__ */ new Map();
780
+ this.parameterTextureCache = /* @__PURE__ */ new Map();
771
781
  this.parser = parser;
772
782
  this.injectedKtx2Loader = options.ktx2Loader;
773
783
  this.ktx2TranscoderPath = options.ktx2TranscoderPath;
@@ -905,11 +915,22 @@ var MToonAtlasLoaderPlugin = class {
905
915
  }
906
916
  return { data: floatData, width, height };
907
917
  }
918
+ /**
919
+ * パラメータ画像をキャッシュ付きで16bit精度デコードする(TSL / UBO 版向け)
920
+ */
921
+ _getParameterData16bit(textureIndex) {
922
+ let cached = this.parameterDataCache.get(textureIndex);
923
+ if (!cached) {
924
+ cached = this._loadParameterData16bit(textureIndex);
925
+ this.parameterDataCache.set(textureIndex, cached);
926
+ }
927
+ return cached;
928
+ }
908
929
  /**
909
930
  * パラメータテクスチャを16bit精度で読み込む(GLSL 版向け)
910
931
  */
911
932
  async _loadParameterTexture16bit(textureIndex) {
912
- const { data, width, height } = await this._loadParameterData16bit(textureIndex);
933
+ const { data, width, height } = await this._getParameterData16bit(textureIndex);
913
934
  const texture = new import_three2.DataTexture(data, width, height, import_three2.RGBAFormat, import_three2.FloatType);
914
935
  texture.flipY = false;
915
936
  texture.colorSpace = import_three2.NoColorSpace;
@@ -918,6 +939,17 @@ var MToonAtlasLoaderPlugin = class {
918
939
  texture.needsUpdate = true;
919
940
  return texture;
920
941
  }
942
+ /**
943
+ * パラメータテクスチャをキャッシュ付きで取得する(GLSL 版向け)
944
+ */
945
+ _getParameterTexture16bit(textureIndex) {
946
+ let cached = this.parameterTextureCache.get(textureIndex);
947
+ if (!cached) {
948
+ cached = this._loadParameterTexture16bit(textureIndex);
949
+ this.parameterTextureCache.set(textureIndex, cached);
950
+ }
951
+ return cached;
952
+ }
921
953
  /**
922
954
  * KTX2テクスチャを直接読み込む
923
955
  * GLTFLoaderのloadTextureがKTX2を正しく処理できない場合のフォールバック
@@ -988,51 +1020,78 @@ var MToonAtlasLoaderPlugin = class {
988
1020
  return null;
989
1021
  }
990
1022
  }
991
- async _loadMaterialAsync(materialIndex) {
992
- const materialDef = this.parser.json.materials[materialIndex];
993
- const extension = materialDef.extensions[MTOON_ATLAS_EXTENSION_NAME];
994
- const pending = [];
995
- const atlasedTextures = {};
996
- const loadTexture = async (key, textureInfo) => {
997
- if (textureInfo) {
998
- try {
999
- const textureDef = this.parser.json.textures?.[textureInfo.index];
1000
- const isKtx2 = textureDef?.extensions?.KHR_texture_basisu !== void 0;
1001
- let loadedTexture = null;
1002
- if (isKtx2) {
1003
- loadedTexture = await this._loadKtx2Texture(textureInfo.index);
1004
- } else {
1005
- loadedTexture = await this.parser.loadTexture(textureInfo.index);
1023
+ /**
1024
+ * アトラステクスチャをキャッシュ付きで取得する
1025
+ *
1026
+ * WebGPURenderer は THREE.Texture インスタンスごとに GPU テクスチャを作成・アップロードするため、
1027
+ * 同一 textureIndex に対してマテリアル数分だけ clone() すると GPU メモリとアップロードが増殖する。
1028
+ * そのためロード単位で Promise をメモ化し、全マテリアルで同一インスタンスを共有する。
1029
+ *
1030
+ * - KTX2: DFD 由来の colorSpace をそのまま尊重し、自前生成の CompressedTexture を clone() せず共有する。
1031
+ * - 非 KTX2 (PNG 等): GLTFLoader の内部キャッシュを汚さないよう初回のみ clone() し、
1032
+ * `textureIndex:colorSpace` 単位で共有する(別スロットでの sRGB / NoColorSpace 衝突を防ぐ)。
1033
+ */
1034
+ _getAtlasedTexture(key, textureIndex) {
1035
+ const textureDef = this.parser.json.textures?.[textureIndex];
1036
+ const isKtx2 = textureDef?.extensions?.KHR_texture_basisu !== void 0;
1037
+ if (isKtx2) {
1038
+ let cached2 = this.ktx2TextureCache.get(textureIndex);
1039
+ if (!cached2) {
1040
+ cached2 = this._loadKtx2Texture(textureIndex).then((texture) => {
1041
+ if (!texture) {
1042
+ console.warn(`MToonAtlasLoaderPlugin: \u30C6\u30AF\u30B9\u30C1\u30E3 ${key} (index: ${textureIndex}) \u306E\u8AAD\u307F\u8FBC\u307F\u306B\u5931\u6557\u3057\u307E\u3057\u305F`);
1006
1043
  }
1044
+ return texture;
1045
+ });
1046
+ this.ktx2TextureCache.set(textureIndex, cached2);
1047
+ }
1048
+ return cached2;
1049
+ }
1050
+ const colorSpace = resolveAtlasTextureColorSpace(key);
1051
+ const cacheKey = `${textureIndex}:${colorSpace}`;
1052
+ let cached = this.atlasTextureCache.get(cacheKey);
1053
+ if (!cached) {
1054
+ cached = (async () => {
1055
+ try {
1056
+ const loadedTexture = await this.parser.loadTexture(textureIndex);
1007
1057
  if (!loadedTexture) {
1008
- console.warn(`MToonAtlasLoaderPlugin: \u30C6\u30AF\u30B9\u30C1\u30E3 ${key} (index: ${textureInfo.index}) \u306E\u8AAD\u307F\u8FBC\u307F\u306B\u5931\u6557\u3057\u307E\u3057\u305F`);
1009
- return;
1058
+ console.warn(`MToonAtlasLoaderPlugin: \u30C6\u30AF\u30B9\u30C1\u30E3 ${key} (index: ${textureIndex}) \u306E\u8AAD\u307F\u8FBC\u307F\u306B\u5931\u6557\u3057\u307E\u3057\u305F`);
1059
+ return null;
1010
1060
  }
1011
1061
  const texture = loadedTexture.clone();
1012
1062
  if ("source" in loadedTexture && loadedTexture.source) {
1013
1063
  texture.source = loadedTexture.source;
1014
1064
  }
1015
1065
  texture.flipY = false;
1016
- applyAtlasTextureColorSpace(texture, key, isKtx2);
1017
- atlasedTextures[key] = texture;
1066
+ applyAtlasTextureColorSpace(texture, key, false);
1067
+ return texture;
1018
1068
  } catch (error) {
1019
- 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);
1069
+ console.warn(`MToonAtlasLoaderPlugin: \u30C6\u30AF\u30B9\u30C1\u30E3 ${key} (index: ${textureIndex}) \u306E\u8AAD\u307F\u8FBC\u307F\u4E2D\u306B\u30A8\u30E9\u30FC\u304C\u767A\u751F\u3057\u307E\u3057\u305F:`, error);
1070
+ return null;
1020
1071
  }
1021
- }
1022
- };
1072
+ })();
1073
+ this.atlasTextureCache.set(cacheKey, cached);
1074
+ }
1075
+ return cached;
1076
+ }
1077
+ async _loadMaterialAsync(materialIndex) {
1078
+ const materialDef = this.parser.json.materials[materialIndex];
1079
+ const extension = materialDef.extensions[MTOON_ATLAS_EXTENSION_NAME];
1080
+ const pending = [];
1081
+ const atlasedTextures = {};
1023
1082
  const prefersBuffer = this.materialType.prefersParameterBuffer === true;
1024
1083
  let parameterData = null;
1025
1084
  let parameterTexture = null;
1026
1085
  if (extension.parameterTexture && extension.parameterTexture.index >= 0) {
1027
1086
  if (prefersBuffer) {
1028
1087
  pending.push(
1029
- this._loadParameterData16bit(extension.parameterTexture.index).then(({ data }) => {
1088
+ this._getParameterData16bit(extension.parameterTexture.index).then(({ data }) => {
1030
1089
  parameterData = data;
1031
1090
  })
1032
1091
  );
1033
1092
  } else {
1034
1093
  pending.push(
1035
- this._loadParameterTexture16bit(extension.parameterTexture.index).then((tex) => {
1094
+ this._getParameterTexture16bit(extension.parameterTexture.index).then((tex) => {
1036
1095
  parameterTexture = tex;
1037
1096
  })
1038
1097
  );
@@ -1040,7 +1099,15 @@ var MToonAtlasLoaderPlugin = class {
1040
1099
  }
1041
1100
  if (extension.atlasedTextures) {
1042
1101
  for (const [key, value] of Object.entries(extension.atlasedTextures)) {
1043
- pending.push(loadTexture(key, value));
1102
+ if (value) {
1103
+ pending.push(
1104
+ this._getAtlasedTexture(key, value.index).then((texture) => {
1105
+ if (texture) {
1106
+ atlasedTextures[key] = texture;
1107
+ }
1108
+ })
1109
+ );
1110
+ }
1044
1111
  }
1045
1112
  }
1046
1113
  await Promise.all(pending);