@webxr-jp/mtoon-atlas 0.4.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
@@ -32,19 +32,26 @@ var src_exports = {};
32
32
  __export(src_exports, {
33
33
  DEFAULT_KTX2_TRANSCODER_PATH: () => DEFAULT_KTX2_TRANSCODER_PATH,
34
34
  DEFAULT_PARAMETER_LAYOUT: () => DEFAULT_PARAMETER_LAYOUT,
35
+ DEFAULT_TEXELS_PER_SLOT: () => DEFAULT_TEXELS_PER_SLOT,
36
+ MAX_PARAMETER_UBO_BYTES: () => MAX_PARAMETER_UBO_BYTES,
35
37
  MTOON_ATLAS_EXTENSION_NAME: () => MTOON_ATLAS_EXTENSION_NAME,
36
38
  MTOON_TRANSPARENT_RENDER_ORDER: () => MTOON_TRANSPARENT_RENDER_ORDER,
37
39
  MTOON_TRANSPARENT_ZWRITE_RENDER_ORDER: () => MTOON_TRANSPARENT_ZWRITE_RENDER_ORDER,
38
40
  MToonAtlasExporterPlugin: () => MToonAtlasExporterPlugin,
39
41
  MToonAtlasLoaderPlugin: () => MToonAtlasLoaderPlugin,
40
42
  MToonAtlasMaterial: () => MToonAtlasMaterial,
43
+ PARAMETER_UBO_BUCKET_SLOTS: () => PARAMETER_UBO_BUCKET_SLOTS,
41
44
  applyAtlasTextureColorSpace: () => applyAtlasTextureColorSpace,
42
45
  clearKtx2LoaderCache: () => clearKtx2LoaderCache,
46
+ createPaddedParameterBuffer: () => createPaddedParameterBuffer,
47
+ extractParameterBufferFromTexture: () => extractParameterBufferFromTexture,
43
48
  getKtx2Renderer: () => getKtx2Renderer,
44
49
  getKtx2TranscoderPath: () => getKtx2TranscoderPath,
45
50
  getMToonRenderOrder: () => getMToonRenderOrder,
51
+ getMaxParameterUBOSlots: () => getMaxParameterUBOSlots,
46
52
  rememberKtx2Source: () => rememberKtx2Source,
47
53
  resolveKtx2Loader: () => resolveKtx2Loader,
54
+ resolveParameterBucketSlots: () => resolveParameterBucketSlots,
48
55
  setKtx2Renderer: () => setKtx2Renderer,
49
56
  setKtx2TranscoderPath: () => setKtx2TranscoderPath
50
57
  });
@@ -543,6 +550,77 @@ var DEFAULT_PARAMETER_LAYOUT = [
543
550
  { id: "shadingShift", texel: 8, channels: ["r"] }
544
551
  ];
545
552
 
