From 72ee09082c693370ef7856eb7d7426c91c178cc1 Mon Sep 17 00:00:00 2001 From: PouleyKetchoupp Date: Wed, 22 Apr 2020 14:35:25 +0200 Subject: [PATCH] Fix get_active_material when a material is directly set on the mesh Makes MeshInstance3D::get_active_material consistent with the logic in the rendering system. Fixes #38108 --- scene/3d/mesh_instance_3d.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/scene/3d/mesh_instance_3d.cpp b/scene/3d/mesh_instance_3d.cpp index d56a095a5b..cdc8db8aea 100644 --- a/scene/3d/mesh_instance_3d.cpp +++ b/scene/3d/mesh_instance_3d.cpp @@ -307,19 +307,22 @@ Ref MeshInstance3D::get_surface_material(int p_surface) const { Ref MeshInstance3D::get_active_material(int p_surface) const { - if (get_material_override() != Ref()) { - return get_material_override(); - } else if (p_surface < materials.size()) { - return materials[p_surface]; - } else { - Ref mesh = get_mesh(); + Ref material_override = get_material_override(); + if (material_override.is_valid()) { + return material_override; + } - if (mesh.is_null() || p_surface >= mesh->get_surface_count()) { - return Ref(); - } + Ref surface_material = get_surface_material(p_surface); + if (surface_material.is_valid()) { + return surface_material; + } + Ref mesh = get_mesh(); + if (mesh.is_valid()) { return mesh->surface_get_material(p_surface); } + + return Ref(); } void MeshInstance3D::_mesh_changed() {