This commit is contained in:
2026-05-06 12:16:29 +08:00
parent 4207fcf7c2
commit 6cefd063f2
5 changed files with 26 additions and 54 deletions

View File

@ -88,18 +88,26 @@ export class GameManager extends Monobehiver {
const scene = this.mainApp.appScene?.object;
if (!scene) return;
this.materialDic.Clear();
this.meshDic.Clear();
// 更新材质字典
for (const mat of scene.materials) {
if (!this.materialDic.Has(mat.name)) {
if (!(mat as any)._isDisposed && !this.materialDic.Has(mat.name)) {
this.materialDic.Set(mat.name, mat as PBRMaterial);
}
}
// 更新网格字典
for (const mesh of scene.meshes) {
if (mesh instanceof Mesh && !this.meshDic.Has(mesh.name)) {
if (mesh instanceof Mesh && !mesh.isDisposed() && !this.meshDic.Has(mesh.name)) {
this.meshDic.Set(mesh.name, mesh);
}
const mat = mesh.material;
if (mat instanceof PBRMaterial && !(mat as any)._isDisposed) {
this.materialDic.Set(mat.name, mat);
}
}
}
@ -742,17 +750,15 @@ export class GameManager extends Monobehiver {
* @param target 目标对象
* @param material 材质路径
*/
applyMaterial(target: string, attribute: string, value: number | string): void {
// 这里需要根据实际的材质管理逻辑实现
console.log(`Applying attribute ${attribute} to ${value}`);
console.log(this.materialDic);
applyMaterial(target: string, attribute: string, value:string): void {
if (attribute !== 'baseColor' || typeof value !== 'string') return;
this.updateDictionaries();
// 示例实现:根据目标和材质路径应用材质
// 1. 查找目标网格
const targetMaterials: PBRMaterial[] = [];
this.materialDic.Values().forEach(material => {
if (material.name === target) {
console.log(`${this.materialDic.Get(material.name)}`, material);
targetMaterials.push(material);
}
});
@ -767,30 +773,15 @@ console.log(this.materialDic);
// 例如paint/blue 可以映射到特定的材质配置
// 3. 应用材质到目标网格
const color = Color3.FromHexString(value);
targetMaterials.forEach(material => {
if (attribute === 'baseColor' && typeof value === 'string') {
// 如果是 baseColor 且值是字符串16进制颜色转换为 Color3
const color = Color3.FromHexString(value);
console.log(`Before: albedoColor =`, material.albedoColor);
console.log(`Before: albedoTexture =`, material.albedoTexture);
material.albedoColor = color;
material.albedoColor.copyFrom(color);
// 如果有纹理,颜色会作为纹理的乘法因子
if (material.albedoTexture) {
console.log(`Material ${material.name} has albedoTexture, color will tint the texture`);
}
// 强制刷新材质
material.markDirty();
console.log(`After: albedoColor =`, material.albedoColor);
console.log(`Applying baseColor ${value} to material: ${material.name}`, color);
} else if (material[attribute]) {
material[attribute] = value;
console.log(`Applying attribute ${attribute} to ${value} to mesh: ${material.name}`);
}
});
}
}