@webxr-jp/mtoon-atlas 0.3.2 → 0.5.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.js CHANGED
@@ -113,6 +113,9 @@ var MToonAtlasMaterial = class _MToonAtlasMaterial extends THREE.ShaderMaterial
113
113
  }
114
114
  ]);
115
115
  if (parameters) {
116
+ if (parameters.name !== void 0) {
117
+ this.name = parameters.name;
118
+ }
116
119
  if (parameters.color !== void 0) {
117
120
  this.uniforms.litFactor.value.set(parameters.color);
118
121
  }
@@ -464,6 +467,101 @@ var MToonAtlasMaterial = class _MToonAtlasMaterial extends THREE.ShaderMaterial
464
467
  }
465
468
  };
466
469
 
470
+ // src/types.ts
471
+ var DEFAULT_PARAMETER_LAYOUT = [
472
+ { id: "baseColor", texel: 0, channels: ["r", "g", "b"] },
473
+ { id: "opacity", texel: 0, channels: ["a"] },
474
+ { id: "shadeColor", texel: 1, channels: ["r", "g", "b"] },
475
+ { id: "shadingShiftTextureScale", texel: 1, channels: ["a"] },
476
+ { id: "emissiveColor", texel: 2, channels: ["r", "g", "b"] },
477
+ { id: "emissiveIntensity", texel: 2, channels: ["a"] },
478
+ { id: "matcapColor", texel: 3, channels: ["r", "g", "b"] },
479
+ { id: "outlineWidth", texel: 3, channels: ["a"] },
480
+ { id: "outlineColor", texel: 4, channels: ["r", "g", "b"] },
481
+ { id: "outlineLightingMix", texel: 4, channels: ["a"] },
482
+ { id: "parametricRimColor", texel: 5, channels: ["r", "g", "b"] },
483
+ { id: "parametricRimLift", texel: 5, channels: ["a"] },
484
+ { id: "parametricRimFresnelPower", texel: 6, channels: ["r"] },
485
+ { id: "shadingToony", texel: 6, channels: ["g"] },
486
+ { id: "rimLightingMix", texel: 6, channels: ["b"] },
487
+ { id: "uvAnimationRotation", texel: 6, channels: ["a"] },
488
+ { id: "normalScale", texel: 7, channels: ["r", "g"] },
489
+ { id: "uvAnimationScrollX", texel: 7, channels: ["b"] },
490
+ { id: "uvAnimationScrollY", texel: 7, channels: ["a"] },
491
+ { id: "shadingShift", texel: 8, channels: ["r"] }
492
+ ];
493
+
494
+ // src/parameter-buffer.ts
495
+ var MAX_PARAMETER_UBO_BYTES = 16384;
496
+ var DEFAULT_TEXELS_PER_SLOT = 9;
497
+ var BYTES_PER_VEC4 = 16;
498
+ var PARAMETER_UBO_BUCKET_SLOTS = [8, 16, 32, 64, 112];
499
+ function getMaxParameterUBOSlots(texelsPerSlot = DEFAULT_TEXELS_PER_SLOT) {
500
+ if (!Number.isInteger(texelsPerSlot) || texelsPerSlot <= 0) {
501
+ 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`);
502
+ }
503
+ return Math.floor(MAX_PARAMETER_UBO_BYTES / (texelsPerSlot * BYTES_PER_VEC4));
504
+ }
505
+ function resolveParameterBucketSlots(slotCount, texelsPerSlot = DEFAULT_TEXELS_PER_SLOT) {
506
+ if (!Number.isInteger(slotCount) || slotCount < 0) {
507
+ 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`);
508
+ }
509
+ const maxSlots = getMaxParameterUBOSlots(texelsPerSlot);
510
+ if (slotCount > maxSlots) {
511
+ throw new RangeError(
512
+ `\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`
513
+ );
514
+ }
515
+ const effectiveSlots = Math.max(1, slotCount);
516
+ for (const bucket of PARAMETER_UBO_BUCKET_SLOTS) {
517
+ if (effectiveSlots <= bucket && bucket <= maxSlots) {
518
+ return bucket;
519
+ }
520
+ }
521
+ return maxSlots;
522
+ }
523
+ function createPaddedParameterBuffer(data, slotCount, texelsPerSlot = DEFAULT_TEXELS_PER_SLOT) {
524
+ const bucketSlots = resolveParameterBucketSlots(slotCount, texelsPerSlot);
525
+ const paddedLength = bucketSlots * texelsPerSlot * 4;
526
+ const buffer = new Float32Array(paddedLength);
527
+ const copyLength = Math.min(data.length, slotCount * texelsPerSlot * 4, paddedLength);
528
+ if (copyLength > 0) {
529
+ buffer.set(data.subarray(0, copyLength), 0);
530
+ }
531
+ return { buffer, bucketSlots };
532
+ }
533
+ function extractParameterBufferFromTexture(descriptor) {
534
+ const { texture, slotCount, texelsPerSlot, semantics, atlasedTextures } = descriptor;
535
+ const expectedLength = Math.max(0, slotCount * texelsPerSlot * 4);
536
+ const image = texture.image;
537
+ const raw = image?.data;
538
+ let data;
539
+ if (raw instanceof Float32Array) {
540
+ data = raw;
541
+ } else if (raw instanceof Uint16Array) {
542
+ data = new Float32Array(raw.length);
543
+ for (let i = 0; i < raw.length; i++) {
544
+ data[i] = raw[i] / 65535;
545
+ }
546
+ } else if (raw instanceof Uint8Array || raw instanceof Uint8ClampedArray) {
547
+ data = new Float32Array(raw.length);
548
+ for (let i = 0; i < raw.length; i++) {
549
+ data[i] = raw[i] / 255;
550
+ }
551
+ } else if (raw) {
552
+ data = Float32Array.from(raw);
553
+ } else {
554
+ data = new Float32Array(expectedLength);
555
+ }
556
+ return {
557
+ data,
558
+ slotCount,
559
+ texelsPerSlot,
560
+ semantics,
561
+ atlasedTextures
562
+ };
563
+ }
564
+
467
565
  // src/extensions/MToonAtlasLoaderPlugin.ts
