|
@@ -0,0 +1,138 @@
|
|
|
+package com.fdkankan.scene.util;
|
|
|
+
|
|
|
+import cn.hutool.core.img.ImgUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.io.file.FileReader;
|
|
|
+import cn.hutool.core.util.RuntimeUtil;
|
|
|
+import com.fdkankan.common.constant.ErrorCode;
|
|
|
+import com.fdkankan.common.exception.BusinessException;
|
|
|
+import com.fdkankan.common.util.OBJToGLBUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.File;
|
|
|
+import java.util.LinkedHashSet;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Locale;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class OBJToGLBExtUtil extends OBJToGLBUtil {
|
|
|
+
|
|
|
+ public static void objToGlb(String buildCallPath, String objPath, String glbPath) {
|
|
|
+ checkObj(objPath);
|
|
|
+ objPath = getObj(objPath);
|
|
|
+ String command = buildCallPath + File.separator + "obj2gltf.exe -i " + objPath + " -o " + glbPath;
|
|
|
+ log.info("开始执行obj转换gbl命令-{}", command);
|
|
|
+ Process exec = RuntimeUtil.exec(new String[]{command});
|
|
|
+ log.info("结束执行obj转换gbl命令-{}", command);
|
|
|
+ }
|
|
|
+
|
|
|
+ static String getObj(String objPath) {
|
|
|
+ List<File> files = FileUtil.loopFiles(objPath);
|
|
|
+ if (objPath.toLowerCase(Locale.ROOT).contains(".obj")) {
|
|
|
+ return objPath;
|
|
|
+ } else {
|
|
|
+ for(File file2 : files) {
|
|
|
+ if (!file2.isDirectory() && FileUtil.extName(file2.getName()).toLowerCase(Locale.ROOT).equals("obj")) {
|
|
|
+ return file2.getAbsolutePath();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean checkObj(String objPath) {
|
|
|
+ if (objPath.contains(".obg")) {
|
|
|
+ objPath = FileUtil.file(objPath).getParent();
|
|
|
+ }
|
|
|
+
|
|
|
+ File file1 = new File(objPath);
|
|
|
+ File[] files = file1.listFiles();
|
|
|
+ if (files != null && files.length > 0) {
|
|
|
+ File mtlFile = null;
|
|
|
+ File objFile = null;
|
|
|
+
|
|
|
+ for(File file2 : files) {
|
|
|
+ if (file2.getName().endsWith(".obj")) {
|
|
|
+ objFile = file2;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (file2.getName().endsWith(".mtl")) {
|
|
|
+ mtlFile = file2;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (FileUtil.getType(file2).equals("jpg") || FileUtil.getType(file2).equals("png")) {
|
|
|
+ BufferedImage read = ImgUtil.read(file2);
|
|
|
+ int widthImg = read.getWidth();
|
|
|
+ int heightImg = read.getHeight();
|
|
|
+ if (widthImg > 2048 && heightImg > 2048) {
|
|
|
+ log.info("尺寸大2k,执行压缩");
|
|
|
+ ImgUtil.scale(file2, file2, 2048, 2048, (Color)null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (mtlFile != null && objFile != null) {
|
|
|
+ return checkMtl(file1, mtlFile);
|
|
|
+ } else {
|
|
|
+ throw new BusinessException(ErrorCode.FAILURE_CODE_5059);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new BusinessException(ErrorCode.FAILURE_CODE_7014);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean checkMtl(File allFile, File file) {
|
|
|
+ if (!file.getName().endsWith(".mtl")) {
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ LinkedHashSet<String> imgName = new LinkedHashSet();
|
|
|
+ if (allFile != null && allFile.length() > 0L) {
|
|
|
+ File[] files = allFile.listFiles();
|
|
|
+ if (files != null && files.length > 0) {
|
|
|
+ for(File listFile : files) {
|
|
|
+ String modelName = listFile.getName();
|
|
|
+ imgName.add(modelName);
|
|
|
+ }
|
|
|
+
|
|
|
+ for(String mtlName : readMtlFile(file.getPath())) {
|
|
|
+ if (!imgName.contains(mtlName)) {
|
|
|
+ throw new BusinessException(ErrorCode.FAILURE_CODE_5065);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static LinkedHashSet<String> readMtlFile(String mtlPath) {
|
|
|
+ LinkedHashSet<String> imgName = new LinkedHashSet();
|
|
|
+ FileReader fileReader = new FileReader(mtlPath);
|
|
|
+
|
|
|
+ try {
|
|
|
+ for(String line : fileReader.readLines()) {
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|