优化 分解场景的中间件

This commit is contained in:
hawk86104 2024-05-14 15:13:41 +08:00
parent d05924a5ab
commit 734b35f9e4
2 changed files with 48 additions and 34 deletions

View File

@ -1,3 +1,5 @@
/* eslint-disable no-undefined */
/* eslint-disable guard-for-in */
import * as THREE from 'three'
window.THREE = THREE // Used by APP Scripts.

View File

@ -6,7 +6,7 @@
* @Autor: 地虎降天龙
* @Date: 2024-05-10 10:32:35
* @LastEditors: 地虎降天龙
* @LastEditTime: 2024-05-11 15:53:41
* @LastEditTime: 2024-05-14 15:12:17
*/
import { request } from '@fesjs/fes'
import JSZip from 'jszip'
@ -27,13 +27,13 @@ export const loadJson = (filepath) =>
export const convertImageToBase64 = async (imageUrl) => {
const response = await fetch(imageUrl)
const blob = await response.blob()
return await new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onloadend = () => resolve(reader.result)
reader.onerror = reject
reader.readAsDataURL(blob)
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onloadend = () => resolve(reader.result)
reader.onerror = reject
reader.readAsDataURL(blob)
})
}
}
export const getImageFormat = (base64Data) => {
const header = base64Data.substring(0, 15)
@ -46,16 +46,16 @@ export const getImageFormat = (base64Data) => {
const webpHeader = 'data:image/webp;base64,'
const webp2Header = 'data:image/WEBP;base64,'
if (jpgHeader.startsWith(header)||jpg2Header.startsWith(header)) {
if (jpgHeader.startsWith(header) || jpg2Header.startsWith(header)) {
return 'JPEG'
}
if (pngHeader.startsWith(header)||png2Header.startsWith(header)) {
if (pngHeader.startsWith(header) || png2Header.startsWith(header)) {
return 'PNG'
}
if (gifHeader.startsWith(header)||gif2Header.startsWith(header)) {
if (gifHeader.startsWith(header) || gif2Header.startsWith(header)) {
return 'GIF'
}
if (webpHeader.startsWith(header||webp2Header.startsWith(header))) {
if (webpHeader.startsWith(header || webp2Header.startsWith(header))) {
return 'WEBP'
}
return 'Unknown'
@ -66,7 +66,7 @@ export const exporterJsonZip = (jsonObjct) => {
jsonObjct.scene.geometries.forEach((geometry) => {
if (geometry.type === 'BufferGeometry') {
geometrieList.push({ uuid: geometry.uuid, data: geometry.data })
geometry.data = 'zip'
geometry.data = `url:geometries/${geometry.uuid}.json`
}
})
}
@ -75,7 +75,7 @@ export const exporterJsonZip = (jsonObjct) => {
jsonObjct.scene.images.forEach((image) => {
if (image.url) {
imagesList.push({ uuid: image.uuid, url: image.url })
image.url = 'zip.' + getImageFormat(image.url)
image.url = `url:images/${image.uuid}.${getImageFormat(image.url)}`
}
})
}
@ -103,7 +103,24 @@ const checkFinishImportJson = (srcNum, gJson, handler) => {
handler(gJson)
}
}
const getMatchingKeys = (obj, name) => Object.keys(obj).filter((key) => key.startsWith(name))
// const getMatchingKeys = (obj, name) => Object.keys(obj).filter((key) => key.startsWith(name))
const getResourceNum = (gJson) => {
const srcNum = {
geometries: 0,
images: 0,
}
gJson.scene.geometries?.forEach((geometry) => {
if (geometry.data.startsWith('url:')) {
srcNum.geometries++
}
})
gJson.scene.images?.forEach((image) => {
if (image.url.startsWith('url:')) {
srcNum.images++
}
})
return srcNum
}
export const importJsonZip = (file, handler) => {
let gJson = null
JSZip.loadAsync(file).then(
@ -112,34 +129,29 @@ export const importJsonZip = (file, handler) => {
console.error(`非标准文件:不存在 app.json`)
return
}
const srcNum = {
geometries: getMatchingKeys(zip.files, 'geometries/').length - 1,
images: getMatchingKeys(zip.files, 'images/').length - 1,
}
zip.files['app.json'].async('string').then((jdata) => {
gJson = JSON.parse(jdata)
checkFinishImportJson(srcNum, gJson, handler)
const resourceNum = getResourceNum(gJson)
checkFinishImportJson(resourceNum, gJson, handler)
gJson.scene.geometries?.forEach((geometry) => {
if (geometry.data === 'zip') {
zip.files[`geometries/${geometry.uuid}.json`].async('string').then((gdata) => {
if (geometry.data.startsWith('url:')) {
zip.files[geometry.data.slice(4)].async('string').then((gdata) => {
geometry.data = JSON.parse(gdata)
srcNum.geometries--
checkFinishImportJson(srcNum, gJson, handler)
resourceNum.geometries--
checkFinishImportJson(resourceNum, gJson, handler)
})
}
})
gJson.scene.images?.forEach((image) => {
if (image.url.startsWith('zip')) {
const matchingKeys = getMatchingKeys(zip.files, `images/${image.uuid}`)
if (matchingKeys.length) {
zip.files[matchingKeys[0]].async('base64').then((idata) => {
const fileNameParts = matchingKeys[0].split('.')
const extension = fileNameParts[fileNameParts.length - 1].toUpperCase()
image.url = `data:image/${extension};base64,${idata}`
srcNum.images--
checkFinishImportJson(srcNum, gJson, handler)
})
}
if (image.url.startsWith('url:')) {
const imgName = image.url.slice(4)
zip.files[imgName].async('base64').then((idata) => {
const fileNameParts = imgName.split('.')
const extension = fileNameParts[fileNameParts.length - 1].toUpperCase()
image.url = `data:image/${extension};base64,${idata}`
resourceNum.images--
checkFinishImportJson(resourceNum, gJson, handler)
})
}
})
})