@webxr-jp/mtoon-atlas 0.4.0 → 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.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");
@@ -754,11 +832,11 @@ var MToonAtlasLoaderPlugin = class {
754
832
  return null;
755
833
  }
756
834
  /**
757
- * パラメータテクスチャを16bit精度で読み込む
835
+ * パラメータ画像を16bit精度でデコードして生 Float32Array として読み込む
758
836
  * GLTFLoaderのloadTextureはImageBitmap経由で8bitに変換されるため、
759
837
  * 直接PNGデータを取得してfast-pngでデコードする
760
838
  */
761
- async _loadParameterTexture16bit(textureIndex) {
839
+ async _loadParameterData16bit(textureIndex) {
762
840
  const json = this.parser.json;
763
841
  if (textureIndex < 0 || textureIndex >= json.textures.length) {
764
842
  throw new Error(`MToonAtlasLoaderPlugin: Invalid texture index: ${textureIndex}`);
@@ -825,7 +903,14 @@ var MToonAtlasLoaderPlugin = class {
825
903
  }
826
904
  }
827
905
  }
828
- const texture = new import_three2.DataTexture(floatData, width, height, import_three2.RGBAFormat, import_three2.FloatType);
906
+ return { data: floatData, width, height };
907
+ }
908
+ /**
909
+ * パラメータテクスチャを16bit精度で読み込む(GLSL 版向け)
910
+ */
911
+ async _loadParameterTexture16bit(textureIndex) {
912
+ const { data, width, height } = await this._loadParameterData16bit(textureIndex);
913
+ const texture = new import_three2.DataTexture(data, width, height, import_three2.RGBAFormat, import_three2.FloatType);
829
914
  texture.flipY = false;
830
915
  texture.colorSpace = import_three2.NoColorSpace;
831
916
  texture.minFilter = import_three2.NearestFilter;
@@ -935,13 +1020,23 @@ var MToonAtlasLoaderPlugin = class {
935
1020
  }
936
1021
  }
937
1022
  };
1023
+ const prefersBuffer = this.materialType.prefersParameterBuffer === true;
1024
+ let parameterData = null;
938
1025
  let parameterTexture = null;
939
1026
  if (extension.parameterTexture && extension.parameterTexture.index >= 0) {
940
- pending.push(
941
- this._loadParameterTexture16bit(extension.parameterTexture.index).then((tex) => {
942
- parameterTexture = tex;
943
- })
944
- );
1027
+ if (prefersBuffer) {
1028
+ pending.push(
1029
+ this._loadParameterData16bit(extension.parameterTexture.index).then(({ data }) => {
1030
+ parameterData = data;
1031
+ })
1032
+ );
1033
+ } else {
1034
+ pending.push(
1035
+ this._loadParameterTexture16bit(extension.parameterTexture.index).then((tex) => {
1036
+ parameterTexture = tex;
1037
+ })
1038
+ );
1039
+ }
945
1040
  }
