This commit is contained in:
2026-05-25 13:47:09 +08:00
parent 266c0c154e
commit dbde91bbe0
3 changed files with 40 additions and 145 deletions

View File

@ -13,7 +13,7 @@ export interface DragConfig {
step?: number;
boundaryConstraint?: boolean; // 边界限制:拖拽不得超出当前墙面的放置区域
snapToZone?: boolean; // 拖拽吸附:松开时自动吸附到最近的分割区域
onOccupiedZone?: 'return' | 'replace'; // 拖拽到已占用区域时的行为:'return' 返回原位置,'replace' 替换目标位置的模型(默认 'return'
onOccupiedZone?: 'return' | 'replace' | ''; // 拖拽到已占用区域时的行为:'return' 返回原位置,'replace' 替换目标位置的模型'' 什么都不做(允许重叠
}
/**
@ -563,6 +563,9 @@ export class AppModelDrag extends Monobehiver {
} else if (onOccupiedZone === 'replace') {
// 替换目标位置的模型(继续执行后面的逻辑)
console.log(`[拖拽吸附] 配置为替换模型,将替换模型 ${occupyingModelId}`);
} else if (onOccupiedZone === '') {
// 什么都不做,允许重叠(继续执行后面的逻辑,但不会触发替换)
console.log(`[拖拽吸附] 配置为允许重叠,继续放置`);
}
}
@ -672,33 +675,46 @@ export class AppModelDrag extends Monobehiver {
// 检查目标区域是否已有模型
const newKey = `${originalWallName}[${closestZoneIndex}]`;
const existingModelId = appDropZone['zoneModelMap']?.get(newKey);
// 获取拖拽配置
const dragInfo = this.modelDragMap.get(modelId);
const onOccupiedZone = dragInfo?.config.onOccupiedZone || 'return';
if (existingModelId && existingModelId !== modelId) {
console.log(`[边界检测] 目标区域 ${closestZoneIndex} 已有模型 ${existingModelId},交换位置`);
console.log(`[边界检测] 目标区域 ${closestZoneIndex} 已有模型 ${existingModelId}`);
// 将原有模型移动到旧位置
if (currentZoneIndex !== -1) {
const swapKey = `${originalWallName}[${currentZoneIndex}]`;
appDropZone['zoneModelMap']?.set(swapKey, existingModelId);
console.log(`[边界检测] 模型 ${existingModelId} 移动到区域 ${currentZoneIndex}`);
// 只有在 'replace' 模式下才交换位置
if (onOccupiedZone === 'replace') {
console.log(`[边界检测] 配置为替换模式,交换位置`);
// 实际移动被替换模型的物理位置
const existingMeshes = this.mainApp.appModel?.modelDic?.Get(existingModelId);
if (existingMeshes && existingMeshes.length) {
const existingRootMesh = existingMeshes[0];
const swapZone = wallZones[currentZoneIndex];
if (swapZone) {
const offsetDistance = -0.05;
const swapPosition = swapZone.center.add(swapZone.normal.scale(offsetDistance));
existingRootMesh.position.copyFrom(swapPosition);
// 将原有模型移动到旧位置
if (currentZoneIndex !== -1) {
const swapKey = `${originalWallName}[${currentZoneIndex}]`;
appDropZone['zoneModelMap']?.set(swapKey, existingModelId);
console.log(`[边界检测] 模型 ${existingModelId} 移动到区域 ${currentZoneIndex}`);
// 更新旋转
const targetDirection = swapZone.normal.scale(-1);
const angle = Math.atan2(targetDirection.x, targetDirection.z);
existingRootMesh.rotation.y = angle;
// 实际移动被替换模型的物理位置
const existingMeshes = this.mainApp.appModel?.modelDic?.Get(existingModelId);
if (existingMeshes && existingMeshes.length) {
const existingRootMesh = existingMeshes[0];
const swapZone = wallZones[currentZoneIndex];
if (swapZone) {
const offsetDistance = -0.05;
const swapPosition = swapZone.center.add(swapZone.normal.scale(offsetDistance));
existingRootMesh.position.copyFrom(swapPosition);
console.log(`[边界检测] 已将模型 ${existingModelId} 物理移动到区域 ${currentZoneIndex}`);
// 更新旋转
const targetDirection = swapZone.normal.scale(-1);
const angle = Math.atan2(targetDirection.x, targetDirection.z);
existingRootMesh.rotation.y = angle;
console.log(`[边界检测] 已将模型 ${existingModelId} 物理移动到区域 ${currentZoneIndex}`);
}
}
}
} else if (onOccupiedZone === '') {
// 允许重叠,不做任何处理
console.log(`[边界检测] 配置为允许重叠,不交换位置`);
}
}