feat(all): 离线包迁移

This commit is contained in:
plum
2026-04-08 21:54:54 +08:00
parent 404e804143
commit 151bc7c8a2
6 changed files with 52 additions and 16 deletions
+39 -3
View File
@@ -457,7 +457,7 @@ export class Package {
});
// 首包上传
const firstUploadResult = await this.zip(sceneZipData, packConfig.name, packConfig.zipUploadFun);
const firstUploadResult = await this.zip(sceneZipData, packConfig.name, packConfig.zipUploadFun, packConfig.rawUploadFun);
// 进度
let progress = 0;
@@ -520,7 +520,7 @@ export class Package {
const content = JSON.stringify(json);
zipData.push({ name, json: content });
await this.zip(zipData, group.uuid, packConfig.zipUploadFun);
await this.zip(zipData, group.uuid, packConfig.zipUploadFun, packConfig.rawUploadFun);
progress++;
packConfig.onProgress && packConfig.onProgress(parseFloat((progress / groupArr.length * 100).toFixed(2)));
@@ -544,7 +544,43 @@ export class Package {
* @param {string | number} zipName 打包文件名
* @return {Promise<any>} 返回包上传接口结果
*/
private async zip(sourceData: SourceData[], zipName: string | number, zipUploadFun: (zip: File) => Promise<any>): Promise<any> {
private toUint8(data: string | ArrayBuffer | Uint8Array): Uint8Array {
if (data instanceof Uint8Array) return data;
if (typeof data === "string") return strToU8(data);
return new Uint8Array(data);
}
private buildFilesMapFromSourceData(sourceData: SourceData[]): Record<string, Uint8Array> {
const files: Record<string, Uint8Array> = {};
sourceData.forEach((item) => {
if (item.texture) {
files[`Textures/${item.name}`] = this.toUint8(item.texture);
} else if (item.geometry) {
files[`Geometries/${item.name}`] = this.toUint8(item.geometry);
} else if (item.json) {
files[item.name] = this.toUint8(item.json);
} else if (item.drawing) {
files[`Drawing/${item.name}`] = this.toUint8(item.drawing);
}
})
return files;
}
private async zip(
sourceData: SourceData[],
zipName: string | number,
zipUploadFun: (zip: File) => Promise<any>,
rawUploadFun?: (raw: { name: string; files: Record<string, Uint8Array> }) => Promise<any>
): Promise<any> {
if (rawUploadFun) {
const files = this.buildFilesMapFromSourceData(sourceData);
const size = Object.values(files).reduce((sum, data) => sum + data.byteLength, 0);
this.totalSize += size;
return await rawUploadFun({ name: String(zipName), files });
}
const jszip = new JSZip();
const imgFolder = jszip.folder("Textures") as JSZip; // 贴图文件夹
const geometriesFolder = jszip.folder("Geometries") as JSZip; // 几何数据文件夹
+5
View File
@@ -32,6 +32,7 @@ import ParticleEmitter from "@/core/objects/ParticleEmitter.ts";
import {ViewerPathTracer} from "@/core/viewer/ViewerPathTracer.ts";
import {Helper as ScriptHelper} from "../script";
import Tiles from "../objects/Tile.ts";
import {Package} from "@/core/loader/Package";
export interface ViewerEventMap {
// 场景加载完成时执行,仅执行一次
@@ -198,6 +199,7 @@ export default class Viewer extends THREE.EventDispatcher<ViewerEventMap> {
public timer = new Timer();
//整个主场景的box3
public sceneBox3 = new THREE.Box3();
public package: Package;
constructor(options: IViewerSetting) {
super();
@@ -216,6 +218,7 @@ export default class Viewer extends THREE.EventDispatcher<ViewerEventMap> {
this.renderer = this.createEngine();
this.modules = this.initModules();
this.package = new Package(this);
/** helpers **/
if (this.options.grid.enabled) {
@@ -1148,6 +1151,8 @@ export default class Viewer extends THREE.EventDispatcher<ViewerEventMap> {
this.dispatchEvent({type: "afterDestroy"});
this.unInstallScripts();
this.package?.dispose();
this.package = null as any;
}
/* -----------------暂时放在Viewer下的工具方法-------------------- */