diff options
author | clayjohn <claynjohn@gmail.com> | 2020-01-30 17:18:09 -0800 |
---|---|---|
committer | clayjohn <claynjohn@gmail.com> | 2020-03-30 16:53:07 -0700 |
commit | 3362e81674e2c149fe326430ff414ae344d26e19 (patch) | |
tree | 6219cdc7f10179efbf993e12e432046a714d3d6e /scene | |
parent | dfed5efea3b5646cfca6662e52cf20c54558eb42 (diff) |
Add a method to retrieve active material from MeshInstance
Diffstat (limited to 'scene')
-rw-r--r-- | scene/3d/mesh_instance_3d.cpp | 18 | ||||
-rw-r--r-- | scene/3d/mesh_instance_3d.h | 1 |
2 files changed, 19 insertions, 0 deletions
diff --git a/scene/3d/mesh_instance_3d.cpp b/scene/3d/mesh_instance_3d.cpp index 284bbd7091..30aabf6e00 100644 --- a/scene/3d/mesh_instance_3d.cpp +++ b/scene/3d/mesh_instance_3d.cpp @@ -305,6 +305,23 @@ Ref<Material> MeshInstance3D::get_surface_material(int p_surface) const { return materials[p_surface]; } +Ref<Material> MeshInstance3D::get_active_material(int p_surface) const { + + if (get_material_override() != Ref<Material>()) { + return get_material_override(); + } else if (p_surface < materials.size()) { + return materials[p_surface]; + } else { + Ref<Mesh> mesh = get_mesh(); + + if (mesh.is_null() || p_surface >= mesh->get_surface_count()) { + return Ref<Material>(); + } + + return mesh->surface_get_material(p_surface); + } +} + void MeshInstance3D::_mesh_changed() { materials.resize(mesh->get_surface_count()); @@ -397,6 +414,7 @@ void MeshInstance3D::_bind_methods() { ClassDB::bind_method(D_METHOD("get_surface_material_count"), &MeshInstance3D::get_surface_material_count); ClassDB::bind_method(D_METHOD("set_surface_material", "surface", "material"), &MeshInstance3D::set_surface_material); ClassDB::bind_method(D_METHOD("get_surface_material", "surface"), &MeshInstance3D::get_surface_material); + ClassDB::bind_method(D_METHOD("get_active_material", "surface"), &MeshInstance3D::get_active_material); ClassDB::bind_method(D_METHOD("create_trimesh_collision"), &MeshInstance3D::create_trimesh_collision); ClassDB::set_method_flags("MeshInstance3D", "create_trimesh_collision", METHOD_FLAGS_DEFAULT); diff --git a/scene/3d/mesh_instance_3d.h b/scene/3d/mesh_instance_3d.h index e45f68e295..914148f427 100644 --- a/scene/3d/mesh_instance_3d.h +++ b/scene/3d/mesh_instance_3d.h @@ -84,6 +84,7 @@ public: int get_surface_material_count() const; void set_surface_material(int p_surface, const Ref<Material> &p_material); Ref<Material> get_surface_material(int p_surface) const; + Ref<Material> get_active_material(int p_surface) const; Node *create_trimesh_collision_node(); void create_trimesh_collision(); |