36 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
		
		
			
		
	
	
			36 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
| 
								 | 
							
								const gltfPipeline = require("gltf-pipeline");
							 | 
						||
| 
								 | 
							
								const fs = require("fs");
							 | 
						||
| 
								 | 
							
								const path = require("path");
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								const inputDir = "/data"; // 你的模型文件存放目录
							 | 
						||
| 
								 | 
							
								const outputDir = "/data"; // 压缩后的输出目录
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								if (!fs.existsSync(outputDir)) {
							 | 
						||
| 
								 | 
							
								  fs.mkdirSync(outputDir, { recursive: true });
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								const processFile = async (file) => {
							 | 
						||
| 
								 | 
							
								  const inputPath = path.join(inputDir, file);
							 | 
						||
| 
								 | 
							
								  const outputPath = path.join(outputDir, file);
							 | 
						||
| 
								 | 
							
								  const fileData = fs.readFileSync(inputPath);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  if (file.endsWith(".gltf")) {
							 | 
						||
| 
								 | 
							
								    const gltf = JSON.parse(fileData.toString());
							 | 
						||
| 
								 | 
							
								    const options = { dracoOptions: { compressionLevel: 10 } }; // 使用 Draco 压缩
							 | 
						||
| 
								 | 
							
								    const result = await gltfPipeline.processGltf(gltf, options);
							 | 
						||
| 
								 | 
							
								    fs.writeFileSync(outputPath, JSON.stringify(result.gltf));
							 | 
						||
| 
								 | 
							
								  } else if (file.endsWith(".glb")) {
							 | 
						||
| 
								 | 
							
								    const result = await gltfPipeline.glbToGlb(fileData, {
							 | 
						||
| 
								 | 
							
								      dracoOptions: { compressionLevel: 10 }
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								    fs.writeFileSync(outputPath, result.glb);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								fs.readdirSync(inputDir).forEach((file) => {
							 | 
						||
| 
								 | 
							
								  if (file.match(/\.(gltf|glb)$/)) {
							 | 
						||
| 
								 | 
							
								    processFile(file).catch((err) => console.error(`Error processing ${file}:`, err));
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								});
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								console.log("GLTF/GLB compression completed!");
							 |