OBJToGLBUtil.java 3.3 KB

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