点击空白隐藏放置区域

This commit is contained in:
2026-05-14 12:09:10 +08:00
parent 8674efefc7
commit ce73c35b8a
6 changed files with 63 additions and 32 deletions

View File

@ -152,8 +152,31 @@ export class AppModel extends Monobehiver {
/**
* 克隆模型材质,避免多个模型共享同名材质
* @param meshes 网格数组
* @param modelName 模型名称
* @param modelId 模型ID
*/
private cloneMaterials(meshes: AbstractMesh[], modelId: string): void {
const scene = this.mainApp.appScene.object;
const clonedMaterials = new Map<string, any>();
meshes.forEach(mesh => {
if (mesh.material) {
const originalMaterial = mesh.material;
const originalName = originalMaterial.name;
// 如果该材质还没有被克隆过,则克隆它
if (!clonedMaterials.has(originalName)) {
const clonedMaterial = originalMaterial.clone(`${originalName}_${modelId}`);
clonedMaterials.set(originalName, clonedMaterial);
}
// 应用克隆的材质
mesh.material = clonedMaterials.get(originalName);
}
});
console.log(`已为模型 ${modelId} 克隆 ${clonedMaterials.size} 个材质`);
}
/** 为网格设置阴影(投射和接收) */
private createModelRoot(modelId: string, meshes: AbstractMesh[]): AbstractMesh[] {
const scene = this.mainApp.appScene.object;
@ -254,6 +277,9 @@ export class AppModel extends Monobehiver {
});
if (result.success && result.meshes) {
// 克隆材质,确保每个模型有独立的材质
this.cloneMaterials(result.meshes, modelName);
result.meshes = this.createModelRoot(modelName, result.meshes);
this.modelDic.Set(modelName, result.meshes);
@ -304,6 +330,9 @@ export class AppModel extends Monobehiver {
});
if (result.success && result.meshes) {
// 克隆材质,确保每个模型有独立的材质
this.cloneMaterials(result.meshes, modelId);
result.meshes = this.createModelRoot(modelId, result.meshes);
this.modelDic.Set(modelId, result.meshes);

View File

@ -157,7 +157,10 @@ class AppRay extends Monobehiver {
this.mainApp.appSelectionOutline.clear();
this.mainApp.appPositionGizmo.detach();
this.mainApp.appDomTo3D.hideAll()
this.mainApp.appDomTo3D.hideAll();
// 隐藏放置区域
this.mainApp.appDropZone?.hide();
}
}

View File

@ -751,6 +751,7 @@ export class GameManager extends Monobehiver {
*/
applyMaterial(options: {
target: string;
modelId?: string; // 新增指定模型ID只修改该模型的材质
albedoColor?: string;
albedoTexture?: string;
normalMap?: string;
@ -762,15 +763,32 @@ export class GameManager extends Monobehiver {
// 1. 查找目标材质
const targetMaterials: PBRMaterial[] = [];
this.materialDic.Values().forEach(material => {
if (material.name === options.target) {
targetMaterials.push(material);
}
});
if (targetMaterials.length === 0) {
console.warn(`Material not found: ${options.target}`);
return;
if (options.modelId) {
// 如果指定了模型ID只查找该模型的材质
const materialName = `${options.target}_${options.modelId}`;
this.materialDic.Values().forEach(material => {
if (material.name === materialName) {
targetMaterials.push(material);
}
});
if (targetMaterials.length === 0) {
console.warn(`Material not found for model ${options.modelId}: ${materialName}`);
return;
}
} else {
// 没有指定模型ID查找所有匹配的材质旧逻辑
this.materialDic.Values().forEach(material => {
if (material.name === options.target || material.name.startsWith(`${options.target}_`)) {
targetMaterials.push(material);
}
});
if (targetMaterials.length === 0) {
console.warn(`Material not found: ${options.target}`);
return;
}
}
// 2. 应用材质属性到目标材质
@ -810,6 +828,6 @@ export class GameManager extends Monobehiver {
material.markDirty();
});
console.log(`Material applied to: ${options.target}`, options);
console.log(`Material applied to: ${options.target}${options.modelId ? ` (model: ${options.modelId})` : ''}`, options);
}
}

View File

@ -69,6 +69,7 @@ export class KernelAdapter {
*/
apply: (options: {
target: string;
modelId?: string; // 可选指定模型ID只修改该模型的材质
albedoColor?: string;
albedoTexture?: string;
normalMap?: string;