From fb193c05283b6dc823c33c20c8f02d29735cb0c2 Mon Sep 17 00:00:00 2001 From: zguiy <1415466602@qq.com> Date: Mon, 18 May 2026 12:28:10 +0800 Subject: [PATCH] 1 --- examples/app-global.js | 36 ++++++++++++++++++++++++++++++++++-- examples/index.global.js | 24 +++++++++--------------- index.js | 5 +++-- src/babylonjs/AppModel.ts | 10 +++++++++- 4 files changed, 55 insertions(+), 20 deletions(-) diff --git a/examples/app-global.js b/examples/app-global.js index b34d1f1..2a10a1c 100644 --- a/examples/app-global.js +++ b/examples/app-global.js @@ -192,6 +192,17 @@ const executeEvent = async (dropzone_data, result) => { } } +/** + * 检查模型是否已存在 + * @param {string} modelId - 模型ID + * @returns {boolean} 模型是否存在 + */ +const isModelExists = (modelId) => { + const kernel = getKernel(); + // 调用 SDK 的 API 检查模型是否存在 + return kernel.model.exists(modelId); +} + //换棚子 const executeEvent2 = async (result) => { const kernel = getKernel(); @@ -199,10 +210,24 @@ const executeEvent2 = async (result) => { // 检查是否有模型更换事件 const hasModelChange = result.data.events.some(e => e.event_type === 'change_model'); - // 只有在需要更换模型时才清除 + // 检查新模型是否已经存在 + let modelAlreadyExists = false; if (hasModelChange) { + const firstModelEvent = result.data.events.find(e => e.event_type === 'change_model'); + if (firstModelEvent) { + const { category } = firstModelEvent.target_data; + modelAlreadyExists = isModelExists(category); + console.log(`检查模型 ${category} 是否存在:`, modelAlreadyExists); + } + } + + // 只有在需要更换模型且模型不存在时才清除 + if (hasModelChange && !modelAlreadyExists) { + console.log('模型不存在,执行清除操作'); kernel.dropZone.clearZones(); kernel.model.removeAll(); + } else if (modelAlreadyExists) { + console.log('模型已存在,跳过清除操作,仅更新材质'); } // 先处理所有 change_model 事件 @@ -219,6 +244,12 @@ const executeEvent2 = async (result) => { console.log('替换百叶模型:', event); console.log('替换百叶模型类型:', category); + // 如果模型已存在,跳过加载 + if (modelAlreadyExists) { + console.log(`模型 ${category} 已存在,跳过加载`); + continue; + } + if (placement_zone) { const { alpha, border_color, color, show_border, thickness, walls } = placement_zone kernel.dropZone.setData({ @@ -338,5 +369,6 @@ window.AppLogic = { executeEvent, executeEvent2, getHotspot, - getProductConfig + getProductConfig, + isModelExists // 暴露检查模型是否存在的API }; \ No newline at end of file diff --git a/examples/index.global.js b/examples/index.global.js index 465aab8..9710cc6 100644 --- a/examples/index.global.js +++ b/examples/index.global.js @@ -129017,7 +129017,7 @@ discard;}} * @param modelId 模型ID */ cloneMaterials(meshes, modelId) { - const scene = this.mainApp.appScene.object; + this.mainApp.appScene.object; const clonedMaterials = /* @__PURE__ */ new Map(); meshes.forEach((mesh) => { if (mesh.material) { @@ -129027,15 +129027,10 @@ discard;}} const newName = `${originalName}_${modelId}`; const clonedMaterial = originalMaterial.clone(newName); clonedMaterials.set(originalName, clonedMaterial); - console.log(`[cloneMaterials] 克隆材质: ${originalName} -> ${newName}`); - console.log(`[cloneMaterials] 克隆后的材质名称: ${clonedMaterial.name}`); - console.log(`[cloneMaterials] 材质是否在场景中:`, scene.materials.includes(clonedMaterial)); } mesh.material = clonedMaterials.get(originalName); } }); - console.log(`已为模型 ${modelId} 克隆 ${clonedMaterials.size} 个材质`); - console.log(`[cloneMaterials] 场景中的所有材质:`, scene.materials.map((m) => m.name)); } /** 为网格设置阴影(投射和接收) */ createModelRoot(modelId, meshes) { @@ -129226,7 +129221,6 @@ discard;}} * @returns 模型名称,未找到返回 undefined */ findModelNameByMesh(mesh) { - console.log(111111111111); const keys = this.modelDic.Keys(); for (const key of keys) { const meshes = this.modelDic.Get(key); @@ -129269,7 +129263,6 @@ discard;}} * @param modelConfig 模型配置对象 */ async replaceModel(modelConfig) { - console.log(modelConfig.modelId, this.modelDic); this.removeByName(modelConfig.modelId); return await this.addSingle( modelConfig.modelId, @@ -129293,7 +129286,6 @@ discard;}} this.modelDic.Remove(modelName); this.modelMetadataDic.Remove(modelName); this.mainApp.gameManager?.updateDictionaries(); - console.log(`Model removed: ${modelName}`); } /** * 清除所有已添加的模型并释放内存 @@ -129301,7 +129293,6 @@ discard;}} */ removeAll() { const modelNames = this.modelDic.Keys(); - console.log(`开始清除所有模型,共 ${modelNames.length} 个模型`); modelNames.forEach((modelName) => { const meshes = this.modelDic.Get(modelName); if (meshes?.length) { @@ -129459,6 +129450,14 @@ discard;}} } }); } + /** + * 检查模型是否存在 + * @param modelId 模型ID + * @returns 模型是否存在 + */ + exists(modelId) { + return this.modelDic.Has(modelId); + } /** * 应用 transform 到模型 * @param modelId 模型ID @@ -355223,7 +355222,6 @@ clipPos=viewProjection*worldPos;previousClipPos=previousViewProjection*previousW } this.updateDictionaries(); this.cacheRollerDoorMeshes(); - console.log("材质字典:", this.materialDic); this.setRollerDoorScale("Box006.001", new Vector3(0.12, 0.02, 0.118)); this.setRollerDoorScale("Box005.001", new Vector3(0.13, 0.02, 0.12)); } @@ -355749,8 +355747,6 @@ clipPos=viewProjection*worldPos;previousClipPos=previousViewProjection*previousW */ applyMaterial(options) { this.updateDictionaries(); - console.log("[applyMaterial] 查找材质:", options.target); - console.log("[applyMaterial] 当前所有材质:", this.materialDic.Values().map((m) => m.name)); const targetMaterials = []; this.materialDic.Values().forEach((material) => { if (material.name === options.target || material.name.startsWith(`${options.target}_`)) { @@ -355761,7 +355757,6 @@ clipPos=viewProjection*worldPos;previousClipPos=previousViewProjection*previousW console.warn(`Material not found: ${options.target}`); return; } - console.log("[applyMaterial] 找到材质:", targetMaterials.map((m) => m.name)); targetMaterials.forEach((material) => { if (options.albedoColor) { const color = Color3.FromHexString(options.albedoColor); @@ -355784,7 +355779,6 @@ clipPos=viewProjection*worldPos;previousClipPos=previousViewProjection*previousW } material.markDirty(); }); - console.log(`Material applied to ${targetMaterials.length} material(s): ${options.target}`, options); } } diff --git a/index.js b/index.js index fdbf0fa..4340d96 100644 --- a/index.js +++ b/index.js @@ -202,7 +202,8 @@ export const executeEvent2 = async (result) => { // 检查是否有模型更换事件 const hasModelChange = result.data.events.some(e => e.event_type === 'change_model'); - + const modelExists = await kernel.model.exists(modelId); + console.log(modelExists); // 只有在需要更换模型时才清除 if (hasModelChange) { kernel.dropZone.clearZones(); @@ -241,7 +242,7 @@ export const executeEvent2 = async (result) => { modelControlType: model_control_type, }) - console.log(`百叶模型已放置为 ${name}`); + console.log(`模型已放置为 ${name}`); } } diff --git a/src/babylonjs/AppModel.ts b/src/babylonjs/AppModel.ts index 4ec421e..66774c7 100644 --- a/src/babylonjs/AppModel.ts +++ b/src/babylonjs/AppModel.ts @@ -427,7 +427,6 @@ export class AppModel extends Monobehiver { * @returns 模型名称,未找到返回 undefined */ findModelNameByMesh(mesh: AbstractMesh): string | undefined { - console.log(111111111111); const keys = this.modelDic.Keys(); for (const key of keys) { @@ -711,6 +710,15 @@ export class AppModel extends Monobehiver { }); } + /** + * 检查模型是否存在 + * @param modelId 模型ID + * @returns 模型是否存在 + */ + exists(modelId: string): boolean { + return this.modelDic.Has(modelId); + } + /** * 应用 transform 到模型 * @param modelId 模型ID