Browse Source

feat:更新load3d

ananzhusen 1 month ago
parent
commit
34f8b72720
1 changed files with 60 additions and 45 deletions
  1. 60 45
      src/services/load3d.ts

+ 60 - 45
src/services/load3d.ts

@@ -47,7 +47,7 @@ export async function load3d(zip: JSZip, key: string) {
             tags = '我的HDR';
           }
           if (directory) {
-            const res = await uploadFile({
+            const res:any = await uploadFile({
               file: newFile,
               directory,
               type,
@@ -222,7 +222,7 @@ export const uploadFile = async (info: {
   )) as ResponseResult<FileData>;
   if (res.error) {
   }
-  return res.data;
+  return res;
 };
 
 export type UploadFileType =
@@ -295,65 +295,80 @@ const meta3dReplaceUrl = (
   const _data = parseData(data);
   const { scenes } = _data;
   const transUrl = (url: string): string => {
-    if (url.startsWith('data:')) {
-      const str = url.substring(5);
-      const [image, ...arr] = str.split('#').reverse();
-      url = arr.reverse().join('#');
-      const newUrl = transUrl(url);
-      return 'data:' + newUrl + '#' + image;
-    }
     if (url in urlMap) {
       return urlMap[url];
     }
     return url;
   };
-  const loadFile = (data: any, glbMap: any) => {
-    if (data.__glbUUID && glbMap) {
-      const glbInfo = glbMap[data.__glbUUID];
-      if (glbInfo) {
-        const { url, name } = glbInfo;
-        const fullUrl = url + name;
-        if (fullUrl in urlMap) {
-          glbInfo.url = filepath(urlMap[fullUrl]);
-          glbInfo.name = filename(urlMap[fullUrl], true);
-        }
-      }
-    }
-    const urlProps = [
-      'url',
-      'imageSource',
-      'skyboxUrl',
-      'hdrUrl',
-      'colorGradingTexture',
-    ];
-    for (const urlProp of urlProps) {
-      if (urlProp in data === false) {
-        continue;
-      }
-      const url = data[urlProp] || '';
-      data[urlProp] = transUrl(url);
-    }
-    if (data.images?.length) {
-      for (const image of data.images) {
-        const { source } = image;
-        image.source = transUrl(source);
-      }
-    }
-  };
-
   for (const sceneData of scenes) {
     if (!sceneData) {
       continue;
     }
-    const { nodes = [], scene = {}, glbMap = {}, textures = {} } = sceneData;
+    const {
+      nodes = [],
+      scene = {},
+      glbMap = {},
+      textures = {},
+      materials = {},
+      DOMDatas = [],
+    } = sceneData;
+    Object.keys(glbMap).forEach((glbId) => {
+      const { url, name } = glbMap[glbId];
+      const fullUrl = url + name;
+      if (fullUrl in urlMap) {
+        glbMap[glbId].url = filepath(urlMap[fullUrl]);
+        glbMap[glbId].name = filename(urlMap[fullUrl], true);
+      }
+    });
     for (const node of [
       scene,
       ...nodes,
+      ...DOMDatas,
+      ...Object.keys(materials).map((id) => materials[id]),
       ...Object.keys(textures).map((id) => textures[id]),
     ]) {
-      loadFile(node, glbMap);
+      convertResourceAddress(node, transUrl);
     }
   }
 
   return _data;
 };
+
+const urlProps = [
+  'url',
+  'imageSource',
+  'skyboxUrl',
+  'hdrUrl',
+  'colorGradingTexture',
+  'source',
+  'backgroundImage',
+];
+function convertResourceAddress(
+  data: any,
+  transFn: (url: string) => any,
+  reset = true
+) {
+  for (const urlProp of urlProps) {
+    if (urlProp in data === false) {
+      continue;
+    }
+    const url = data[urlProp] || '';
+    const newUrl = transFn(url);
+    if (reset) {
+      data[urlProp] = newUrl;
+    }
+  }
+  if (data.contents) {
+    data.contents.forEach((content: any) =>
+      convertResourceAddress(content, transFn, reset)
+    );
+  }
+  if (data.__initOption) {
+    convertResourceAddress(data.__initOption, transFn, reset);
+  }
+  if (data.children) {
+    data.children.forEach((child: any) =>
+      convertResourceAddress(child, transFn, reset)
+    );
+  }
+}