553
+ // src/parameter-buffer.ts
554
+ var MAX_PARAMETER_UBO_BYTES = 16384;
555
+ var DEFAULT_TEXELS_PER_SLOT = 9;
556
+ var BYTES_PER_VEC4 = 16;
557
+ var PARAMETER_UBO_BUCKET_SLOTS = [8, 16, 32, 64, 112];
558
+ function getMaxParameterUBOSlots(texelsPerSlot = DEFAULT_TEXELS_PER_SLOT) {
559
+ if (!Number.isInteger(texelsPerSlot) || texelsPerSlot <= 0) {
560
+ throw new RangeError(`texelsPerSlot \u306F\u6B63\u306E\u6574\u6570\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059\uFF08\u53D7\u3051\u53D6\u3063\u305F\u5024: ${texelsPerSlot}\uFF09`);
561
+ }
562
+ return Math.floor(MAX_PARAMETER_UBO_BYTES / (texelsPerSlot * BYTES_PER_VEC4));
563
+ }
564
+ function resolveParameterBucketSlots(slotCount, texelsPerSlot = DEFAULT_TEXELS_PER_SLOT) {
565
+ if (!Number.isInteger(slotCount) || slotCount < 0) {
566
+ throw new RangeError(`slotCount \u306F 0 \u4EE5\u4E0A\u306E\u6574\u6570\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059\uFF08\u53D7\u3051\u53D6\u3063\u305F\u5024: ${slotCount}\uFF09`);
567
+ }
568
+ const maxSlots = getMaxParameterUBOSlots(texelsPerSlot);
569
+ if (slotCount > maxSlots) {
570
+ throw new RangeError(
571
+ `\u30DE\u30C6\u30EA\u30A2\u30EB\u30B9\u30ED\u30C3\u30C8\u6570 (${slotCount}) \u304C UBO \u4E0A\u9650 (${maxSlots} \u30B9\u30ED\u30C3\u30C8 / ${MAX_PARAMETER_UBO_BYTES} bytes, texelsPerSlot=${texelsPerSlot}) \u3092\u8D85\u3048\u3066\u3044\u307E\u3059`
572
+ );
573
+ }
574
+ const effectiveSlots = Math.max(1, slotCount);
575
+ for (const bucket of PARAMETER_UBO_BUCKET_SLOTS) {
576
+ if (effectiveSlots <= bucket && bucket <= maxSlots) {
577
+ return bucket;
578
+ }
579
+ }
580
+ return maxSlots;
581
+ }
582
+ function createPaddedParameterBuffer(data, slotCount, texelsPerSlot = DEFAULT_TEXELS_PER_SLOT) {
583
+ const bucketSlots = resolveParameterBucketSlots(slotCount, texelsPerSlot);
584
+ const paddedLength = bucketSlots * texelsPerSlot * 4;
585
+ const buffer = new Float32Array(paddedLength);
586
+ const copyLength = Math.min(data.length, slotCount * texelsPerSlot * 4, paddedLength);
587
+ if (copyLength > 0) {
588
+ buffer.set(data.subarray(0, copyLength), 0);
589
+ }
590
+ return { buffer, bucketSlots };
591
+ }
592
+ function extractParameterBufferFromTexture(descriptor) {
593
+ const { texture, slotCount, texelsPerSlot, semantics, atlasedTextures } = descriptor;
594
+ const expectedLength = Math.max(0, slotCount * texelsPerSlot * 4);
595
+ const image = texture.image;
596
+ const raw = image?.data;
597
+ let data;
598
+ if (raw instanceof Float32Array) {
599
+ data = raw;
600
+ } else if (raw instanceof Uint16Array) {
601
+ data = new Float32Array(raw.length);
602
+ for (let i = 0; i < raw.length; i++) {
603
+ data[i] = raw[i] / 65535;
604
+ }
605
+ } else if (raw instanceof Uint8Array || raw instanceof Uint8ClampedArray) {
606
+ data = new Float32Array(raw.length);
607
+ for (let i = 0; i < raw.length; i++) {
608
+ data[i] = raw[i] / 255;
609
+ }
610
+ } else if (raw) {
611
+ data = Float32Array.from(raw);
612
+ } else {
613
+ data = new Float32Array(expectedLength);
614
+ }
615
+ return {
616
+ data,
617
+ slotCount,
618
+ texelsPerSlot,
619
+ semantics,
620
+ atlasedTextures
621
+ };
622
+ }
623
+
546
624
  // src/extensions/MToonAtlasLoaderPlugin.ts
547
625
  var import_three2 = require("three");
548
626
  var import_fast_png = require("fast-png");
@@ -678,11 +756,14 @@ var MTOON_ATLAS_EXTENSION_NAME = "XRIFT_mtoon_atlas";
678
756
 
679
757
  // src/extensions/MToonAtlasLoaderPlugin.ts
680
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
+ }
681
762
  function applyAtlasTextureColorSpace(texture, slot, isKtx2) {
682
763
  if (isKtx2) {
683
764
  return;
684
765
  }
685
- texture.colorSpace = SRGB_ATLAS_SLOTS.includes(slot) ? import_three2.SRGBColorSpace : import_three2.NoColorSpace;
766
+ texture.colorSpace = resolveAtlasTextureColorSpace(slot);
686
767
  }
