package com.fdkankan.fusion.common.util; import com.fdkankan.fusion.common.ResultCode; import com.fdkankan.fusion.exception.BusinessException; import lombok.extern.slf4j.Slf4j; import java.io.*; import java.util.LinkedHashSet; @Slf4j public class OBJToGLBUtil { public static void objToGlb(String objPath, String glbPath) { log.info("obj转换glb开始,{}",objPath); checkObj(objPath); objPath += "/mesh.obj"; log.info("obj转换glb开始"); String command = "obj2gltf " + objPath + " " + glbPath; log.info("执行obj转换glb命令路径-{}", command); ShellUtil.execCmd(command); log.info("obj转换glb完毕:" + command); } public static boolean checkObj(String objPath) { File file1 = new File(objPath); File[] files = file1.listFiles(); if(files == null || files.length <=0){ throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST); } File mtlFile = null; File objFile = null; for (File file2 : files) { if(file2.isDirectory()){ return checkObj(file2.getPath()); } if(file2.getName().contains("obj") ){ objFile = file2; } if(file2.getName().contains("mtl") ){ mtlFile = file2; } } if(mtlFile == null || objFile == null ){ throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR); } return checkMtl(file1,mtlFile); } private static boolean checkMtl(File allFile,File file) { if(!file.getName().contains("mtl")){ return false; } LinkedHashSet imgName = new LinkedHashSet<>(); if(allFile == null || allFile.length()<=0 ){ return false; } File[] files = allFile.listFiles(); if(files == null || files.length<=0 ){ return false; } for (File listFile : files) { String modelName = listFile.getName(); imgName.add(modelName); } LinkedHashSet imgMtl = readMtlFile(file.getPath()); for (String mtlName : imgMtl) { if(!imgName.contains(mtlName)){ throw new BusinessException(-1,mtlName +".jpg 图片缺失!"); } } return true; } public static LinkedHashSet readMtlFile(String mtlPath) { LinkedHashSet imgName = new LinkedHashSet<>(); FileInputStream fis = null; BufferedReader br = null; try { fis = new FileInputStream(new File(mtlPath)); br = new BufferedReader(new InputStreamReader(fis)); String line = null; while ((line = br.readLine()) != null) { String[] tempsa = line.split("[ ]+"); if (tempsa[0].trim().equals("map_Ka")) { imgName.add(tempsa[1]); } if (tempsa[0].trim().equals("map_Kd")) { imgName.add(tempsa[1]); } } } catch (Exception e) { e.printStackTrace(); } return imgName; } public static void main(String[] args) { System.out.println(checkObj("D:\\model")); } }