946
1041
  if (extension.atlasedTextures) {
947
1042
  for (const [key, value] of Object.entries(extension.atlasedTextures)) {
@@ -949,28 +1044,47 @@ var MToonAtlasLoaderPlugin = class {
949
1044
  }
950
1045
  }
951
1046
  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
1047
  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
- });
1048
+ const slotCount = extension.parameterTexture?.slotCount ?? 0;
1049
+ const texelsPerSlot = extension.parameterTexture?.texelsPerSlot ?? 9;
1050
+ let material;
1051
+ if (prefersBuffer) {
1052
+ const data = parameterData ?? new Float32Array(Math.max(1, slotCount) * texelsPerSlot * 4);
1053
+ material = new this.materialType({
1054
+ parameterBuffer: {
1055
+ data,
1056
+ slotCount,
1057
+ texelsPerSlot,
1058
+ atlasedTextures
1059
+ },
1060
+ slotAttribute: {
1061
+ name: slotAttributeName
1062
+ },
1063
+ name: materialDef.name
1064
+ });
1065
+ } else {
1066
+ if (!parameterTexture) {
1067
+ const dummyData = new Float32Array(4).fill(0);
1068
+ parameterTexture = new import_three2.DataTexture(dummyData, 1, 1, import_three2.RGBAFormat, import_three2.FloatType);
1069
+ parameterTexture.flipY = false;
1070
+ parameterTexture.colorSpace = import_three2.NoColorSpace;
1071
+ parameterTexture.minFilter = import_three2.NearestFilter;
1072
+ parameterTexture.magFilter = import_three2.NearestFilter;
1073
+ parameterTexture.needsUpdate = true;
1074
+ }
1075
+ material = new this.materialType({
1076
+ parameterTexture: {
1077
+ texture: parameterTexture,
1078
+ slotCount,
1079
+ texelsPerSlot,
1080
+ atlasedTextures
1081
+ },
1082
+ slotAttribute: {
1083
+ name: slotAttributeName
1084
+ },
1085
+ name: materialDef.name
1086
+ });
1087
+ }
974
1088
  if (materialDef.doubleSided) {
975
1089
  material.side = import_three2.DoubleSide;
976
1090
  } else {
@@ -1016,6 +1130,9 @@ var MToonAtlasExporterPlugin = class {
1016
1130
  // オブジェクト参照ではキャッシュできない。
1017
1131
  // キーの選び方は processTextureWithCache のコメントを参照
1018
1132
  this.textureCache = /* @__PURE__ */ new Map();
1133
+ // パラメータバッファ(Float32Array参照)からインデックスへのキャッシュ(重複登録防止)
1134
+ // 本体とアウトライン(clone)は同一の parameterBuffer.data を参照するため、1回だけ書き出す
1135
+ this.parameterBufferCache = /* @__PURE__ */ new Map();
1019
1136
  this.writer = writer;
1020
1137
  this.skipStandardMaterialProcessing();
1021
1138
  }
@@ -1053,6 +1170,7 @@ var MToonAtlasExporterPlugin = class {
1053
1170
  this.textureIndices.clear();
1054
1171
  this.pendingTextureIndices.clear();
1055
1172
  this.textureCache.clear();
1173
+ this.parameterBufferCache.clear();
1056
1174
  const roots = Array.isArray(input) ? input : [input];
1057
1175
  for (const root of roots) {
1058
1176
  root.traverse((obj) => {
@@ -1150,12 +1268,16 @@ var MToonAtlasExporterPlugin = class {
1150
1268
  parameterTextureIndex: null,
1151
1269
  atlasedTextureIndices: {}
1152
1270
  };
1153
- if (material.parameterTexture?.texture) {
1271
+ if (material.parameterBuffer) {
1272
+ pendingIndices.parameterTextureIndex = this.processParameterBufferWithCache(
1273
+ material.parameterBuffer
1274
+ );
1275
+ } else if (material.parameterTexture?.texture) {
1154
1276
  pendingIndices.parameterTextureIndex = this.processParameterTextureWithCache(
1155
1277
  material.parameterTexture.texture
1156
1278
  );
1157
1279
  }
1158
- const atlasedTextures = material.parameterTexture?.atlasedTextures;
1280
+ const atlasedTextures = material.parameterBuffer?.atlasedTextures ?? material.parameterTexture?.atlasedTextures;
1159
1281
  if (atlasedTextures) {
1160
1282
  for (const [key, texture] of Object.entries(atlasedTextures)) {
1161
1283
  if (texture) {
@@ -1186,6 +1308,24 @@ var MToonAtlasExporterPlugin = class {
1186
1308
  pendingIndices.resolvePromise = resolveAllPromise;
1187
1309
  this.writer.pending.push(resolveAllPromise);
1188
1310
  }
1311
+ /**
1312
+ * パラメータバッファをキャッシュ付きで処理
1313
+ * 同じ Float32Array 参照は 1 回だけ処理される
1314
+ */
1315
+ processParameterBufferWithCache(descriptor) {
1316
+ const cached = this.parameterBufferCache.get(descriptor.data);
1317
+ if (cached) {
1318
+ return cached;
1319
+ }
1320
+ const promise = this.processParameterData(
1321
+ descriptor.data,
1322
+ descriptor.texelsPerSlot,
1323
+ descriptor.slotCount,
1324
+ "parameterTexture"
1325
+ );
1326
+ this.parameterBufferCache.set(descriptor.data, promise);
1327
+ return promise;
1328
+ }
1189
1329
  /**
1190
1330
  * パラメータテクスチャをキャッシュ付きで処理
1191
1331
  * 同じUUIDのテクスチャは1回だけ処理される
@@ -1229,6 +1369,18 @@ var MToonAtlasExporterPlugin = class {
1229
1369
  * 生のRGBAデータから直接PNGを生成してBINチャンクに格納
1230
1370
  */
1231
1371
  processParameterTexture(texture) {
1372
+ const image = texture.image;
1373
+ return this.processParameterData(
1374
+ image?.data,
1375
+ image?.width ?? 0,
1376
+ image?.height ?? 0,
1377
+ texture.name || "parameterTexture"
1378
+ );
1379
+ }
1380
+ /**
1381
+ * パラメータ配列(Float32Array / Uint8Array 等)を 16bit PNG として BIN チャンクへ書き込む
1382
+ */
1383
+ processParameterData(srcData, width, height, name) {
1232
1384
  const json = this.writer.json;
1233
1385
  json.textures = json.textures || [];
1234
1386
  json.images = json.images || [];
@@ -1251,7 +1403,7 @@ var MToonAtlasExporterPlugin = class {
1251
1403
  }
1252
1404
  const imageIndex = json.images.length;
1253
1405
  const imageDef = {
1254
- name: texture.name || "parameterTexture",
1406
+ name,
1255
1407
  mimeType: "image/png"
1256
1408
  };
1257
1409
  json.images.push(imageDef);
@@ -1259,21 +1411,19 @@ var MToonAtlasExporterPlugin = class {
1259
1411
  json.textures.push({
1260
1412
  sampler: nearestSamplerIndex,
1261
1413
  source: imageIndex,
1262
- name: texture.name || "parameterTexture"
1414
+ name
1263
1415
  });
1264
- const image = texture.image;
1265
- if (image?.data && image.width && image.height) {
1266
- const srcData = image.data;
1416
+ if (srcData && width > 0 && height > 0) {
1267
1417
  const isFloatData = srcData instanceof Float32Array || srcData.constructor?.name === "Float32Array";
1268
- const pixelCount = image.width * image.height * 4;
1418
+ const pixelCount = width * height * 4;
1269
1419
  const uint16Data = new Uint16Array(pixelCount);
1270
1420
  for (let i = 0; i < pixelCount; i++) {
1271
- const value = srcData[i];
1421
+ const value = srcData[i] ?? 0;
1272
1422
  uint16Data[i] = isFloatData ? Math.round(Math.min(1, Math.max(0, value)) * 65535) : value * 257;
1273
1423
  }
1274
1424
  const pngData = (0, import_fast_png2.encode)({
1275
- width: image.width,
1276
- height: image.height,
1425
+ width,
1426
+ height,
1277
1427
  depth: 16,
1278
1428
  channels: 4,
1279
1429
  data: uint16Data
@@ -1827,12 +1977,13 @@ var MToonAtlasExporterPlugin = class {
1827
1977
  */
1828
1978
  createMaterialDef(material) {
1829
1979
  const indices = this.textureIndices.get(material);
1980
+ const parameterMeta = material.parameterBuffer ?? material.parameterTexture;
1830
1981
  const extension = {
1831
1982
  version: "1.0",
1832
1983
  parameterTexture: {
1833
1984
  index: indices?.parameterTextureIndex ?? -1,
1834
- texelsPerSlot: material.parameterTexture?.texelsPerSlot ?? 9,
1835
- slotCount: material.parameterTexture?.slotCount ?? 0
1985
+ texelsPerSlot: parameterMeta?.texelsPerSlot ?? 9,
1986
+ slotCount: parameterMeta?.slotCount ?? 0
1836
1987
  },
1837
1988
  slotAttributeName: "_MTOON_MATERIAL_SLOT",
1838
1989
  atlasedTextures: {}
@@ -1947,19 +2098,26 @@ var MToonAtlasExporterPlugin = class {
1947
2098
  0 && (module.exports = {
1948
2099
  DEFAULT_KTX2_TRANSCODER_PATH,
1949
2100
  DEFAULT_PARAMETER_LAYOUT,
2101
+ DEFAULT_TEXELS_PER_SLOT,
2102
+ MAX_PARAMETER_UBO_BYTES,
1950
2103
  MTOON_ATLAS_EXTENSION_NAME,
1951
2104
  MTOON_TRANSPARENT_RENDER_ORDER,
1952
2105
  MTOON_TRANSPARENT_ZWRITE_RENDER_ORDER,
1953
2106
  MToonAtlasExporterPlugin,
1954
2107
  MToonAtlasLoaderPlugin,
1955
2108
  MToonAtlasMaterial,
2109
+ PARAMETER_UBO_BUCKET_SLOTS,
1956
2110
  applyAtlasTextureColorSpace,
1957
2111
  clearKtx2LoaderCache,
2112
+ createPaddedParameterBuffer,
2113
+ extractParameterBufferFromTexture,
1958
2114
  getKtx2Renderer,
1959
2115
  getKtx2TranscoderPath,
1960
2116
  getMToonRenderOrder,
2117
+ getMaxParameterUBOSlots,
1961
2118
  rememberKtx2Source,
1962
2119
  resolveKtx2Loader,
2120
+ resolveParameterBucketSlots,
1963
2121
  setKtx2Renderer,
1964
2122
  setKtx2TranscoderPath
1965
2123
  });