@webwallgl/editor 3.0.0 → 3.0.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/index.d.ts CHANGED
@@ -175,6 +175,42 @@ export declare function setMdlClipEvents(
175
175
  events: ReadonlyArray<{ frame: number; name: string }>,
176
176
  ): Uint8Array | null;
177
177
 
178
+ /**
179
+ * 调换片段顺序(from 位置 → to 位置,整条记录连同 id 一起搬)。场景 `animationlayers` 按片段
180
+ * **id** 找片段(render/mdl-skin.js:174),全库 137 个多片段模型的 id 都不是「位置 + 1」,所以搬
181
+ * 记录不会动到层绑定;但 `animations[0]` 是「场景没给层表」时的回退(mdl-skin.js:315-326,全库
182
+ * 30 个实例在用),因此 0 号位两侧的移动一律拒绝。MDLA 记录流靠启发式定界,写回后自带重读自检,
183
+ * 读不回来时返回 null —— W18b
184
+ */
185
+ export declare function moveMdlClip(bytes: Uint8Array, from: number, to: number): Uint8Array | null;
186
+
187
+ /** 一条骨轨道:每帧 9 分量稠密 TRS(t0..2 / r0..2 弧度 / s0..2),帧数 = data.length / 9 —— W18a 对账 */
188
+ export type MdlClipTrack = { frameCount: number; data: Float32Array };
189
+
190
+ /**
191
+ * 读某片段里某根骨的轨道(编辑器文档视图;无 MDLA / 无该片段 / 无该骨轨道时 null)—— W18a 对账
192
+ */
193
+ export declare function mdlClipTrack(bytes: Uint8Array, animId: number, bone: number): MdlClipTrack | null;
194
+
195
+ /**
196
+ * 引擎解析出的绑定姿势 TRS 九分量(= computeSkinMatrices 的 base,与 invBindWorld 同源);
197
+ * 无骨骼 / 骨号越界 / 解析失败时 null —— W18a 对账
198
+ */
199
+ export declare function mdlBindTRS(bytes: Uint8Array, bone: number): Float32Array | null;
200
+
201
+ /**
202
+ * 片段 animId + 时间 time ⇒ 骨 bone 合成后的局部 TRS 九分量(采样索引 / 最短弧 / 无轨道回落绑定姿势
203
+ * 全部与引擎 render/mdl-skin.js 同口径)。editPose = 编辑器预览增量(t/r 相加、s 相乘);
204
+ * 无 MDLA / 无该片段 / 骨号越界 / time 非有限时 null。**只覆盖单条替换层**(多层加算栈不在范围)—— W18a 对账
205
+ */
206
+ export declare function mdlClipFrameTrs(
207
+ bytes: Uint8Array,
208
+ animId: number,
209
+ bone: number,
210
+ time: number,
211
+ editPose?: MdlBoneDelta | null,
212
+ ): Float32Array | null;
213
+
178
214
  /** MDLS 段的骨数(无 MDLS 段 / 网格表不可结构化读出时 null)—— P0 */
179
215
  export declare function mdlBoneCount(bytes: Uint8Array): number | null;
180
216
 
@@ -268,6 +304,21 @@ export declare function setMdlSkeleton(
268
304
  opts?: { tail?: "auto" | "keep" | "drop" },
269
305
  ): Uint8Array | null;
270
306
 
307
+ /**
308
+ * 读第 boneIndex 根骨的**静态装配姿势**局部矩阵(MDLS 尾表,引擎 `mdl.staticPoseTRS[i]` 的原始
309
+ * 16 分量)。只在「模型一条动画轨道都没有且尾表能解析出姿势」时有值 —— 这正是引擎
310
+ * `useStaticPose` 的判据(render/mdl-skin.js:310-311);其余情况(有动画 / 无尾表姿势 / 越界)
311
+ * 一律 null,因为引擎不读那张表 —— W18a
312
+ */
313
+ export declare function mdlRestPose(bytes: Uint8Array, boneIndex: number): Float32Array | null;
314
+
315
+ /**
316
+ * 写第 boneIndex 根骨的静态装配姿势局部矩阵(矩阵 16 分量且有限)。这是无动画模型摆姿势的
317
+ * **唯一**可见杠杆:改 MDLS 骨记录的局部矩阵会同时改 `bindTRS` 与 `invBindWorld`,两者同源时
318
+ * 蒙皮恒为单位变换,改了看不见。尾表长度不变,目标骨之外逐字节原样 —— W18a
319
+ */
320
+ export declare function setMdlRestPose(bytes: Uint8Array, boneIndex: number, matrix: ArrayLike<number>): Uint8Array | null;
321
+
271
322
  /** 蒙皮槽:每顶点 4 个骨号 + 4 个权重(非蒙皮 / 不可编辑时 null)—— P2 */
