-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresize-bullets.mjs
More file actions
91 lines (75 loc) · 2.56 KB
/
Copy pathresize-bullets.mjs
File metadata and controls
91 lines (75 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
console.log('============================================');
console.log('弹幕纹理优化脚本');
console.log('============================================\n');
console.log('当前问题:弹幕纹理是2816x1536,解码后占用17MB/张');
console.log('优化目标:缩小到512x512,减少内存占用到1MB/张\n');
console.log('这将节省约 130MB 内存!\n');
// 检查是否安装sharp
let sharp;
try {
sharp = (await import('sharp')).default;
console.log('✓ sharp已安装\n');
} catch (e) {
console.error('错误:sharp未安装!');
console.error('请运行:npm install sharp\n');
process.exit(1);
}
const mapDir = path.join(__dirname, 'public', 'map');
const backupDir = path.join(mapDir, 'backup_original');
// 创建备份目录
if (!fs.existsSync(backupDir)) {
fs.mkdirSync(backupDir, { recursive: true });
}
// 弹幕纹理列表
const bullets = [
'amulet.png',
'star.png',
'yinyang.png',
'knife.png',
'laser_head.png',
'arrow_bullet.png',
'big_bullet.png',
'rice_bullet.png'
];
console.log('开始处理...\n');
for (const filename of bullets) {
const filePath = path.join(mapDir, filename);
const backupPath = path.join(backupDir, filename);
if (!fs.existsSync(filePath)) {
console.log(`[跳过] ${filename} - 文件不存在`);
continue;
}
console.log(`[处理] ${filename}`);
try {
// 获取原始信息
const metadata = await sharp(filePath).metadata();
console.log(` 原始尺寸:${metadata.width}x${metadata.height}`);
// 备份原文件
fs.copyFileSync(filePath, backupPath);
// 缩小到512x512
await sharp(filePath)
.resize(512, 512, {
fit: 'contain',
background: { r: 0, g: 0, b: 0, alpha: 0 } // 透明背景
})
.png({ quality: 95, compressionLevel: 9 })
.toFile(filePath + '.tmp');
// 替换原文件
fs.renameSync(filePath + '.tmp', filePath);
const newMetadata = await sharp(filePath).metadata();
console.log(` 新尺寸:${newMetadata.width}x${newMetadata.height}`);
console.log(` ✓ 完成\n`);
} catch (err) {
console.error(` ✗ 失败:${err.message}\n`);
}
}
console.log('============================================');
console.log('优化完成!');
console.log('============================================\n');
console.log(`原始文件已备份到:${backupDir}\n`);
console.log('预计节省内存:约 130MB\n');