modelstage 0.10.1 → 0.11.2
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/CHANGELOG.md +5 -1
- package/THIRD_PARTY_NOTICES.md +20 -0
- package/config/models/live2d.root.json +1 -0
- package/config/models/spine.root.json +1 -0
- package/package.json +1 -1
- package/plugins/unity-live2d-bundle/index.js +76 -4
- package/plugins/unity-live2d-bundle/lib/astc.js +1315 -0
- package/plugins/unity-live2d-bundle/lib/texdecode.js +60 -15
- package/plugins/unity-live2d-bundle/plugin.json +1 -0
- package/plugins/unity-spine-bundle/index.js +192 -16
- package/plugins/unity-spine-bundle/lib/astc.js +1315 -0
- package/plugins/unity-spine-bundle/lib/extract.js +69 -0
- package/plugins/unity-spine-bundle/lib/texdecode.js +60 -15
- package/plugins/unity-spine-bundle/plugin.json +1 -0
- package/src/server/unpack/plugin-runner.mjs +39 -5
- package/src/server/unpack/runtime/exec.mjs +19 -1
- package/src/web/infra/plugin-host-unpack.mjs +29 -4
- package/src/web/main.js +42 -2
- package/src/web/render/camera/constants.js +41 -3
- package/src/web/render/demo-3d.js +131 -0
- package/src/web/render/interaction.js +129 -12
- package/src/web/render/surfaces.js +4 -0
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
// plugins/unity-live2d-bundle/lib/texdecode.js —— 纹理格式分发
|
|
3
3
|
//
|
|
4
4
|
// 职责:读 Texture2D 的 m_TextureFormat,分发给对应解码器,返回 RGBA 字节。
|
|
5
|
-
// · 已实现:
|
|
6
|
-
//
|
|
5
|
+
// · 已实现:ASTC 全系列(48~59,解码器在 lib/astc.js) / ETC2_RGBA8(47) / ETC2_RGBA1(46)
|
|
6
|
+
// / ETC2_RGB(45) / ETC1_RGB4(34) / DXT1/3/4/5(10/12/26/27) / 未压缩 RGBA32/RGB24/Alpha8 等直通格式
|
|
7
|
+
// · 未实现:PVRTC / BC6H / BC7 等 —— **抛可读错误**并把格式名与格式号带上,
|
|
7
8
|
// 绝不返回空白图或猜出来的图(否则上层会把"解码失败"当成"资源损坏")。
|
|
8
9
|
//
|
|
9
10
|
// 纹理尺寸与数据量的自检:按格式的块大小算出期望数据量,与给的字节数比对;
|
|
@@ -27,8 +28,24 @@
|
|
|
27
28
|
7: { name: 'RGB565', block: 2, size: 1 },
|
|
28
29
|
34: { name: 'ETC_RGB4', compress: 'etc1', block: 8, size: 4 },
|
|
29
30
|
45: { name: 'ETC2_RGB', compress: 'etc2', block: 8, size: 4 },
|
|
31
|
+
46: { name: 'ETC2_RGBA1', compress: 'etc2a1', block: 16, size: 4 },
|
|
30
32
|
47: { name: 'ETC2_RGBA8', compress: 'etc2a8', block: 16, size: 4 },
|
|
31
|
-
48
|
|
33
|
+
// 48~59 是 ASTC 系列(UnityPy/enums/TextureFormat.py;并被真实包的 m_CompleteImageSize
|
|
34
|
+
// 反推验证:440x440/87616 只有 6x6 唯一解、1024x1024/672400 只有 5x5)。
|
|
35
|
+
// **48 不是 ETC2_RGBA1**(那个是 46)。块边长各不相同,必须逐条写 bw/bh,
|
|
36
|
+
// 否则 expectedSize 会按 4x4 算错长度,把"格式未支持"错报成"数据不足"。
|
|
37
|
+
48: { name: 'ASTC_RGB_4x4', block: 16, size: 4, bw: 4, bh: 4 },
|
|
38
|
+
49: { name: 'ASTC_RGB_5x5', block: 16, size: 4, bw: 5, bh: 5 },
|
|
39
|
+
50: { name: 'ASTC_RGB_6x6', block: 16, size: 4, bw: 6, bh: 6 },
|
|
40
|
+
51: { name: 'ASTC_RGB_8x8', block: 16, size: 4, bw: 8, bh: 8 },
|
|
41
|
+
52: { name: 'ASTC_RGB_10x10', block: 16, size: 4, bw: 10, bh: 10 },
|
|
42
|
+
53: { name: 'ASTC_RGB_12x12', block: 16, size: 4, bw: 12, bh: 12 },
|
|
43
|
+
54: { name: 'ASTC_RGBA_4x4', block: 16, size: 4, bw: 4, bh: 4 },
|
|
44
|
+
55: { name: 'ASTC_RGBA_5x5', block: 16, size: 4, bw: 5, bh: 5 },
|
|
45
|
+
56: { name: 'ASTC_RGBA_6x6', block: 16, size: 4, bw: 6, bh: 6 },
|
|
46
|
+
57: { name: 'ASTC_RGBA_8x8', block: 16, size: 4, bw: 8, bh: 8 },
|
|
47
|
+
58: { name: 'ASTC_RGBA_10x10', block: 16, size: 4, bw: 10, bh: 10 },
|
|
48
|
+
59: { name: 'ASTC_RGBA_12x12', block: 16, size: 4, bw: 12, bh: 12 },
|
|
32
49
|
// BC/DXT 家族(PC/主机资源包常用;块式压缩,解码实现见 lib/dxt.js)
|
|
33
50
|
10: { name: 'DXT1', compress: 'bc1', block: 8, size: 4 },
|
|
34
51
|
12: { name: 'DXT5', compress: 'bc3', block: 16, size: 4 },
|
|
@@ -46,8 +63,10 @@
|
|
|
46
63
|
const f = FORMATS[fmt];
|
|
47
64
|
if (!f) return null;
|
|
48
65
|
if (f.size === 1) return width * height * f.block;
|
|
49
|
-
|
|
50
|
-
|
|
66
|
+
// 块边长按格式取(缺省 4):ETC/BC 全是 4x4 走缺省,ASTC 是 4~12 各不相同。
|
|
67
|
+
// 写死 4 会让 ASTC 算出偏大的期望长度 ⇒ 抛 TEX_DATA_SHORT,把"格式未支持"藏成"数据不足"。
|
|
68
|
+
const bw = Math.ceil(width / (f.bw || 4));
|
|
69
|
+
const bh = Math.ceil(height / (f.bh || 4));
|
|
51
70
|
return bw * bh * f.block;
|
|
52
71
|
}
|
|
53
72
|
|
|
@@ -128,19 +147,42 @@
|
|
|
128
147
|
* 调用方**不得依赖**返回的 rgba 与入参共享底层 buffer(有的格式共享、有的不共享,
|
|
129
148
|
* 原地修改它可能改到源数据、也可能改不到)。
|
|
130
149
|
*/
|
|
150
|
+
/**
|
|
151
|
+
* 生成"某个 ASTC 格式号"的解码器入口。
|
|
152
|
+
*
|
|
153
|
+
* 为什么要工厂而不是十二条手写箭头函数:块边长(4x4 ~ 12x12)只有 FORMATS 一处真相,
|
|
154
|
+
* 手抄一份数字到 DECODERS 就多了一处会漂移的副本 —— FORMATS 改了而这里没改时,
|
|
155
|
+
* 解码器会按错的块尺寸切块,表现为"图能出但整片错位",比直接报错难查得多。
|
|
156
|
+
*/
|
|
157
|
+
const astcDecoder = (fmt) => (d, w, h) => {
|
|
158
|
+
const entry = FORMATS[fmt];
|
|
159
|
+
// astc.js 由文件清单在 texdecode.js 之前注入;缺了要说清是"没加载"而不是"格式不支持"
|
|
160
|
+
if (!NS.astc || typeof NS.astc.decodeAstc !== 'function') {
|
|
161
|
+
throw NS.errors.fail('ULB_ERR_TEX_DECODE',
|
|
162
|
+
`ASTC 解码器未加载(lib/astc.js 未按序注入?),无法解码 ${entry.name}(${fmt})`);
|
|
163
|
+
}
|
|
164
|
+
return NS.astc.decodeAstc(d, w, h, entry.bw, entry.bh);
|
|
165
|
+
};
|
|
166
|
+
|
|
131
167
|
const DECODERS = {
|
|
132
168
|
// 压缩格式:解码器自身产出 RGBA
|
|
133
169
|
34: (d, w, h) => NS.etc2.decodeEtc1Rgb(d, w, h),
|
|
134
170
|
45: (d, w, h) => NS.etc2.decodeEtc2Rgb(d, w, h),
|
|
135
171
|
47: (d, w, h) => NS.etc2.decodeEtc2Rgba8(d, w, h),
|
|
136
|
-
//
|
|
137
|
-
// lib/etc2.js 的 decodeEtc2Rgba1
|
|
138
|
-
//
|
|
139
|
-
|
|
172
|
+
// 46 ETC2_RGBA1:ETC2 RGB 块(8 字节)+ 1bit alpha 平面(8 字节)。块布局与位序见
|
|
173
|
+
// lib/etc2.js 的 decodeEtc2Rgba1 注释。
|
|
174
|
+
// 注意编号:ETC2_RGBA1 是 **46**,不是 48(48 是 ASTC_RGB_4x4,见 FORMATS 注释)。
|
|
175
|
+
46: (d, w, h) => NS.etc2.decodeEtc2Rgba1(d, w, h),
|
|
140
176
|
10: (d, w, h) => NS.dxt.decodeBc1(d, w, h),
|
|
141
177
|
12: (d, w, h) => NS.dxt.decodeBc3(d, w, h),
|
|
142
178
|
26: (d, w, h) => NS.dxt.decodeBc4(d, w, h),
|
|
143
179
|
27: (d, w, h) => NS.dxt.decodeBc5(d, w, h),
|
|
180
|
+
// ASTC 全系列(48~53 无 alpha 变体 / 54~59 带 alpha 变体):同一个解码器,只有块边长不同。
|
|
181
|
+
// 块边长从 FORMATS 的 bw/bh 取(见 astcDecoder 的注释),这里只登记"哪个格式号归 ASTC"。
|
|
182
|
+
48: astcDecoder(48), 49: astcDecoder(49), 50: astcDecoder(50),
|
|
183
|
+
51: astcDecoder(51), 52: astcDecoder(52), 53: astcDecoder(53),
|
|
184
|
+
54: astcDecoder(54), 55: astcDecoder(55), 56: astcDecoder(56),
|
|
185
|
+
57: astcDecoder(57), 58: astcDecoder(58), 59: astcDecoder(59),
|
|
144
186
|
// 未压缩格式:切片后按通道展开
|
|
145
187
|
4: (d, w, h, need) => d.slice(0, need),
|
|
146
188
|
5: (d, w, h, need) => argbToRgba(d.slice(0, need)),
|
|
@@ -159,12 +201,15 @@
|
|
|
159
201
|
function decode(meta, data, opts) {
|
|
160
202
|
const fmt = meta.format;
|
|
161
203
|
const f = FORMATS[fmt];
|
|
162
|
-
if (!f) throw NS.errors.fail('ULB_ERR_TEX_FORMAT_UNSUPPORTED', `暂不支持纹理格式 ${fmt}(${meta.name || ''})——已支持:ETC2_RGBA8(47)、ETC2_RGBA1(
|
|
204
|
+
if (!f) throw NS.errors.fail('ULB_ERR_TEX_FORMAT_UNSUPPORTED', `暂不支持纹理格式 ${fmt}(${meta.name || ''})——已支持:ETC2_RGBA8(47)、ETC2_RGBA1(46)、ETC2_RGB(45)、ETC_RGB4(34)、DXT1(10)、DXT5(12)、BC4(26)、BC5(27)、ASTC 全系列(48~59)、RGBA32(4)、ARGB32(5)、RGB24(3)、Alpha8(1)、RGB565(7);未实现:PVRTC(30~33)、BC6H(24)、BC7(25)`);
|
|
205
|
+
// 解码器检查放在长度检查**之前**:表里有、但没登记解码器(PVRTC / BC6H / BC7 这类)
|
|
206
|
+
// 属于"格式未支持",不是"数据不足"。顺序反了会用尺寸错误掩盖真实原因。
|
|
207
|
+
const decoder = DECODERS[fmt];
|
|
208
|
+
if (!decoder) {
|
|
209
|
+
throw NS.errors.fail('ULB_ERR_TEX_FORMAT_UNSUPPORTED', `纹理格式 ${f.name}(${fmt})尚未实现解码器——当前未接入的格式只有 PVRTC / BC6H / BC7`);
|
|
210
|
+
}
|
|
163
211
|
const need = expectedSize(fmt, meta.width, meta.height);
|
|
164
212
|
if (data.length < need) throw NS.errors.fail('ULB_ERR_TEX_DATA_SHORT', `纹理 ${meta.name || ''}(${f.name} ${meta.width}x${meta.height})数据不足:需要 ${need} 字节,实际 ${data.length}`);
|
|
165
|
-
// 表里有格式、但没有登记解码器(如 ASTC 50 系列)⇒ 继续走这条兜底
|
|
166
|
-
const decoder = DECODERS[fmt];
|
|
167
|
-
if (!decoder) throw NS.errors.fail('ULB_ERR_TEX_DECODE', `纹理格式 ${f.name} 的分发分支缺失`);
|
|
168
213
|
const rgba = decoder(data, meta.width, meta.height, need);
|
|
169
214
|
// 统一翻转:原实现用 finish() 包住每个 return,等价于在这里一次做完
|
|
170
215
|
return !opts || opts.flipY !== false ? flipY(rgba, meta.width, meta.height) : rgba;
|
|
@@ -174,8 +219,8 @@
|
|
|
174
219
|
* 某个 Unity TextureFormat 能否**真正解码**。
|
|
175
220
|
*
|
|
176
221
|
* 为什么需要它:FORMATS 是"格式字典"(名称 / 尺寸 / block 布局),**不代表能解码** ——
|
|
177
|
-
* 例如
|
|
178
|
-
*
|
|
222
|
+
* 例如 PVRTC 可能在 FORMATS 里有条目、但 DECODERS 没登记,decode() 会抛
|
|
223
|
+
* ULB_ERR_TEX_FORMAT_UNSUPPORTED。若外部(预检 / 面板提示)拿 `!!FORMATS[fmt]` 当"支持",
|
|
179
224
|
* 就会先报"支持"、真正解包时才炸 —— 同一件事两套判据。
|
|
180
225
|
* ⇒ 能力判定只认这张表:**这是"能不能解码"的唯一真相**。
|
|
181
226
|
*
|
|
@@ -125,8 +125,9 @@ function install() {
|
|
|
125
125
|
? fields.m_Name.text : String(fields.m_Name || '');
|
|
126
126
|
const raw = fields.m_Script && fields.m_Script.bytes;
|
|
127
127
|
if (!raw) continue;
|
|
128
|
-
|
|
129
|
-
|
|
128
|
+
// 带上 pathID:同名的资源靠它区分(m_Name 不保证唯一,实测单包有 2 份同名 .skel / .atlas)
|
|
129
|
+
if (/\.skel$/i.test(name)) skel.push({ name, bytes: raw, pathId: String(obj.pathID) });
|
|
130
|
+
else if (/\.atlas$/i.test(name)) atlas.push({ name, bytes: raw, pathId: String(obj.pathID) });
|
|
130
131
|
}
|
|
131
132
|
return { skel, atlas };
|
|
132
133
|
}
|
|
@@ -150,6 +151,118 @@ function install() {
|
|
|
150
151
|
return pages;
|
|
151
152
|
}
|
|
152
153
|
|
|
154
|
+
/**
|
|
155
|
+
* 与 parseAtlasPages 同源,但**连每页声明的尺寸一起返回**:`[{page, w, h}]`。
|
|
156
|
+
*
|
|
157
|
+
* 为什么不改 parseAtlasPages 本身:它被单测与 `__test` 直接调用(返回值是纯页名数组)。
|
|
158
|
+
* 新增函数而不是改签名 ⇒ 现有调用方零影响。
|
|
159
|
+
*
|
|
160
|
+
* 为什么需要尺寸:同角色多套外观会有**同名不同尺寸**的贴图(实测两份 char_010_chen:
|
|
161
|
+
* 256x256 与 512x512),而两份 atlas 也都声明页名 char_010_chen.png。只按名字对号
|
|
162
|
+
* ⇒ 把 256² 的图贴到按 512² 算的 region 坐标上 ⇒ 错位/缺图。
|
|
163
|
+
*/
|
|
164
|
+
function parseAtlasPagesDetailed(atlasBytes) {
|
|
165
|
+
const text = new TextDecoder('utf-8').decode(atlasBytes).replace(/^\uFEFF/, '');
|
|
166
|
+
const out = [];
|
|
167
|
+
let cur = null;
|
|
168
|
+
for (const raw of text.split('\n')) {
|
|
169
|
+
const line = raw.replace(/\r$/, '');
|
|
170
|
+
const trimmed = line.trim();
|
|
171
|
+
if (!trimmed) continue;
|
|
172
|
+
if (/^size\s*:/i.test(trimmed)) {
|
|
173
|
+
if (cur && cur.w == null) {
|
|
174
|
+
const parts = trimmed.split(':')[1].split(',');
|
|
175
|
+
const w = parseInt(parts[0], 10);
|
|
176
|
+
const h = parseInt(parts[1], 10);
|
|
177
|
+
if (Number.isFinite(w) && Number.isFinite(h)) { cur.w = w; cur.h = h; }
|
|
178
|
+
}
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
// 行首无空白 + 以图片扩展名结尾 ⇒ 页名行(与 parseAtlasPages 同判据)
|
|
182
|
+
if (/^[^\s].*\.(png|jpg|jpeg|webp)\s*$/i.test(line)) {
|
|
183
|
+
cur = { page: trimmed, w: null, h: null };
|
|
184
|
+
out.push(cur);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return out;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* 同名资源的键名:`<stem>__<pathId><ext>`(与 Python worker 同口径)。
|
|
192
|
+
*
|
|
193
|
+
* 为什么需要:`m_Name` 不保证唯一。用名字当 Map 键时后写的会覆盖先写的,
|
|
194
|
+
* 覆盖后 entry 里写的名字可能指向**另一套**的内容 ⇒ 前端报 Region not found。
|
|
195
|
+
* 为什么用 pathID 而不是序号:pathID 在同一 serialized file 内天然唯一,
|
|
196
|
+
* 与对象遍历顺序无关 ⇒ 同一包两次导出结果一致(可复现)。
|
|
197
|
+
*/
|
|
198
|
+
function disambiguateKey(name, pathId) {
|
|
199
|
+
const dot = String(name).lastIndexOf('.');
|
|
200
|
+
const stem = dot > 0 ? String(name).slice(0, dot) : String(name);
|
|
201
|
+
const ext = dot > 0 ? String(name).slice(dot) : '';
|
|
202
|
+
return `${stem}__${pathId}${ext}`;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* 按 **region 命中率** 选与 skel 匹配的 atlas(与 worker 的 _pick_atlas_for_skel 同判据)。
|
|
207
|
+
*
|
|
208
|
+
* 为什么不能按名字配对:同名覆盖下 `char_010_chen.skel` 与 `char_010_chen.atlas`
|
|
209
|
+
* 名字永远对得上,哪怕它们来自不同的一套。只有"atlas 的 region 名能否在 skel 的
|
|
210
|
+
* string table 里找到"才是内容层面的真判据。
|
|
211
|
+
*
|
|
212
|
+
* 为什么不能要求唯一命中:同一角色的多套外观会**共享一部分 region 名**(实测 1~2 个噪声),
|
|
213
|
+
* 所以取命中数最高者;并列时按 atlas 在包内的先后取第一个(稳定,不依赖遍历细节)。
|
|
214
|
+
*/
|
|
215
|
+
function pickAtlasForSkel(skelBytes, atlases) {
|
|
216
|
+
// ⚠️ 必须先把 skel 的字节**当成字符串**再查子串:
|
|
217
|
+
// `Uint8Array.prototype.includes()` 是"数组里有没有这个**字节值**",
|
|
218
|
+
// 传一个 Uint8Array 进去永远 false —— 那会让命中率恒为 0、配对悄悄退化成"取第一个"。
|
|
219
|
+
// latin1 解码是 1:1 字节映射,所以 latin1 字符串的 includes 等价于字节子串查找。
|
|
220
|
+
const haystack = new TextDecoder('latin1').decode(skelBytes);
|
|
221
|
+
let best = null;
|
|
222
|
+
let bestHits = -1;
|
|
223
|
+
for (const a of atlases) {
|
|
224
|
+
let hits = 0;
|
|
225
|
+
// region 名(行首无缩进、非属性、非页名)才是 skel string table 里的东西;
|
|
226
|
+
// 页名带 `.png` 后缀,skel 里不会有,不参与统计。
|
|
227
|
+
for (const region of atlasRegions(a.bytes)) {
|
|
228
|
+
if (region && haystack.includes(region)) hits += 1;
|
|
229
|
+
}
|
|
230
|
+
if (hits > bestHits) { best = a; bestHits = hits; }
|
|
231
|
+
}
|
|
232
|
+
return best || (atlases[0] || null);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** atlas 里的 region 名:行首无缩进、非属性行、非页名行(与 worker 的 _atlas_regions 同判据)。 */
|
|
236
|
+
function atlasRegions(atlasBytes) {
|
|
237
|
+
const text = new TextDecoder('utf-8').decode(atlasBytes).replace(/^\uFEFF/, '');
|
|
238
|
+
const out = [];
|
|
239
|
+
for (const raw of text.split('\n')) {
|
|
240
|
+
const line = raw.replace(/\r$/, '');
|
|
241
|
+
const trimmed = line.trim();
|
|
242
|
+
if (!trimmed) continue;
|
|
243
|
+
if (line[0] === ' ' || line[0] === '\t') continue; // 属性行
|
|
244
|
+
if (/^(size|format|filter|repeat|pma|scale)\s*:/i.test(trimmed)) continue;
|
|
245
|
+
if (/\.(png|jpg|jpeg|webp)\s*$/i.test(trimmed)) continue; // 页名行
|
|
246
|
+
out.push(trimmed);
|
|
247
|
+
}
|
|
248
|
+
return out;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* 按名字 + **尺寸**找纹理(与 worker 的 _find_texture 同判据):
|
|
253
|
+
* 尺寸匹配优先,对不上才回落同名第一份。
|
|
254
|
+
*/
|
|
255
|
+
function findTextureForPage(textures, pageStem, w, h) {
|
|
256
|
+
const cands = textures.filter((t) => String(t.name) === pageStem
|
|
257
|
+
|| String(t.name).toLowerCase() === pageStem.toLowerCase());
|
|
258
|
+
if (!cands.length) return null;
|
|
259
|
+
if (w && h) {
|
|
260
|
+
const exact = cands.find((t) => t.width === w && t.height === h);
|
|
261
|
+
if (exact) return exact;
|
|
262
|
+
}
|
|
263
|
+
return cands[0];
|
|
264
|
+
}
|
|
265
|
+
|
|
153
266
|
/**
|
|
154
267
|
* 读 spine 二进制头部的版本串(形如 "4.1.20"),用于自检与错误信息。
|
|
155
268
|
* .skel 头 8 字节是 hash,随后是版本串(本项目样本里是明文 ASCII)。
|
|
@@ -297,9 +410,12 @@ function install() {
|
|
|
297
410
|
|
|
298
411
|
// 按「去扩展名的同名」配对(c200_01.skel ↔ c200_01.atlas);配不上就退第一个,不猜内容
|
|
299
412
|
const stemOf = (n) => String(n).replace(/\.[^.\\/]+$/, '');
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
413
|
+
// 选**数据量最大**的骨架作主模型(与 worker 的 max(skels, key=字节数) 同判据)。
|
|
414
|
+
// 为什么不能取第一个:一个包里可能有多套外观(实测 char_010_chen 与 build_char_010_chen),
|
|
415
|
+
// 取第一个取决于对象序,换版本就可能变。主角色的数据量通常远大于附属,这条判据不依赖命名。
|
|
416
|
+
const skelPick = skel.reduce((a, b) => ((b.bytes && b.bytes.length) > (a.bytes && a.bytes.length) ? b : a), skel[0]);
|
|
417
|
+
// 配对按 **region 命中率**,不按 stem 同名(同名永远对得上,却可能跨套)。
|
|
418
|
+
const atlasPick = pickAtlasForSkel(skelPick.bytes, atlas);
|
|
303
419
|
if (!atlasPick) throw NS.errors.fail(NS.errors.CODES.NO_ATLAS, `有 ${skelPick.name} 但包里没有 .atlas`);
|
|
304
420
|
|
|
305
421
|
// 版本串读不到只记录不失败:不同 Spine 版本的头部布局可能不同,硬失败会误拒可用的包
|
|
@@ -307,16 +423,19 @@ function install() {
|
|
|
307
423
|
if (!spineVersion) ulbLog('warn', `.skel 头部未识别出 Spine 版本串(${skelPick.name}),继续解包`);
|
|
308
424
|
|
|
309
425
|
// —— 纹理:按 atlas 页名找同名 Texture2D,解码后编 PNG ——
|
|
310
|
-
const pages =
|
|
426
|
+
const pages = parseAtlasPagesDetailed(atlasPick.bytes);
|
|
311
427
|
if (!pages.length) throw NS.errors.fail(NS.errors.CODES.NO_ATLAS, 'atlas 里没有解析到贴图页名');
|
|
312
428
|
const allTextures = NS.extract.findTextures(sf);
|
|
429
|
+
// 只算一次:Material 数量很少,纹理页可能多页
|
|
430
|
+
const alphaPairs = NS.extract.collectAlphaPairs(sf);
|
|
313
431
|
|
|
314
432
|
const files = new Map();
|
|
315
433
|
const textureFiles = [];
|
|
316
|
-
for (const
|
|
434
|
+
for (const pg of pages) {
|
|
435
|
+
const page = pg.page;
|
|
317
436
|
const pageStem = stemOf(page);
|
|
318
|
-
|
|
319
|
-
|
|
437
|
+
// 按 **atlas 声明的尺寸** 找同名纹理(尺寸是这几者唯一能互相印证的东西)
|
|
438
|
+
const tex = findTextureForPage(allTextures, pageStem, pg.w, pg.h);
|
|
320
439
|
if (!tex) {
|
|
321
440
|
throw NS.errors.fail(NS.errors.CODES.EXTRACT,
|
|
322
441
|
`atlas 声明了贴图页「${page}」,但包里找不到同名 Texture2D(${pageStem})`);
|
|
@@ -324,11 +443,45 @@ function install() {
|
|
|
324
443
|
// 取压缩数据(.resS 切片或对象内联)→ 解码成 RGBA(flipY: Unity 自下而上存储,
|
|
325
444
|
// atlas 坐标自上而下,翻过来才是正确朝向)→ 编 PNG
|
|
326
445
|
const compressed = NS.extract.textureCompressedBytes(tex, resSBytes);
|
|
327
|
-
|
|
446
|
+
let rgba = NS.texdecode.decode(
|
|
328
447
|
{ format: tex.format, width: tex.width, height: tex.height, name: tex.name },
|
|
329
448
|
compressed,
|
|
330
449
|
{ flipY: true },
|
|
331
450
|
);
|
|
451
|
+
// 主图的透明度可能不在它自己的 alpha 通道里:有的包主图是 ETC_RGB4(没有 alpha),
|
|
452
|
+
// 有的甚至是 RGBA32 但 alpha 整幅 255 —— 两种情况真正的透明度都在 Material
|
|
453
|
+
// 引用的 `_AlphaTex` 灰度里。所以判据看**内容**(alpha 是否有真实透明像素),不看格式。
|
|
454
|
+
// 配对只认 Material 的 PPtr pathID —— 同名贴图在包里可以有多份且内容不同,
|
|
455
|
+
// 名字无法唯一标识(与 Python worker 同一口径,否则前后端结论会漂移)。
|
|
456
|
+
const alphaId = alphaPairs.get(String(tex.object && tex.object.pathID));
|
|
457
|
+
if (alphaId && !NS.extract.alphaIsEffective(rgba)) {
|
|
458
|
+
const alphaTex = allTextures.find(
|
|
459
|
+
(t) => String(t.object && t.object.pathID) === alphaId,
|
|
460
|
+
);
|
|
461
|
+
if (!alphaTex) {
|
|
462
|
+
ulbLog('warn', `贴图页 ${page}:找不到 _AlphaTex 对象 ${alphaId},回退主图`);
|
|
463
|
+
} else {
|
|
464
|
+
try {
|
|
465
|
+
const alphaCompressed = NS.extract.textureCompressedBytes(alphaTex, resSBytes);
|
|
466
|
+
const alphaRgba = NS.texdecode.decode(
|
|
467
|
+
{ format: alphaTex.format, width: alphaTex.width, height: alphaTex.height, name: alphaTex.name },
|
|
468
|
+
alphaCompressed,
|
|
469
|
+
{ flipY: true },
|
|
470
|
+
);
|
|
471
|
+
const masked = NS.extract.applyAlphaMask(
|
|
472
|
+
rgba, tex.width, tex.height, alphaRgba, alphaTex.width, alphaTex.height,
|
|
473
|
+
);
|
|
474
|
+
rgba = masked.rgba;
|
|
475
|
+
if (masked.scaled) {
|
|
476
|
+
ulbLog('warn', `贴图页 ${page}:_AlphaTex 尺寸 ${alphaTex.width}x${alphaTex.height} `
|
|
477
|
+
+ `≠ 主图 ${tex.width}x${tex.height},已最近邻缩放`);
|
|
478
|
+
}
|
|
479
|
+
} catch (e) {
|
|
480
|
+
// 不中断:回退主图比整包失败更好,但必须留痕
|
|
481
|
+
ulbLog('warn', `贴图页 ${page}:_AlphaTex 解码失败(${e.code || e.message}),回退主图`);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
332
485
|
let pngBytes;
|
|
333
486
|
try {
|
|
334
487
|
pngBytes = await NS.png.encodePngAsync(rgba, tex.width, tex.height);
|
|
@@ -340,14 +493,37 @@ function install() {
|
|
|
340
493
|
textureFiles.push(page);
|
|
341
494
|
}
|
|
342
495
|
|
|
343
|
-
// —— 骨架 /
|
|
344
|
-
|
|
345
|
-
|
|
496
|
+
// —— 骨架 / 图集:**全部**同名资源都进表,不再互相覆盖 ——
|
|
497
|
+
//
|
|
498
|
+
// `Map.set` 用 name 当键时,同名的后一份会把前一份覆盖掉:实测本包有 2 份
|
|
499
|
+
// `char_010_chen.skel`(328066 / 114901)与 2 份 `char_010_chen.atlas`(3969 / 6703)。
|
|
500
|
+
// 一旦覆盖,entry 里写的名字可能指向**另一套**的内容 ⇒ atlas 的 region 在 skel
|
|
501
|
+
// 里一个都找不到 ⇒ 前端报 "Region not found in atlas"。
|
|
502
|
+
// 同名时按 `<stem>__<pathId><ext>` 区分(与 Python worker 完全同口径:pathID 天然唯一、
|
|
503
|
+
// 与对象遍历顺序无关,所以可复现)。
|
|
504
|
+
const allTextAssets = [...skel, ...atlas];
|
|
505
|
+
const nameCount = new Map();
|
|
506
|
+
for (const t of allTextAssets) nameCount.set(t.name, (nameCount.get(t.name) || 0) + 1);
|
|
507
|
+
const fileKeyOf = (t) => {
|
|
508
|
+
if (!t || !t.name) return null;
|
|
509
|
+
// 只出现一次 ⇒ 保持原名(零回归:单套资源的包产物与以前完全一样)
|
|
510
|
+
if ((nameCount.get(t.name) || 1) <= 1) return t.name;
|
|
511
|
+
return disambiguateKey(t.name, t.pathId);
|
|
512
|
+
};
|
|
513
|
+
// **只写选中的那一套**(未选中的同名副本不落盘):
|
|
514
|
+
// 产物因此是 4 个文件(skel + atlas + 贴图 + entry),而不是把包内所有文本资源都存一遍。
|
|
515
|
+
// 但文件名仍带 pathID —— 同名文件在包里确实存在,名字必须能区分,
|
|
516
|
+
// 否则前端拿到 `char_010_chen.atlas` 这种歧义名,无法自证用的是哪一份。
|
|
517
|
+
for (const t of [skelPick, atlasPick]) {
|
|
518
|
+
if (!t) continue;
|
|
519
|
+
const key = fileKeyOf(t);
|
|
520
|
+
if (key && !files.has(key)) files.set(key, new Blob([t.bytes], { type: 'application/octet-stream' }));
|
|
521
|
+
}
|
|
346
522
|
|
|
347
523
|
// —— 入口清单:字段名与前端约定一致(skeletonFile / atlasFile / textureFiles)——
|
|
348
524
|
const entryJson = {
|
|
349
|
-
skeletonFile: skelPick
|
|
350
|
-
atlasFile: atlasPick
|
|
525
|
+
skeletonFile: fileKeyOf(skelPick),
|
|
526
|
+
atlasFile: fileKeyOf(atlasPick),
|
|
351
527
|
textureFiles,
|
|
352
528
|
};
|
|
353
529
|
const entryBytes = new TextEncoder().encode(JSON.stringify(entryJson, null, 2));
|
|
@@ -553,7 +729,7 @@ function install() {
|
|
|
553
729
|
// node 侧返回解析入口:服务端解包直接从返回值取 canHandle/resolve,不经过宿主注册。
|
|
554
730
|
// __test 只给单测用(纯函数、无副作用、宿主不读它):parseAtlasPages 是闭包内私有函数,
|
|
555
731
|
// 没有它就无法为"带 BOM 的 atlas 首行"补单测(这正是 NIKKE 根 34 份带 BOM 的 atlas 丢第一页贴图的根因)。
|
|
556
|
-
return { canHandle, resolve, estimateMemory, probeContainer, analyze, PLUGIN_ID, __test: { parseAtlasPages } };
|
|
732
|
+
return { canHandle, resolve, estimateMemory, probeContainer, analyze, PLUGIN_ID, __test: { parseAtlasPages, parseAtlasPagesDetailed, pickAtlasForSkel, atlasRegions, findTextureForPage, disambiguateKey } };
|
|
557
733
|
}
|
|
558
734
|
|
|
559
735
|
// 执行安装。为什么不用 `export { canHandle }`:那会把文件变成 ESM,
|