OBJToGLBUtil.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package com.fdkankan.fusion.common.util;
  2. import com.fdkankan.fusion.common.ResultCode;
  3. import com.fdkankan.fusion.exception.BusinessException;
  4. import lombok.extern.slf4j.Slf4j;
  5. import java.io.*;
  6. import java.util.LinkedHashSet;
  7. @Slf4j
  8. public class OBJToGLBUtil {
  9. public static void objToGlb(String objPath, String glbPath) {
  10. log.info("obj转换glb开始,{}",objPath);
  11. if(!checkObj(objPath)){
  12. throw new BusinessException(-1,"obj文件错误");
  13. }
  14. objPath += "/mesh.obj";
  15. log.info("obj转换glb开始");
  16. String command = "obj2gltf " + objPath + " " + glbPath;
  17. log.info("执行obj转换glb命令路径-{}", command);
  18. ShellUtil.execCmd(command);
  19. log.info("obj转换glb完毕:" + command);
  20. }
  21. public static boolean checkObj(String objPath) {
  22. File file1 = new File(objPath);
  23. File[] files = file1.listFiles();
  24. if(files == null || files.length <=0){
  25. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  26. }
  27. File mtlFile = null;
  28. File objFile = null;
  29. for (File file2 : files) {
  30. if(file2.isDirectory()){
  31. return checkObj(file2.getPath());
  32. }
  33. if(file2.getName().contains(".obj") ){
  34. objFile = file2;
  35. }
  36. if(file2.getName().contains(".mtl") ){
  37. mtlFile = file2;
  38. }
  39. }
  40. if(mtlFile == null || objFile == null ){
  41. throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
  42. }
  43. return checkMtl(file1,mtlFile);
  44. }
  45. private static boolean checkMtl(File allFile,File file) {
  46. if(!file.getName().contains("mtl")){
  47. return false;
  48. }
  49. LinkedHashSet<String> imgName = new LinkedHashSet<>();
  50. if(allFile == null || allFile.length()<=0 ){
  51. return false;
  52. }
  53. File[] files = allFile.listFiles();
  54. if(files == null || files.length<=0 ){
  55. return false;
  56. }
  57. for (File listFile : files) {
  58. String modelName = listFile.getName();
  59. imgName.add(modelName);
  60. }
  61. LinkedHashSet<String> imgMtl = readMtlFile(file.getPath());
  62. for (String mtlName : imgMtl) {
  63. if(!imgName.contains(mtlName)){
  64. throw new BusinessException(-1,mtlName +".jpg 图片缺失!");
  65. }
  66. }
  67. return true;
  68. }
  69. public static LinkedHashSet<String> readMtlFile(String mtlPath) {
  70. LinkedHashSet<String> imgName = new LinkedHashSet<>();
  71. FileInputStream fis = null;
  72. BufferedReader br = null;
  73. try {
  74. fis = new FileInputStream(new File(mtlPath));
  75. br = new BufferedReader(new InputStreamReader(fis));
  76. String line = null;
  77. while ((line = br.readLine()) != null) {
  78. String[] tempsa = line.split("[ ]+");
  79. if (tempsa[0].trim().equals("map_Ka")) {
  80. imgName.add(tempsa[1]);
  81. }
  82. if (tempsa[0].trim().equals("map_Kd")) {
  83. imgName.add(tempsa[1]);
  84. }
  85. }
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. }
  89. return imgName;
  90. }
  91. public static void main(String[] args) {
  92. System.out.println(checkObj("D:\\model"));
  93. }
  94. }