Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ae8a68e98f | ||
|
|
8e8cc0475b |
@@ -101,7 +101,7 @@ opendance-web/
|
|||||||
│ ├── admin/ # React 管理端
|
│ ├── admin/ # React 管理端
|
||||||
│ │ ├── src/components/ModelReviewPanel.tsx # 模型审核面板 + 骨骼详细界面
|
│ │ ├── src/components/ModelReviewPanel.tsx # 模型审核面板 + 骨骼详细界面
|
||||||
│ │ └── src/lib/bone-tree.ts # 骨骼树过滤(搜索保留祖先链)
|
│ │ └── src/lib/bone-tree.ts # 骨骼树过滤(搜索保留祖先链)
|
||||||
│ ├── test/ # 单元测试 + 端到端冒烟(12 文件 / 193 项)
|
│ ├── test/ # 单元测试 + 端到端冒烟(14 文件 / 211 项)
|
||||||
│ │ # mmd-vmd 31 / model-archive 24 / model-inspector 25
|
│ │ # mmd-vmd 31 / model-archive 24 / model-inspector 25
|
||||||
│ │ # / model-thumbnail 8 / admin-bone-filter 8
|
│ │ # / model-thumbnail 8 / admin-bone-filter 8
|
||||||
│ │ # / preprocess 25 / init-env 16 / admin-port 16
|
│ │ # / preprocess 25 / init-env 16 / admin-port 16
|
||||||
@@ -112,6 +112,7 @@ opendance-web/
|
|||||||
├── deploy/ # ★ 业务服务部署(不含 MySQL、不含反代)
|
├── deploy/ # ★ 业务服务部署(不含 MySQL、不含反代)
|
||||||
│ ├── init-env.sh # ★ 首次启动的.env 交互引导(install.sh 缺.env 时调用)
|
│ ├── init-env.sh # ★ 首次启动的.env 交互引导(install.sh 缺.env 时调用)
|
||||||
│ ├── env-lib.sh # ★ deploy/.env 语法预检(被 install.sh / ctl.sh 引用)
|
│ ├── env-lib.sh # ★ deploy/.env 语法预检(被 install.sh / ctl.sh 引用)
|
||||||
|
│ ├── assimp-lib.sh # ★ Assimp 定位与安装(install.sh / check-access.sh 引用)
|
||||||
│ ├── install.sh # 一键部署(systemd / OpenRC / SysVinit / 后台进程)
|
│ ├── install.sh # 一键部署(systemd / OpenRC / SysVinit / 后台进程)
|
||||||
│ ├── uninstall.sh
|
│ ├── uninstall.sh
|
||||||
│ ├── ctl.sh # 无 init 系统时的进程控制
|
│ ├── ctl.sh # 无 init 系统时的进程控制
|
||||||
@@ -334,13 +335,22 @@ POST /api/models ─┬─.glb /.vrm /.gltf ─▶ importModelFile() 直接导
|
|||||||
T-pose 角色图;真实 10MB VRM(83 关节、5307 顶点)单张耗时 60-121ms(中位 79ms)。
|
T-pose 角色图;真实 10MB VRM(83 关节、5307 顶点)单张耗时 60-121ms(中位 79ms)。
|
||||||
- 管理端 3D 预览放开到 `vrm/glb/gltf`(`fbx` 仍提示"仅归档,请先转换为 GLB/VRM");
|
- 管理端 3D 预览放开到 `vrm/glb/gltf`(`fbx` 仍提示"仅归档,请先转换为 GLB/VRM");
|
||||||
列表页显示 40px 缩略图。
|
列表页显示 40px 缩略图。
|
||||||
- 测试:`npx vitest run` -> **12 个文件 / 193 项通过**
|
- 测试:`npx vitest run` -> **14 个文件 / 211 项通过**
|
||||||
(`model-inspector` 25、`model-thumbnail` 8、`admin-bone-filter` 8、
|
(`model-inspector` 25、`model-thumbnail` 8、`admin-bone-filter` 8、
|
||||||
`model-archive` 24、`mmd-vmd` 31、`preprocess` 25、`init-env` 16、
|
`model-archive` 24、`mmd-vmd` 31、`preprocess` 25、`init-env` 16、
|
||||||
`admin-port` 16、`access-log` 9、`vrm-inspector` 6)。
|
`admin-port` 16、`access-log` 9、`vrm-inspector` 6)。
|
||||||
|
|
||||||
## 12. 首次启动的.env 引导(deploy/init-env.sh)、.env 预检与监听地址
|
## 12. 首次启动的.env 引导(deploy/init-env.sh)、.env 预检、Assimp 与监听地址
|
||||||
|
|
||||||
|
- `deploy/assimp-lib.sh`: Assimp(外部命令行工具)的定位与安装。`install.sh` 按发行版
|
||||||
|
自动安装(apt-get / dnf / yum / zypper / apk / pacman),装在非常规目录时把绝对路径写进
|
||||||
|
`deploy/.env` 的 `ASSIMP_BIN`;装不上只告警不中止(`SKIP_ASSIMP_INSTALL=1` 跳过)。
|
||||||
|
缺失只影响 `.pmx/.fbx` 转换与 FBX/glTF 动作预处理,`.glb/.vrm` 不受影响。
|
||||||
|
- `models.stats` 必须是 **MEDIUMTEXT**(迁移 `AddModelStatsMediumText`):骨骼树按骨骼逐条
|
||||||
|
展开,实测 643 骨骼的 PMX 分析结果 70,980 字节 > TEXT 上限 65,535,写入报 1406 并让上传
|
||||||
|
500。服务层另有 `shrinkStatsForStorage` 兜底(只裁骨骼树,保留全部汇总字段)。
|
||||||
|
- 压缩包文件名解码:Shift-JIS 试解后若含**私用区字符**同样视为解错并回退 GBK(WHATWG 的
|
||||||
|
shift_jis 把未定义码位映射成 PUA 而非 U+FFFD,会让 `tex/髮.png` 丢字符、贴图对不上)。
|
||||||
- `deploy/env-lib.sh`: `.env` 语法预检。`install.sh` / `ctl.sh` 用
|
- `deploy/env-lib.sh`: `.env` 语法预检。`install.sh` / `ctl.sh` 用
|
||||||
`set -a; . deploy/.env` 载入配置时 shell 会真的执行该文件,值含空格却未加引号
|
`set -a; . deploy/.env` 载入配置时 shell 会真的执行该文件,值含空格却未加引号
|
||||||
会被切成「赋值 + 命令」(实测报 `line 53: Miku.pmx: command not found`,退出码 127)。
|
会被切成「赋值 + 命令」(实测报 `line 53: Miku.pmx: command not found`,退出码 127)。
|
||||||
|
|||||||
@@ -10,7 +10,20 @@ OpenDance Studio **阶段一**: Web 管理服务端(NestJS + TypeORM + 现有 My
|
|||||||
|
|
||||||
## 状态
|
## 状态
|
||||||
|
|
||||||
**阶段一已完成**: T-WEB-001 -> T-WEB-010 全部落地,当前版本 **v0.2.2**(契约 `rest-api / data-models / model-formats 0.4.0`、`database 0.3.0`)。
|
**阶段一已完成**: T-WEB-001 -> T-WEB-010 全部落地,当前版本 **v0.2.4**(契约 `rest-api / data-models / model-formats 0.4.0`、`database 0.3.0`)。
|
||||||
|
|
||||||
|
v0.2.4 修订:
|
||||||
|
|
||||||
|
1. **修复缩略图"白模"**:缩略图渲染器原先**刻意不解码贴图**,只用 `baseColorFactor` 上色;而 MMD(PMX 转换)模型的 baseColorFactor 基本都是白色,于是列表页与审核面板里的缩略图全是白色模型。现在渲染器内置极简 PNG 解码器(zlib + 反滤波,8 位非隔行,colorType 0/2/3/4/6)并按 `baseColorFactor x 贴图` 逐像素采样,同时按 `alphaMode`(OPAQUE / MASK / BLEND)处理透明度;解码失败或非 PNG 时回退纯色,绝不抛错;
|
||||||
|
2. 缩略图取景收紧(FRAMING 1.6 -> 1.3),40px 列表缩略图里主体更清楚;
|
||||||
|
3. 新增 6 项贴图/透明度/朝向测试(`test/model-thumbnail-texture.spec.ts`);
|
||||||
|
4. **说明**:GLB 数据本身没问题——贴图逐字节内嵌(18/18)、39 个材质的贴图对应关系与 PMX 完全一致、UV 取到的就是皮肤/五官区域;白模只出在缩略图渲染器。
|
||||||
|
|
||||||
|
v0.2.3 修订:
|
||||||
|
|
||||||
|
1. **修复上传 PMX 返回 500**(`models.stats` 容量):分析结果里的骨骼树按骨骼逐条展开,实测一个 643 骨骼的 PMX(【奥黛塔】_by_原神.zip)就有 **70,980 字节**,而该列是 `TEXT`(上限 65,535),MySQL 严格模式直接拒绝写入(1406)导致 500。列迁移到 **MEDIUMTEXT**(`AddModelStatsMediumText`),并新增 `shrinkStatsForStorage` 兜底:即使库没迁移,也只截断骨骼树、保留面数/骨骼数/humanoid 覆盖/可驱动性,绝不让上传失败;
|
||||||
|
2. **修复 GBK 压缩包丢贴图**:文件名解码先试 Shift-JIS,而 WHATWG 的 shift_jis 会把**未定义码位映射成私用区不可见字符**(不是 U+FFFD),于是像 `tex/髮.png`(GBK 字节 `F3 8C`)这样的名字被判为「解码成功」而永不回退 GBK,贴图名对不上就丢图。现在私用区也算解错信号;实测该模型贴图由 17/18 变为 **18/18 全部内嵌**;
|
||||||
|
3. 新增 12 项测试(`test/model-stats-limit.spec.ts` 9 项 + 压缩包编码/目录条目 3 项)。
|
||||||
|
|
||||||
v0.2.2 修订:
|
v0.2.2 修订:
|
||||||
|
|
||||||
@@ -53,15 +66,15 @@ v0.2.0 主要变更:
|
|||||||
3. 取得发布包并校验(地址用你们自己的制品库替换; 下面是本仓库的发布地址)
|
3. 取得发布包并校验(地址用你们自己的制品库替换; 下面是本仓库的发布地址)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
curl -LO https://gitea.kasugano.cn/zijunchiang/opendance-web/releases/download/v0.2.2/opendance-web-v0.2.2.tar.gz
|
curl -LO https://gitea.kasugano.cn/zijunchiang/opendance-web/releases/download/v0.2.4/opendance-web-v0.2.4.tar.gz
|
||||||
curl -LO https://gitea.kasugano.cn/zijunchiang/opendance-web/releases/download/v0.2.2/SHA256SUMS
|
curl -LO https://gitea.kasugano.cn/zijunchiang/opendance-web/releases/download/v0.2.4/SHA256SUMS
|
||||||
sha256sum -c SHA256SUMS # 期望输出 "OK"
|
sha256sum -c SHA256SUMS # 期望输出 "OK"
|
||||||
```
|
```
|
||||||
|
|
||||||
4. 解压并生成配置(交互式引导,逐项问答,口令不回显)
|
4. 解压并生成配置(交互式引导,逐项问答,口令不回显)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
tar -xzf opendance-web-v0.2.2.tar.gz && cd opendance-web-v0.2.2
|
tar -xzf opendance-web-v0.2.4.tar.gz && cd opendance-web-v0.2.4
|
||||||
bash deploy/init-env.sh
|
bash deploy/init-env.sh
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -131,8 +144,8 @@ FLUSH PRIVILEGES;
|
|||||||
### 5. 部署步骤
|
### 5. 部署步骤
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
tar -xzf opendance-web-v0.2.2.tar.gz
|
tar -xzf opendance-web-v0.2.4.tar.gz
|
||||||
cd opendance-web-v0.2.2
|
cd opendance-web-v0.2.4
|
||||||
bash deploy/init-env.sh # 首次启动引导: 逐项问答生成 deploy/.env(见下)
|
bash deploy/init-env.sh # 首次启动引导: 逐项问答生成 deploy/.env(见下)
|
||||||
bash deploy/install.sh # 已以 root 登录, 不需要 sudo
|
bash deploy/install.sh # 已以 root 登录, 不需要 sudo
|
||||||
```
|
```
|
||||||
@@ -273,6 +286,8 @@ bash /opt/opendance/deploy/ctl.sh status | restart | logs
|
|||||||
| 服务本机 curl 通、外部访问不了 | `bash deploy/check-access.sh` 一次定位:打印 `HOST`/`PORT`、实际监听地址、对外 IP 自测、80/443 有无反代、防火墙放行情况,并给出修复命令 |
|
| 服务本机 curl 通、外部访问不了 | `bash deploy/check-access.sh` 一次定位:打印 `HOST`/`PORT`、实际监听地址、对外 IP 自测、80/443 有无反代、防火墙放行情况,并给出修复命令 |
|
||||||
| 管理页面打开是 404 | 说明访问的是软件 API 端口(默认 3000);管理页面在管理端端口(默认 3001) |
|
| 管理页面打开是 404 | 说明访问的是软件 API 端口(默认 3000);管理页面在管理端端口(默认 3001) |
|
||||||
| 上传 `.zip` / `.pmx` / `.fbx` 报转换失败(ENOENT / Assimp 不可用) | PMX/FBX 转换依赖外部 Assimp:`apt-get install -y assimp-utils`(RHEL 系 `dnf install -y assimp`,`install.sh` 现在会尝试自动安装)。装好后**不需要重启服务**;若不在服务 PATH 中,在 `deploy/.env` 设 `ASSIMP_BIN` 为绝对路径。只上传 `.glb` / `.vrm` 则不需要它 |
|
| 上传 `.zip` / `.pmx` / `.fbx` 报转换失败(ENOENT / Assimp 不可用) | PMX/FBX 转换依赖外部 Assimp:`apt-get install -y assimp-utils`(RHEL 系 `dnf install -y assimp`,`install.sh` 现在会尝试自动安装)。装好后**不需要重启服务**;若不在服务 PATH 中,在 `deploy/.env` 设 `ASSIMP_BIN` 为绝对路径。只上传 `.glb` / `.vrm` 则不需要它 |
|
||||||
|
| 列表/审核里的缩略图是"白模"(纯白模型) | v0.2.4 前的缩略图渲染器不采样贴图,而 MMD 模型的 `baseColorFactor` 是白色。升级到 v0.2.4 后重新生成缩略图:`cd /opt/opendance/web-server && node scripts/reanalyze-models.mjs --url http://127.0.0.1:3000 --force` |
|
||||||
|
| 导入模型返回 **500 Internal server error** | 老库上多为 `models.stats` 超长(骨骼树按骨骼展开,643 骨骼即 70,980 字节 > TEXT 上限):执行 `ALTER TABLE models MODIFY stats MEDIUMTEXT NULL;` 或部署 v0.2.3(`install.sh` 会自动跑迁移)。v0.2.3 起服务端另有兜底,不会再 500 |
|
||||||
| 安装时报 `command not found` / 退出码 127,且提示指向 `deploy/.env` 某一行 | 该行的值含空格或 shell 元字符却没加引号,被 shell 当成命令执行了。按预检提示给这个值加引号(如 `MODEL_CONVERT_PMX="Hatsune Miku.pmx"`)后重跑;`deploy/env-lib.sh` 会提前拦住并给出行号 |
|
| 安装时报 `command not found` / 退出码 127,且提示指向 `deploy/.env` 某一行 | 该行的值含空格或 shell 元字符却没加引号,被 shell 当成命令执行了。按预检提示给这个值加引号(如 `MODEL_CONVERT_PMX="Hatsune Miku.pmx"`)后重跑;`deploy/env-lib.sh` 会提前拦住并给出行号 |
|
||||||
| 模型审核界面显示"未分析 / 无预览图" | 该模型是升级前入库的:调 `POST /api/models/<id>/reanalyze`,或批量跑 `node scripts/reanalyze-models.mjs --url http://127.0.0.1:3000` |
|
| 模型审核界面显示"未分析 / 无预览图" | 该模型是升级前入库的:调 `POST /api/models/<id>/reanalyze`,或批量跑 `node scripts/reanalyze-models.mjs --url http://127.0.0.1:3000` |
|
||||||
|
|
||||||
@@ -302,11 +317,12 @@ npm run dev # http://localhost:5173(/api 与 /health 代理到 3000)
|
|||||||
cd web-server
|
cd web-server
|
||||||
npm run contracts:check # 契约版本一致性
|
npm run contracts:check # 契约版本一致性
|
||||||
npm run typecheck # TypeScript 严格检查
|
npm run typecheck # TypeScript 严格检查
|
||||||
npm run test # 193 项单元测试 / 12 个文件(VMD 31、模型压缩包 24、
|
npm run test # 211 项单元测试 / 14 个文件(VMD 31、模型压缩包 27、
|
||||||
# 模型检查器 25、缩略图 8、骨骼过滤 8、
|
# 模型检查器 25、缩略图 8、骨骼过滤 8、
|
||||||
# 预处理 25、init-env 16、admin-port 16、
|
# 预处理 25、init-env 16、admin-port 16、
|
||||||
# access-log 9、VRM 检查器 6、.env 预检 12、
|
# access-log 9、VRM 检查器 6、.env 预检 12、
|
||||||
# Assimp 定位与安装 13)
|
# Assimp 定位与安装 13、stats 容量与编码 12、
|
||||||
|
# 缩略图贴图采样 6)
|
||||||
npm run build # 生产构建
|
npm run build # 生产构建
|
||||||
npm run start:prod & # 启动
|
npm run start:prod & # 启动
|
||||||
npm run test:smoke # 57 项端到端冒烟(需要数据库可连通)
|
npm run test:smoke # 57 项端到端冒烟(需要数据库可连通)
|
||||||
@@ -427,10 +443,10 @@ node scripts/reanalyze-models.mjs --url http://127.0.0.1:3000
|
|||||||
| 能力 | 实现 |
|
| 能力 | 实现 |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
| 分析 | `model-inspector.ts` 解析 GLB/VRM:面数 / 顶点 / 网格 / 材质 / 贴图 / 节点 / 形变目标 / 动画 / 蒙皮骨骼数 / 骨骼树 / humanoid 覆盖 / 可驱动性(缺哪些必需骨骼)/ warnings,写入 `models.stats`(迁移 `AddModelStats1789460000000`,新增 `models.stats TEXT`) |
|
| 分析 | `model-inspector.ts` 解析 GLB/VRM:面数 / 顶点 / 网格 / 材质 / 贴图 / 节点 / 形变目标 / 动画 / 蒙皮骨骼数 / 骨骼树 / humanoid 覆盖 / 可驱动性(缺哪些必需骨骼)/ warnings,写入 `models.stats`(迁移 `AddModelStats1789460000000`,新增 `models.stats TEXT`) |
|
||||||
| 缩略图 | `model-thumbnail.ts` 用**纯 TS 软件光栅化**生成 PNG(读 GLB/glTF -> 应用节点层级与蒙皮 -> 包围盒自动构图 -> 透视投影 -> z-buffer 逐三角形平面着色 -> 手写 PNG 编码),**零原生依赖**(不需要 canvas / headless-gl / puppeteer)。默认 512x512,写入 `models.thumbnail_asset_id` / `thumbnailUrl`;失败或非 glTF 容器时留空,列表页退化为格式占位图 |
|
| 缩略图 | `model-thumbnail.ts` 用**纯 TS 软件光栅化**生成 PNG(读 GLB/glTF -> 应用节点层级与蒙皮 -> 包围盒自动构图 -> 透视投影 -> z-buffer + 平面着色 + **逐像素采样 baseColorTexture**,内置 PNG 解码器 -> 手写 PNG 编码),**零原生依赖**(不需要 canvas / headless-gl / puppeteer);按 `alphaMode` 处理 OPAQUE / MASK / BLEND。默认 512x512,写入 `models.thumbnail_asset_id` / `thumbnailUrl`;失败或非 glTF 容器时留空,列表页退化为格式占位图 |
|
||||||
| 预览 | 管理端 3D 预览放开到 `vrm/glb/gltf`(`.fbx` 仍提示「仅归档,请先转换为 GLB/VRM」);列表页显示 40px 小缩略图,审核面板显示 116px 缩略图 |
|
| 预览 | 管理端 3D 预览放开到 `vrm/glb/gltf`(`.fbx` 仍提示「仅归档,请先转换为 GLB/VRM」);列表页显示 40px 小缩略图,审核面板显示 116px 缩略图 |
|
||||||
|
|
||||||
实测:512x512 PNG / 15,043 字节,目视确认为居中的 T-pose 角色图;真实 10MB VRM(83 关节、5307 顶点)单张耗时 60-121ms(中位 79ms)。
|
实测:512x512 PNG / 15-42 KB(带贴图后变大),目视确认为居中的 T-pose 角色图且能看到肤色/眼睛/服装贴图;真实 10MB VRM(83 关节、5307 顶点)单张耗时 60-121ms(中位 79ms),27MB PMX 转换件约 200-400ms。
|
||||||
|
|
||||||
### 审核流程(模型)
|
### 审核流程(模型)
|
||||||
|
|
||||||
|
|||||||
+40
-7
@@ -1,5 +1,5 @@
|
|||||||
================================================================
|
================================================================
|
||||||
OpenDance Studio -- Web 管理服务端 v0.2.2 使用说明
|
OpenDance Studio -- Web 管理服务端 v0.2.4 使用说明
|
||||||
================================================================
|
================================================================
|
||||||
|
|
||||||
本服务端是 OpenDance Studio 三端中的第一端, 负责管理歌曲、VRM 模型与
|
本服务端是 OpenDance Studio 三端中的第一端, 负责管理歌曲、VRM 模型与
|
||||||
@@ -8,6 +8,30 @@ OpenDance Studio -- Web 管理服务端 v0.2.2 使用说明
|
|||||||
重要: 本服务端 不部署、不管理 MySQL。数据库为部署方提供的现有实例,
|
重要: 本服务端 不部署、不管理 MySQL。数据库为部署方提供的现有实例,
|
||||||
连接信息通过 .env 注入。
|
连接信息通过 .env 注入。
|
||||||
|
|
||||||
|
v0.2.4 修订
|
||||||
|
1. 修复缩略图"白模": 缩略图渲染器原先刻意不解码贴图, 只用 baseColorFactor
|
||||||
|
上色; 而 MMD(PMX 转换)模型的 baseColorFactor 基本都是白色, 于是列表页与
|
||||||
|
审核面板里的缩略图全是白色模型。现在内置极简 PNG 解码器(zlib + 反滤波,
|
||||||
|
8 位非隔行, colorType 0/2/3/4/6), 按 baseColorFactor x 贴图逐像素采样,
|
||||||
|
并按 alphaMode(OPAQUE / MASK / BLEND)处理透明度; 解码失败回退纯色。
|
||||||
|
2. 缩略图取景收紧(FRAMING 1.6 -> 1.3), 40px 列表缩略图里主体更清楚。
|
||||||
|
3. 新增 6 项贴图/透明度测试(test/model-thumbnail-texture.spec.ts)。
|
||||||
|
4. 说明: GLB 数据本身没问题 - 贴图逐字节内嵌(18/18)、39 个材质的贴图对应
|
||||||
|
关系与 PMX 完全一致; 白模只出在缩略图渲染器。
|
||||||
|
|
||||||
|
v0.2.3 修订
|
||||||
|
1. 修复上传 PMX 返回 500(models.stats 容量): 分析结果里的骨骼树按骨骼逐条
|
||||||
|
展开, 实测一个 643 骨骼的 PMX(【奥黛塔】_by_原神.zip)就有 70,980 字节,
|
||||||
|
而该列是 TEXT(上限 65,535), MySQL 严格模式直接拒绝写入(1406)导致 500。
|
||||||
|
列迁移到 MEDIUMTEXT(AddModelStatsMediumText), 并新增
|
||||||
|
shrinkStatsForStorage 兜底: 即使库没迁移, 也只截断骨骼树、保留面数/
|
||||||
|
骨骼数/humanoid 覆盖/可驱动性, 绝不让上传失败。
|
||||||
|
2. 修复 GBK 压缩包丢贴图: 文件名解码先试 Shift-JIS, 而 WHATWG 的 shift_jis
|
||||||
|
会把未定义码位映射成私用区不可见字符(不是 U+FFFD), 于是像 tex/髮.png
|
||||||
|
(GBK 字节 F3 8C)这样的名字被判为"解码成功"而永不回退 GBK, 贴图名对不上
|
||||||
|
就丢图。现在私用区也算解错信号; 实测该模型贴图由 17/18 变为 18/18 全部内嵌。
|
||||||
|
3. 新增 12 项测试(test/model-stats-limit.spec.ts 9 项 + 压缩包编码/目录条目 3 项)。
|
||||||
|
|
||||||
v0.2.2 修订
|
v0.2.2 修订
|
||||||
1. 修复 Assimp 缺失时的报错与部署体验: 服务器没装 Assimp 时, 上传 .pmx/.fbx
|
1. 修复 Assimp 缺失时的报错与部署体验: 服务器没装 Assimp 时, 上传 .pmx/.fbx
|
||||||
(或含它们的压缩包)只得到两条 导出失败(ENOENT): spawn assimp ENOENT,
|
(或含它们的压缩包)只得到两条 导出失败(ENOENT): spawn assimp ENOENT,
|
||||||
@@ -45,11 +69,11 @@ v0.2.1 修订
|
|||||||
2. 准备一个现有的 MySQL / MariaDB 实例, 并建好库与账号(见下方"前置要求")
|
2. 准备一个现有的 MySQL / MariaDB 实例, 并建好库与账号(见下方"前置要求")
|
||||||
本项目不会创建数据库本身
|
本项目不会创建数据库本身
|
||||||
3. 取得发布包并校验(地址用你们自己的制品库替换)
|
3. 取得发布包并校验(地址用你们自己的制品库替换)
|
||||||
curl -LO https://<制品库地址>/opendance-web-v0.2.2.tar.gz
|
curl -LO https://<制品库地址>/opendance-web-v0.2.4.tar.gz
|
||||||
curl -LO https://<制品库地址>/SHA256SUMS
|
curl -LO https://<制品库地址>/SHA256SUMS
|
||||||
sha256sum -c SHA256SUMS # 期望输出 "OK"
|
sha256sum -c SHA256SUMS # 期望输出 "OK"
|
||||||
4. 解压并生成配置(交互式引导, 逐项问答, 口令不回显)
|
4. 解压并生成配置(交互式引导, 逐项问答, 口令不回显)
|
||||||
tar -xzf opendance-web-v0.2.2.tar.gz && cd opendance-web-v0.2.2
|
tar -xzf opendance-web-v0.2.4.tar.gz && cd opendance-web-v0.2.4
|
||||||
bash deploy/init-env.sh
|
bash deploy/init-env.sh
|
||||||
5. 安装(建用户与目录 -> 装依赖 -> 迁移 -> 注册系统服务 -> 等健康检查)
|
5. 安装(建用户与目录 -> 装依赖 -> 迁移 -> 注册系统服务 -> 等健康检查)
|
||||||
bash deploy/install.sh
|
bash deploy/install.sh
|
||||||
@@ -107,8 +131,8 @@ v0.2.1 修订
|
|||||||
(本项目不会创建数据库本身, 也不需要 GRANT OPTION / FILE / SUPER 等管理权限)
|
(本项目不会创建数据库本身, 也不需要 GRANT OPTION / FILE / SUPER 等管理权限)
|
||||||
|
|
||||||
部署步骤
|
部署步骤
|
||||||
tar -xzf opendance-web-v0.2.2.tar.gz
|
tar -xzf opendance-web-v0.2.4.tar.gz
|
||||||
cd opendance-web-v0.2.2
|
cd opendance-web-v0.2.4
|
||||||
bash deploy/init-env.sh # 首次启动引导: 逐项问答生成 deploy/.env(见下)
|
bash deploy/init-env.sh # 首次启动引导: 逐项问答生成 deploy/.env(见下)
|
||||||
bash deploy/install.sh # 已以 root 登录, 不需要 sudo
|
bash deploy/install.sh # 已以 root 登录, 不需要 sudo
|
||||||
|
|
||||||
@@ -239,6 +263,15 @@ deploy/.env 初始化引导(deploy/init-env.sh)
|
|||||||
(RHEL 系 dnf install -y assimp; install.sh 现在会尝试自动安装)。
|
(RHEL 系 dnf install -y assimp; install.sh 现在会尝试自动安装)。
|
||||||
装好后不需要重启服务; 若不在服务 PATH 中, 在 deploy/.env 里把 ASSIMP_BIN
|
装好后不需要重启服务; 若不在服务 PATH 中, 在 deploy/.env 里把 ASSIMP_BIN
|
||||||
设为绝对路径。只上传 .glb / .vrm 则不需要它。
|
设为绝对路径。只上传 .glb / .vrm 则不需要它。
|
||||||
|
- 列表/审核里的缩略图是"白模"(纯白模型)
|
||||||
|
v0.2.4 前的缩略图渲染器不采样贴图, 而 MMD 模型的 baseColorFactor 是白色。
|
||||||
|
升级到 v0.2.4 后重新生成缩略图: cd /opt/opendance/web-server &&
|
||||||
|
node scripts/reanalyze-models.mjs --url http://127.0.0.1:3000 --force
|
||||||
|
- 导入模型返回 500 Internal server error
|
||||||
|
老库上多为 models.stats 超长(骨骼树按骨骼展开, 643 骨骼即 70,980 字节
|
||||||
|
> TEXT 上限): 执行 ALTER TABLE models MODIFY stats MEDIUMTEXT NULL;
|
||||||
|
或部署 v0.2.3(install.sh 会自动跑迁移)。v0.2.3 起服务端另有兜底,
|
||||||
|
不会再 500。
|
||||||
- 安装时报 command not found / 退出码 127, 且提示指向 deploy/.env 某一行
|
- 安装时报 command not found / 退出码 127, 且提示指向 deploy/.env 某一行
|
||||||
该行的值含空格或 shell 元字符却没加引号, 被 shell 当成命令执行了。按预检
|
该行的值含空格或 shell 元字符却没加引号, 被 shell 当成命令执行了。按预检
|
||||||
提示给这个值加引号(如 MODEL_CONVERT_PMX="Hatsune Miku.pmx")后重跑。
|
提示给这个值加引号(如 MODEL_CONVERT_PMX="Hatsune Miku.pmx")后重跑。
|
||||||
@@ -269,11 +302,11 @@ deploy/.env 初始化引导(deploy/init-env.sh)
|
|||||||
cd web-server
|
cd web-server
|
||||||
npm run contracts:check # 契约版本一致性
|
npm run contracts:check # 契约版本一致性
|
||||||
npm run typecheck # TypeScript 严格检查
|
npm run typecheck # TypeScript 严格检查
|
||||||
npm run test # 193 项单元测试 / 12 个文件(VMD 31、模型压缩包 24、
|
npm run test # 211 项单元测试 / 14 个文件(VMD 31、模型压缩包 24、
|
||||||
# 模型检查器 25、缩略图 8、骨骼过滤 8、
|
# 模型检查器 25、缩略图 8、骨骼过滤 8、
|
||||||
# 预处理 25、init-env 16、admin-port 16、
|
# 预处理 25、init-env 16、admin-port 16、
|
||||||
# access-log 9、VRM 检查器 6、.env 预检 12、
|
# access-log 9、VRM 检查器 6、.env 预检 12、
|
||||||
# Assimp 定位与安装 13)
|
# Assimp 定位与安装 13、stats 容量与编码 12、缩略图贴图 6)
|
||||||
npm run build # 生产构建
|
npm run build # 生产构建
|
||||||
npm run start:prod & # 启动
|
npm run start:prod & # 启动
|
||||||
npm run test:smoke # 57 项端到端冒烟(需要数据库可连通)
|
npm run test:smoke # 57 项端到端冒烟(需要数据库可连通)
|
||||||
|
|||||||
+2
-2
@@ -1,5 +1,5 @@
|
|||||||
project: opendance-web-server
|
project: opendance-web-server
|
||||||
version: 0.2.2
|
version: 0.2.4
|
||||||
repository: opendance-web
|
repository: opendance-web
|
||||||
default_branch: main
|
default_branch: main
|
||||||
contracts_version: 0.4.0
|
contracts_version: 0.4.0
|
||||||
@@ -60,7 +60,7 @@ tasks:
|
|||||||
status: done
|
status: done
|
||||||
|
|
||||||
deliverable:
|
deliverable:
|
||||||
artifact: opendance-web-v0.2.2.tar.gz
|
artifact: opendance-web-v0.2.4.tar.gz
|
||||||
packaging: Linux 业务服务(系统服务托管,双端口:软件 API + 管理端,不含 MySQL、不含反向代理、不使用 Docker)
|
packaging: Linux 业务服务(系统服务托管,双端口:软件 API + 管理端,不含 MySQL、不含反向代理、不使用 Docker)
|
||||||
includes:
|
includes:
|
||||||
- web-server/ # NestJS 源码 + 构建产物(含 BVH / VMD 内置解析、ZIP 读取与模型压缩包转换)
|
- web-server/ # NestJS 源码 + 构建产物(含 BVH / VMD 内置解析、ZIP 读取与模型压缩包转换)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# ============================================================
|
# ============================================================
|
||||||
# 打包发布产物:opendance-web-v0.2.2.tar.gz
|
# 打包发布产物:opendance-web-v0.2.4.tar.gz
|
||||||
#
|
#
|
||||||
# 产物结构(Linux 业务服务部署,不使用 Docker、不部署反向代理):
|
# 产物结构(Linux 业务服务部署,不使用 Docker、不部署反向代理):
|
||||||
# web-server/ NestJS 源码 + 构建产物
|
# web-server/ NestJS 源码 + 构建产物
|
||||||
@@ -21,7 +21,7 @@ set -euo pipefail
|
|||||||
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||||||
cd "$REPO_ROOT"
|
cd "$REPO_ROOT"
|
||||||
|
|
||||||
VERSION="${1:-0.2.2}"
|
VERSION="${1:-0.2.4}"
|
||||||
NAME="opendance-web-v${VERSION}"
|
NAME="opendance-web-v${VERSION}"
|
||||||
OUT_DIR="$REPO_ROOT/dist-release"
|
OUT_DIR="$REPO_ROOT/dist-release"
|
||||||
STAGE="$OUT_DIR/$NAME"
|
STAGE="$OUT_DIR/$NAME"
|
||||||
|
|||||||
@@ -160,7 +160,7 @@ node scripts/import-default-model.mjs --write-source-only --model-id 22 --dry-ru
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
cd web-server
|
cd web-server
|
||||||
npx vitest run # 12 个文件 / 193 项通过
|
npx vitest run # 14 个文件 / 211 项通过
|
||||||
npm run test:smoke # 冒烟 [4] 段落
|
npm run test:smoke # 冒烟 [4] 段落
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ draft ──submit──▶ pending ──approve──▶ published
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
cd web-server
|
cd web-server
|
||||||
npx vitest run # 12 个文件 / 193 项通过(含 admin-bone-filter 8 项、.env 预检 12 项、Assimp 13 项)
|
npx vitest run # 14 个文件 / 211 项通过(含 admin-bone-filter 8 项、.env 预检 12 项、Assimp 13 项、缩略图贴图 6 项)
|
||||||
npm run test:smoke
|
npm run test:smoke
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "opendance-admin",
|
"name": "opendance-admin",
|
||||||
"private": true,
|
"private": true,
|
||||||
"version": "0.2.2",
|
"version": "0.2.4",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"description": "OpenDance Studio 管理端(React + TypeScript + Vite)",
|
"description": "OpenDance Studio 管理端(React + TypeScript + Vite)",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "opendance-web-server",
|
"name": "opendance-web-server",
|
||||||
"version": "0.2.2",
|
"version": "0.2.4",
|
||||||
"description": "OpenDance Studio Web 管理服务端(NestJS + TypeORM + 现有 MySQL 实例)",
|
"description": "OpenDance Studio Web 管理服务端(NestJS + TypeORM + 现有 MySQL 实例)",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"private": true,
|
"private": true,
|
||||||
|
|||||||
@@ -46,8 +46,21 @@ function decoderFor(encoding: string): TextDecoder | null {
|
|||||||
return decoders.get(encoding) ?? null;
|
return decoders.get(encoding) ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 半角片假名:Shift-JIS 被误用时会出现,用来判断该不该换编码 */
|
/**
|
||||||
|
* 半角片假名:Shift-JIS 被误用时会出现,用来判断该不该换编码。
|
||||||
|
*
|
||||||
|
* 私用区(PUA)同样是「Shift-JIS 解错了」的信号:WHATWG 的 shift_jis 表把
|
||||||
|
* **未定义的码位**映射到私用区,而不是 U+FFFD,于是解码结果看起来「成功」,
|
||||||
|
* 却是看不见的乱码字。实测 GBK 的 `髮`(字节 F3 8C)就落在这里:
|
||||||
|
* 先试 Shift-JIS 得到 `tex/\uE0xx.png`(不可见),永远不会回退到 GBK,
|
||||||
|
* 贴图名对不上,模型入库后丢贴图。
|
||||||
|
*/
|
||||||
const HALF_WIDTH_KATAKANA = /[\uFF61-\uFF9F]/;
|
const HALF_WIDTH_KATAKANA = /[\uFF61-\uFF9F]/;
|
||||||
|
const PRIVATE_USE_AREA = /[\uE000-\uF8FF]/;
|
||||||
|
/** Shift-JIS 解码结果是否明显解错了(该换 GBK 再试) */
|
||||||
|
function looksMisdecoded(text: string): boolean {
|
||||||
|
return text.includes('\uFFFD') || HALF_WIDTH_KATAKANA.test(text) || PRIVATE_USE_AREA.test(text);
|
||||||
|
}
|
||||||
|
|
||||||
/** 解压包内文件名:UTF-8 标志位优先,否则试 Shift-JIS(中文/日文压缩包常见) */
|
/** 解压包内文件名:UTF-8 标志位优先,否则试 Shift-JIS(中文/日文压缩包常见) */
|
||||||
function decodeEntryName(bytes: Buffer, utf8Flag: boolean): string {
|
function decodeEntryName(bytes: Buffer, utf8Flag: boolean): string {
|
||||||
@@ -57,7 +70,7 @@ function decodeEntryName(bytes: Buffer, utf8Flag: boolean): string {
|
|||||||
const shiftJis = decoderFor('shift_jis');
|
const shiftJis = decoderFor('shift_jis');
|
||||||
if (shiftJis) {
|
if (shiftJis) {
|
||||||
const decoded = shiftJis.decode(bytes);
|
const decoded = shiftJis.decode(bytes);
|
||||||
if (!decoded.includes('\uFFFD') && !HALF_WIDTH_KATAKANA.test(decoded)) return decoded;
|
if (!looksMisdecoded(decoded)) return decoded;
|
||||||
}
|
}
|
||||||
const gbk = decoderFor('gbk');
|
const gbk = decoderFor('gbk');
|
||||||
if (gbk) {
|
if (gbk) {
|
||||||
|
|||||||
@@ -52,8 +52,35 @@ export class CharacterModel {
|
|||||||
/**
|
/**
|
||||||
* 模型分析结果(面数 / 骨骼树 / humanoid 覆盖 / 可驱动性…),见 model-inspector.ts。
|
* 模型分析结果(面数 / 骨骼树 / humanoid 覆盖 / 可驱动性…),见 model-inspector.ts。
|
||||||
* 存 JSON 文本:审核界面要展示,且骨骼树可能上千行,不适合拆列。
|
* 存 JSON 文本:审核界面要展示,且骨骼树可能上千行,不适合拆列。
|
||||||
|
*
|
||||||
|
* 必须是 **MEDIUMTEXT**(16MB)而不是 TEXT(64KB):骨骼树按骨骼逐条展开,
|
||||||
|
* 实测一个 643 骨骼的 PMX(【奥黛塔】)分析结果就有 70,980 字节,超过 TEXT 上限,
|
||||||
|
* MySQL 严格模式下写入直接报 1406,上传接口 500。
|
||||||
|
* 用 mediumtext + transformer 而不是 simple-json:simple-json 固定映射到 text,
|
||||||
|
* 实体元数据会和数据库列型不一致(后续 migration:generate 会把列改回 text)。
|
||||||
*/
|
*/
|
||||||
@Column({ name: 'stats', type: 'simple-json', nullable: true })
|
@Column({
|
||||||
|
name: 'stats',
|
||||||
|
type: 'mediumtext',
|
||||||
|
nullable: true,
|
||||||
|
transformer: {
|
||||||
|
to: (value: Record<string, unknown> | null): string | null =>
|
||||||
|
value === null || value === undefined
|
||||||
|
? null
|
||||||
|
: typeof value === 'string'
|
||||||
|
? value
|
||||||
|
: JSON.stringify(value),
|
||||||
|
from: (value: string | null): Record<string, unknown> | null => {
|
||||||
|
if (value === null || value === undefined || value === '') return null;
|
||||||
|
try {
|
||||||
|
return JSON.parse(value) as Record<string, unknown>;
|
||||||
|
} catch {
|
||||||
|
// 历史脏数据不应该让整个接口 500
|
||||||
|
return { parseError: true, raw: value.slice(0, 200) };
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
stats!: Record<string, unknown> | null;
|
stats!: Record<string, unknown> | null;
|
||||||
|
|
||||||
// ---- 来源五元组(必填)----
|
// ---- 来源五元组(必填)----
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* `models.stats` 由 TEXT(64KB)扩到 MEDIUMTEXT(16MB)。
|
||||||
|
*
|
||||||
|
* 起因(真实故障):上传含 PMX 的压缩包(【奥黛塔】_by_原神.zip,643 根骨骼)
|
||||||
|
* 时接口返回 500。模型转换本身成功,但分析结果里的骨骼树按骨骼逐条展开,
|
||||||
|
* JSON 实测 70,980 字节 > TEXT 上限 65,535,MySQL / MariaDB 严格模式直接拒绝
|
||||||
|
* 写入(ER_DATA_TOO_LONG / 1406),而写入这一步没有兜底。
|
||||||
|
*
|
||||||
|
* MMD / VRM 模型骨骼上千根很常见,MEDIUMTEXT 给到 16MB 足够;
|
||||||
|
* 服务端另有 shrinkStatsForStorage 兜底,避免在未迁移的库上再次 500。
|
||||||
|
*/
|
||||||
|
export class AddModelStatsMediumText1789550000000 implements MigrationInterface {
|
||||||
|
name = 'AddModelStatsMediumText1789550000000'
|
||||||
|
|
||||||
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.query(`ALTER TABLE \`models\` MODIFY \`stats\` mediumtext NULL`);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
// 回退可能截断超长数据:这里只改列型,不做内容裁剪
|
||||||
|
await queryRunner.query(`ALTER TABLE \`models\` MODIFY \`stats\` text NULL`);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -667,3 +667,70 @@ export async function inspectModelFile(absPath: string, fileSize?: number): Prom
|
|||||||
|
|
||||||
return analyzeGltfDocument(gltf, glb ? 'glb' : 'gltf', size, warnings);
|
return analyzeGltfDocument(gltf, glb ? 'glb' : 'gltf', size, warnings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* --------------------------------------------------- 存储兜底:裁剪超大的 stats */
|
||||||
|
|
||||||
|
/** MySQL / MariaDB 的 TEXT 上限(未迁移 MEDIUMTEXT 的老库仍是这个值) */
|
||||||
|
export const MYSQL_TEXT_LIMIT = 65535;
|
||||||
|
|
||||||
|
/** 裁剪后至少保留的骨骼条目数:太少审核界面就看不出结构了 */
|
||||||
|
const MIN_BONE_TREE_KEPT = 24;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 把过大的 `ModelStats` 裁到能安全写进 TEXT 的大小(纯函数,永不抛异常)。
|
||||||
|
*
|
||||||
|
* 为什么需要:`models.stats` 里最占地方的是骨骼树,按骨骼逐条展开。
|
||||||
|
* 实测一个 643 骨骼的 PMX 分析结果 70,980 字节 > TEXT 上限 65,535,
|
||||||
|
* MySQL 严格模式会直接拒绝写入(1406)。迁移已把列扩到 MEDIUMTEXT,
|
||||||
|
* 这里再兜一层:万一跑在没迁移的库上,也宁可少存骨骼明细,不能让上传 500。
|
||||||
|
*
|
||||||
|
* 保留策略:面数 / 骨骼数 / humanoid 覆盖 / 可驱动性 / warnings 一律保留,
|
||||||
|
* 只裁骨骼树(父在前,从头截断即保留祖先链),并追加一条 warning 说明被裁过。
|
||||||
|
*/
|
||||||
|
export function shrinkStatsForStorage(
|
||||||
|
stats: ModelStats,
|
||||||
|
maxBytes: number = MYSQL_TEXT_LIMIT,
|
||||||
|
): ModelStats {
|
||||||
|
const fits = (candidate: ModelStats): boolean => {
|
||||||
|
try {
|
||||||
|
return JSON.stringify(candidate).length <= maxBytes;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if (fits(stats)) return stats;
|
||||||
|
|
||||||
|
const note = `骨骼树过大(原 ${stats.boneTree.length} 条),存储前已截断`;
|
||||||
|
const withNote = (boneTree: BoneNode[]): ModelStats => ({
|
||||||
|
...stats,
|
||||||
|
boneTree,
|
||||||
|
warnings: stats.warnings.includes(note) ? stats.warnings : [...stats.warnings, note],
|
||||||
|
});
|
||||||
|
|
||||||
|
// 二分找能放下的最大前缀长度(骨骼树父在前,前缀即祖先链完整的子集)
|
||||||
|
let low = Math.min(MIN_BONE_TREE_KEPT, stats.boneTree.length);
|
||||||
|
let high = stats.boneTree.length;
|
||||||
|
let best = withNote(stats.boneTree.slice(0, low));
|
||||||
|
while (low <= high) {
|
||||||
|
const mid = Math.floor((low + high) / 2);
|
||||||
|
const candidate = withNote(stats.boneTree.slice(0, mid));
|
||||||
|
if (fits(candidate)) {
|
||||||
|
best = candidate;
|
||||||
|
low = mid + 1;
|
||||||
|
} else {
|
||||||
|
high = mid - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 极端情况:连最小骨骼树都放不下 -> 只留汇总字段
|
||||||
|
if (!fits(best)) {
|
||||||
|
const summary = withNote([]);
|
||||||
|
if (fits(summary)) return summary;
|
||||||
|
return {
|
||||||
|
...summary,
|
||||||
|
missingRequiredBones: summary.missingRequiredBones.slice(0, 16),
|
||||||
|
warnings: [note],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return best;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { readFile } from 'node:fs/promises';
|
import { readFile } from 'node:fs/promises';
|
||||||
|
import { inflateSync } from 'node:zlib';
|
||||||
import { dirname, resolve as resolvePath } from 'node:path';
|
import { dirname, resolve as resolvePath } from 'node:path';
|
||||||
import { deflateSync } from 'node:zlib';
|
import { deflateSync } from 'node:zlib';
|
||||||
|
|
||||||
@@ -22,9 +23,12 @@ import { deflateSync } from 'node:zlib';
|
|||||||
* **退化为不蒙皮**,按节点世界矩阵画绑定姿态(bind pose)——多数 VRM 的绑定姿态
|
* **退化为不蒙皮**,按节点世界矩阵画绑定姿态(bind pose)——多数 VRM 的绑定姿态
|
||||||
* 就是可看的 T/A 姿态,比错误的蒙皮矩阵导致的"爆炸形状"安全得多。
|
* 就是可看的 T/A 姿态,比错误的蒙皮矩阵导致的"爆炸形状"安全得多。
|
||||||
* - 按 glTF 规范,蒙皮网格节点自身的变换被忽略(顶点直接由关节矩阵驱动)。
|
* - 按 glTF 规范,蒙皮网格节点自身的变换被忽略(顶点直接由关节矩阵驱动)。
|
||||||
* 2. **不采样贴图**:`pbrMetallicRoughness.baseColorFactor` 作为纯色基色,
|
* 2. **采样 baseColorTexture**:内置极简 PNG 解码器(zlib + 反滤波,8 位、非隔行、
|
||||||
* 即使存在 `baseColorTexture` 也不解码(避免再引入 PNG/JPEG 解码器),
|
* colorType 0/2/3/4/6),按 `baseColorFactor x 贴图` 逐像素取色,并按
|
||||||
* 因此带贴图的模型缩略图会是纯色块,但轮廓与光影正确。
|
* `alphaMode`(OPAQUE / MASK / BLEND)处理透明度。
|
||||||
|
* 这一版之前**不采样贴图**:MMD 模型(PMX 转换而来)的 `baseColorFactor` 几乎
|
||||||
|
* 都是白色,于是缩略图全是"白模"——用户报的正是这个。
|
||||||
|
* 解码失败/非 PNG/隔行扫描时回退为 `baseColorFactor` 纯色,绝不抛错。
|
||||||
* 3. **稀疏 accessor(sparse)不支持**:遇到就直接跳过该 primitive。
|
* 3. **稀疏 accessor(sparse)不支持**:遇到就直接跳过该 primitive。
|
||||||
* 4. **只画三角形(mode=4)**,其余图元(点/线/条带/扇形)跳过。
|
* 4. **只画三角形(mode=4)**,其余图元(点/线/条带/扇形)跳过。
|
||||||
* 5. 大模型(>17M 索引)不会出现,索引统一按 uint32 读取。
|
* 5. 大模型(>17M 索引)不会出现,索引统一按 uint32 读取。
|
||||||
@@ -70,7 +74,8 @@ const DEFAULT_BACKGROUND: [number, number, number] = [24, 26, 32];
|
|||||||
/** 垂直视场角(度) */
|
/** 垂直视场角(度) */
|
||||||
const FOV_Y_DEG = 35;
|
const FOV_Y_DEG = 35;
|
||||||
/** 相机距离系数:让模型占画面约 1/1.6,保证不裁剪 */
|
/** 相机距离系数:让模型占画面约 1/1.6,保证不裁剪 */
|
||||||
const FRAMING = 1.6;
|
// 1.6 -> 1.3:列表页只有 40px,主体太小就只剩一团像素
|
||||||
|
const FRAMING = 1.3;
|
||||||
/** 主光方向(世界空间,未归一化,用前会归一化) */
|
/** 主光方向(世界空间,未归一化,用前会归一化) */
|
||||||
const LIGHT_DIR: [number, number, number] = normalize3(0.4, 0.8, 0.6);
|
const LIGHT_DIR: [number, number, number] = normalize3(0.4, 0.8, 0.6);
|
||||||
/** 环境光强度 */
|
/** 环境光强度 */
|
||||||
@@ -244,8 +249,27 @@ interface GltfSkin {
|
|||||||
interface GltfMaterial {
|
interface GltfMaterial {
|
||||||
pbrMetallicRoughness?: {
|
pbrMetallicRoughness?: {
|
||||||
baseColorFactor?: number[];
|
baseColorFactor?: number[];
|
||||||
baseColorTexture?: unknown;
|
baseColorTexture?: { index?: number; texCoord?: number };
|
||||||
};
|
};
|
||||||
|
alphaMode?: string;
|
||||||
|
alphaCutoff?: number;
|
||||||
|
doubleSided?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface GltfTexture {
|
||||||
|
source?: number;
|
||||||
|
sampler?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface GltfImage {
|
||||||
|
bufferView?: number;
|
||||||
|
mimeType?: string;
|
||||||
|
uri?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface GltfSampler {
|
||||||
|
wrapS?: number;
|
||||||
|
wrapT?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface GltfScene {
|
interface GltfScene {
|
||||||
@@ -262,6 +286,9 @@ interface GltfDocument {
|
|||||||
bufferViews?: GltfBufferView[];
|
bufferViews?: GltfBufferView[];
|
||||||
buffers?: GltfBufferRef[];
|
buffers?: GltfBufferRef[];
|
||||||
materials?: GltfMaterial[];
|
materials?: GltfMaterial[];
|
||||||
|
textures?: GltfTexture[];
|
||||||
|
images?: GltfImage[];
|
||||||
|
samplers?: GltfSampler[];
|
||||||
skins?: GltfSkin[];
|
skins?: GltfSkin[];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -278,9 +305,26 @@ interface AccessorData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** 收集好的三角形:positions 每 9 个数一个三角形,colors 每 3 个数一个三角形 */
|
/** 收集好的三角形:positions 每 9 个数一个三角形,colors 每 3 个数一个三角形 */
|
||||||
|
interface DecodedImage {
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
/** RGBA,行优先,长度 = width*height*4 */
|
||||||
|
rgba: Uint8Array;
|
||||||
|
}
|
||||||
|
|
||||||
interface SceneGeometry {
|
interface SceneGeometry {
|
||||||
positions: Float32Array;
|
positions: Float32Array;
|
||||||
colors: Float32Array;
|
colors: Float32Array;
|
||||||
|
/** 每顶点 UV(2 个/顶点);没有 TEXCOORD_0 时为 null */
|
||||||
|
uvs: Float32Array | null;
|
||||||
|
/** 每顶点贴图槽位(索引进 textures);-1 表示该顶点无贴图 */
|
||||||
|
texSlots: Int32Array | null;
|
||||||
|
/** 每顶点 alpha 模式:0=OPAQUE 1=MASK 2=BLEND */
|
||||||
|
alphaModes: Uint8Array | null;
|
||||||
|
/** 每顶点 MASK 阈值 */
|
||||||
|
cutoffs: Float32Array | null;
|
||||||
|
/** 解码后的贴图(RGBA) */
|
||||||
|
textures: DecodedImage[];
|
||||||
triangleCount: number;
|
triangleCount: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -667,6 +711,36 @@ function materialBaseColor(doc: GltfDocument, materialIndex: number | undefined)
|
|||||||
return [0.8, 0.8, 0.8];
|
return [0.8, 0.8, 0.8];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 取 bufferView 的原始字节(贴图解压用);越界返回 null */
|
||||||
|
function readBufferViewBytes(
|
||||||
|
doc: GltfDocument,
|
||||||
|
buffers: Array<Buffer | null>,
|
||||||
|
bufferViewIndex: number,
|
||||||
|
): Buffer | null {
|
||||||
|
const view = doc.bufferViews?.[bufferViewIndex];
|
||||||
|
if (!view || typeof view.buffer !== 'number') return null;
|
||||||
|
const buffer = buffers[view.buffer];
|
||||||
|
if (!buffer) return null;
|
||||||
|
const offset = view.byteOffset ?? 0;
|
||||||
|
const length = view.byteLength ?? 0;
|
||||||
|
if (offset < 0 || length <= 0 || offset + length > buffer.length) return null;
|
||||||
|
return buffer.subarray(offset, offset + length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 解码 GLB 内嵌的 PNG 贴图;外部 uri / 非 PNG / 解码失败都返回 null(回退纯色) */
|
||||||
|
function decodeEmbeddedImage(
|
||||||
|
doc: GltfDocument,
|
||||||
|
buffers: Array<Buffer | null>,
|
||||||
|
imageIndex: number,
|
||||||
|
): DecodedImage | null {
|
||||||
|
const image = doc.images?.[imageIndex];
|
||||||
|
if (!image || typeof image.bufferView !== 'number') return null;
|
||||||
|
if (typeof image.mimeType === 'string' && !image.mimeType.toLowerCase().includes('png')) return null;
|
||||||
|
const bytes = readBufferViewBytes(doc, buffers, image.bufferView);
|
||||||
|
if (!bytes) return null;
|
||||||
|
return decodePng(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
/** 遍历场景图收集世界空间三角形 */
|
/** 遍历场景图收集世界空间三角形 */
|
||||||
function collectTriangles(container: GltfContainer): SceneGeometry | null {
|
function collectTriangles(container: GltfContainer): SceneGeometry | null {
|
||||||
const { doc, buffers } = container;
|
const { doc, buffers } = container;
|
||||||
@@ -676,8 +750,49 @@ function collectTriangles(container: GltfContainer): SceneGeometry | null {
|
|||||||
|
|
||||||
const positions: number[] = [];
|
const positions: number[] = [];
|
||||||
const colors: number[] = [];
|
const colors: number[] = [];
|
||||||
|
const uvs: number[] = [];
|
||||||
|
const texSlots: number[] = [];
|
||||||
|
const alphaModes: number[] = [];
|
||||||
|
const cutoffs: number[] = [];
|
||||||
|
const textures: DecodedImage[] = [];
|
||||||
|
const materialTextureCache = new Map<number, { slot: number; alphaMode: number; cutoff: number }>();
|
||||||
|
const imageCache = new Map<number, DecodedImage | null>();
|
||||||
const skinScratch = new Float64Array(16);
|
const skinScratch = new Float64Array(16);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析材质的 baseColorTexture:解码一次并登记槽位。
|
||||||
|
* MMD 模型的 baseColorFactor 基本都是白色,颜色全在贴图里 —— 不解码就是"白模"。
|
||||||
|
*/
|
||||||
|
const resolveMaterialTexture = (
|
||||||
|
materialIndex: number | undefined,
|
||||||
|
): { slot: number; alphaMode: number; cutoff: number } => {
|
||||||
|
const key = typeof materialIndex === 'number' ? materialIndex : -1;
|
||||||
|
const cached = materialTextureCache.get(key);
|
||||||
|
if (cached) return cached;
|
||||||
|
const material = typeof materialIndex === 'number' ? doc.materials?.[materialIndex] : undefined;
|
||||||
|
const alphaMode = material?.alphaMode === 'MASK' ? 1 : material?.alphaMode === 'BLEND' ? 2 : 0;
|
||||||
|
const rawCutoff = typeof material?.alphaCutoff === 'number' ? material.alphaCutoff : 0.5;
|
||||||
|
const cutoff = clamp(rawCutoff, 0, 1);
|
||||||
|
let slot = -1;
|
||||||
|
const textureIndex = material?.pbrMetallicRoughness?.baseColorTexture?.index;
|
||||||
|
if (typeof textureIndex === 'number') {
|
||||||
|
const imageIndex = doc.textures?.[textureIndex]?.source;
|
||||||
|
if (typeof imageIndex === 'number') {
|
||||||
|
if (!imageCache.has(imageIndex)) {
|
||||||
|
imageCache.set(imageIndex, decodeEmbeddedImage(doc, buffers, imageIndex));
|
||||||
|
}
|
||||||
|
const image = imageCache.get(imageIndex) ?? null;
|
||||||
|
if (image) {
|
||||||
|
slot = textures.length;
|
||||||
|
textures.push(image);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const resolved = { slot, alphaMode, cutoff };
|
||||||
|
materialTextureCache.set(key, resolved);
|
||||||
|
return resolved;
|
||||||
|
};
|
||||||
|
|
||||||
const appendMesh = (nodeIndex: number): void => {
|
const appendMesh = (nodeIndex: number): void => {
|
||||||
const node = nodes[nodeIndex];
|
const node = nodes[nodeIndex];
|
||||||
if (!node || typeof node.mesh !== 'number') return;
|
if (!node || typeof node.mesh !== 'number') return;
|
||||||
@@ -710,6 +825,10 @@ function collectTriangles(container: GltfContainer): SceneGeometry | null {
|
|||||||
|
|
||||||
const vertexCount = positionData.count;
|
const vertexCount = positionData.count;
|
||||||
const [cr, cg, cb] = materialBaseColor(doc, primitive.material);
|
const [cr, cg, cb] = materialBaseColor(doc, primitive.material);
|
||||||
|
const materialTexture = resolveMaterialTexture(primitive.material);
|
||||||
|
const uvData =
|
||||||
|
materialTexture.slot >= 0 ? readAccessorFloats(doc, buffers, attributes.TEXCOORD_0, 2) : null;
|
||||||
|
const usableUv = uvData && uvData.count === vertexCount ? uvData : null;
|
||||||
|
|
||||||
// 顶点只变换一次:蒙皮或节点世界矩阵
|
// 顶点只变换一次:蒙皮或节点世界矩阵
|
||||||
const px = new Float64Array(vertexCount);
|
const px = new Float64Array(vertexCount);
|
||||||
@@ -758,6 +877,18 @@ function collectTriangles(container: GltfContainer): SceneGeometry | null {
|
|||||||
}
|
}
|
||||||
positions.push(x0, y0, z0, x1, y1, z1, x2, y2, z2);
|
positions.push(x0, y0, z0, x1, y1, z1, x2, y2, z2);
|
||||||
colors.push(cr, cg, cb);
|
colors.push(cr, cg, cb);
|
||||||
|
if (usableUv) {
|
||||||
|
uvs.push(
|
||||||
|
usableUv.values[i0 * 2], usableUv.values[i0 * 2 + 1],
|
||||||
|
usableUv.values[i1 * 2], usableUv.values[i1 * 2 + 1],
|
||||||
|
usableUv.values[i2 * 2], usableUv.values[i2 * 2 + 1],
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
uvs.push(0, 0, 0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
texSlots.push(materialTexture.slot, materialTexture.slot, materialTexture.slot);
|
||||||
|
alphaModes.push(materialTexture.alphaMode, materialTexture.alphaMode, materialTexture.alphaMode);
|
||||||
|
cutoffs.push(materialTexture.cutoff, materialTexture.cutoff, materialTexture.cutoff);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -784,6 +915,11 @@ function collectTriangles(container: GltfContainer): SceneGeometry | null {
|
|||||||
return {
|
return {
|
||||||
positions: Float32Array.from(positions),
|
positions: Float32Array.from(positions),
|
||||||
colors: Float32Array.from(colors),
|
colors: Float32Array.from(colors),
|
||||||
|
uvs: Float32Array.from(uvs),
|
||||||
|
texSlots: Int32Array.from(texSlots),
|
||||||
|
alphaModes: Uint8Array.from(alphaModes),
|
||||||
|
cutoffs: Float32Array.from(cutoffs),
|
||||||
|
textures,
|
||||||
triangleCount,
|
triangleCount,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -831,13 +967,25 @@ function edge(ax: number, ay: number, bx: number, by: number, px: number, py: nu
|
|||||||
* 屏幕空间有符号面积为负的是正面(NDC 里 CCW 正面经 y 翻转后变成负),
|
* 屏幕空间有符号面积为负的是正面(NDC 里 CCW 正面经 y 翻转后变成负),
|
||||||
* 因此 cullBackFace 时丢弃 area >= 0 的三角形。
|
* 因此 cullBackFace 时丢弃 area >= 0 的三角形。
|
||||||
*/
|
*/
|
||||||
|
/** 采样贴图(REPEAT 环绕;glTF/PNG 的 UV 原点在左上,v 不翻转) */
|
||||||
|
function sampleTexture(image: DecodedImage, u: number, v: number, out: Float32Array): void {
|
||||||
|
const wrap = (value: number): number => value - Math.floor(value);
|
||||||
|
const x = Math.min(image.width - 1, Math.floor(wrap(u) * image.width));
|
||||||
|
const y = Math.min(image.height - 1, Math.floor(wrap(v) * image.height));
|
||||||
|
const o = (y * image.width + x) * 4;
|
||||||
|
out[0] = image.rgba[o] / 255;
|
||||||
|
out[1] = image.rgba[o + 1] / 255;
|
||||||
|
out[2] = image.rgba[o + 2] / 255;
|
||||||
|
out[3] = image.rgba[o + 3] / 255;
|
||||||
|
}
|
||||||
|
|
||||||
function rasterizeTriangles(
|
function rasterizeTriangles(
|
||||||
geometry: SceneGeometry,
|
geometry: SceneGeometry,
|
||||||
viewProj: Mat4,
|
viewProj: Mat4,
|
||||||
options: RasterOptions,
|
options: RasterOptions,
|
||||||
): RasterOutput {
|
): RasterOutput {
|
||||||
const { size, background, cullBackFace } = options;
|
const { size, background, cullBackFace } = options;
|
||||||
const { positions, colors, triangleCount } = geometry;
|
const { positions, colors, uvs, texSlots, alphaModes, cutoffs, textures, triangleCount } = geometry;
|
||||||
|
|
||||||
const pixels = Buffer.alloc(size * size * 3);
|
const pixels = Buffer.alloc(size * size * 3);
|
||||||
for (let i = 0; i < size * size; i += 1) {
|
for (let i = 0; i < size * size; i += 1) {
|
||||||
@@ -909,9 +1057,23 @@ function rasterizeTriangles(
|
|||||||
const baseR = colors[t * 3];
|
const baseR = colors[t * 3];
|
||||||
const baseG = colors[t * 3 + 1];
|
const baseG = colors[t * 3 + 1];
|
||||||
const baseB = colors[t * 3 + 2];
|
const baseB = colors[t * 3 + 2];
|
||||||
const r = clamp(Math.round(baseR * intensity * 255), 0, 255);
|
|
||||||
const g = clamp(Math.round(baseG * intensity * 255), 0, 255);
|
// 贴图与透明度(每三角形常量;顶点数组按 3 顶点/三角形展开)
|
||||||
const b = clamp(Math.round(baseB * intensity * 255), 0, 255);
|
const slot = texSlots ? texSlots[t * 3] : -1;
|
||||||
|
const image = slot >= 0 ? textures[slot] ?? null : null;
|
||||||
|
const alphaMode = alphaModes ? alphaModes[t * 3] : 0;
|
||||||
|
const cutoff = cutoffs ? cutoffs[t * 3] : 0.5;
|
||||||
|
const u0 = uvs ? uvs[t * 6] : 0;
|
||||||
|
const v0 = uvs ? uvs[t * 6 + 1] : 0;
|
||||||
|
const u1 = uvs ? uvs[t * 6 + 2] : 0;
|
||||||
|
const v1 = uvs ? uvs[t * 6 + 3] : 0;
|
||||||
|
const u2 = uvs ? uvs[t * 6 + 4] : 0;
|
||||||
|
const v2 = uvs ? uvs[t * 6 + 5] : 0;
|
||||||
|
const texel = new Float32Array(4);
|
||||||
|
// 透视校正用:1/w 在三个顶点上的值
|
||||||
|
const iw0 = 1 / p0[3];
|
||||||
|
const iw1 = 1 / p1[3];
|
||||||
|
const iw2 = 1 / p2[3];
|
||||||
|
|
||||||
const invArea = 1 / area;
|
const invArea = 1 / area;
|
||||||
for (let y = minY; y <= maxY; y += 1) {
|
for (let y = minY; y <= maxY; y += 1) {
|
||||||
@@ -926,6 +1088,27 @@ function rasterizeTriangles(
|
|||||||
const d = w0 * p0[2] + w1 * p1[2] + w2 * p2[2];
|
const d = w0 * p0[2] + w1 * p1[2] + w2 * p2[2];
|
||||||
const index = rowBase + x;
|
const index = rowBase + x;
|
||||||
if (d < depth[index]) {
|
if (d < depth[index]) {
|
||||||
|
let r = clamp(Math.round(baseR * intensity * 255), 0, 255);
|
||||||
|
let g = clamp(Math.round(baseG * intensity * 255), 0, 255);
|
||||||
|
let b = clamp(Math.round(baseB * intensity * 255), 0, 255);
|
||||||
|
if (image) {
|
||||||
|
const iw = w0 * iw0 + w1 * iw1 + w2 * iw2;
|
||||||
|
const u = (w0 * iw0 * u0 + w1 * iw1 * u1 + w2 * iw2 * u2) / (iw || 1);
|
||||||
|
const v = (w0 * iw0 * v0 + w1 * iw1 * v1 + w2 * iw2 * v2) / (iw || 1);
|
||||||
|
sampleTexture(image, u, v, texel);
|
||||||
|
if (alphaMode === 1 && texel[3] < cutoff) continue; // MASK:整像素丢弃
|
||||||
|
const a = alphaMode === 0 ? 1 : texel[3];
|
||||||
|
r = clamp(Math.round(baseR * texel[0] * intensity * 255), 0, 255);
|
||||||
|
g = clamp(Math.round(baseG * texel[1] * intensity * 255), 0, 255);
|
||||||
|
b = clamp(Math.round(baseB * texel[2] * intensity * 255), 0, 255);
|
||||||
|
if (alphaMode === 2 && a < 1) {
|
||||||
|
// BLEND:与背景/已画像素做 source-over(缩略图不做排序,够看即可)
|
||||||
|
const q = index * 3;
|
||||||
|
r = clamp(Math.round(r * a + pixels[q] * (1 - a)), 0, 255);
|
||||||
|
g = clamp(Math.round(g * a + pixels[q + 1] * (1 - a)), 0, 255);
|
||||||
|
b = clamp(Math.round(b * a + pixels[q + 2] * (1 - a)), 0, 255);
|
||||||
|
}
|
||||||
|
}
|
||||||
depth[index] = d;
|
depth[index] = d;
|
||||||
const p = index * 3;
|
const p = index * 3;
|
||||||
pixels[p] = r;
|
pixels[p] = r;
|
||||||
@@ -1045,6 +1228,133 @@ function buildViewProjection(geometry: SceneGeometry, size: number, yawDeg: numb
|
|||||||
return mat4Multiply(proj, view);
|
return mat4Multiply(proj, view);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// PNG 解码(缩略图要贴图才不是"白模";只做 8 位非隔行,够用且零原生依赖)
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 极简 PNG 解码器:zlib 解压 + 逐行反滤波,输出 RGBA。
|
||||||
|
* 不支持 16 位 / 隔行扫描 / 未知 colorType —— 返回 null 由调用方回退纯色。
|
||||||
|
*/
|
||||||
|
function decodePng(buffer: Buffer): DecodedImage | null {
|
||||||
|
if (buffer.length < 8 + 25 || !buffer.subarray(0, 8).equals(PNG_SIGNATURE)) return null;
|
||||||
|
let offset = 8;
|
||||||
|
let width = 0;
|
||||||
|
let height = 0;
|
||||||
|
let bitDepth = 0;
|
||||||
|
let colorType = 0;
|
||||||
|
let interlace = 0;
|
||||||
|
let palette: Uint8Array | null = null;
|
||||||
|
let transparency: Uint8Array | null = null;
|
||||||
|
const idat: Buffer[] = [];
|
||||||
|
|
||||||
|
while (offset + 8 <= buffer.length) {
|
||||||
|
const length = buffer.readUInt32BE(offset);
|
||||||
|
const type = buffer.toString('ascii', offset + 4, offset + 8);
|
||||||
|
const dataStart = offset + 8;
|
||||||
|
if (dataStart + length + 4 > buffer.length) return null;
|
||||||
|
const data = buffer.subarray(dataStart, dataStart + length);
|
||||||
|
if (type === 'IHDR') {
|
||||||
|
if (length < 13) return null;
|
||||||
|
width = data.readUInt32BE(0);
|
||||||
|
height = data.readUInt32BE(4);
|
||||||
|
bitDepth = data[8];
|
||||||
|
colorType = data[9];
|
||||||
|
interlace = data[12];
|
||||||
|
} else if (type === 'PLTE') {
|
||||||
|
palette = new Uint8Array(data);
|
||||||
|
} else if (type === 'tRNS') {
|
||||||
|
transparency = new Uint8Array(data);
|
||||||
|
} else if (type === 'IDAT') {
|
||||||
|
idat.push(Buffer.from(data));
|
||||||
|
} else if (type === 'IEND') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
offset = dataStart + length + 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bitDepth !== 8 || interlace !== 0) return null;
|
||||||
|
if (!(width > 0) || !(height > 0) || width * height > 64 * 1024 * 1024) return null;
|
||||||
|
if (idat.length === 0) return null;
|
||||||
|
|
||||||
|
const channels =
|
||||||
|
colorType === 0 ? 1 : colorType === 2 ? 3 : colorType === 3 ? 1 : colorType === 4 ? 2 : colorType === 6 ? 4 : 0;
|
||||||
|
if (channels === 0) return null;
|
||||||
|
if (colorType === 3 && !palette) return null;
|
||||||
|
|
||||||
|
let raw: Buffer;
|
||||||
|
try {
|
||||||
|
raw = inflateSync(Buffer.concat(idat));
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const stride = width * channels;
|
||||||
|
if (raw.length < (stride + 1) * height) return null;
|
||||||
|
|
||||||
|
const out = new Uint8Array(width * height * 4);
|
||||||
|
const line = new Uint8Array(stride);
|
||||||
|
const prev = new Uint8Array(stride);
|
||||||
|
let rawPos = 0;
|
||||||
|
for (let y = 0; y < height; y += 1) {
|
||||||
|
const filter = raw[rawPos];
|
||||||
|
rawPos += 1;
|
||||||
|
for (let i = 0; i < stride; i += 1) {
|
||||||
|
const value = raw[rawPos + i];
|
||||||
|
const left = i >= channels ? line[i - channels] : 0;
|
||||||
|
const up = prev[i];
|
||||||
|
const upLeft = i >= channels ? prev[i - channels] : 0;
|
||||||
|
let restored: number;
|
||||||
|
switch (filter) {
|
||||||
|
case 0: restored = value; break;
|
||||||
|
case 1: restored = value + left; break;
|
||||||
|
case 2: restored = value + up; break;
|
||||||
|
case 3: restored = value + ((left + up) >> 1); break;
|
||||||
|
case 4: {
|
||||||
|
const p = left + up - upLeft;
|
||||||
|
const pa = Math.abs(p - left);
|
||||||
|
const pb = Math.abs(p - up);
|
||||||
|
const pc = Math.abs(p - upLeft);
|
||||||
|
restored = value + (pa <= pb && pa <= pc ? left : pb <= pc ? up : upLeft);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: return null;
|
||||||
|
}
|
||||||
|
line[i] = restored & 0xff;
|
||||||
|
}
|
||||||
|
rawPos += stride;
|
||||||
|
for (let x = 0; x < width; x += 1) {
|
||||||
|
const o = (y * width + x) * 4;
|
||||||
|
if (colorType === 6) {
|
||||||
|
out[o] = line[x * 4];
|
||||||
|
out[o + 1] = line[x * 4 + 1];
|
||||||
|
out[o + 2] = line[x * 4 + 2];
|
||||||
|
out[o + 3] = line[x * 4 + 3];
|
||||||
|
} else if (colorType === 2) {
|
||||||
|
out[o] = line[x * 3];
|
||||||
|
out[o + 1] = line[x * 3 + 1];
|
||||||
|
out[o + 2] = line[x * 3 + 2];
|
||||||
|
out[o + 3] = 255;
|
||||||
|
} else if (colorType === 0) {
|
||||||
|
const g = line[x];
|
||||||
|
out[o] = g; out[o + 1] = g; out[o + 2] = g; out[o + 3] = 255;
|
||||||
|
} else if (colorType === 4) {
|
||||||
|
const g = line[x * 2];
|
||||||
|
out[o] = g; out[o + 1] = g; out[o + 2] = g; out[o + 3] = line[x * 2 + 1];
|
||||||
|
} else {
|
||||||
|
const index = line[x];
|
||||||
|
out[o] = palette![index * 3] ?? 0;
|
||||||
|
out[o + 1] = palette![index * 3 + 1] ?? 0;
|
||||||
|
out[o + 2] = palette![index * 3 + 2] ?? 0;
|
||||||
|
out[o + 3] = transparency && index < transparency.length ? transparency[index] : 255;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
prev.set(line);
|
||||||
|
}
|
||||||
|
return { width, height, rgba: out };
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// PNG 编码(8 位 RGB、无隔行、filter=0;不引第三方库)
|
// PNG 编码(8 位 RGB、无隔行、filter=0;不引第三方库)
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -1133,6 +1443,8 @@ export async function renderModelThumbnail(
|
|||||||
) {
|
) {
|
||||||
throw new Error('缩略图背景色非法:需要 [r, g, b] 三个有限数字');
|
throw new Error('缩略图背景色非法:需要 [r, g, b] 三个有限数字');
|
||||||
}
|
}
|
||||||
|
// 默认看**正面**:eye = 中心 + (sin yaw, .., cos yaw)*d,yaw=0 时相机在 +Z;
|
||||||
|
// MMD(PMX)与 VRM 1.0 都是 +Z 朝前,因此 -30 是"正面偏 30 度"的常规 3/4 视角
|
||||||
const yawDeg = options?.yawDeg ?? -30;
|
const yawDeg = options?.yawDeg ?? -30;
|
||||||
const rawPitchDeg = options?.pitchDeg ?? 12;
|
const rawPitchDeg = options?.pitchDeg ?? 12;
|
||||||
if (!Number.isFinite(yawDeg) || !Number.isFinite(rawPitchDeg)) {
|
if (!Number.isFinite(yawDeg) || !Number.isFinite(rawPitchDeg)) {
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ import {
|
|||||||
convertModelFile,
|
convertModelFile,
|
||||||
importModelFile,
|
importModelFile,
|
||||||
} from './model-convert';
|
} from './model-convert';
|
||||||
import { ModelStats, inspectModelFile } from './model-inspector';
|
import { ModelStats, inspectModelFile, shrinkStatsForStorage } from './model-inspector';
|
||||||
import { renderModelThumbnail } from './model-thumbnail';
|
import { renderModelThumbnail } from './model-thumbnail';
|
||||||
import { CharacterModel } from '../entities/model.entity';
|
import { CharacterModel } from '../entities/model.entity';
|
||||||
import { AppConfig } from '../config/configuration';
|
import { AppConfig } from '../config/configuration';
|
||||||
@@ -38,6 +38,21 @@ import { QueryModelDto, UpdateModelDto } from './model.dto';
|
|||||||
import { ModelDto, toModelDto } from './model.serializer';
|
import { ModelDto, toModelDto } from './model.serializer';
|
||||||
import { inspectVrm } from './vrm-inspector';
|
import { inspectVrm } from './vrm-inspector';
|
||||||
|
|
||||||
|
/** 判断是否为「数据超长」类数据库错误(MySQL 1406 / MariaDB ER_DATA_TOO_LONG) */
|
||||||
|
function isDataTooLongError(error: unknown): boolean {
|
||||||
|
const err = error as {
|
||||||
|
code?: string;
|
||||||
|
errno?: number;
|
||||||
|
driverError?: { code?: string; errno?: number };
|
||||||
|
message?: string;
|
||||||
|
};
|
||||||
|
const code = err.driverError?.code ?? err.code;
|
||||||
|
const errno = err.driverError?.errno ?? err.errno;
|
||||||
|
if (errno === 1406) return true;
|
||||||
|
if (typeof code === 'string' && /DATA_TOO_LONG/i.test(code)) return true;
|
||||||
|
return typeof err.message === 'string' && /Data too long/i.test(err.message);
|
||||||
|
}
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ModelsService {
|
export class ModelsService {
|
||||||
private readonly logger = new Logger(ModelsService.name);
|
private readonly logger = new Logger(ModelsService.name);
|
||||||
@@ -202,7 +217,7 @@ export class ModelsService {
|
|||||||
// 上传一律待审核:模型必须过审核才可能被客户端消费
|
// 上传一律待审核:模型必须过审核才可能被客户端消费
|
||||||
status: PublishStatus.Pending,
|
status: PublishStatus.Pending,
|
||||||
});
|
});
|
||||||
const saved = await this.models.save(model);
|
const saved = await this.saveWithStatsFallback(model);
|
||||||
this.logger.log(
|
this.logger.log(
|
||||||
`模型已入库待审:#${saved.id} ${name}(${imported.format},${imported.direct ? '直接导入' : '转换'})` +
|
`模型已入库待审:#${saved.id} ${name}(${imported.format},${imported.direct ? '直接导入' : '转换'})` +
|
||||||
` 面数=${stats.triangles} 骨骼=${stats.bones} humanoid=${stats.humanoidMapped}/${stats.humanoidTotal}` +
|
` 面数=${stats.triangles} 骨骼=${stats.bones} humanoid=${stats.humanoidMapped}/${stats.humanoidTotal}` +
|
||||||
@@ -241,7 +256,7 @@ export class ModelsService {
|
|||||||
if (thumbnailAssetId !== null) model.thumbnailAssetId = thumbnailAssetId;
|
if (thumbnailAssetId !== null) model.thumbnailAssetId = thumbnailAssetId;
|
||||||
if (stats.vrmVersion) model.vrmVersion = stats.vrmVersion;
|
if (stats.vrmVersion) model.vrmVersion = stats.vrmVersion;
|
||||||
|
|
||||||
const saved = await this.models.save(model);
|
const saved = await this.saveWithStatsFallback(model);
|
||||||
this.logger.log(
|
this.logger.log(
|
||||||
`模型 #${id} 重新分析完成:面数=${stats.triangles} 骨骼=${stats.bones} ` +
|
`模型 #${id} 重新分析完成:面数=${stats.triangles} 骨骼=${stats.bones} ` +
|
||||||
`humanoid=${stats.humanoidMapped}/${stats.humanoidTotal} 可驱动=${stats.drivable}` +
|
`humanoid=${stats.humanoidMapped}/${stats.humanoidTotal} 可驱动=${stats.drivable}` +
|
||||||
@@ -251,6 +266,30 @@ export class ModelsService {
|
|||||||
return dto;
|
return dto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存模型;若因 `stats` 超长被数据库拒绝,裁剪后重试一次。
|
||||||
|
*
|
||||||
|
* 骨骼树上千根的大模型并不罕见:TEXT(64KB) 时代实测 643 骨骼的 PMX 就会
|
||||||
|
* 触发 1406 报错,而这一步抛出去就是 500。列已迁到 MEDIUMTEXT,
|
||||||
|
* 这里再兜一层,保证「分析结果太大」永远不会让上传失败。
|
||||||
|
*/
|
||||||
|
private async saveWithStatsFallback(model: CharacterModel): Promise<CharacterModel> {
|
||||||
|
try {
|
||||||
|
return await this.models.save(model);
|
||||||
|
} catch (error) {
|
||||||
|
if (!isDataTooLongError(error)) throw error;
|
||||||
|
const original = model.stats as unknown as ModelStats | null;
|
||||||
|
if (!original) throw error;
|
||||||
|
const shrunk = shrinkStatsForStorage(original);
|
||||||
|
this.logger.warn(
|
||||||
|
`模型「${model.name}」分析结果超出数据库字段上限(${JSON.stringify(original).length} 字节),` +
|
||||||
|
`已裁剪骨骼树至 ${shrunk.boneTree.length} 条后重试`,
|
||||||
|
);
|
||||||
|
model.stats = shrunk as unknown as Record<string, unknown>;
|
||||||
|
return await this.models.save(model);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** 分析模型;任何异常都降级为一条 warning,不阻断上传 */
|
/** 分析模型;任何异常都降级为一条 warning,不阻断上传 */
|
||||||
private async analyzeSafely(
|
private async analyzeSafely(
|
||||||
absolutePath: string,
|
absolutePath: string,
|
||||||
|
|||||||
@@ -22,6 +22,11 @@ interface FakeEntry {
|
|||||||
encrypted?: boolean;
|
encrypted?: boolean;
|
||||||
/** 未知压缩方式 */
|
/** 未知压缩方式 */
|
||||||
badMethod?: number;
|
badMethod?: number;
|
||||||
|
/**
|
||||||
|
* 直接指定文件名的原始字节,且**不置 UTF-8 标志位**(模拟 GBK / Shift-JIS 压缩包)。
|
||||||
|
* 中文 MMD 压缩包很常见:有的条目是 UTF-8、有的是 GBK,且都没打标志位。
|
||||||
|
*/
|
||||||
|
rawName?: Buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 手写一个 ZIP(测试用;不依赖任何第三方库) */
|
/** 手写一个 ZIP(测试用;不依赖任何第三方库) */
|
||||||
@@ -31,7 +36,9 @@ function buildZip(entries: FakeEntry[]): Buffer {
|
|||||||
let offset = 0;
|
let offset = 0;
|
||||||
|
|
||||||
for (const entry of entries) {
|
for (const entry of entries) {
|
||||||
const name = Buffer.from(entry.path, 'utf8');
|
const name = entry.rawName ?? Buffer.from(entry.path, 'utf8');
|
||||||
|
// 给了 rawName 就当作「未声明 UTF-8」的老压缩包
|
||||||
|
const nameFlags = entry.rawName ? (entry.encrypted ? 0x0001 : 0) : (entry.encrypted ? 0x0001 : 0) | 0x0800;
|
||||||
const method = entry.badMethod ?? entry.method ?? 8;
|
const method = entry.badMethod ?? entry.method ?? 8;
|
||||||
const raw = entry.content;
|
const raw = entry.content;
|
||||||
const data = method === 8 ? deflateRawSync(raw) : raw;
|
const data = method === 8 ? deflateRawSync(raw) : raw;
|
||||||
@@ -40,7 +47,7 @@ function buildZip(entries: FakeEntry[]): Buffer {
|
|||||||
const local = Buffer.alloc(30 + name.length);
|
const local = Buffer.alloc(30 + name.length);
|
||||||
local.writeUInt32LE(0x04034b50, 0);
|
local.writeUInt32LE(0x04034b50, 0);
|
||||||
local.writeUInt16LE(20, 4);
|
local.writeUInt16LE(20, 4);
|
||||||
local.writeUInt16LE((entry.encrypted ? 0x0001 : 0) | 0x0800, 6);
|
local.writeUInt16LE(nameFlags, 6);
|
||||||
local.writeUInt16LE(method, 8);
|
local.writeUInt16LE(method, 8);
|
||||||
local.writeUInt32LE(checksum, 14);
|
local.writeUInt32LE(checksum, 14);
|
||||||
local.writeUInt32LE(data.length, 18);
|
local.writeUInt32LE(data.length, 18);
|
||||||
@@ -53,7 +60,7 @@ function buildZip(entries: FakeEntry[]): Buffer {
|
|||||||
central.writeUInt32LE(0x02014b50, 0);
|
central.writeUInt32LE(0x02014b50, 0);
|
||||||
central.writeUInt16LE(20, 4);
|
central.writeUInt16LE(20, 4);
|
||||||
central.writeUInt16LE(20, 6);
|
central.writeUInt16LE(20, 6);
|
||||||
central.writeUInt16LE((entry.encrypted ? 0x0001 : 0) | 0x0800, 8);
|
central.writeUInt16LE(nameFlags, 8);
|
||||||
central.writeUInt16LE(method, 10);
|
central.writeUInt16LE(method, 10);
|
||||||
central.writeUInt32LE(checksum, 16);
|
central.writeUInt32LE(checksum, 16);
|
||||||
central.writeUInt32LE(data.length, 20);
|
central.writeUInt32LE(data.length, 20);
|
||||||
@@ -220,6 +227,35 @@ describe('ZIP 读取器', () => {
|
|||||||
expect(findZipEntry(entries, 'HATSUNE MIKU.PMX')?.path).toBe('Hatsune Miku.pmx');
|
expect(findZipEntry(entries, 'HATSUNE MIKU.PMX')?.path).toBe('Hatsune Miku.pmx');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('GBK 压缩包:Shift-JIS 解出私用区乱码时回退 GBK(发/髮 这类字不再丢字符)', () => {
|
||||||
|
// b'tex/\xf3\x8c.png' 是 GBK 的「tex/髮.png」;Shift-JIS 会把它解成一个
|
||||||
|
// 私用区不可见字符(不是 U+FFFD),旧实现会直接采信,导致贴图名对不上而丢图。
|
||||||
|
const zip = buildZip([
|
||||||
|
{ path: 'raw', content: Buffer.from('PMX '.repeat(50)), rawName: Buffer.from([0x74, 0x65, 0x78, 0x2f, 0xf3, 0x8c, 0x2e, 0x70, 0x6e, 0x67]) },
|
||||||
|
]);
|
||||||
|
const entries = readZipEntries(zip);
|
||||||
|
expect(entries.map((e) => e.path)).toEqual(['tex/髮.png']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Shift-JIS 名字仍然优先按 Shift-JIS 解(日文 MMD 压缩包)', () => {
|
||||||
|
// b'\x83\x41\x83\x43.pmx' 是 Shift-JIS 的「アイ.pmx」(半角片假名判断的姊妹用例)
|
||||||
|
const zip = buildZip([
|
||||||
|
{ path: 'raw', content: Buffer.from('PMX '.repeat(50)), rawName: Buffer.from([0x83, 0x41, 0x83, 0x43, 0x2e, 0x70, 0x6d, 0x78]) },
|
||||||
|
]);
|
||||||
|
const entries = readZipEntries(zip);
|
||||||
|
expect(entries[0].path).toBe('アイ.pmx');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('目录条目不会当成文件(避免解压时把目录当文件写)', () => {
|
||||||
|
const zip = buildZip([
|
||||||
|
{ path: 'spa/', content: Buffer.alloc(0), method: 0 },
|
||||||
|
{ path: 'spa/a.png', content: Buffer.from('PNG') },
|
||||||
|
{ path: '奥黛塔.pmx', content: Buffer.from('PMX '.repeat(50)) },
|
||||||
|
]);
|
||||||
|
const entries = readZipEntries(zip);
|
||||||
|
expect(entries.map((e) => e.path)).toEqual(['spa/a.png', '奥黛塔.pmx']);
|
||||||
|
});
|
||||||
|
|
||||||
it('拒绝上跳路径(zip-slip)', () => {
|
it('拒绝上跳路径(zip-slip)', () => {
|
||||||
const zip = buildZip([{ path: '../../etc/passwd', content: Buffer.from('x') }]);
|
const zip = buildZip([{ path: '../../etc/passwd', content: Buffer.from('x') }]);
|
||||||
expect(() => readZipEntries(zip)).toThrow(/上跳路径/);
|
expect(() => readZipEntries(zip)).toThrow(/上跳路径/);
|
||||||
|
|||||||
@@ -0,0 +1,187 @@
|
|||||||
|
import { readFileSync, readdirSync } from 'node:fs';
|
||||||
|
import { join, resolve } from 'node:path';
|
||||||
|
import { getMetadataArgsStorage } from 'typeorm';
|
||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
|
||||||
|
import { CharacterModel } from '../src/entities/model.entity';
|
||||||
|
import {
|
||||||
|
MYSQL_TEXT_LIMIT,
|
||||||
|
type BoneNode,
|
||||||
|
type ModelStats,
|
||||||
|
shrinkStatsForStorage,
|
||||||
|
} from '../src/models/model-inspector';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 骨骼树超出数据库字段上限导致的 500。
|
||||||
|
*
|
||||||
|
* 起因(真实故障):上传含 PMX 的压缩包(【奥黛塔】_by_原神.zip,643 根骨骼)
|
||||||
|
* 返回 500。转换本身成功,但分析结果里骨骼树按骨骼逐条展开,JSON 实测
|
||||||
|
* **70,980 字节** > `models.stats` 的 TEXT 上限 65,535,MySQL / MariaDB 严格模式
|
||||||
|
* 直接拒绝写入(ER_DATA_TOO_LONG / 1406),而写入没有兜底。
|
||||||
|
*
|
||||||
|
* 这里锁三件事:
|
||||||
|
* 1. 列必须是 MEDIUMTEXT(实体 + 迁移一致);
|
||||||
|
* 2. 服务层对「数据超长」有裁剪重试兜底;
|
||||||
|
* 3. 裁剪函数本身永不抛、且保留审核必需的汇总字段。
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** 造一个规模接近真实的骨骼树(【奥黛塔】实测 643 根、每根约 110 字节) */
|
||||||
|
function buildBoneTree(count: number): BoneNode[] {
|
||||||
|
return Array.from({ length: count }, (_, index) => ({
|
||||||
|
index,
|
||||||
|
name: `Bone_${String(index).padStart(4, '0')}_髪_腕捩_ねじ`,
|
||||||
|
depth: Math.min(index % 40, 35),
|
||||||
|
parentName: index === 0 ? null : `Bone_${String(Math.max(0, index - 1)).padStart(4, '0')}_髪_腕捩_ねじ`,
|
||||||
|
childCount: index % 3,
|
||||||
|
inSkin: index % 7 !== 0,
|
||||||
|
humanoid: index < 7 ? ['hips', 'spine', 'chest', 'neck', 'head', 'leftShoulder', 'leftUpperArm'][index] : null,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildStats(bones: number): ModelStats {
|
||||||
|
return {
|
||||||
|
container: 'glb',
|
||||||
|
vrmVersion: null,
|
||||||
|
fileSize: 23_700_000,
|
||||||
|
meshes: 12,
|
||||||
|
primitives: 18,
|
||||||
|
vertices: 120_000,
|
||||||
|
triangles: 93_350,
|
||||||
|
materials: 9,
|
||||||
|
textures: 17,
|
||||||
|
images: 17,
|
||||||
|
animations: 0,
|
||||||
|
morphTargets: 24,
|
||||||
|
nodes: 700,
|
||||||
|
bones,
|
||||||
|
skins: 1,
|
||||||
|
humanoidMapped: 0,
|
||||||
|
humanoidTotal: 22,
|
||||||
|
missingRequiredBones: ['hips', 'spine', 'chest', 'neck', 'head', 'leftUpperArm'],
|
||||||
|
drivable: false,
|
||||||
|
maxBoneDepth: 35,
|
||||||
|
boneTree: buildBoneTree(bones),
|
||||||
|
warnings: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function findMigrationsDir(): string {
|
||||||
|
const candidates = [
|
||||||
|
resolve(process.cwd(), 'src', 'migrations'),
|
||||||
|
resolve(process.cwd(), '..', 'src', 'migrations'),
|
||||||
|
];
|
||||||
|
for (const candidate of candidates) {
|
||||||
|
const files = readdirSync(candidate).filter((f) => f.endsWith('.ts'));
|
||||||
|
if (files.length > 0) return candidate;
|
||||||
|
}
|
||||||
|
throw new Error('找不到 src/migrations');
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('models.stats 字段容量', () => {
|
||||||
|
it('643 根骨骼的分析结果确实超过 TEXT 上限(这就是 500 的原因)', () => {
|
||||||
|
const json = JSON.stringify(buildStats(643));
|
||||||
|
expect(json.length).toBeGreaterThan(MYSQL_TEXT_LIMIT);
|
||||||
|
// 与真实故障量级一致(实测 70,980 字节)
|
||||||
|
expect(json.length).toBeGreaterThan(65_000);
|
||||||
|
expect(json.length).toBeLessThan(120_000);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('实体列型是 mediumtext,且能 JSON 往返(不再被 simple-json 钉死在 text)', () => {
|
||||||
|
const column = getMetadataArgsStorage().columns.find(
|
||||||
|
(item) => item.target === CharacterModel && item.propertyName === 'stats',
|
||||||
|
);
|
||||||
|
expect(column?.options.type).toBe('mediumtext');
|
||||||
|
const transformer = column?.options.transformer as
|
||||||
|
| { to?: (v: unknown) => unknown; from?: (v: unknown) => unknown }
|
||||||
|
| undefined;
|
||||||
|
expect(typeof transformer?.to).toBe('function');
|
||||||
|
expect(typeof transformer?.from).toBe('function');
|
||||||
|
const stats = buildStats(643);
|
||||||
|
const stored = transformer!.to!(stats) as string;
|
||||||
|
expect(typeof stored).toBe('string');
|
||||||
|
expect(JSON.parse(stored).bones).toBe(643);
|
||||||
|
expect((transformer!.from!(stored) as ModelStats).bones).toBe(643);
|
||||||
|
// 脏数据不能让接口 500
|
||||||
|
expect(transformer!.from!('{ 坏 JSON')).toEqual({ parseError: true, raw: '{ 坏 JSON' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('存在把 stats 扩到 MEDIUMTEXT 的迁移(install.sh 会自动执行)', () => {
|
||||||
|
const dir = findMigrationsDir();
|
||||||
|
const file = readdirSync(dir).find((f) => /AddModelStatsMediumText.*\.ts$/.test(f));
|
||||||
|
expect(file, '缺少 AddModelStatsMediumText 迁移').toBeTruthy();
|
||||||
|
const sql = readFileSync(join(dir, file as string), 'utf8');
|
||||||
|
// TS 源码里反引号是转义的,这里只断言关键字与方向
|
||||||
|
expect(sql).toMatch(/MODIFY/i);
|
||||||
|
expect(sql).toMatch(/mediumtext/i);
|
||||||
|
expect(sql).toMatch(/ALTER TABLE/i);
|
||||||
|
expect(sql).toMatch(/implements MigrationInterface/);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('shrinkStatsForStorage:写入前的兜底裁剪', () => {
|
||||||
|
it('没超限时原样返回(不做无谓修改)', () => {
|
||||||
|
const stats = buildStats(20);
|
||||||
|
expect(shrinkStatsForStorage(stats)).toBe(stats);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('超限时裁剪骨骼树到能放下,并追加一条说明用的 warning', () => {
|
||||||
|
const stats = buildStats(643);
|
||||||
|
const shrunk = shrinkStatsForStorage(stats);
|
||||||
|
expect(JSON.stringify(shrunk).length).toBeLessThanOrEqual(MYSQL_TEXT_LIMIT);
|
||||||
|
expect(shrunk.boneTree.length).toBeLessThan(stats.boneTree.length);
|
||||||
|
// 审核必需的汇总字段一个都不能少
|
||||||
|
expect(shrunk.triangles).toBe(93_350);
|
||||||
|
expect(shrunk.bones).toBe(643);
|
||||||
|
expect(shrunk.humanoidMapped).toBe(0);
|
||||||
|
expect(shrunk.humanoidTotal).toBe(22);
|
||||||
|
expect(shrunk.drivable).toBe(false);
|
||||||
|
expect(shrunk.missingRequiredBones).toEqual(stats.missingRequiredBones);
|
||||||
|
expect(shrunk.warnings.join()).toContain('已截断');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('保留的骨骼仍是父在前的祖先链(审核界面按 depth 缩进不会错乱)', () => {
|
||||||
|
const stats = buildStats(643);
|
||||||
|
const shrunk = shrinkStatsForStorage(stats);
|
||||||
|
const kept = shrunk.boneTree;
|
||||||
|
expect(kept.length).toBeGreaterThan(0);
|
||||||
|
for (let i = 1; i < kept.length; i += 1) {
|
||||||
|
expect(kept[i].depth).toBeLessThanOrEqual(kept[i - 1].depth + 1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('极端情况(连最小骨骼树都放不下)也不会抛,仍返回可存储的结果', () => {
|
||||||
|
const stats = buildStats(400);
|
||||||
|
const shrunk = shrinkStatsForStorage(stats, 512);
|
||||||
|
expect(JSON.stringify(shrunk).length).toBeLessThanOrEqual(1024);
|
||||||
|
expect(shrunk.triangles).toBe(93_350);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('服务层兜底:数据超长时裁剪重试而不是 500', () => {
|
||||||
|
const source = readFileSync(
|
||||||
|
[
|
||||||
|
resolve(process.cwd(), 'src', 'models', 'models.service.ts'),
|
||||||
|
resolve(process.cwd(), '..', 'src', 'models', 'models.service.ts'),
|
||||||
|
].find((p) => {
|
||||||
|
try {
|
||||||
|
readFileSync(p);
|
||||||
|
return true;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}) as string,
|
||||||
|
'utf8',
|
||||||
|
);
|
||||||
|
|
||||||
|
it('create 与 reanalyze 都走 saveWithStatsFallback', () => {
|
||||||
|
const calls = source.match(/saveWithStatsFallback\(model\)/g) ?? [];
|
||||||
|
expect(calls.length).toBeGreaterThanOrEqual(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('兜底只用 1406 / DATA_TOO_LONG 触发,其它数据库错误照旧抛出', () => {
|
||||||
|
expect(source).toContain('isDataTooLongError');
|
||||||
|
expect(source).toMatch(/errno === 1406/);
|
||||||
|
expect(source).toMatch(/DATA_TOO_LONG/);
|
||||||
|
expect(source).toContain('if (!isDataTooLongError(error)) throw error;');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,232 @@
|
|||||||
|
import { mkdtempSync, writeFileSync } from 'node:fs';
|
||||||
|
import { tmpdir } from 'node:os';
|
||||||
|
import { join } from 'node:path';
|
||||||
|
import { inflateSync } from 'node:zlib';
|
||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import { renderModelThumbnail } from '../src/models/model-thumbnail';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 缩略图**贴图采样**测试。
|
||||||
|
*
|
||||||
|
* 背景(真实故障):缩略图渲染器原先刻意不解码 baseColorTexture,只用
|
||||||
|
* `baseColorFactor` 上色;而 MMD(PMX 转换)模型的 baseColorFactor 基本全是白色,
|
||||||
|
* 于是缩略图全是"白模"——列表页与审核面板看到的都是白色模型,用户报的正是这个。
|
||||||
|
* 现在渲染器内置 PNG 解码并逐像素采样贴图,这里锁住行为。
|
||||||
|
*/
|
||||||
|
|
||||||
|
const dir = mkdtempSync(join(tmpdir(), 'opendance-thumb-tex-'));
|
||||||
|
|
||||||
|
function write(name: string, content: Buffer): string {
|
||||||
|
const path = join(dir, name);
|
||||||
|
writeFileSync(path, content);
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 12 字节头 + JSON chunk + BIN chunk */
|
||||||
|
function buildGlb(json: unknown, bin: Buffer): Buffer {
|
||||||
|
const jsonRaw = Buffer.from(JSON.stringify(json), 'utf8');
|
||||||
|
const jsonChunk = Buffer.concat([jsonRaw, Buffer.alloc((4 - (jsonRaw.length % 4)) % 4, 0x20)]);
|
||||||
|
const binChunk = Buffer.concat([bin, Buffer.alloc((4 - (bin.length % 4)) % 4)]);
|
||||||
|
const chunks: Buffer[] = [];
|
||||||
|
const header = Buffer.alloc(12);
|
||||||
|
header.write('glTF', 0, 'ascii');
|
||||||
|
header.writeUInt32LE(2, 4);
|
||||||
|
chunks.push(header);
|
||||||
|
const jsonHeader = Buffer.alloc(8);
|
||||||
|
jsonHeader.writeUInt32LE(jsonChunk.length, 0);
|
||||||
|
jsonHeader.writeUInt32LE(0x4e4f534a, 4);
|
||||||
|
chunks.push(jsonHeader, jsonChunk);
|
||||||
|
const binHeader = Buffer.alloc(8);
|
||||||
|
binHeader.writeUInt32LE(binChunk.length, 0);
|
||||||
|
binHeader.writeUInt32LE(0x004e4942, 4);
|
||||||
|
chunks.push(binHeader, binChunk);
|
||||||
|
const out = Buffer.concat(chunks);
|
||||||
|
out.writeUInt32LE(out.length, 8);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
function float32Buffer(values: number[]): Buffer {
|
||||||
|
const buffer = Buffer.alloc(values.length * 4);
|
||||||
|
values.forEach((value, index) => buffer.writeFloatLE(value, index * 4));
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
function uint16Buffer(values: number[]): Buffer {
|
||||||
|
const buffer = Buffer.alloc(values.length * 2);
|
||||||
|
values.forEach((value, index) => buffer.writeUInt16LE(value, index * 2));
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------ 贴图夹具 */
|
||||||
|
|
||||||
|
/** 2x2 纯红(colorType 6,带 alpha=255) */
|
||||||
|
const RED_PNG = Buffer.from(
|
||||||
|
'iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEUlEQVR4nGP4z8DwH4QZYAwAR8oH+WdZbrcAAAAASUVORK5CYII=',
|
||||||
|
'base64',
|
||||||
|
);
|
||||||
|
/** 2x2 纯绿(colorType 2,无 alpha) */
|
||||||
|
const GREEN_PNG = Buffer.from(
|
||||||
|
'iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAD0lEQVR4nGNg+M8AQhAKABvyA/1tVLjHAAAAAElFTkSuQmCC',
|
||||||
|
'base64',
|
||||||
|
);
|
||||||
|
/** 2x2 全透明(RGBA 的 alpha=0) */
|
||||||
|
const TRANSPARENT_PNG = Buffer.from(
|
||||||
|
'iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEUlEQVR4nGP4z8DAAMIMMAYAKegD/QE6Y34AAAAASUVORK5CYII=',
|
||||||
|
'base64',
|
||||||
|
);
|
||||||
|
|
||||||
|
interface QuadOptions {
|
||||||
|
texture?: Buffer;
|
||||||
|
mimeType?: string;
|
||||||
|
baseColorFactor?: number[];
|
||||||
|
alphaMode?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 一个正对相机(+Z)的方形面片,UV 铺满贴图;baseColorFactor 默认纯白(MMD 的常见情况) */
|
||||||
|
function texturedQuadGlb(options: QuadOptions = {}): Buffer {
|
||||||
|
const positions = [-1, -1, 0, 1, -1, 0, 1, 1, 0, -1, 1, 0];
|
||||||
|
const uvs = [0, 1, 1, 1, 1, 0, 0, 0];
|
||||||
|
const indices = [0, 1, 2, 0, 2, 3];
|
||||||
|
const posBin = float32Buffer(positions);
|
||||||
|
const uvBin = float32Buffer(uvs);
|
||||||
|
const idxBin = uint16Buffer(indices);
|
||||||
|
const image = options.texture ?? RED_PNG;
|
||||||
|
const bin = Buffer.concat([posBin, uvBin, idxBin, image]);
|
||||||
|
const imageOffset = posBin.length + uvBin.length + idxBin.length;
|
||||||
|
|
||||||
|
const json = {
|
||||||
|
asset: { version: '2.0' },
|
||||||
|
scene: 0,
|
||||||
|
scenes: [{ nodes: [0] }],
|
||||||
|
nodes: [{ mesh: 0 }],
|
||||||
|
meshes: [{ primitives: [{ attributes: { POSITION: 0, TEXCOORD_0: 1 }, indices: 2, material: 0 }] }],
|
||||||
|
materials: [
|
||||||
|
{
|
||||||
|
pbrMetallicRoughness: {
|
||||||
|
baseColorFactor: options.baseColorFactor ?? [1, 1, 1, 1],
|
||||||
|
baseColorTexture: { index: 0 },
|
||||||
|
},
|
||||||
|
...(options.alphaMode ? { alphaMode: options.alphaMode, alphaCutoff: 0.5 } : {}),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
textures: [{ source: 0 }],
|
||||||
|
images: [{ bufferView: 3, mimeType: options.mimeType ?? 'image/png' }],
|
||||||
|
accessors: [
|
||||||
|
{ bufferView: 0, componentType: 5126, count: 4, type: 'VEC3' },
|
||||||
|
{ bufferView: 1, componentType: 5126, count: 4, type: 'VEC2' },
|
||||||
|
{ bufferView: 2, componentType: 5123, count: 6, type: 'SCALAR' },
|
||||||
|
],
|
||||||
|
bufferViews: [
|
||||||
|
{ buffer: 0, byteOffset: 0, byteLength: posBin.length },
|
||||||
|
{ buffer: 0, byteOffset: posBin.length, byteLength: uvBin.length },
|
||||||
|
{ buffer: 0, byteOffset: posBin.length + uvBin.length, byteLength: idxBin.length },
|
||||||
|
{ buffer: 0, byteOffset: imageOffset, byteLength: image.length },
|
||||||
|
],
|
||||||
|
buffers: [{ byteLength: bin.length }],
|
||||||
|
};
|
||||||
|
return buildGlb(json, bin);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 解 PNG(我们自己写的编码器:8 位 RGB、filter=0),返回平均 RGB */
|
||||||
|
function averageColor(png: Buffer): { r: number; g: number; b: number; nonBackground: number } {
|
||||||
|
let offset = 8;
|
||||||
|
let width = 0;
|
||||||
|
let height = 0;
|
||||||
|
const idat: Buffer[] = [];
|
||||||
|
while (offset + 8 <= png.length) {
|
||||||
|
const length = png.readUInt32BE(offset);
|
||||||
|
const type = png.toString('ascii', offset + 4, offset + 8);
|
||||||
|
const data = png.subarray(offset + 8, offset + 8 + length);
|
||||||
|
if (type === 'IHDR') {
|
||||||
|
width = data.readUInt32BE(0);
|
||||||
|
height = data.readUInt32BE(4);
|
||||||
|
expect(data[8]).toBe(8);
|
||||||
|
expect(data[9]).toBe(2);
|
||||||
|
expect(data[12]).toBe(0);
|
||||||
|
} else if (type === 'IDAT') {
|
||||||
|
idat.push(Buffer.from(data));
|
||||||
|
} else if (type === 'IEND') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
offset += 12 + length;
|
||||||
|
}
|
||||||
|
const raw = inflateSync(Buffer.concat(idat));
|
||||||
|
let r = 0;
|
||||||
|
let g = 0;
|
||||||
|
let b = 0;
|
||||||
|
let count = 0;
|
||||||
|
let nonBackground = 0;
|
||||||
|
for (let y = 0; y < height; y += 1) {
|
||||||
|
const rowStart = y * (1 + width * 3);
|
||||||
|
expect(raw[rowStart]).toBe(0); // filter=0
|
||||||
|
for (let x = 0; x < width; x += 1) {
|
||||||
|
const i = rowStart + 1 + x * 3;
|
||||||
|
r += raw[i];
|
||||||
|
g += raw[i + 1];
|
||||||
|
b += raw[i + 2];
|
||||||
|
count += 1;
|
||||||
|
// 默认背景是 [24,26,32](深色);明显偏离即视为主体像素
|
||||||
|
if (Math.abs(raw[i] - 24) > 12 || Math.abs(raw[i + 1] - 26) > 12 || Math.abs(raw[i + 2] - 32) > 12) {
|
||||||
|
nonBackground += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { r: r / count, g: g / count, b: b / count, nonBackground };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function render(glb: Buffer, name: string, extra: Record<string, unknown> = {}) {
|
||||||
|
const result = await renderModelThumbnail(write(name, glb), { size: 128, ...extra });
|
||||||
|
expect(result).not.toBeNull();
|
||||||
|
return averageColor((result as { png: Buffer }).png);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------ 测试 */
|
||||||
|
|
||||||
|
describe('缩略图贴图采样', () => {
|
||||||
|
it('baseColorFactor 全白时也按贴图上色(回归:不再输出"白模")', async () => {
|
||||||
|
const color = await render(texturedQuadGlb(), 'red.glb');
|
||||||
|
expect(color.nonBackground).toBeGreaterThan(0);
|
||||||
|
// 贴图是纯红:主体像素整体应明显偏红
|
||||||
|
expect(color.r).toBeGreaterThan(color.g + 20);
|
||||||
|
expect(color.r).toBeGreaterThan(color.b + 20);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('colorType 2(无 alpha 通道)的 PNG 也能解码', async () => {
|
||||||
|
const color = await render(texturedQuadGlb({ texture: GREEN_PNG }), 'green.glb');
|
||||||
|
expect(color.g).toBeGreaterThan(color.r + 20);
|
||||||
|
expect(color.g).toBeGreaterThan(color.b + 20);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('OPAQUE:忽略贴图 alpha(MMD 语义,alpha=0 也照样画出来)', async () => {
|
||||||
|
const color = await render(
|
||||||
|
texturedQuadGlb({ texture: TRANSPARENT_PNG, baseColorFactor: [0.9, 0.1, 0.1, 1] }),
|
||||||
|
'opaque.glb',
|
||||||
|
);
|
||||||
|
expect(color.nonBackground).toBeGreaterThan(0);
|
||||||
|
expect(color.r).toBeGreaterThan(color.g + 20);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('MASK:alpha 低于阈值的主体像素被丢弃(只剩背景)', async () => {
|
||||||
|
const color = await render(
|
||||||
|
texturedQuadGlb({ texture: TRANSPARENT_PNG, baseColorFactor: [0.9, 0.1, 0.1, 1], alphaMode: 'MASK' }),
|
||||||
|
'mask.glb',
|
||||||
|
);
|
||||||
|
expect(color.nonBackground).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('贴图解码失败时回退 baseColorFactor 纯色,且不抛错', async () => {
|
||||||
|
const broken = Buffer.from('这不是 PNG 数据', 'utf8');
|
||||||
|
const color = await render(
|
||||||
|
texturedQuadGlb({ texture: broken, baseColorFactor: [0.1, 0.1, 0.9, 1] }),
|
||||||
|
'broken.glb',
|
||||||
|
);
|
||||||
|
expect(color.nonBackground).toBeGreaterThan(0);
|
||||||
|
expect(color.b).toBeGreaterThan(color.r + 20);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('默认视角是正面(相机在 +Z):+Z 面颜色明显主导', async () => {
|
||||||
|
// 面片正对 +Z,默认视角下必须看得见(若默认看背面,主体像素会接近 0)
|
||||||
|
const color = await render(texturedQuadGlb(), 'front.glb');
|
||||||
|
expect(color.nonBackground).toBeGreaterThan(128 * 128 * 0.05);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user