468
566
  import { Mesh, SRGBColorSpace, NoColorSpace, DoubleSide, FrontSide as FrontSide2, NearestFilter, DataTexture, FloatType, RGBAFormat, LinearFilter, LinearMipmapLinearFilter, RepeatWrapping } from "three";
469
567
  import { decode as decodePng } from "fast-png";
@@ -504,14 +602,56 @@ function normalizeTranscoderPath(path) {
504
602
  }
505
603
  var defaultTranscoderPath = DEFAULT_KTX2_TRANSCODER_PATH;
506
604
  var loaderCache = /* @__PURE__ */ new Map();
605
+ var rendererLoaderCache = /* @__PURE__ */ new WeakMap();
606
+ var rendererLoaders = /* @__PURE__ */ new Set();
607
+ var defaultRenderer = null;
507
608
  function setKtx2TranscoderPath(path) {
508
609
  defaultTranscoderPath = normalizeTranscoderPath(path);
509
610
  }
510
611
  function getKtx2TranscoderPath() {
511
612
  return defaultTranscoderPath;
512
613
  }
513
- function resolveKtx2Loader(transcoderPath) {
614
+ function setKtx2Renderer(renderer) {
615
+ defaultRenderer = renderer;
616
+ }
617
+ function getKtx2Renderer() {
618
+ return defaultRenderer;
619
+ }
620
+ function resolveLoaderForRenderer(path, renderer) {
621
+ let byPath = rendererLoaderCache.get(renderer);
622
+ const cached = byPath?.get(path);
623
+ if (cached) {
624
+ return cached;
625
+ }
626
+ const loader = new KTX2Loader();
627
+ loader.setTranscoderPath(path);
628
+ try {
629
+ loader.detectSupport(renderer);
630
+ } catch (error) {
631
+ console.warn(
632
+ "MToonAtlasLoaderPlugin: \u6307\u5B9A\u3055\u308C\u305F\u30EC\u30F3\u30C0\u30E9\u30FC\u3067 KTX2 \u306E\u5BFE\u5FDC\u5F62\u5F0F\u3092\u5224\u5B9A\u3067\u304D\u306A\u3044\u305F\u3081\u3001WebGL \u3067\u5224\u5B9A\u3057\u307E\u3059\uFF08WebGPURenderer \u306F await renderer.init() \u6E08\u307F\u3067\u3042\u308B\u3053\u3068\uFF09:",
633
+ error
634
+ );
635
+ loader.dispose();
636
+ return null;
637
+ }
638
+ if (!byPath) {
639
+ byPath = /* @__PURE__ */ new Map();
640
+ rendererLoaderCache.set(renderer, byPath);
641
+ }
642
+ byPath.set(path, loader);
643
+ rendererLoaders.add(loader);
644
+ return loader;
645
+ }
646
+ function resolveKtx2Loader(transcoderPath, renderer) {
514
647
  const path = transcoderPath === void 0 ? defaultTranscoderPath : normalizeTranscoderPath(transcoderPath);
648
+ const supportRenderer = renderer ?? defaultRenderer;
649
+ if (supportRenderer) {
650
+ const loader = resolveLoaderForRenderer(path, supportRenderer);
651
+ if (loader) {
652
+ return loader;
653
+ }
654
+ }
515
655
  const cached = loaderCache.get(path);
516
656
  if (cached) {
517
657
  return cached;
@@ -519,7 +659,7 @@ function resolveKtx2Loader(transcoderPath) {
519
659
  if (typeof document === "undefined" || typeof WebGLRenderingContext === "undefined") {
520
660
  return null;
521
661
  }
522
- let renderer;
662
+ let tempRenderer;
523
663
  try {
524
664
  const canvas = document.createElement("canvas");
525
665
  const gl = canvas.getContext("webgl2");
@@ -527,17 +667,17 @@ function resolveKtx2Loader(transcoderPath) {
527
667
  console.warn("MToonAtlasLoaderPlugin: WebGL2\u304C\u30B5\u30DD\u30FC\u30C8\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
528
668
  return null;
529
669
  }
530
- renderer = new WebGLRenderer({ canvas, context: gl });
670
+ tempRenderer = new WebGLRenderer({ canvas, context: gl });
531
671
  const loader = new KTX2Loader();
532
672
  loader.setTranscoderPath(path);
533
- loader.detectSupport(renderer);
673
+ loader.detectSupport(tempRenderer);
534
674
  loaderCache.set(path, loader);
535
675
  return loader;
536
676
  } catch (error) {
537
677
  console.warn("MToonAtlasLoaderPlugin: KTX2Loader\u306E\u521D\u671F\u5316\u306B\u5931\u6557:", error);
538
678
  return null;
539
679
  } finally {
540
- renderer?.dispose();
680
+ tempRenderer?.dispose();
541
681
  }
542
682
  }
543
683
  function clearKtx2LoaderCache() {
@@ -545,6 +685,11 @@ function clearKtx2LoaderCache() {
545
685
  loader.dispose();
546
686
  }
547
687
  loaderCache.clear();
688
+ for (const loader of rendererLoaders) {
689
+ loader.dispose();
690
+ }
691
+ rendererLoaders.clear();
692
+ rendererLoaderCache = /* @__PURE__ */ new WeakMap();
548
693
  }
549
694
 
550
695
  // src/extensions/types.ts
@@ -567,13 +712,15 @@ var MToonAtlasLoaderPlugin = class {
567
712
  this.parser = parser;
568
713
  this.injectedKtx2Loader = options.ktx2Loader;
569
714
  this.ktx2TranscoderPath = options.ktx2TranscoderPath;
715
+ this.materialType = options.materialType ?? MToonAtlasMaterial;
716
+ this.ktx2Renderer = options.ktx2Renderer;
570
717
  }
571
718
  /**
572
719
  * このプラグインが使う KTX2Loader を取得する
573
720
  * 注入されたインスタンス > 指定パス > アプリ全体の既定値 の順に解決する
574
721
  */
575
722
  getKtx2Loader() {
576
- return this.injectedKtx2Loader ?? resolveKtx2Loader(this.ktx2TranscoderPath);
723
+ return this.injectedKtx2Loader ?? resolveKtx2Loader(this.ktx2TranscoderPath, this.ktx2Renderer);
577
724
  }
578
725
  /**
579
726
  * 再エクスポート用に元の KTX2 バイナリの複製を取得する(画像単位でメモ化)
@@ -619,18 +766,18 @@ var MToonAtlasLoaderPlugin = class {
619
766
  gltf.scene.traverse((object) => {
620
767
  if (!(object instanceof Mesh)) return;
621
768
  const material = Array.isArray(object.material) ? object.material[0] : object.material;
622
- if (!(material instanceof MToonAtlasMaterial)) return;
769
+ if (!material || !("isMToonAtlasMaterial" in material)) return;
623
770
  if (!material.transparent) return;
624
771
  object.renderOrder = getMToonRenderOrder(material);
625
772
  });
626
773
  return null;
627
774
  }
628
775
  /**
629
- * パラメータテクスチャを16bit精度で読み込む
776
+ * パラメータ画像を16bit精度でデコードして生 Float32Array として読み込む
630
777
  * GLTFLoaderのloadTextureはImageBitmap経由で8bitに変換されるため、
631
778
  * 直接PNGデータを取得してfast-pngでデコードする
632
779
  */
633
- async _loadParameterTexture16bit(textureIndex) {
780
+ async _loadParameterData16bit(textureIndex) {
634
781
  const json = this.parser.json;
635
782
  if (textureIndex < 0 || textureIndex >= json.textures.length) {
636
783
  throw new Error(`MToonAtlasLoaderPlugin: Invalid texture index: ${textureIndex}`);
@@ -697,7 +844,14 @@ var MToonAtlasLoaderPlugin = class {
697
844
  }
698
845
  }
699
846
  }
700
- const texture = new DataTexture(floatData, width, height, RGBAFormat, FloatType);
847
+ return { data: floatData, width, height };
848
+ }
849
+ /**
850
+ * パラメータテクスチャを16bit精度で読み込む(GLSL 版向け)
851
+ */
852
+ async _loadParameterTexture16bit(textureIndex) {
853
+ const { data, width, height } = await this._loadParameterData16bit(textureIndex);
854
+ const texture = new DataTexture(data, width, height, RGBAFormat, FloatType);
701
855
  texture.flipY = false;
702
856
  texture.colorSpace = NoColorSpace;
703
857
  texture.minFilter = NearestFilter;
@@ -807,13 +961,23 @@ var MToonAtlasLoaderPlugin = class {
807
961
  }
808
962
  }
809
963
  };
964
+ const prefersBuffer = this.materialType.prefersParameterBuffer === true;
965
+ let parameterData = null;
810
966
  let parameterTexture = null;
811
967
  if (extension.parameterTexture && extension.parameterTexture.index >= 0) {
812
- pending.push(
813
- this._loadParameterTexture16bit(extension.parameterTexture.index).then((tex) => {
814
- parameterTexture = tex;
815
- })
816
- );
968
+ if (prefersBuffer) {
969
+ pending.push(
970
+ this._loadParameterData16bit(extension.parameterTexture.index).then(({ data }) => {
971
+ parameterData = data;
972
+ })
973
+ );
974
+ } else {
975
+ pending.push(
976
+ this._loadParameterTexture16bit(extension.parameterTexture.index).then((tex) => {
977
+ parameterTexture = tex;
978
+ })
979
+ );
980
+ }
817
981
  }
818
982
  if (extension.atlasedTextures) {
819
983
  for (const [key, value] of Object.entries(extension.atlasedTextures)) {
@@ -821,28 +985,47 @@ var MToonAtlasLoaderPlugin = class {
821
985
  }
822
986
  }
823
987
  await Promise.all(pending);
824
- if (!parameterTexture) {
825
- const dummyData = new Float32Array(4).fill(0);
826
- parameterTexture = new DataTexture(dummyData, 1, 1, RGBAFormat, FloatType);
827
- parameterTexture.flipY = false;
828
- parameterTexture.colorSpace = NoColorSpace;
829
- parameterTexture.minFilter = NearestFilter;
830
- parameterTexture.magFilter = NearestFilter;
831
- parameterTexture.needsUpdate = true;
832
- }
833
988
  const slotAttributeName = extension.slotAttributeName.toLowerCase();
834
- const material = new MToonAtlasMaterial({
835
- parameterTexture: {
836
- texture: parameterTexture,
837
- slotCount: extension.parameterTexture.slotCount,
838
- texelsPerSlot: extension.parameterTexture.texelsPerSlot,
839
- atlasedTextures
840
- },
841
- slotAttribute: {
842
- name: slotAttributeName
843
- },
844
- name: materialDef.name
845
- });
989
+ const slotCount = extension.parameterTexture?.slotCount ?? 0;
990
+ const texelsPerSlot = extension.parameterTexture?.texelsPerSlot ?? 9;
991
+ let material;
992
+ if (prefersBuffer) {
993
+ const data = parameterData ?? new Float32Array(Math.max(1, slotCount) * texelsPerSlot * 4);
994
+ material = new this.materialType({
995
+ parameterBuffer: {
996
+ data,
997
+ slotCount,
998
+ texelsPerSlot,
999
+ atlasedTextures
1000
+ },
1001
+ slotAttribute: {
1002
+ name: slotAttributeName
1003
+ },
1004
+ name: materialDef.name
1005
+ });
1006
+ } else {
1007
+ if (!parameterTexture) {
1008
+ const dummyData = new Float32Array(4).fill(0);
1009
+ parameterTexture = new DataTexture(dummyData, 1, 1, RGBAFormat, FloatType);
1010
+ parameterTexture.flipY = false;
1011
+ parameterTexture.colorSpace = NoColorSpace;
1012
+ parameterTexture.minFilter = NearestFilter;
1013
+ parameterTexture.magFilter = NearestFilter;
1014
+ parameterTexture.needsUpdate = true;
1015
+ }
1016
+ material = new this.materialType({
1017
+ parameterTexture: {
1018
+ texture: parameterTexture,
1019
+ slotCount,
1020
+ texelsPerSlot,
1021
+ atlasedTextures
1022
+ },
1023
+ slotAttribute: {
1024
+ name: slotAttributeName
1025
+ },
1026
+ name: materialDef.name
1027
+ });
1028
+ }
846
1029
  if (materialDef.doubleSided) {
847
1030
  material.side = DoubleSide;
848
1031
  } else {
@@ -888,7 +1071,27 @@ var MToonAtlasExporterPlugin = class {
888
1071
  // オブジェクト参照ではキャッシュできない。
889
1072
  // キーの選び方は processTextureWithCache のコメントを参照
890
1073
  this.textureCache = /* @__PURE__ */ new Map();
1074
+ // パラメータバッファ(Float32Array参照)からインデックスへのキャッシュ(重複登録防止)
1075
+ // 本体とアウトライン(clone)は同一の parameterBuffer.data を参照するため、1回だけ書き出す
1076
+ this.parameterBufferCache = /* @__PURE__ */ new Map();
891
1077
  this.writer = writer;
1078
+ this.skipStandardMaterialProcessing();
1079
+ }
1080
+ /**
1081
+ * アトラスマテリアルを GLTFExporter 標準のマテリアル変換から外す
1082
+ *
1083
+ * 標準の変換は MeshStandardMaterial 前提で `material.color` などを読む。
1084
+ * GLSL 版は ShaderMaterial として警告付きで null になるだけだが、
1085
+ * TSL 版(NodeMaterial)は color を持たないため例外で書き出しが止まる。
1086
+ * どちらも afterParse で自前のマテリアル定義に差し替えるので、ここで null を返す
1087
+ */
1088
+ skipStandardMaterialProcessing() {
1089
+ const original = this.writer.processMaterialAsync?.bind(this.writer);
1090
+ if (!original) return;
1091
+ this.writer.processMaterialAsync = async (material) => {
1092
+ if (material && "isMToonAtlasMaterial" in material) return null;
1093
+ return original(material);
1094
+ };
892
1095
  }
893
1096
  /**
894
1097
  * テクスチャ圧縮オプションを設定
@@ -908,6 +1111,7 @@ var MToonAtlasExporterPlugin = class {
908
1111
  this.textureIndices.clear();
909
1112
  this.pendingTextureIndices.clear();
910
1113
  this.textureCache.clear();
1114
+ this.parameterBufferCache.clear();
911
1115
  const roots = Array.isArray(input) ? input : [input];
912
1116
  for (const root of roots) {
913
1117
  root.traverse((obj) => {
@@ -1005,12 +1209,16 @@ var MToonAtlasExporterPlugin = class {
1005
1209
  parameterTextureIndex: null,
1006
1210
  atlasedTextureIndices: {}
1007
1211
  };
1008
- if (material.parameterTexture?.texture) {
1212
+ if (material.parameterBuffer) {
1213
+ pendingIndices.parameterTextureIndex = this.processParameterBufferWithCache(
1214
+ material.parameterBuffer
1215
+ );
1216
+ } else if (material.parameterTexture?.texture) {
1009
1217
  pendingIndices.parameterTextureIndex = this.processParameterTextureWithCache(
1010
1218
  material.parameterTexture.texture
1011
1219
  );
1012
1220
  }
1013
- const atlasedTextures = material.parameterTexture?.atlasedTextures;
1221
+ const atlasedTextures = material.parameterBuffer?.atlasedTextures ?? material.parameterTexture?.atlasedTextures;
1014
1222
  if (atlasedTextures) {
1015
1223
  for (const [key, texture] of Object.entries(atlasedTextures)) {
1016
1224
  if (texture) {
@@ -1041,6 +1249,24 @@ var MToonAtlasExporterPlugin = class {
1041
1249
  pendingIndices.resolvePromise = resolveAllPromise;
1042
1250
  this.writer.pending.push(resolveAllPromise);
1043
1251
  }
1252
+ /**
1253
+ * パラメータバッファをキャッシュ付きで処理
1254
+ * 同じ Float32Array 参照は 1 回だけ処理される
1255
+ */
1256
+ processParameterBufferWithCache(descriptor) {
1257
+ const cached = this.parameterBufferCache.get(descriptor.data);
1258
+ if (cached) {
1259
+ return cached;
1260
+ }
1261
+ const promise = this.processParameterData(
1262
+ descriptor.data,
1263
+ descriptor.texelsPerSlot,
1264
+ descriptor.slotCount,
1265
+ "parameterTexture"
1266
+ );
1267
+ this.parameterBufferCache.set(descriptor.data, promise);
1268
+ return promise;
1269
+ }
1044
1270
  /**
1045
1271
  * パラメータテクスチャをキャッシュ付きで処理
1046
1272
  * 同じUUIDのテクスチャは1回だけ処理される
@@ -1084,6 +1310,18 @@ var MToonAtlasExporterPlugin = class {
1084
1310
  * 生のRGBAデータから直接PNGを生成してBINチャンクに格納
1085
1311
  */
1086
1312
  processParameterTexture(texture) {
1313
+ const image = texture.image;
1314
+ return this.processParameterData(
1315
+ image?.data,
1316
+ image?.width ?? 0,
1317
+ image?.height ?? 0,
1318
+ texture.name || "parameterTexture"
1319
+ );
1320
+ }
1321
+ /**
1322
+ * パラメータ配列(Float32Array / Uint8Array 等)を 16bit PNG として BIN チャンクへ書き込む
1323
+ */
1324
+ processParameterData(srcData, width, height, name) {
1087
1325
  const json = this.writer.json;
1088
1326
  json.textures = json.textures || [];
1089
1327
  json.images = json.images || [];
@@ -1106,7 +1344,7 @@ var MToonAtlasExporterPlugin = class {
1106
1344
  }
1107
1345
  const imageIndex = json.images.length;
1108
1346
  const imageDef = {
1109
- name: texture.name || "parameterTexture",
1347
+ name,
1110
1348
  mimeType: "image/png"
1111
1349
  };
1112
1350
  json.images.push(imageDef);
@@ -1114,21 +1352,19 @@ var MToonAtlasExporterPlugin = class {
1114
1352
  json.textures.push({
1115
1353
  sampler: nearestSamplerIndex,
1116
1354
  source: imageIndex,
1117
- name: texture.name || "parameterTexture"
1355
+ name
1118
1356
  });
1119
- const image = texture.image;
1120
- if (image?.data && image.width && image.height) {
1121
- const srcData = image.data;
1357
+ if (srcData && width > 0 && height > 0) {
1122
1358
  const isFloatData = srcData instanceof Float32Array || srcData.constructor?.name === "Float32Array";
1123
- const pixelCount = image.width * image.height * 4;
1359
+ const pixelCount = width * height * 4;
1124
1360
  const uint16Data = new Uint16Array(pixelCount);
1125
1361
  for (let i = 0; i < pixelCount; i++) {
1126
- const value = srcData[i];
1362
+ const value = srcData[i] ?? 0;
1127
1363
  uint16Data[i] = isFloatData ? Math.round(Math.min(1, Math.max(0, value)) * 65535) : value * 257;
1128
1364
  }
1129
1365
  const pngData = encodePng16({
1130
- width: image.width,
1131
- height: image.height,
1366
+ width,
1367
+ height,
1132
1368
  depth: 16,
1133
1369
  channels: 4,
1134
1370
  data: uint16Data
@@ -1682,12 +1918,13 @@ var MToonAtlasExporterPlugin = class {
1682
1918
  */
1683
1919
  createMaterialDef(material) {
1684
1920
  const indices = this.textureIndices.get(material);
1921
+ const parameterMeta = material.parameterBuffer ?? material.parameterTexture;
1685
1922
  const extension = {
1686
1923
  version: "1.0",
1687
1924
  parameterTexture: {
1688
1925
  index: indices?.parameterTextureIndex ?? -1,
1689
- texelsPerSlot: material.parameterTexture?.texelsPerSlot ?? 9,
1690
- slotCount: material.parameterTexture?.slotCount ?? 0
1926
+ texelsPerSlot: parameterMeta?.texelsPerSlot ?? 9,
1927
+ slotCount: parameterMeta?.slotCount ?? 0
1691
1928
  },
1692
1929
  slotAttributeName: "_MTOON_MATERIAL_SLOT",
1693
1930
  atlasedTextures: {}
@@ -1800,18 +2037,28 @@ var MToonAtlasExporterPlugin = class {
1800
2037
  };
1801
2038
  export {
1802
2039
  DEFAULT_KTX2_TRANSCODER_PATH,
2040
+ DEFAULT_PARAMETER_LAYOUT,
2041
+ DEFAULT_TEXELS_PER_SLOT,
2042
+ MAX_PARAMETER_UBO_BYTES,
1803
2043
  MTOON_ATLAS_EXTENSION_NAME,
1804
2044
  MTOON_TRANSPARENT_RENDER_ORDER,
1805
2045
  MTOON_TRANSPARENT_ZWRITE_RENDER_ORDER,
1806
2046
  MToonAtlasExporterPlugin,
1807
2047
  MToonAtlasLoaderPlugin,
1808
2048
  MToonAtlasMaterial,
2049
+ PARAMETER_UBO_BUCKET_SLOTS,
1809
2050
  applyAtlasTextureColorSpace,
1810
2051
  clearKtx2LoaderCache,
2052
+ createPaddedParameterBuffer,
2053
+ extractParameterBufferFromTexture,
2054
+ getKtx2Renderer,
1811
2055
  getKtx2TranscoderPath,
1812
2056
  getMToonRenderOrder,
2057
+ getMaxParameterUBOSlots,
1813
2058
  rememberKtx2Source,
1814
2059
  resolveKtx2Loader,
2060
+ resolveParameterBucketSlots,
2061
+ setKtx2Renderer,
1815
2062
  setKtx2TranscoderPath
1816
2063
  };
1817
2064
  //# sourceMappingURL=index.js.map