chore: 初始化glTF模型压缩工具包
添加了完整的项目配置、工具函数、纹理压缩与模型优化实现,以及文档说明
This commit is contained in:
65
src/utils.ts
Normal file
65
src/utils.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
export function showWarn(message: string) {
|
||||
console.error('\x1b[33m%s\x1b[0m', `Warn: ${message}`);
|
||||
}
|
||||
|
||||
export function showInfo(message: string) {
|
||||
console.info('\x1b[32m%s\x1b[0m', message);
|
||||
}
|
||||
|
||||
const GLTF_EXTENSIONS = new Set(['.gltf', '.glb']);
|
||||
|
||||
export function isGLTFFile(filePath: string) {
|
||||
return GLTF_EXTENSIONS.has(path.extname(filePath).toLowerCase());
|
||||
}
|
||||
|
||||
export async function readFileJson(filePath: string): Promise<object> {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readFile(filePath, { encoding: 'utf8' }, (error, content) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(JSON.parse(content));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function readFileBuffer(filePath: string): Promise<Buffer> {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readFile(filePath, (error, content) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(content);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function writeFile(filePath: string, buffer: Buffer | string): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const dirPath = path.dirname(filePath);
|
||||
if (!fs.existsSync(dirPath)) {
|
||||
fs.mkdirSync(dirPath, { recursive: true });
|
||||
}
|
||||
|
||||
fs.writeFile(
|
||||
filePath,
|
||||
buffer,
|
||||
typeof buffer === 'string' ? { encoding: 'utf8' } : {},
|
||||
(error) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(filePath);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user