From 62eda8189597ad95bba9e3ebcdf89b32a39663d6 Mon Sep 17 00:00:00 2001 From: zguiy <1415466602@qq.com> Date: Mon, 25 May 2026 10:56:24 +0800 Subject: [PATCH] 1 --- src/babylonjs/AppModelDrag.ts | 109 ++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/src/babylonjs/AppModelDrag.ts b/src/babylonjs/AppModelDrag.ts index 18d24c0..8b29607 100644 --- a/src/babylonjs/AppModelDrag.ts +++ b/src/babylonjs/AppModelDrag.ts @@ -121,6 +121,9 @@ export class AppModelDrag extends Monobehiver { pointerDragBehavior.onDragEndObservable.add(() => { // 恢复相机控制 this.enableCameraControl(); + + // 拖拽结束后,检测模型所属的分割区域并更新映射 + this.updateModelZoneMapping(modelId); }); return pointerDragBehavior; @@ -239,6 +242,112 @@ export class AppModelDrag extends Monobehiver { } } + /** + * 更新模型所属的分割区域映射 + * @param modelId 模型ID + */ + private updateModelZoneMapping(modelId: string): void { + const meshes = this.mainApp.appModel?.modelDic?.Get(modelId); + if (!meshes || !meshes.length) return; + + const rootMesh = meshes[0]; + const modelPosition = rootMesh.position; + + console.log(`[边界检测] 模型 ${modelId} 拖拽结束,当前位置:`, modelPosition); + + // 获取 AppDropZone + const appDropZone = this.mainApp.appDropZone; + if (!appDropZone) return; + + // 查找该模型原本所在的墙面 + let originalWallName: string | null = null; + appDropZone['zoneModelMap']?.forEach((id: string, zoneKey: string) => { + if (id === modelId) { + const match = zoneKey.match(/^(.+)\[(\d+)\]$/); + if (match) { + originalWallName = match[1]; + } + } + }); + + if (!originalWallName) { + console.log(`[边界检测] 模型 ${modelId} 未找到原始墙面,跳过检测`); + return; + } + + console.log(`[边界检测] 模型 ${modelId} 原始墙面: ${originalWallName}`); + + // 获取该墙面的所有分割区域 + const wallZones = appDropZone.getZonesByWall(originalWallName); + if (!wallZones.length) return; + + console.log(`[边界检测] 墙面 ${originalWallName} 有 ${wallZones.length} 个分割区域`); + + // 计算模型与每个分割区域的距离,找到最近的区域 + let closestZoneIndex = -1; + let minDistance = Number.POSITIVE_INFINITY; + + wallZones.forEach((zone, index) => { + // 计算模型位置到区域中心的距离 + const distance = modelPosition.subtract(zone.center).length(); + console.log(`[边界检测] 区域 ${index} 中心:`, zone.center, `距离: ${distance.toFixed(3)}`); + + if (distance < minDistance) { + minDistance = distance; + closestZoneIndex = index; + } + }); + + if (closestZoneIndex === -1) { + console.log(`[边界检测] 未找到最近的区域`); + return; + } + + console.log(`[边界检测] 模型 ${modelId} 最接近区域 ${closestZoneIndex},距离: ${minDistance.toFixed(3)}`); + + // 查找模型当前所在的区域索引 + let currentZoneIndex = -1; + appDropZone['zoneModelMap']?.forEach((id: string, zoneKey: string) => { + if (id === modelId) { + const match = zoneKey.match(/^.+\[(\d+)\]$/); + if (match) { + currentZoneIndex = parseInt(match[1]); + } + } + }); + + // 如果模型移动到了新的区域,更新映射 + if (currentZoneIndex !== closestZoneIndex) { + console.log(`[边界检测] 模型 ${modelId} 从区域 ${currentZoneIndex} 移动到区域 ${closestZoneIndex}`); + + // 删除旧映射 + if (currentZoneIndex !== -1) { + const oldKey = `${originalWallName}[${currentZoneIndex}]`; + appDropZone['zoneModelMap']?.delete(oldKey); + console.log(`[边界检测] 删除旧映射: ${oldKey}`); + } + + // 检查目标区域是否已有模型 + const newKey = `${originalWallName}[${closestZoneIndex}]`; + const existingModelId = appDropZone['zoneModelMap']?.get(newKey); + if (existingModelId && existingModelId !== modelId) { + console.log(`[边界检测] 目标区域 ${closestZoneIndex} 已有模型 ${existingModelId},交换位置`); + // 将原有模型移动到旧位置 + if (currentZoneIndex !== -1) { + const swapKey = `${originalWallName}[${currentZoneIndex}]`; + appDropZone['zoneModelMap']?.set(swapKey, existingModelId); + console.log(`[边界检测] 模型 ${existingModelId} 移动到区域 ${currentZoneIndex}`); + } + } + + // 添加新映射 + appDropZone['zoneModelMap']?.set(newKey, modelId); + console.log(`[边界检测] 添加新映射: ${newKey} -> ${modelId}`); + } else { + console.log(`[边界检测] 模型 ${modelId} 仍在区域 ${currentZoneIndex},无需更新映射`); + } + } + /** * 清理资源 */