687
768
  var MToonAtlasLoaderPlugin = class {
688
769
  constructor(parser, options = {}) {
@@ -690,6 +771,13 @@ var MToonAtlasLoaderPlugin = class {
690
771
  // 画像インデックス -> 元 KTX2 バイナリの複製(再エクスポート用、issue #39)
691
772
  // 同じアトラス画像が複数マテリアルから参照されるため、画像単位でメモ化する
692
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();
693
781
  this.parser = parser;
694
782
  this.injectedKtx2Loader = options.ktx2Loader;
695
783
  this.ktx2TranscoderPath = options.ktx2TranscoderPath;
@@ -754,11 +842,11 @@ var MToonAtlasLoaderPlugin = class {
754
842
  return null;
755
843
  }
756
844
  /**
757
- * パラメータテクスチャを16bit精度で読み込む
845
+ * パラメータ画像を16bit精度でデコードして生 Float32Array として読み込む
758
846
  * GLTFLoaderのloadTextureはImageBitmap経由で8bitに変換されるため、
759
847
  * 直接PNGデータを取得してfast-pngでデコードする
760
848
  */
761
- async _loadParameterTexture16bit(textureIndex) {
849
+ async _loadParameterData16bit(textureIndex) {
762
850
  const json = this.parser.json;
763
851
  if (textureIndex < 0 || textureIndex >= json.textures.length) {
764
852
  throw new Error(`MToonAtlasLoaderPlugin: Invalid texture index: ${textureIndex}`);
@@ -825,7 +913,25 @@ var MToonAtlasLoaderPlugin = class {
825
913
  }
826
914
  }
827
915
  }
828
- const texture = new import_three2.DataTexture(floatData, width, height, import_three2.RGBAFormat, import_three2.FloatType);
916
+ return { data: floatData, width, height };
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
+ }
929
+ /**
930
+ * パラメータテクスチャを16bit精度で読み込む(GLSL 版向け)
931
+ */
932
+ async _loadParameterTexture16bit(textureIndex) {
933
+ const { data, width, height } = await this._getParameterData16bit(textureIndex);
934
+ const texture = new import_three2.DataTexture(data, width, height, import_three2.RGBAFormat, import_three2.FloatType);
829
935
  texture.flipY = false;
830
936
  texture.colorSpace = import_three2.NoColorSpace;
831
937
  texture.minFilter = import_three2.NearestFilter;
@@ -833,6 +939,17 @@ var MToonAtlasLoaderPlugin = class {
833
939
  texture.needsUpdate = true;
834
940
  return texture;
835
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
+ }
836
953
  /**
837
954
  * KTX2テクスチャを直接読み込む
838
955
  * GLTFLoaderのloadTextureがKTX2を正しく処理できない場合のフォールバック
@@ -903,74 +1020,138 @@ var MToonAtlasLoaderPlugin = class {
903
1020
  return null;
904
1021
  }
905
1022
  }
906
- async _loadMaterialAsync(materialIndex) {
907
- const materialDef = this.parser.json.materials[materialIndex];
908
- const extension = materialDef.extensions[MTOON_ATLAS_EXTENSION_NAME];
909
- const pending = [];
910
- const atlasedTextures = {};
911
- const loadTexture = async (key, textureInfo) => {
912
- if (textureInfo) {
913
- try {
914
- const textureDef = this.parser.json.textures?.[textureInfo.index];
915
- const isKtx2 = textureDef?.extensions?.KHR_texture_basisu !== void 0;
916
- let loadedTexture = null;
917
- if (isKtx2) {
918
- loadedTexture = await this._loadKtx2Texture(textureInfo.index);
919
- } else {
920
- 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`);
921
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);
922
1057
  if (!loadedTexture) {
923
- console.warn(`MToonAtlasLoaderPlugin: \u30C6\u30AF\u30B9\u30C1\u30E3 ${key} (index: ${textureInfo.index}) \u306E\u8AAD\u307F\u8FBC\u307F\u306B\u5931\u6557\u3057\u307E\u3057\u305F`);
924
- 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;
925
1060
  }
926
1061
  const texture = loadedTexture.clone();
927
1062
  if ("source" in loadedTexture && loadedTexture.source) {
928
1063
  texture.source = loadedTexture.source;
929
1064
  }
930
1065
  texture.flipY = false;
931
- applyAtlasTextureColorSpace(texture, key, isKtx2);
932
- atlasedTextures[key] = texture;
1066
+ applyAtlasTextureColorSpace(texture, key, false);
1067
+ return texture;
933
1068
  } catch (error) {
934
- 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;
935
1071
  }
936
- }
937
- };
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 = {};
1082
+ const prefersBuffer = this.materialType.prefersParameterBuffer === true;
1083
+ let parameterData = null;
938
1084
  let parameterTexture = null;
939
1085
  if (extension.parameterTexture && extension.parameterTexture.index >= 0) {
940
- pending.push(
941
- this._loadParameterTexture16bit(extension.parameterTexture.index).then((tex) => {
942
- parameterTexture = tex;
943
- })
944
- );
1086
+ if (prefersBuffer) {
1087
+ pending.push(
1088
+ this._getParameterData16bit(extension.parameterTexture.index).then(({ data }) => {
1089
+ parameterData = data;
1090
+ })
1091
+ );
1092
+ } else {
1093
+ pending.push(
1094
+ this._getParameterTexture16bit(extension.parameterTexture.index).then((tex) => {
1095
+ parameterTexture = tex;
1096
+ })
1097
+ );
1098
+ }
945
1099
  }
946
1100
  if (extension.atlasedTextures) {
947
1101
  for (const [key, value] of Object.entries(extension.atlasedTextures)) {
948
- 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
+ }
949
1111
  }
950
1112
  }
951
1113
  await Promise.all(pending);
952
- if (!parameterTexture) {
953
- const dummyData = new Float32Array(4).fill(0);
954
- parameterTexture = new import_three2.DataTexture(dummyData, 1, 1, import_three2.RGBAFormat, import_three2.FloatType);
955
- parameterTexture.flipY = false;
956
- parameterTexture.colorSpace = import_three2.NoColorSpace;
957
- parameterTexture.minFilter = import_three2.NearestFilter;
958
- parameterTexture.magFilter = import_three2.NearestFilter;
959
- parameterTexture.needsUpdate = true;
960
- }
961
1114
  const slotAttributeName = extension.slotAttributeName.toLowerCase();
962
- const material = new this.materialType({
963
- parameterTexture: {
964
- texture: parameterTexture,
965
- slotCount: extension.parameterTexture.slotCount,
966
- texelsPerSlot: extension.parameterTexture.texelsPerSlot,
967
- atlasedTextures
968
- },
969
- slotAttribute: {
970
- name: slotAttributeName
971
- },
972
- name: materialDef.name
973
- });
1115
+ const slotCount = extension.parameterTexture?.slotCount ?? 0;
1116
+ const texelsPerSlot = extension.parameterTexture?.texelsPerSlot ?? 9;
1117
+ let material;
1118
+ if (prefersBuffer) {
1119
+ const data = parameterData ?? new Float32Array(Math.max(1, slotCount) * texelsPerSlot * 4);
1120
+ material = new this.materialType({
1121
+ parameterBuffer: {
1122
+ data,
1123
+ slotCount,
1124
+ texelsPerSlot,
1125
+ atlasedTextures
1126
+ },
1127
+ slotAttribute: {
1128
+ name: slotAttributeName
1129
+ },
1130
+ name: materialDef.name
1131
+ });
1132
+ } else {
1133
+ if (!parameterTexture) {
1134
+ const dummyData = new Float32Array(4).fill(0);
1135
+ parameterTexture = new import_three2.DataTexture(dummyData, 1, 1, import_three2.RGBAFormat, import_three2.FloatType);
1136
+ parameterTexture.flipY = false;
1137
+ parameterTexture.colorSpace = import_three2.NoColorSpace;
1138
+ parameterTexture.minFilter = import_three2.NearestFilter;
1139
+ parameterTexture.magFilter = import_three2.NearestFilter;
1140
+ parameterTexture.needsUpdate = true;
1141
+ }
1142
+ material = new this.materialType({
1143
+ parameterTexture: {
1144
+ texture: parameterTexture,
1145
+ slotCount,
1146
+ texelsPerSlot,
1147
+ atlasedTextures
1148
+ },
1149
+ slotAttribute: {
1150
+ name: slotAttributeName
1151
+ },
1152
+ name: materialDef.name
1153
+ });
1154
+ }
974
1155
  if (materialDef.doubleSided) {
975
1156
  material.side = import_three2.DoubleSide;
976
1157
  } else {
@@ -1016,6 +1197,9 @@ var MToonAtlasExporterPlugin = class {
1016
1197
  // オブジェクト参照ではキャッシュできない。
1017
1198
  // キーの選び方は processTextureWithCache のコメントを参照
1018
1199
  this.textureCache = /* @__PURE__ */ new Map();
1200
+ // パラメータバッファ(Float32Array参照)からインデックスへのキャッシュ(重複登録防止)
1201
+ // 本体とアウトライン(clone)は同一の parameterBuffer.data を参照するため、1回だけ書き出す
1202
+ this.parameterBufferCache = /* @__PURE__ */ new Map();
1019
1203
  this.writer = writer;
1020
1204
  this.skipStandardMaterialProcessing();
1021
1205
  }
@@ -1053,6 +1237,7 @@ var MToonAtlasExporterPlugin = class {
1053
1237
  this.textureIndices.clear();
1054
1238
  this.pendingTextureIndices.clear();
1055
1239
  this.textureCache.clear();
1240
+ this.parameterBufferCache.clear();
1056
1241
  const roots = Array.isArray(input) ? input : [input];
1057
1242
  for (const root of roots) {
1058
1243
  root.traverse((obj) => {
@@ -1150,12 +1335,16 @@ var MToonAtlasExporterPlugin = class {
1150
1335
  parameterTextureIndex: null,
1151
1336
  atlasedTextureIndices: {}
1152
1337
  };
1153
- if (material.parameterTexture?.texture) {
1338
+ if (material.parameterBuffer) {
1339
+ pendingIndices.parameterTextureIndex = this.processParameterBufferWithCache(
1340
+ material.parameterBuffer
1341
+ );
1342
+ } else if (material.parameterTexture?.texture) {
1154
1343
  pendingIndices.parameterTextureIndex = this.processParameterTextureWithCache(
1155
1344
  material.parameterTexture.texture
1156
1345
  );
1157
1346
  }
1158
- const atlasedTextures = material.parameterTexture?.atlasedTextures;
1347
+ const atlasedTextures = material.parameterBuffer?.atlasedTextures ?? material.parameterTexture?.atlasedTextures;
1159
1348
  if (atlasedTextures) {
1160
1349
  for (const [key, texture] of Object.entries(atlasedTextures)) {
1161
1350
  if (texture) {
@@ -1186,6 +1375,24 @@ var MToonAtlasExporterPlugin = class {
1186
1375
  pendingIndices.resolvePromise = resolveAllPromise;
1187
1376
  this.writer.pending.push(resolveAllPromise);
1188
1377
  }
1378
+ /**
1379
+ * パラメータバッファをキャッシュ付きで処理
1380
+ * 同じ Float32Array 参照は 1 回だけ処理される
1381
+ */
1382
+ processParameterBufferWithCache(descriptor) {
1383
+ const cached = this.parameterBufferCache.get(descriptor.data);
1384
+ if (cached) {
1385
+ return cached;
1386
+ }
1387
+ const promise = this.processParameterData(
1388
+ descriptor.data,
1389
+ descriptor.texelsPerSlot,
1390
+ descriptor.slotCount,
1391
+ "parameterTexture"
1392
+ );
1393
+ this.parameterBufferCache.set(descriptor.data, promise);
1394
+ return promise;
1395
+ }
1189
1396
  /**
1190
1397
  * パラメータテクスチャをキャッシュ付きで処理
1191
1398
  * 同じUUIDのテクスチャは1回だけ処理される
@@ -1229,6 +1436,18 @@ var MToonAtlasExporterPlugin = class {
1229
1436
  * 生のRGBAデータから直接PNGを生成してBINチャンクに格納
1230
1437
  */
1231
1438
  processParameterTexture(texture) {
1439
+ const image = texture.image;
1440
+ return this.processParameterData(
1441
+ image?.data,
1442
+ image?.width ?? 0,
1443
+ image?.height ?? 0,
1444
+ texture.name || "parameterTexture"
1445
+ );
1446
+ }
1447
+ /**
1448
+ * パラメータ配列(Float32Array / Uint8Array 等)を 16bit PNG として BIN チャンクへ書き込む
1449
+ */
1450
+ processParameterData(srcData, width, height, name) {
1232
1451
  const json = this.writer.json;
1233
1452
  json.textures = json.textures || [];
1234
1453
  json.images = json.images || [];
@@ -1251,7 +1470,7 @@ var MToonAtlasExporterPlugin = class {
1251
1470
  }
1252
1471
  const imageIndex = json.images.length;
1253
1472
  const imageDef = {
1254
- name: texture.name || "parameterTexture",
1473
+ name,
1255
1474
  mimeType: "image/png"
1256
1475
  };
1257
1476
  json.images.push(imageDef);
@@ -1259,21 +1478,19 @@ var MToonAtlasExporterPlugin = class {
1259
1478
  json.textures.push({
1260
1479
  sampler: nearestSamplerIndex,
1261
1480
  source: imageIndex,
1262
- name: texture.name || "parameterTexture"
1481
+ name
1263
1482
  });
1264
- const image = texture.image;
1265
- if (image?.data && image.width && image.height) {
1266
- const srcData = image.data;
1483
+ if (srcData && width > 0 && height > 0) {
1267
1484
  const isFloatData = srcData instanceof Float32Array || srcData.constructor?.name === "Float32Array";
1268
- const pixelCount = image.width * image.height * 4;
1485
+ const pixelCount = width * height * 4;
1269
1486
  const uint16Data = new Uint16Array(pixelCount);
1270
1487
  for (let i = 0; i < pixelCount; i++) {
1271
- const value = srcData[i];
1488
+ const value = srcData[i] ?? 0;
1272
1489
  uint16Data[i] = isFloatData ? Math.round(Math.min(1, Math.max(0, value)) * 65535) : value * 257;
1273
1490
  }
1274
1491
  const pngData = (0, import_fast_png2.encode)({
1275
- width: image.width,
1276
- height: image.height,
1492
+ width,
1493
+ height,
1277
1494
  depth: 16,
1278
1495
  channels: 4,
1279
1496
  data: uint16Data
@@ -1827,12 +2044,13 @@ var MToonAtlasExporterPlugin = class {
1827
2044
  */
1828
2045
  createMaterialDef(material) {
1829
2046
  const indices = this.textureIndices.get(material);
2047
+ const parameterMeta = material.parameterBuffer ?? material.parameterTexture;
1830
2048
  const extension = {
1831
2049
  version: "1.0",
1832
2050
  parameterTexture: {
1833
2051
  index: indices?.parameterTextureIndex ?? -1,
1834
- texelsPerSlot: material.parameterTexture?.texelsPerSlot ?? 9,
1835
- slotCount: material.parameterTexture?.slotCount ?? 0
2052
+ texelsPerSlot: parameterMeta?.texelsPerSlot ?? 9,
2053
+ slotCount: parameterMeta?.slotCount ?? 0
1836
2054
  },
1837
2055
  slotAttributeName: "_MTOON_MATERIAL_SLOT",
1838
2056
  atlasedTextures: {}
@@ -1947,19 +2165,26 @@ var MToonAtlasExporterPlugin = class {
1947
2165
  0 && (module.exports = {
1948
2166
  DEFAULT_KTX2_TRANSCODER_PATH,
1949
2167
  DEFAULT_PARAMETER_LAYOUT,
2168
+ DEFAULT_TEXELS_PER_SLOT,
2169
+ MAX_PARAMETER_UBO_BYTES,
1950
2170
  MTOON_ATLAS_EXTENSION_NAME,
1951
2171
  MTOON_TRANSPARENT_RENDER_ORDER,
1952
2172
  MTOON_TRANSPARENT_ZWRITE_RENDER_ORDER,
1953
2173
  MToonAtlasExporterPlugin,
1954
2174
  MToonAtlasLoaderPlugin,
1955
2175
  MToonAtlasMaterial,
2176
+ PARAMETER_UBO_BUCKET_SLOTS,
1956
2177
  applyAtlasTextureColorSpace,
1957
2178
  clearKtx2LoaderCache,
2179
+ createPaddedParameterBuffer,
2180
+ extractParameterBufferFromTexture,
1958
2181
  getKtx2Renderer,
1959
2182
  getKtx2TranscoderPath,
1960
2183
  getMToonRenderOrder,
2184
+ getMaxParameterUBOSlots,
1961
2185
  rememberKtx2Source,
1962
2186
  resolveKtx2Loader,
2187
+ resolveParameterBucketSlots,
1963
2188
  setKtx2Renderer,
1964
2189
  setKtx2TranscoderPath
1965
2190
  });