Fix model material access with empty entries

Allow model instance material APIs to fall back to asset material slots when instance entries have not been initialized yet. Lazily initialize entries before writing material overrides so imported prefab StaticModel instances can call GetMaterial and SetMaterial without assertion failures.\n\nFixes #3801.
This commit is contained in:
luchu1993
2026-06-20 10:22:51 +08:00
parent 396f412f87
commit 9943959e76
4 changed files with 15 additions and 8 deletions
+4 -4
View File
@@ -128,7 +128,7 @@ MaterialBase* StaticModel::GetMaterial(int32 meshIndex, int32 lodIndex) const
Math::IsInRange(meshIndex, 0, model->LODs[lodIndex].Meshes.Count()));
const auto& mesh = model->LODs[lodIndex].Meshes[meshIndex];
const auto materialSlotIndex = mesh.GetMaterialSlotIndex();
MaterialBase* material = Entries[materialSlotIndex].Material.Get();
MaterialBase* material = materialSlotIndex < Entries.Count() ? Entries[materialSlotIndex].Material.Get() : nullptr;
return material ? material : model->MaterialSlots[materialSlotIndex].Material.Get();
}
@@ -586,9 +586,9 @@ MaterialBase* StaticModel::GetMaterial(int32 entryIndex)
{
if (!Model || Model->WaitForLoaded())
return nullptr;
CHECK_RETURN(entryIndex >= 0 && entryIndex < Entries.Count(), nullptr);
MaterialBase* material = Entries[entryIndex].Material.Get();
if (!material && entryIndex < Model->MaterialSlots.Count())
CHECK_RETURN(entryIndex >= 0 && entryIndex < Model->MaterialSlots.Count(), nullptr);
MaterialBase* material = entryIndex < Entries.Count() ? Entries[entryIndex].Material.Get() : nullptr;
if (!material)
{
material = Model->MaterialSlots[entryIndex].Material.Get();
if (!material)