272
323
  export declare function mdlSkin(
273
324
  bytes: Uint8Array,
@@ -344,3 +395,23 @@ export declare function moveCameraPathClip(doc: unknown, from: number, to: numbe
344
395
 
345
396
  /** 队列删段(`doc.paths` 内就地 splice);下标非法返回 false */
346
397
  export declare function removeCameraPathClip(doc: unknown, index: number): boolean;
398
+
399
+ /**
400
+ * `.tex` 贴图缩略图(W16 地基,纯数据层):挑一个长边 ≤ `max`(缺省 64,夹取到
401
+ * `[1, 1024]`)的 mip 级,必要时从该级 CPU 重采样;返回 `{ width, height, rgba }`,
402
+ * `rgba` 长 `width * height * 4`。
403
+ *
404
+ * 内嵌 PNG/JPEG 的贴图(`freeImageFormat` 非 UNKNOWN)与视频贴图返回 null —— 它们没有
405
+ * RGBA 载荷,调用方改走 `decodeTexImageBitmap` 的 Blob 路径。坏字节 / 截断 / 非 .tex
406
+ * 一律返回 null,不抛。
407
+ */
408
+ export declare function texPreviewPixels(
409
+ bytes: Uint8Array,
410
+ max?: number,
411
+ ): { width: number; height: number; rgba: Uint8Array } | null;
412
+
413
+ /**
414
+ * 只读 TEXI 头部声明的贴图尺寸(内容尺寸,不是带 POT 填充的 mip 画布尺寸),不解像素。
415
+ * 解析失败返回 null,不抛。
416
+ */
417
+ export declare function texImageSize(bytes: Uint8Array): { width: number; height: number } | null;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@webwallgl/editor",
3
3
  "description": "WebWallGL editor API: editable scene graph, picking, frame capture, .pkg export (superset of webwallgl)",
4
- "version": "3.0.0",
4
+ "version": "3.0.2",
5
5
  "license": "MIT",
6
6
  "author": "oneincase <462534624@qq.com>",
7
7
  "repository": {
@@ -36,7 +36,7 @@
36
36
  "./package.json": "./package.json"
37
37
  },
38
38
  "peerDependencies": {
39
- "webwallgl": "3.0.0"
39
+ "webwallgl": "3.0.2"
40
40
  },
41
41
  "publishConfig": {
42
42
  "access": "public"
@@ -127,6 +127,12 @@ const SANDBOX_PARAM_NAMES = [
127
127
  // 否则脚本顶层若声明同名变量会 SyntaxError 报废整个脚本。
128
128
  "MediaPlaybackEvent"
129
129
  ];
130
+ function arrangeSandboxParams(head, tail) {
131
+ const moved = /* @__PURE__ */ new Set([...head, ...tail]);
132
+ const rest = SANDBOX_PARAM_NAMES.filter((n) => !moved.has(n));
133
+ return [...rest.slice(0, 2), ...head, ...rest.slice(2), ...tail];
134
+ }
135
+ arrangeSandboxParams(["thisLayer", "thisScene"], ["localStorage"]);
130
136
  const HWS = "[ \\t\\u00a0\\u3000\\ufeff]";
131
137
  const SANDBOX_PARAM_DECL_RE = new RegExp(
132
138
  `(^${HWS}*|;|\\})${HWS}*(?:var|let|const)${HWS}+(?=(?:${SANDBOX_PARAM_NAMES.join("|")})\\b)`,
@@ -291,7 +297,343 @@ function decodeName(buf, start, len) {
291
297
  return ascii(buf, start, len);
292
298
  }
293
299
  }
294
- const FIF = { UNKNOWN: -1, JPEG: 2, PNG: 13 };
300
+ const TEX_LIMITS = Object.freeze({
301
+ maxImages: 64,
302
+ maxMips: 32,
303
+ maxDim: 16384,
304
+ maxPixels: 1 << 27
305
+ });
306
+ function throwBound(what, value, min, max, where) {
307
+ throw new Error(`素材字段越界:${what}=${value}(允许 ${min}..${max})${where ? " @ " + where : ""}`);
308
+ }
309
+ function assertCount(what, value, max, where) {
310
+ if (!Number.isInteger(value) || value < 1 || value > max) throwBound(what, value, 1, max, where);
311
+ }
312
+ function assertDims(w, h, where) {
313
+ if (!Number.isInteger(w) || !Number.isInteger(h) || w < 1 || h < 1) {
314
+ throwBound("mip 尺寸", w + "x" + h, 1, TEX_LIMITS.maxDim, where);
315
+ }
316
+ if (w > TEX_LIMITS.maxDim || h > TEX_LIMITS.maxDim) {
317
+ throwBound("mip 单轴", w + "x" + h, 1, TEX_LIMITS.maxDim, where);
318
+ }
319
+ if (w * h > TEX_LIMITS.maxPixels) {
320
+ throwBound("mip 像素数", w * h, 1, TEX_LIMITS.maxPixels, where);
321
+ }
322
+ }
323
+ function assertPayload(p, n, bufLen, where) {
324
+ if (!Number.isInteger(n) || n < 0) throwBound("载荷长度", n, 0, Math.max(0, bufLen - p), where);
325
+ if (p + n > bufLen) throwBound("载荷越界", p + "+" + n, 0, bufLen, where);
326
+ }
327
+ function assertDecodeSize(n, where) {
328
+ const max = TEX_LIMITS.maxPixels * 4;
329
+ if (!Number.isInteger(n) || n < 0 || n > max) throwBound("解压目标尺寸", n, 0, max, where);
330
+ }
331
+ const TEXTURE_FORMATS = {
332
+ 0: "ARGB8888",
333
+ 1: "RGB888",
334
+ 2: "RGB565",
335
+ 4: "DXT5",
336
+ 6: "DXT3",
337
+ 7: "DXT1",
338
+ 8: "RG88",
339
+ 9: "R8",
340
+ 10: "RG1616f",
341
+ 11: "R16f",
342
+ 12: "BC7",
343
+ 13: "RGBa1010102",
344
+ 14: "RGBA16161616f",
345
+ 15: "RGB161616f"
346
+ };
347
+ const FIF = { UNKNOWN: -1, JPEG: 2, PNG: 13, GIF: 25, WEBP: 35, MP4: 35 };
348
+ function decodeMipLevel(tex, level) {
349
+ const image = tex.images[0];
350
+ if (!image || image.length === 0) throw new Error("无图像数据");
351
+ const k = Math.max(0, Math.min(image.length - 1, Math.floor(level) || 0));
352
+ const m = image[k];
353
+ if (tex.isVideo) return { width: m.width, height: m.height, video: m.data, level: k };
354
+ if (tex.freeImageFormat === FIF.PNG) return { width: m.width, height: m.height, png: m.data, level: k };
355
+ if (tex.freeImageFormat !== FIF.UNKNOWN) {
356
+ return { width: m.width, height: m.height, image: m.data, fif: tex.freeImageFormat, level: k };
357
+ }
358
+ const rgba = decodePixels(tex.format, m.data, m.width, m.height);
359
+ if (!tex.frames?.list?.length) {
360
+ const sw = Math.min(m.width, Math.max(1, Math.round((tex.width || m.width) / 2 ** k)));
361
+ const sh = Math.min(m.height, Math.max(1, Math.round((tex.height || m.height) / 2 ** k)));
362
+ if (m.width !== sw || m.height !== sh) {
363
+ return { width: sw, height: sh, rgba: cropRgba(rgba, m.width, m.height, sw, sh), level: k };
364
+ }
365
+ }
366
+ return { width: m.width, height: m.height, rgba, level: k };
367
+ }
368
+ function cropRgba(rgba, sw, sh, w, h) {
369
+ const out = new Uint8Array(w * h * 4);
370
+ for (let y = 0; y < h; y++) {
371
+ out.set(rgba.subarray(y * sw * 4, y * sw * 4 + w * 4), y * w * 4);
372
+ }
373
+ return out;
374
+ }
375
+ function decodePixels(format, data, w, h) {
376
+ switch (format) {
377
+ case 0:
378
+ return fromARGB8888(data, w, h);
379
+ case 1:
380
+ return fromRGB888(data, w, h);
381
+ case 2:
382
+ return fromRGB565(data, w, h);
383
+ case 4:
384
+ return decodeDXT5(data, w, h);
385
+ case 6:
386
+ return decodeDXT3(data, w, h);
387
+ case 7:
388
+ return decodeDXT1(data, w, h);
389
+ case 8:
390
+ return fromRG88(data, w, h);
391
+ case 9:
392
+ return fromR8(data, w, h);
393
+ case 12:
394
+ throw new Error("BC7 解码暂未实现(format=12)");
395
+ default:
396
+ throw new Error("未支持的纹理格式: " + (TEXTURE_FORMATS[format] || format));
397
+ }
398
+ }
399
+ function fromARGB8888(d, w, h) {
400
+ return new Uint8Array(d.buffer, d.byteOffset, w * h * 4).slice();
401
+ }
402
+ function fromRGB888(d, w, h) {
403
+ const out = new Uint8Array(w * h * 4);
404
+ for (let i = 0; i < w * h; i++) {
405
+ out[i * 4] = d[i * 3];
406
+ out[i * 4 + 1] = d[i * 3 + 1];
407
+ out[i * 4 + 2] = d[i * 3 + 2];
408
+ out[i * 4 + 3] = 255;
409
+ }
410
+ return out;
411
+ }
412
+ function fromRGB565(d, w, h) {
413
+ const out = new Uint8Array(w * h * 4);
414
+ for (let i = 0; i < w * h; i++) {
415
+ const c = d[i * 2] | d[i * 2 + 1] << 8;
416
+ const r = c >> 11 & 31;
417
+ const g2 = c >> 5 & 63;
418
+ const b = c & 31;
419
+ out[i * 4] = r << 3 | r >> 2;
420
+ out[i * 4 + 1] = g2 << 2 | g2 >> 4;
421
+ out[i * 4 + 2] = b << 3 | b >> 2;
422
+ out[i * 4 + 3] = 255;
423
+ }
424
+ return out;
425
+ }
426
+ function fromRG88(d, w, h) {
427
+ const out = new Uint8Array(w * h * 4);
428
+ for (let i = 0; i < w * h; i++) {
429
+ out[i * 4] = d[i * 2 + 1];
430
+ out[i * 4 + 1] = d[i * 2 + 1];
431
+ out[i * 4 + 2] = d[i * 2 + 1];
432
+ out[i * 4 + 3] = d[i * 2];
433
+ }
434
+ return out;
435
+ }
436
+ function fromR8(d, w, h) {
437
+ const out = new Uint8Array(w * h * 4);
438
+ for (let i = 0; i < w * h; i++) {
439
+ out[i * 4] = d[i];
440
+ out[i * 4 + 1] = d[i];
441
+ out[i * 4 + 2] = d[i];
442
+ out[i * 4 + 3] = 255;
443
+ }
444
+ return out;
445
+ }
446
+ function decodeDXT1(d, w, h) {
447
+ return decodeDxtCommon(d, w, h, true, 0);
448
+ }
449
+ function decodeDXT3(d, w, h) {
450
+ return decodeDxtCommon(d, w, h, false, 3);
451
+ }
452
+ function decodeDXT5(d, w, h) {
453
+ return decodeDxtCommon(d, w, h, false, 5);
454
+ }
455
+ function decodeDxtCommon(d, w, h, isDxt1, alphaMode) {
456
+ const rgba = new Uint8Array(w * h * 4);
457
+ const bytesPerBlock = isDxt1 ? 8 : 16;
458
+ const block = new Uint8Array(16);
459
+ let src = 0;
460
+ for (let y = 0; y < h; y += 4) {
461
+ for (let x = 0; x < w; x += 4) {
462
+ if (src + bytesPerBlock > d.length) return rgba;
463
+ block.set(d.subarray(src, src + bytesPerBlock));
464
+ src += bytesPerBlock;
465
+ const colors = colorCodes(block, isDxt1);
466
+ const indices = colorIndices(block, isDxt1);
467
+ const alphas = alphaMode === 3 ? dxt3Alphas(block) : alphaMode === 5 ? dxt5Alphas(block) : null;
468
+ for (let py = 0; py < 4; py++) {
469
+ for (let px = 0; px < 4; px++) {
470
+ const sx = x + px;
471
+ const sy = y + py;
472
+ if (sx >= w || sy >= h) continue;
473
+ const i = py * 4 + px;
474
+ const o = (sy * w + sx) * 4;
475
+ rgba[o] = colors[indices[i] * 4];
476
+ rgba[o + 1] = colors[indices[i] * 4 + 1];
477
+ rgba[o + 2] = colors[indices[i] * 4 + 2];
478
+ rgba[o + 3] = alphas !== null ? alphas[i] : colors[indices[i] * 4 + 3];
479
+ }
480
+ }
481
+ }
482
+ }
483
+ return rgba;
484
+ }
485
+ function unpack565(block, off) {
486
+ const value = block[off] | block[off + 1] << 8;
487
+ const red = value >> 11 & 31;
488
+ const green = value >> 5 & 63;
489
+ const blue = value & 31;
490
+ return [
491
+ value,
492
+ red << 3 | red >> 2,
493
+ green << 2 | green >> 4,
494
+ blue << 3 | blue >> 2
495
+ ];
496
+ }
497
+ function colorCodes(block, isDxt1) {
498
+ const colorOff = isDxt1 ? 0 : 8;
499
+ const a = unpack565(block, colorOff);
500
+ const b = unpack565(block, colorOff + 2);
501
+ const codes = new Uint8Array(16);
502
+ codes[0] = a[1];
503
+ codes[1] = a[2];
504
+ codes[2] = a[3];
505
+ codes[3] = 255;
506
+ codes[4] = b[1];
507
+ codes[5] = b[2];
508
+ codes[6] = b[3];
509
+ codes[7] = 255;
510
+ if (isDxt1 && a[0] <= b[0]) {
511
+ codes[8] = a[1] + b[1] >> 1;
512
+ codes[9] = a[2] + b[2] >> 1;
513
+ codes[10] = a[3] + b[3] >> 1;
514
+ codes[11] = 255;
515
+ codes[12] = 0;
516
+ codes[13] = 0;
517
+ codes[14] = 0;
518
+ codes[15] = 0;
519
+ } else {
520
+ codes[8] = Math.floor((2 * a[1] + b[1]) / 3);
521
+ codes[9] = Math.floor((2 * a[2] + b[2]) / 3);
522
+ codes[10] = Math.floor((2 * a[3] + b[3]) / 3);
523
+ codes[11] = 255;
524
+ codes[12] = Math.floor((a[1] + 2 * b[1]) / 3);
525
+ codes[13] = Math.floor((a[2] + 2 * b[2]) / 3);
526
+ codes[14] = Math.floor((a[3] + 2 * b[3]) / 3);
527
+ codes[15] = 255;
528
+ }
529
+ return codes;
530
+ }
531
+ function colorIndices(block, isDxt1) {
532
+ const base = isDxt1 ? 4 : 12;
533
+ const indices = new Uint8Array(16);
534
+ for (let i = 0; i < 4; i++) {
535
+ const packed = block[base + i];
536
+ indices[i * 4] = packed & 3;
537
+ indices[i * 4 + 1] = packed >> 2 & 3;
538
+ indices[i * 4 + 2] = packed >> 4 & 3;
539
+ indices[i * 4 + 3] = packed >> 6 & 3;
540
+ }
541
+ return indices;
542
+ }
543
+ function dxt3Alphas(block) {
544
+ const alphas = new Uint8Array(16);
545
+ for (let i = 0; i < 8; i++) {
546
+ const quant = block[i];
547
+ const lo = quant & 15;
548
+ const hi = quant & 240;
549
+ alphas[2 * i] = lo | lo << 4;
550
+ alphas[2 * i + 1] = hi | hi >> 4;
551
+ }
552
+ return alphas;
553
+ }
554
+ function dxt5Alphas(block) {
555
+ const a0 = block[0];
556
+ const a1 = block[1];
557
+ const codes = new Uint8Array(8);
558
+ codes[0] = a0;
559
+ codes[1] = a1;
560
+ if (a0 <= a1) {
561
+ for (let i = 1; i < 5; i++) codes[1 + i] = Math.floor(((5 - i) * a0 + i * a1) / 5);
562
+ codes[6] = 0;
563
+ codes[7] = 255;
564
+ } else {
565
+ for (let i = 1; i < 7; i++) codes[i + 1] = Math.floor(((7 - i) * a0 + i * a1) / 7);
566
+ }
567
+ const indices = new Uint8Array(16);
568
+ let p = 0;
569
+ for (let g2 = 0; g2 < 2; g2++) {
570
+ let value = 0;
571
+ for (let j = 0; j < 3; j++) value |= block[2 + g2 * 3 + j] << 8 * j;
572
+ for (let j = 0; j < 8; j++) indices[p++] = value >> 3 * j & 7;
573
+ }
574
+ const alphas = new Uint8Array(16);
575
+ for (let i = 0; i < 16; i++) alphas[i] = codes[indices[i]];
576
+ return alphas;
577
+ }
578
+ function lazyLz4Mip(width, height, raw, uncompressedSize) {
579
+ let cache;
580
+ const mip = { width, height, compression: 1, raw, uncompressedSize };
581
+ Object.defineProperty(mip, "data", {
582
+ enumerable: true,
583
+ configurable: true,
584
+ get() {
585
+ if (cache === void 0) cache = lz4Decompress(raw, uncompressedSize);
586
+ return cache;
587
+ }
588
+ });
589
+ return mip;
590
+ }
591
+ function lz4Decompress(src, outSize) {
592
+ assertDecodeSize(outSize, "LZ4");
593
+ const out = new Uint8Array(outSize);
594
+ let ip = 0;
595
+ let op = 0;
596
+ while (ip < src.length) {
597
+ const token = src[ip++];
598
+ let litLen = token >> 4;
599
+ if (litLen === 15) {
600
+ let b;
601
+ do {
602
+ b = src[ip++];
603
+ litLen += b;
604
+ } while (b === 255);
605
+ }
606
+ for (let i = 0; i < litLen; i++) out[op++] = src[ip++];
607
+ if (ip >= src.length) break;
608
+ const offset = src[ip] | src[ip + 1] << 8;
609
+ ip += 2;
610
+ let matchLen = 4 + (token & 15);
611
+ if ((token & 15) === 15) {
612
+ let b;
613
+ do {
614
+ b = src[ip++];
615
+ matchLen += b;
616
+ } while (b === 255);
617
+ }
618
+ const start = op - offset;
619
+ for (let i = 0; i < matchLen; i++) out[op++] = out[start + i];
620
+ }
621
+ return out;
622
+ }
623
+ function u32(buf, p) {
624
+ return (buf[p] | buf[p + 1] << 8 | buf[p + 2] << 16 | buf[p + 3] << 24) >>> 0;
625
+ }
626
+ function i32(buf, p) {
627
+ return u32(buf, p) | 0;
628
+ }
629
+ const f32View = new DataView(new ArrayBuffer(4));
630
+ function f32(buf, p) {
631
+ f32View.setUint8(0, buf[p]);
632
+ f32View.setUint8(1, buf[p + 1]);
633
+ f32View.setUint8(2, buf[p + 2]);
634
+ f32View.setUint8(3, buf[p + 3]);
635
+ return f32View.getFloat32(0, true);
636
+ }
295
637
  const enc$1 = new TextEncoder();
296
638
  const TEX_FLAG_CLAMP = 2;
297
639
  let Writer$1 = class Writer {
@@ -826,6 +1168,7 @@ function parseScene(sceneJson, project) {
826
1168
  }
827
1169
  const layers = objects.map((o, i) => {
828
1170
  const world = local[i];
1171
+ noteUnsupportedObjectFields(o);
829
1172
  const isPost = typeof o.image === "string" && (o.image.indexOf("models/util/projectlayer") === 0 || o.image.indexOf("models/util/fullscreenlayer") === 0);
830
1173
  let layerSize = parseVec2(o.size || "0 0");
831
1174
  let layerOrigin = world.origin;
@@ -1322,6 +1665,23 @@ function parseScene(sceneJson, project) {
1322
1665
  properties
1323
1666
  };
1324
1667
  }
1668
+ const G4_IGNORED_FIELDS = ["locktransforms", "ledsource", "blockalign", "particlesrc"];
1669
+ const g4Noted = /* @__PURE__ */ new Set();
1670
+ function noteUnsupportedObjectFields(o) {
1671
+ if (!o || typeof o !== "object") return null;
1672
+ for (const f of G4_IGNORED_FIELDS) {
1673
+ const v = o[f];
1674
+ if (v === void 0 || v === null || v === false || v === 0 || v === "") continue;
1675
+ if (!g4Noted.has(f)) {
1676
+ g4Noted.add(f);
1677
+ console.info(
1678
+ `[we-scene] 对象字段 ${f}=${JSON.stringify(v)} 本轮显式忽略(行为未实现,见 verify-defprojects 判据 10):${o.name || o.id || ""}`
1679
+ );
1680
+ }
1681
+ return f;
1682
+ }
1683
+ return null;
1684
+ }
1325
1685
  function parseJsonTolerant(text2) {
1326
1686
  try {
1327
1687
  return JSON.parse(text2);
@@ -1492,6 +1852,14 @@ const BUILTIN_MODELS = {
1492
1852
  "models/util/fullscreenlayer.json": { material: "materials/util/solidlayer.json" },
1493
1853
  "models/util/projectlayer.json": { material: "materials/util/solidlayer.json" }
1494
1854
  };
1855
+ for (const [path, model] of Object.entries({
1856
+ "models/util/solidlayer": "models/util/solidlayer.json",
1857
+ "models/util/composelayer": "models/util/composelayer.json",
1858
+ "models/util/fullscreenlayer": "models/util/fullscreenlayer.json",
1859
+ "models/util/projectlayer": "models/util/projectlayer.json"
1860
+ })) {
1861
+ BUILTIN_MODELS[path] = BUILTIN_MODELS[model];
1862
+ }
1495
1863
  const dec = new TextDecoder();
1496
1864
  const text$1 = (b) => dec.decode(b).replace(/^\uFEFF/, "");
1497
1865
  function json(b) {
@@ -3839,6 +4207,22 @@ function setMdlClipEvents(bytes, id, events) {
3839
4207
  a.events = events.map((e) => ({ frame: Math.min(Math.max(Math.round(e.frame), 0), a.frameCount), name: e.name })).sort((x, y) => x.frame - y.frame).map((e) => ({ time: e.frame / a.fps, json: old.get(`${e.frame}\0${e.name}`) ?? eventJson(e.frame, e.name) }));
3840
4208
  return writeMdlDoc(d);
3841
4209
  }
4210
+ function moveMdlClip(bytes, from, to) {
4211
+ const d = docOf(bytes);
4212
+ const anims = d && animsOf(d);
4213
+ if (!d || !anims || anims.length < 2) return null;
4214
+ if (!Number.isInteger(from) || !Number.isInteger(to)) return null;
4215
+ if (from < 0 || from >= anims.length || to < 0 || to >= anims.length || from === to) return null;
4216
+ if (from === 0 || to === 0) return null;
4217
+ const moved = anims.splice(from, 1)[0];
4218
+ anims.splice(to, 0, moved);
4219
+ const out = writeMdlDoc(d);
4220
+ const back = docOf(out);
4221
+ const after = back && animsOf(back) || [];
4222
+ const key = (a) => `${a.id}\0${a.name}\0${a.frameCount}\0${a.tracks.length}`;
4223
+ if (after.length !== anims.length || after.map(key).join("|") !== anims.map(key).join("|")) return null;
4224
+ return out;
4225
+ }
3842
4226
  function boneDeltaWeights(frames, frame, radius, loop) {
3843
4227
  const w = new Float64Array(Math.max(0, frames));
3844
4228
  const span = Math.max(1, frames - 1);
@@ -4194,6 +4578,61 @@ function setMdlSkeleton(bytes, bones, opts) {
4194
4578
  if (mode === "drop" || mode === "auto" && !sameCount) sec.tail = new Uint8Array(0);
4195
4579
  return writeMdlDoc(d);
4196
4580
  }
4581
+ const POSE_MATRIX_BYTES = 64;
4582
+ function staticPoseOffset(tail, count) {
4583
+ const pose = POSE_MATRIX_BYTES * count;
4584
+ const group2 = 76 * count;
4585
+ const perm = 1 + 4 * count;
4586
+ const cand = [];
4587
+ const byLayout = tail.length - perm - group2 - 9 - pose;
4588
+ if (byLayout >= 0) cand.push(byLayout);
4589
+ const scanEnd = Math.min(32, tail.length - pose);
4590
+ for (let off = 0; off < scanEnd; off++) if (!cand.includes(off)) cand.push(off);
4591
+ const dv = dvOf(tail);
4592
+ const okMatrix = (at) => {
4593
+ const m = (k) => dv.getFloat32(at + k * 4, true);
4594
+ if (!(Number.isFinite(m(12)) && Number.isFinite(m(13)))) return false;
4595
+ if (Math.abs(m(15) - 1) > 1e-3) return false;
4596
+ if (Math.abs(m(3)) > 1e-3 || Math.abs(m(7)) > 1e-3 || Math.abs(m(11)) > 1e-3) return false;
4597
+ const len = (a, b, c) => Math.hypot(m(a), m(b), m(c));
4598
+ return Math.abs(len(0, 1, 2) - 1) < 0.05 && Math.abs(len(4, 5, 6) - 1) < 0.05 && Math.abs(len(8, 9, 10) - 1) < 0.05;
4599
+ };
4600
+ for (const start of cand) {
4601
+ if (start < 0 || start + pose > tail.length) continue;
4602
+ let ok = true;
4603
+ for (let i = 0; i < count && ok; i++) if (!okMatrix(start + i * POSE_MATRIX_BYTES)) ok = false;
4604
+ if (ok) return start;
4605
+ }
4606
+ return -1;
4607
+ }
4608
+ function restPoseAt(bytes) {
4609
+ const d = docOf(bytes);
4610
+ const sec = d?.sections?.find((s) => s.type === "MDLS");
4611
+ if (!d || !sec || sec.raw || !sec.bones || !sec.tail || sec.bones.length === 0) return null;
4612
+ const anims = d.sections?.find((s) => s.type === "MDLA");
4613
+ if (anims && (anims.raw || (anims.anims?.length ?? 0) > 0)) return null;
4614
+ const start = staticPoseOffset(sec.tail, sec.bones.length);
4615
+ return start < 0 ? null : { d, sec, count: sec.bones.length, start };
4616
+ }
4617
+ function mdlRestPose(bytes, boneIndex) {
4618
+ const hit = restPoseAt(bytes);
4619
+ if (!hit || !Number.isInteger(boneIndex) || boneIndex < 0 || boneIndex >= hit.count) return null;
4620
+ const dv = dvOf(hit.sec.tail);
4621
+ const at = hit.start + boneIndex * POSE_MATRIX_BYTES;
4622
+ const out = new Float32Array(16);
4623
+ for (let k = 0; k < 16; k++) out[k] = dv.getFloat32(at + k * 4, true);
4624
+ return out;
4625
+ }
4626
+ function setMdlRestPose(bytes, boneIndex, matrix) {
4627
+ const hit = restPoseAt(bytes);
4628
+ if (!hit || !Number.isInteger(boneIndex) || boneIndex < 0 || boneIndex >= hit.count) return null;
4629
+ if (!boneMatrixOk(matrix)) return null;
4630
+ hit.sec.tail = Uint8Array.from(hit.sec.tail);
4631
+ const dv = dvOf(hit.sec.tail);
4632
+ const at = hit.start + boneIndex * POSE_MATRIX_BYTES;
4633
+ for (let k = 0; k < 16; k++) dv.setFloat32(at + k * 4, matrix[k], true);
4634
+ return writeMdlDoc(hit.d);
4635
+ }
4197
4636
  function mdlSkin(bytes, meshIndex) {
4198
4637
  const hit = meshOf(bytes, meshIndex);
4199
4638
  if (!hit || !hit.c.stride) return null;
@@ -4272,6 +4711,108 @@ function aabbFromVertexData(data, stride, vertexCount) {
4272
4711
  }
4273
4712
  return a;
4274
4713
  }
4714
+ function clipTrackOf(anim, bone, boneCount) {
4715
+ if (boneCount > 0 && anim.tracks.length === boneCount && anim.tracks[bone]) return anim.tracks[bone];
4716
+ return anim.tracks.find((t) => t.boneId === bone) ?? null;
4717
+ }
4718
+ function mdlClipTrack(bytes, animId, bone) {
4719
+ const d = docOf(bytes);
4720
+ const anims = d && animsOf(d);
4721
+ if (!d || !anims || !Number.isInteger(bone) || bone < 0) return null;
4722
+ const anim = anims.find((a) => a.id === animId);
4723
+ const track = anim && clipTrackOf(anim, bone, bonesOf(d)?.length ?? 0);
4724
+ if (!anim || !track) return null;
4725
+ return { frameCount: Math.floor(track.data.length / 9), data: Float32Array.from(track.data) };
4726
+ }
4727
+ function mdlBindTRS(bytes, bone) {
4728
+ if (!Number.isInteger(bone) || bone < 0) return null;
4729
+ let m = null;
4730
+ try {
4731
+ m = parseMDL(bytes);
4732
+ } catch {
4733
+ return null;
4734
+ }
4735
+ const b = m && m.bindTRS && m.bindTRS[bone];
4736
+ return b && b.length >= 9 ? Float32Array.from(b.subarray(0, 9)) : null;
4737
+ }
4738
+ function clipFrameIndices(mode, fps, time, n) {
4739
+ const frame = time * fps;
4740
+ if (n <= 1) return { f0: 0, f1: 0, t: 0 };
4741
+ if (mode === "loop") {
4742
+ const span = Math.max(1, n - 1);
4743
+ const w = (frame % span + span) % span;
4744
+ const f02 = Math.floor(w);
4745
+ return { f0: f02, f1: (f02 + 1) % n, t: w - f02 };
4746
+ }
4747
+ if (mode === "mirror") {
4748
+ const span = Math.max(1, n - 1);
4749
+ const period = span * 2;
4750
+ let w = (frame % period + period) % period;
4751
+ if (w > span) w = period - w;
4752
+ const f02 = Math.floor(w);
4753
+ return { f0: f02, f1: Math.min(f02 + 1, n - 1), t: w - f02 };
4754
+ }
4755
+ const c = Math.min(Math.max(frame, 0), n - 1);
4756
+ const f0 = Math.floor(c);
4757
+ return { f0, f1: Math.min(f0 + 1, n - 1), t: c - f0 };
4758
+ }
4759
+ function sampleClipTrs(data, mode, fps, time, out) {
4760
+ const n = Math.floor(data.length / 9);
4761
+ if (n === 0) {
4762
+ out[0] = 0;
4763
+ out[1] = 0;
4764
+ out[2] = 0;
4765
+ out[3] = 0;
4766
+ out[4] = 0;
4767
+ out[5] = 0;
4768
+ out[6] = 1;
4769
+ out[7] = 1;
4770
+ out[8] = 1;
4771
+ return out;
4772
+ }
4773
+ const { f0, f1, t } = clipFrameIndices(mode, fps, time, n);
4774
+ const a = f0 * 9;
4775
+ const b = f1 * 9;
4776
+ const it = 1 - t;
4777
+ for (let i = 0; i < 9; i++) {
4778
+ if (i >= 3 && i <= 5) {
4779
+ let d = data[b + i] - data[a + i];
4780
+ if (d > Math.PI) d -= 2 * Math.PI;
4781
+ else if (d < -Math.PI) d += 2 * Math.PI;
4782
+ out[i] = data[a + i] + d * t;
4783
+ } else {
4784
+ out[i] = data[a + i] * it + data[b + i] * t;
4785
+ }
4786
+ }
4787
+ return out;
4788
+ }
4789
+ function mdlClipFrameTrs(bytes, animId, bone, time, editPose) {
4790
+ const d = docOf(bytes);
4791
+ const anims = d && animsOf(d);
4792
+ if (!d || !anims || !Number.isFinite(time)) return null;
4793
+ const anim = anims.find((a) => a.id === animId);
4794
+ if (!anim) return null;
4795
+ const boneCount = bonesOf(d)?.length ?? 0;
4796
+ if (!Number.isInteger(bone) || bone < 0 || bone >= boneCount) return null;
4797
+ const out = new Float32Array(9);
4798
+ const track = clipTrackOf(anim, bone, boneCount);
4799
+ if (!track || track.data.length < 9) {
4800
+ const base = mdlBindTRS(bytes, bone);
4801
+ if (!base) return null;
4802
+ out.set(base);
4803
+ } else {
4804
+ sampleClipTrs(track.data, text(anim.mode), anim.fps, time, out);
4805
+ }
4806
+ if (editPose) {
4807
+ const t = editPose.t ?? [0, 0, 0];
4808
+ const r = editPose.r ?? [0, 0, 0];
4809
+ const s = editPose.s ?? [1, 1, 1];
4810
+ for (let k = 0; k < 3; k++) out[k] += t[k] ?? 0;
4811
+ for (let k = 0; k < 3; k++) out[3 + k] += r[k] ?? 0;
4812
+ for (let k = 0; k < 3; k++) out[6 + k] *= s[k] ?? 1;
4813
+ }
4814
+ return out;
4815
+ }
4275
4816
  const TEXT_EM_SCALE = 4;
4276
4817
  const SYSTEM_FONT_FAMILIES = {
4277
4818
  systemfont_segoe: "'Segoe UI', 'Helvetica Neue', Arial, sans-serif",
@@ -4876,8 +5417,12 @@ const vec3 = (v, dflt) => {
4876
5417
  return [...dflt];
4877
5418
  };
4878
5419
  const num = (v, dflt) => {
4879
- const n = Number(v);
4880
- return Number.isFinite(n) ? n : dflt;
5420
+ if (typeof v === "number") return Number.isFinite(v) ? v : dflt;
5421
+ if (typeof v === "string" && v.trim() !== "") {
5422
+ const n = Number(v);
5423
+ return Number.isFinite(n) ? n : dflt;
5424
+ }
5425
+ return dflt;
4881
5426
  };
4882
5427
  const CHANNELS = ["eye", "center", "up", "fov", "zoom"];
4883
5428
  function describeCameraPath(doc, queueMode) {
@@ -5048,6 +5593,351 @@ function removeCameraPathClip(doc, index) {
5048
5593
  list.splice(index, 1);
5049
5594
  return true;
5050
5595
  }
5596
+ const FREE_IMAGE_MAGIC = {
5597
+ [FIF.PNG]: [137, 80, 78, 71],
5598
+ [FIF.JPEG]: [255, 216, 255],
5599
+ [FIF.GIF]: [71, 73, 70],
5600
+ [FIF.WEBP]: [82, 73, 70, 70]
5601
+ };
5602
+ function parseTex(buf, opts = {}) {
5603
+ const name = typeof opts.name === "string" && opts.name ? opts.name : "";
5604
+ const at = (suffix) => name ? name + (suffix || "") : suffix || "tex";
5605
+ let p = 0;
5606
+ const magic1 = asciiTex(buf, p, 9);
5607
+ p += 9;
5608
+ if (magic1 === "TEXV0004\0") return parseTexV4(buf, { name });
5609
+ if (magic1 !== "TEXV0005\0") throw new Error("不是 .tex 文件: " + magic1);
5610
+ const magic2 = asciiTex(buf, p, 9);
5611
+ p += 9;
5612
+ if (magic2 !== "TEXI0001\0") throw new Error("未知 TEXI 子容器: " + magic2);
5613
+ const format = u32(buf, p);
5614
+ p += 4;
5615
+ const flags = u32(buf, p);
5616
+ p += 4;
5617
+ const textureWidth = u32(buf, p);
5618
+ p += 4;
5619
+ const textureHeight = u32(buf, p);
5620
+ p += 4;
5621
+ let containerOffset = 46;
5622
+ for (let off = 34; off <= 64; off++) {
5623
+ const m = asciiTex(buf, off, 9);
5624
+ if (m === "TEXB0001\0" || m === "TEXB0002\0" || m === "TEXB0003\0" || m === "TEXB0004\0") {
5625
+ containerOffset = off;
5626
+ break;
5627
+ }
5628
+ }
5629
+ const width = u32(buf, containerOffset - 12);
5630
+ const height = u32(buf, containerOffset - 8);
5631
+ p = containerOffset;
5632
+ const containerMagic = asciiTex(buf, p, 9);
5633
+ p += 9;
5634
+ const imageCount = u32(buf, p);
5635
+ p += 4;
5636
+ assertCount("imageCount", imageCount, TEX_LIMITS.maxImages, at(""));
5637
+ let freeImageFormat = FIF.UNKNOWN;
5638
+ let containerVersion = 0;
5639
+ if (containerMagic === "TEXB0004\0") {
5640
+ freeImageFormat = u32(buf, p) | 0;
5641
+ p += 4;
5642
+ const hasMipExtension = u32(buf, p);
5643
+ p += 4;
5644
+ if (freeImageFormat === FIF.UNKNOWN && hasMipExtension === 1) freeImageFormat = FIF.MP4;
5645
+ containerVersion = hasMipExtension === 1 ? 4 : 3;
5646
+ } else if (containerMagic === "TEXB0003\0") {
5647
+ freeImageFormat = u32(buf, p) | 0;
5648
+ p += 4;
5649
+ containerVersion = 3;
5650
+ } else if (containerMagic === "TEXB0002\0") {
5651
+ containerVersion = 2;
5652
+ } else if (containerMagic === "TEXB0001\0") {
5653
+ containerVersion = 1;
5654
+ } else {
5655
+ throw new Error("未知 TEXB 容器: " + containerMagic);
5656
+ }
5657
+ const isVideo = (flags & 32) !== 0 || freeImageFormat === FIF.MP4;
5658
+ const images = [];
5659
+ for (let i = 0; i < imageCount; i++) {
5660
+ const mipCount = u32(buf, p);
5661
+ p += 4;
5662
+ assertCount("mipCount", mipCount, TEX_LIMITS.maxMips, at("[img" + i + "]"));
5663
+ const mips = [];
5664
+ for (let m = 0; m < mipCount; m++) {
5665
+ if (containerVersion === 4) {
5666
+ p += 8;
5667
+ while (p < buf.length && buf[p] !== 0) p++;
5668
+ p += 1;
5669
+ p += 4;
5670
+ }
5671
+ const mw = u32(buf, p);
5672
+ p += 4;
5673
+ const mh = u32(buf, p);
5674
+ p += 4;
5675
+ assertDims(mw, mh, at("[img" + i + "/mip" + m + "]"));
5676
+ let mipExtra = 0;
5677
+ if (flags & 64 && containerVersion >= 2 && !isVideo) {
5678
+ const cmpA = u32(buf, p);
5679
+ const sizeA = i32(buf, p + 8);
5680
+ const cmpB = u32(buf, p + 4);
5681
+ const sizeB = i32(buf, p + 12);
5682
+ const startsAt = (off) => {
5683
+ if (!(sizeB >= 0)) return false;
5684
+ if (off + sizeB > buf.length) return false;
5685
+ const fm = FREE_IMAGE_MAGIC[freeImageFormat];
5686
+ if (fm) return fm.every((v, k) => buf[off + k] === v);
5687
+ return true;
5688
+ };
5689
+ const okA = cmpA <= 1 && sizeA >= 0 && p + 12 + sizeA <= buf.length;
5690
+ const okB = cmpB <= 1 && startsAt(p + 16);
5691
+ if (!okA && okB) mipExtra = 4;
5692
+ }
5693
+ p += mipExtra;
5694
+ let compression = 0;
5695
+ let uncompressedSize = 0;
5696
+ if (containerVersion >= 2) {
5697
+ compression = u32(buf, p);
5698
+ p += 4;
5699
+ uncompressedSize = i32(buf, p);
5700
+ p += 4;
5701
+ }
5702
+ const compressedSize = i32(buf, p);
5703
+ p += 4;
5704
+ if (isVideo) {
5705
+ mips.push({ width: mw, height: mh, compression: 0, data: buf.subarray(p) });
5706
+ p = buf.length;
5707
+ continue;
5708
+ }
5709
+ const mipWhere = at("[img" + i + "/mip" + m + "]");
5710
+ if (containerVersion >= 2 && compression === 1) assertDecodeSize(uncompressedSize, mipWhere);
5711
+ assertPayload(p, compressedSize, buf.length, mipWhere);
5712
+ if (compression === 0) uncompressedSize = compressedSize;
5713
+ const raw = buf.subarray(p, p + compressedSize);
5714
+ p += compressedSize;
5715
+ mips.push(
5716
+ compression === 1 ? lazyLz4Mip(mw, mh, raw, uncompressedSize) : { width: mw, height: mh, compression, data: raw }
5717
+ );
5718
+ }
5719
+ images.push(mips);
5720
+ }
5721
+ const mip0 = images[0] && images[0][0];
5722
+ const atlasW = mip0 ? mip0.width : textureWidth;
5723
+ const atlasH = mip0 ? mip0.height : textureHeight;
5724
+ let frames = null;
5725
+ if (p + 13 <= buf.length) {
5726
+ const texsMagic = asciiTex(buf, p, 9);
5727
+ if (texsMagic.indexOf("TEXS") === 0) {
5728
+ let q = p + 9;
5729
+ const frameCount = u32(buf, q);
5730
+ q += 4;
5731
+ let frameW = 0;
5732
+ let frameH = 0;
5733
+ if (texsMagic === "TEXS0003\0") {
5734
+ frameW = u32(buf, q);
5735
+ q += 4;
5736
+ frameH = u32(buf, q);
5737
+ q += 4;
5738
+ }
5739
+ const rem = buf.length - q;
5740
+ const stride = frameCount > 0 && rem > 0 && rem % frameCount === 0 ? rem / frameCount : 0;
5741
+ if (frameCount > 0 && frameCount <= 4096 && stride >= 32) {
5742
+ const list = [];
5743
+ for (let i = 0; i < frameCount; i++) {
5744
+ const off = q + i * stride;
5745
+ if (off + 32 > buf.length) break;
5746
+ const ox = f32(buf, off + 8);
5747
+ const oy = f32(buf, off + 12);
5748
+ const uX = f32(buf, off + 16);
5749
+ const uY = f32(buf, off + 20);
5750
+ const vX = f32(buf, off + 24);
5751
+ const vY = f32(buf, off + 28);
5752
+ const uLen = Math.hypot(uX, uY);
5753
+ const vLen = Math.hypot(vX, vY);
5754
+ list.push({
5755
+ imageId: f32(buf, off) | 0,
5756
+ duration: f32(buf, off + 4),
5757
+ x: ox,
5758
+ y: oy,
5759
+ width: (uLen > 1e-30 ? uLen : 0) || frameW,
5760
+ height: (vLen > 1e-30 ? vLen : 0) || frameH,
5761
+ // 仿射基(像素单位)。rotated=true 时 u 不再沿 +x,渲染端必须走
5762
+ // 「origin + s·uDir + t·vDir」而不能退化成矩形。
5763
+ uDir: [uX, uY],
5764
+ vDir: [vX, vY],
5765
+ rotated: Math.abs(uY) > 0.5 || Math.abs(vX) > 0.5
5766
+ });
5767
+ }
5768
+ const sane = list.length === frameCount && list.every((f) => {
5769
+ if (!(f.width > 0 && f.height > 0)) return false;
5770
+ const xs = [f.x, f.x + f.uDir[0], f.x + f.vDir[0], f.x + f.uDir[0] + f.vDir[0]];
5771
+ const ys = [f.y, f.y + f.uDir[1], f.y + f.vDir[1], f.y + f.uDir[1] + f.vDir[1]];
5772
+ return Math.min(...xs) >= -1 && Math.max(...xs) <= atlasW + 1 && Math.min(...ys) >= -1 && Math.max(...ys) <= atlasH + 1;
5773
+ });
5774
+ if (sane) {
5775
+ frames = {
5776
+ magic: texsMagic,
5777
+ frameWidth: frameW,
5778
+ frameHeight: frameH,
5779
+ // 帧矩形的归一化分母(渲染/粒子端都得用这个,别再用 textureWidth)
5780
+ atlasWidth: atlasW,
5781
+ atlasHeight: atlasH,
5782
+ list
5783
+ };
5784
+ }
5785
+ }
5786
+ }
5787
+ }
5788
+ return {
5789
+ format,
5790
+ formatName: TEXTURE_FORMATS[format] || String(format),
5791
+ flags,
5792
+ textureWidth,
5793
+ textureHeight,
5794
+ width,
5795
+ height,
5796
+ freeImageFormat,
5797
+ containerMagic,
5798
+ containerVersion,
5799
+ isVideo,
5800
+ images,
5801
+ // 序列帧表;无 TEXS 段时为 null
5802
+ frames
5803
+ };
5804
+ }
5805
+ function parseTexV4(buf, opts = {}) {
5806
+ const name = typeof opts.name === "string" && opts.name ? opts.name : "";
5807
+ const at = (suffix) => name ? name + (suffix || "") : suffix || "tex";
5808
+ let p = 9;
5809
+ const format = u32(buf, p);
5810
+ p += 4;
5811
+ const flags = u32(buf, p);
5812
+ p += 4;
5813
+ const textureWidth = u32(buf, p);
5814
+ p += 4;
5815
+ const textureHeight = u32(buf, p);
5816
+ p += 4;
5817
+ const width = u32(buf, p);
5818
+ p += 4;
5819
+ const height = u32(buf, p);
5820
+ p += 4;
5821
+ const imageCount = u32(buf, p);
5822
+ p += 4;
5823
+ assertCount("imageCount", imageCount, TEX_LIMITS.maxImages, at(""));
5824
+ const images = [];
5825
+ for (let i = 0; i < imageCount; i++) {
5826
+ if (p + 12 > buf.length) throw new Error("TEXV0004 mip 记录越界 @" + i);
5827
+ const mw = u32(buf, p);
5828
+ p += 4;
5829
+ const mh = u32(buf, p);
5830
+ p += 4;
5831
+ const size = u32(buf, p);
5832
+ p += 4;
5833
+ assertDims(mw, mh, at("[img" + i + "]"));
5834
+ if (p + size > buf.length) throw new Error("TEXV0004 载荷越界 @" + i);
5835
+ images.push([{ width: mw, height: mh, compression: 0, data: buf.subarray(p, p + size) }]);
5836
+ p += size;
5837
+ }
5838
+ return {
5839
+ format,
5840
+ formatName: TEXTURE_FORMATS[format] || String(format),
5841
+ flags,
5842
+ textureWidth,
5843
+ textureHeight,
5844
+ width,
5845
+ height,
5846
+ freeImageFormat: FIF.UNKNOWN,
5847
+ containerMagic: "TEXV0004",
5848
+ containerVersion: 0,
5849
+ isVideo: false,
5850
+ images,
5851
+ frames: null
5852
+ };
5853
+ }
5854
+ function asciiTex(buf, start, len) {
5855
+ let s = "";
5856
+ for (let i = start; i < start + len; i++) s += String.fromCharCode(buf[i]);
5857
+ return s;
5858
+ }
5859
+ function resampleRgba(src, w2, h2) {
5860
+ const { width: w, height: h, rgba } = src;
5861
+ const W = Math.max(1, Math.round(w2));
5862
+ const H = Math.max(1, Math.round(h2));
5863
+ if (W === w && H === h) return src;
5864
+ const out = new Uint8Array(W * H * 4);
5865
+ const sx = w / W;
5866
+ const sy = h / H;
5867
+ for (let y = 0; y < H; y++) {
5868
+ const fy = Math.min(h - 1, Math.max(0, (y + 0.5) * sy - 0.5));
5869
+ const y0 = Math.floor(fy);
5870
+ const y1 = Math.min(h - 1, y0 + 1);
5871
+ const ty = fy - y0;
5872
+ for (let x = 0; x < W; x++) {
5873
+ const fx = Math.min(w - 1, Math.max(0, (x + 0.5) * sx - 0.5));
5874
+ const x0 = Math.floor(fx);
5875
+ const x1 = Math.min(w - 1, x0 + 1);
5876
+ const tx = fx - x0;
5877
+ const o00 = (y0 * w + x0) * 4;
5878
+ const o10 = (y0 * w + x1) * 4;
5879
+ const o01 = (y1 * w + x0) * 4;
5880
+ const o11 = (y1 * w + x1) * 4;
5881
+ const o = (y * W + x) * 4;
5882
+ for (let c = 0; c < 4; c++) {
5883
+ const a = rgba[o00 + c] * (1 - tx) + rgba[o10 + c] * tx;
5884
+ const b = rgba[o01 + c] * (1 - tx) + rgba[o11 + c] * tx;
5885
+ out[o + c] = Math.round(a * (1 - ty) + b * ty);
5886
+ }
5887
+ }
5888
+ }
5889
+ return { width: W, height: H, rgba: out };
5890
+ }
5891
+ const TEX_PREVIEW_MAX = 64;
5892
+ const TEX_PREVIEW_CAP = 1024;
5893
+ function clampPreviewMax(max) {
5894
+ const n = typeof max === "number" && Number.isFinite(max) ? Math.floor(max) : TEX_PREVIEW_MAX;
5895
+ return Math.max(1, Math.min(TEX_PREVIEW_CAP, n));
5896
+ }
5897
+ function texImageSize(bytes) {
5898
+ try {
5899
+ const tex = parseTex(bytes);
5900
+ const w = Number(tex.width);
5901
+ const h = Number(tex.height);
5902
+ return w > 0 && h > 0 ? { width: w, height: h } : null;
5903
+ } catch {
5904
+ return null;
5905
+ }
5906
+ }
5907
+ function texPreviewPixels(bytes, max = TEX_PREVIEW_MAX) {
5908
+ try {
5909
+ const tex = parseTex(bytes);
5910
+ if (tex.isVideo) return null;
5911
+ const mipCount = tex.images?.[0]?.length ?? 0;
5912
+ if (mipCount <= 0) return null;
5913
+ const limit = clampPreviewMax(max);
5914
+ let first = null;
5915
+ let picked = null;
5916
+ for (let k = 0; k < mipCount; k++) {
5917
+ const d = decodeMipLevel(tex, k);
5918
+ const rgba = "rgba" in d ? d.rgba : void 0;
5919
+ if (!rgba) return null;
5920
+ const cur = { width: Number(d.width), height: Number(d.height), rgba };
5921
+ if (!first) first = cur;
5922
+ if (Math.max(d.width, d.height) <= limit) {
5923
+ picked = cur;
5924
+ break;
5925
+ }
5926
+ }
5927
+ const src = picked ?? first;
5928
+ if (!src) return null;
5929
+ const long = Math.max(src.width, src.height);
5930
+ if (long <= limit) return src;
5931
+ const scale = limit / long;
5932
+ return resampleRgba(
5933
+ src,
5934
+ Math.max(1, Math.round(src.width * scale)),
5935
+ Math.max(1, Math.round(src.height * scale))
5936
+ );
5937
+ } catch {
5938
+ return null;
5939
+ }
5940
+ }
5051
5941
  export {
5052
5942
  CLIP_MODES,
5053
5943
  SYSTEM_FONT_FAMILIES,
@@ -5063,17 +5953,22 @@ export {
5063
5953
  editorOf,
5064
5954
  encodeMdl,
5065
5955
  kindOfCameraPathDoc,
5956
+ mdlBindTRS,
5066
5957
  mdlBoneCount,
5067
5958
  mdlBoneMeta,
5068
5959
  mdlBones,
5960
+ mdlClipFrameTrs,
5961
+ mdlClipTrack,
5069
5962
  mdlClips,
5070
5963
  mdlMeshCount,
5071
5964
  mdlMeshIndices,
5072
5965
  mdlMeshInfo,
5073
5966
  mdlMeshMaterials,
5074
5967
  mdlMeshPositions,
5968
+ mdlRestPose,
5075
5969
  mdlSkin,
5076
5970
  moveCameraPathClip,
5971
+ moveMdlClip,
5077
5972
  normalizeQueueMode,
5078
5973
  readQueueMode,
5079
5974
  removeCameraPathClip,
@@ -5089,9 +5984,12 @@ export {
5089
5984
  setMdlClipMeta,
5090
5985
  setMdlParts,
5091
5986
  setMdlPositions,
5987
+ setMdlRestPose,
5092
5988
  setMdlSkeleton,
5093
5989
  setMdlTopology,
5094
5990
  setMdlVertexZ,
5095
5991
  setMdlWeights,
5992
+ texImageSize,
5993
+ texPreviewPixels,
5096
5994
  writeQueueMode
5097
5995
  };