This commit is contained in:
2026-04-24 12:18:24 +08:00
parent 09359a1647
commit 6c94559383
7 changed files with 70 additions and 49 deletions

View File

@ -19,7 +19,7 @@ type LoadResult = {
* 模型管理类- 负责加载、缓存和管理3D模型
*/
export class AppModel extends Monobehiver {
private modelDic: Dictionary<AbstractMesh[]>;
private modelDic: Dictionary<AbstractMesh>;
private loadedMeshes: AbstractMesh[];
private skeletonManager: any;
private outfitManager: any;
@ -28,7 +28,7 @@ export class AppModel extends Monobehiver {
constructor(mainApp: any) {
super(mainApp);
this.modelDic = new Dictionary<AbstractMesh[]>();
this.modelDic = new Dictionary<AbstractMesh>();
this.loadedMeshes = [];
this.skeletonManager = null;
this.outfitManager = null;
@ -96,16 +96,14 @@ export class AppModel extends Monobehiver {
*/
async loadSingleModel(modelUrl: string, onProgress?: (event: ISceneLoaderProgressEvent) => void): Promise<LoadResult> {
try {
const cached = this.getCachedMeshes(modelUrl);
if (cached) return { success: true, meshes: cached };
const scene: Scene | null = this.mainApp.appScene.object;
if (!scene) return { success: false, error: '场景未初始化' };
const result = await ImportMeshAsync(modelUrl, scene, { onProgress });
if (!result?.meshes?.length) return { success: false, error: '未找到网格' };
this.modelDic.Set(modelUrl, result.meshes);
// 存储根网格(通常是第一个网格)
const rootMesh = result.meshes[0];
this.loadedMeshes.push(...result.meshes);
return { success: true, meshes: result.meshes, skeletons: result.skeletons };
} catch (e: any) {
@ -131,12 +129,12 @@ export class AppModel extends Monobehiver {
}
/** 获取缓存的网格 */
getCachedMeshes(url: string): AbstractMesh[] | undefined {
getCachedMeshes(url: string): AbstractMesh | undefined {
return this.modelDic.Get(url);
}
/** 清理所有资源 */
clean(): void {
@ -151,9 +149,19 @@ export class AppModel extends Monobehiver {
/**
* 添加模型到场景
* @param modelName 模型名称(用于后续引用)
* @param modelUrl 模型URL路径
*/
async addModel(modelUrl: string): Promise<LoadResult> {
async add(modelName: string, modelUrl: string): Promise<LoadResult> {
// 检查是否已经存在该模型
const existingMesh = this.modelDic.Get(modelName);
if (existingMesh && !existingMesh.isDisposed()) {
console.log(`模型 ${modelName} 已存在,直接显示`);
existingMesh.setEnabled(true);
existingMesh.getChildMeshes().forEach(child => child.setEnabled(true));
return { success: true, meshes: [existingMesh, ...existingMesh.getChildMeshes()] };
}
const handleProgress = (event: ISceneLoaderProgressEvent): void => {
const progress = event.lengthComputable && event.total > 0
? Math.min(1, event.loaded / event.total)
@ -175,7 +183,10 @@ export class AppModel extends Monobehiver {
const result = await this.loadSingleModel(modelUrl, handleProgress);
if (result.success) {
if (result.success && result.meshes) {
// 使用modelName作为key存储根网格
const rootMesh = result.meshes[0];
this.modelDic.Set(modelName, rootMesh);
EventBridge.modelLoaded({ urls: [modelUrl] });
} else {
EventBridge.modelLoadError({ url: modelUrl, error: result.error });
@ -191,35 +202,27 @@ export class AppModel extends Monobehiver {
*/
async replaceModel(modelName: string, newModelUrl: string): Promise<LoadResult> {
// 先销毁旧模型
this.destroyModel(modelName);
this.remove(modelName);
// 加载新模型
return await this.addModel(newModelUrl);
return await this.add(modelName, newModelUrl);
}
/**
* 销毁指定模型
* @param modelName 模型名称
*/
destroyModel(modelName: string): void {
// 遍历模型字典,查找匹配的模型
const keys = this.modelDic.Keys();
for (const key of keys) {
if (key.includes(modelName)) {
const meshes = this.modelDic.Get(key);
if (meshes) {
// 销毁所有网格
meshes.forEach(mesh => mesh?.dispose());
// 从字典中移除
this.modelDic.Remove(key);
// 从加载的网格列表中移除
this.loadedMeshes = this.loadedMeshes.filter(mesh => !meshes.includes(mesh));
console.log(`Model destroyed: ${modelName}`);
return;
}
}
remove(modelName: string): void {
const mesh = this.modelDic.Get(modelName);
if (mesh) {
// 隐藏网格而不是销毁,以便后续可以重新显示
mesh.setEnabled(false);
mesh.getChildMeshes().forEach(child => child.setEnabled(false));
console.log(`Model hidden: ${modelName}`);
} else {
console.warn(`Model not found: ${modelName}`);
}
console.warn(`Model not found: ${modelName}`);
}
}