Explorar o código

直接加载obj模型

lyhzzz hai 8 meses
pai
achega
67d32a712e

+ 39 - 4
src/main/java/com/fdkankan/fusion/common/util/OBJToGLBUtil.java

@@ -6,9 +6,12 @@ import com.fdkankan.fusion.common.FilePath;
 import com.fdkankan.fusion.common.ResultCode;
 import com.fdkankan.fusion.exception.BusinessException;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
 
 import java.io.*;
+import java.nio.charset.StandardCharsets;
+import java.util.Date;
 import java.util.LinkedHashSet;
 
 @Slf4j
@@ -86,15 +89,15 @@ public class OBJToGLBUtil {
 
     private static boolean checkMtl(File allFile,File file) {
         if(!file.getName().contains("mtl")){
-            return false;
+            throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
         }
         LinkedHashSet<String> imgName = new LinkedHashSet<>();
         if(allFile == null || allFile.length()<=0 ){
-            return false;
+            throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
         }
         File[] files = allFile.listFiles();
         if(files == null || files.length<=0 ){
-            return false;
+            throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
         }
         for (File listFile : files) {
             String modelName = listFile.getName();
@@ -103,12 +106,44 @@ public class OBJToGLBUtil {
         LinkedHashSet<String> imgMtl = readMtlFile(file.getPath());
         for (String mtlName : imgMtl) {
             if(!imgName.contains(mtlName)){
-                throw new BusinessException(-1,mtlName +".jpg 图片缺失!");
+                throw new BusinessException(ResultCode.UPLOAD_FILE_MSG_ERROR.code,mtlName +".jpg 图片缺失!");
             }
         }
+        updateMtlFile(file);
         return true;
     }
 
+    private static void updateMtlFile(File file) {
+        String mtlStr = FileUtil.readUtf8String(file);
+        String[] split = mtlStr.split("\n");
+        StringBuilder newMtlStr = new StringBuilder();
+        for (String s : split) {
+            if(s.toLowerCase().contains("tr")){
+                String[] split1 = s.split(" ");
+                if(split1.length <=1){
+                    continue;
+                }
+                if(isNumeric2(split1[1])){
+                    String[] split2 = split1[1].split("\\.");
+                    int number = Integer.parseInt(split2[0]);
+                    if(number >=1){
+                        number = 1 - number;
+                    }
+                    String numStr = number + (split2.length <=1 ?"": "."+split2[1]);
+                    s = s.replace(split1[1],numStr);
+                }
+            }
+            newMtlStr.append(s).append("\n");
+        }
+        FileUtil.copyContent(file,new File(file.getPath()+new Date().getTime()+"back"),true);
+        FileUtil.writeString(newMtlStr.toString(),file, StandardCharsets.UTF_8);
+
+    }
+    public static boolean isNumeric2(String str) {
+        return str != null && str.matches("-?\\d+(\\.\\d+)?");
+    }
+
+
     public static LinkedHashSet<String> readMtlFile(String mtlPath) {
         LinkedHashSet<String> imgName = new LinkedHashSet<>();
         FileInputStream fis = null;

+ 19 - 10
src/main/java/com/fdkankan/fusion/service/impl/ThreadService.java

@@ -81,6 +81,7 @@ public class ThreadService {
 
             String name = file1.getName();
             if(name.contains("obj") || name.contains("OBJ")){
+
 //                model.setModelDateType("b3dm");
 //                model.setModelType("b3dm");
 //                String localPath = file1.getParentFile().getPath()+"/b3dm";
@@ -91,17 +92,25 @@ public class ThreadService {
 //                ShellUtil.yunUpload(localPath,ossPath);
 //                model.setModelGlbUrl(JSONArray.toJSONString(Arrays.asList(queryPath + ossPath+"/tileset.json")));
 
-                String localPath = file1.getPath().replace(".obj", ".glb");
+//                String localPath = file1.getPath().replace(".obj", ".glb");
+//                model.setModelDateType("obj");
+//                model.setModelType("glb");
+//                OBJToGLBUtil.objToGlb2(file1.getPath(),localPath);
+//                redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65");
+//                ShellUtil.yunUpload(objPath,objPath.replace("/mnt/",""));
+//                //uploadToOssUtil.uploadOss(localPath,localPath.replace("/mnt/",""));
+//                if(!uploadToOssUtil.existKey(localPath.replace("/mnt/",""))){
+//                    throw new BusinessException(-1,"缺少.glb文件");
+//                }
+//               model.setModelGlbUrl(JSONArray.toJSONString(Arrays.asList(queryPath + localPath.replace("/mnt/",""))));
+
+                String ossPath = objPathFile.getPath().replace(FilePath.LOCAL_BASE_PATH,"fusion/");
+                OBJToGLBUtil.checkObj(file1.getPath());
                 model.setModelDateType("obj");
-                model.setModelType("glb");
-                OBJToGLBUtil.objToGlb2(file1.getPath(),localPath);
-                redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65");
-                ShellUtil.yunUpload(objPath,objPath.replace("/mnt/",""));
-                //uploadToOssUtil.uploadOss(localPath,localPath.replace("/mnt/",""));
-                if(!uploadToOssUtil.existKey(localPath.replace("/mnt/",""))){
-                    throw new BusinessException(-1,"缺少.glb文件");
-                }
-               model.setModelGlbUrl(JSONArray.toJSONString(Arrays.asList(queryPath + localPath.replace("/mnt/",""))));
+                model.setModelType("obj");
+                redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"60");
+                ShellUtil.yunUpload(objPathFile.getPath(),ossPath);
+                model.setModelGlbUrl((JSONArray.toJSONString(Arrays.asList(queryPath +  file1.getPath().replace(FilePath.LOCAL_BASE_PATH,"fusion/")))));
 
             }
             if(name.contains(".ply")){