This commit is contained in:
2026-05-19 13:48:52 +08:00
parent fef2c4e3bd
commit 8a427d3557
5 changed files with 391 additions and 20 deletions

View File

@ -751,6 +751,7 @@ export class GameManager extends Monobehiver {
*/
applyMaterial(options: {
target: string;
modelId?: string;
albedoColor?: string;
albedoTexture?: string;
normalMap?: string;
@ -760,22 +761,44 @@ export class GameManager extends Monobehiver {
}): void {
this.updateDictionaries();
// 查找目标材质(支持精确匹配和前缀匹配)
const targetMaterials: PBRMaterial[] = [];
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;
// 如果提供了 modelId只查找该模型的材质
if (options.modelId) {
// 获取该模型的所有 meshes
const modelMeshes = this.mainApp.appModel.modelDic.Get(options.modelId);
if (!modelMeshes || modelMeshes.length === 0) {
console.warn(`Model not found: ${options.modelId}`);
return;
}
// 遍历该模型的所有 mesh查找匹配的材质
modelMeshes.forEach((mesh: AbstractMesh) => {
if (mesh.material && mesh.material instanceof PBRMaterial) {
const material = mesh.material as PBRMaterial;
if (material.name === options.target || material.name.startsWith(`${options.target}_`)) {
// 避免重复添加
if (!targetMaterials.includes(material)) {
targetMaterials.push(material);
}
}
}
});
} else {
// 没有提供 modelId全局查找保持向后兼容
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}${options.modelId ? ` in model ${options.modelId}` : ''}`);
return;
}
// 应用材质属性到目标材质
targetMaterials.forEach(material => {