This commit is contained in:
2026-05-17 13:59:16 +08:00
parent f76b19697c
commit 870477f864
7 changed files with 176 additions and 93 deletions

View File

@ -751,7 +751,6 @@ export class GameManager extends Monobehiver {
*/
applyMaterial(options: {
target: string;
modelId?: string; // 新增指定模型ID只修改该模型的材质
albedoColor?: string;
albedoTexture?: string;
normalMap?: string;
@ -761,37 +760,20 @@ export class GameManager extends Monobehiver {
}): void {
this.updateDictionaries();
// 1. 查找目标材质
// 查找目标材质(支持精确匹配和前缀匹配)
const targetMaterials: PBRMaterial[] = [];
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;
this.materialDic.Values().forEach(material => {
if (material.name === options.target || material.name.startsWith(`${options.target}_`)) {
targetMaterials.push(material);
}
} 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;
}
if (targetMaterials.length === 0) {
console.warn(`Material not found: ${options.target}`);
return;
}
// 2. 应用材质属性到目标材质
// 应用材质属性到目标材质
targetMaterials.forEach(material => {
// 应用颜色
if (options.albedoColor) {
@ -828,6 +810,6 @@ export class GameManager extends Monobehiver {
material.markDirty();
});
console.log(`Material applied to: ${options.target}${options.modelId ? ` (model: ${options.modelId})` : ''}`, options);
console.log(`Material applied to ${targetMaterials.length} material(s): ${options.target}`, options);
}
}