OBJToGLBUtil.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.fdkankan.common.util;
  2. import cn.hutool.core.img.ImgUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.io.file.FileReader;
  5. import com.fdkankan.common.constant.ErrorCode;
  6. import com.fdkankan.common.exception.BusinessException;
  7. import java.awt.image.BufferedImage;
  8. import java.io.File;
  9. import java.util.LinkedHashSet;
  10. import java.util.List;
  11. import java.util.Locale;
  12. import lombok.extern.slf4j.Slf4j;
  13. @Slf4j
  14. public class OBJToGLBUtil {
  15. /**
  16. *
  17. * @param objPath obj文件的目录
  18. * @param glbPath glb文件的最对路径
  19. */
  20. public static void objToGlb(String objPath, String glbPath) {
  21. OBJToGLBUtil.checkObj(objPath);
  22. objPath = OBJToGLBUtil.getObj(objPath);
  23. CreateObjUtil.obj2Glb(objPath, glbPath);
  24. log.info("obj转换glb完毕:");
  25. }
  26. static String getObj(String objPath) {
  27. List<File> files = FileUtil.loopFiles(objPath);
  28. if (objPath.toLowerCase(Locale.ROOT).contains(".obj")) {
  29. return objPath;
  30. }
  31. for (File file2 : files) {
  32. if (file2.isDirectory()) {
  33. continue;
  34. }
  35. if (FileUtil.extName(file2.getName()).toLowerCase(Locale.ROOT).equals("obj")) {
  36. return file2.getAbsolutePath();
  37. }
  38. }
  39. return "";
  40. }
  41. private static boolean checkObj(String objPath) {
  42. if (objPath.contains(".obg")) {
  43. objPath = FileUtil.file(objPath).getParent();
  44. }
  45. File file1 = new File(objPath);
  46. File[] files = file1.listFiles();
  47. if (files == null || files.length <= 0) {
  48. throw new BusinessException(ErrorCode.FAILURE_CODE_7014);
  49. }
  50. File mtlFile = null;
  51. File objFile = null;
  52. for (File file2 : files) {
  53. if (file2.getName().endsWith(".obj")) {
  54. // if (StringUtils.isChinese(file2.getName())) {
  55. // throw new ServiceException(HttpStatus.e7008.getMsg(), HttpStatus.e7008.getCode());
  56. // }
  57. objFile = file2;
  58. }
  59. if (file2.getName().endsWith(".mtl")) {
  60. // if (StringUtils.isChinese(file2.getName())) {
  61. // throw new ServiceException(HttpStatus.e7008.getMsg(), HttpStatus.e7008.getCode());
  62. // }
  63. mtlFile = file2;
  64. }
  65. if (FileUtil.getType(file2).equals("jpg") || FileUtil.getType(file2).equals("png")) {
  66. BufferedImage read = ImgUtil.read(file2);
  67. int widthImg = read.getWidth();
  68. int heightImg = read.getHeight();
  69. if (widthImg > 2048 && heightImg > 2048) {
  70. OBJToGLBUtil.log.info("尺寸大2k,执行压缩");
  71. ImgUtil.scale(file2,
  72. file2,
  73. 2048, 2048, null);
  74. }
  75. }
  76. }
  77. if (mtlFile == null || objFile == null) {
  78. throw new BusinessException(ErrorCode.FAILURE_CODE_5059);
  79. }
  80. return OBJToGLBUtil.checkMtl(file1, mtlFile);
  81. }
  82. private static boolean checkMtl(File allFile, File file) {
  83. if (!file.getName().endsWith(".mtl")) {
  84. return false;
  85. }
  86. LinkedHashSet<String> imgName = new LinkedHashSet<>();
  87. if (allFile == null || allFile.length() <= 0) {
  88. return false;
  89. }
  90. File[] files = allFile.listFiles();
  91. if (files == null || files.length <= 0) {
  92. return false;
  93. }
  94. for (File listFile : files) {
  95. String modelName = listFile.getName();
  96. imgName.add(modelName);
  97. }
  98. LinkedHashSet<String> imgMtl = OBJToGLBUtil.readMtlFile(file.getPath());
  99. for (String mtlName : imgMtl) {
  100. if (!imgName.contains(mtlName)) {
  101. throw new BusinessException(-1 ,mtlName + "图片缺失!");
  102. }
  103. }
  104. return true;
  105. }
  106. private static LinkedHashSet<String> readMtlFile(String mtlPath) {
  107. LinkedHashSet<String> imgName = new LinkedHashSet<>();
  108. FileReader fileReader = new FileReader(mtlPath);
  109. try {
  110. List<String> lines = fileReader.readLines();
  111. for (String line : lines) {
  112. String[] tempsa = line.split("[ ]+");
  113. if (tempsa[0].trim().equals("map_Ka")) {
  114. imgName.add(tempsa[1]);
  115. }
  116. if (tempsa[0].trim().equals("map_Kd")) {
  117. imgName.add(tempsa[1]);
  118. }
  119. }
  120. } catch (Exception e) {
  121. e.printStackTrace();
  122. }
  123. return imgName;
  124. }
  125. public static void main(String[] args) {
  126. System.out.println(OBJToGLBUtil.checkObj("F:\\test\\新建文件夹\\police模型"));
  127. }
  128. }