1
This commit is contained in:
@ -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 => {
|
||||
|
||||
Reference in New Issue
Block a user