From 3362e81674e2c149fe326430ff414ae344d26e19 Mon Sep 17 00:00:00 2001 From: clayjohn Date: Thu, 30 Jan 2020 17:18:09 -0800 Subject: [PATCH] Add a method to retrieve active material from MeshInstance --- doc/classes/MeshInstance3D.xml | 15 ++++++++++++--- scene/3d/mesh_instance_3d.cpp | 18 ++++++++++++++++++ scene/3d/mesh_instance_3d.h | 1 + 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/doc/classes/MeshInstance3D.xml b/doc/classes/MeshInstance3D.xml index 9276c5dc65b..c569da2df19 100644 --- a/doc/classes/MeshInstance3D.xml +++ b/doc/classes/MeshInstance3D.xml @@ -4,7 +4,7 @@ Node that instances meshes into a scenario. - MeshInstance3D is a node that takes a [Mesh] resource and adds it to the current scenario by creating an instance of it. This is the class most often used to get 3D geometry rendered and can be used to instance a single [Mesh] in many places. This allows to reuse geometry and save on resources. When a [Mesh] has to be instanced more than thousands of times at close proximity, consider using a [MultiMesh] in a [MultiMeshInstance3D] instead. + MeshInstance3D is a node that takes a [Mesh] resource and adds it to the current scenario by creating an instance of it. This is the class most often used render 3D geometry and can be used to instance a single [Mesh] in many places. This allows reuse of geometry which can save on resources. When a [Mesh] has to be instanced more than thousands of times at close proximity, consider using a [MultiMesh] in a [MultiMeshInstance3D] instead. @@ -30,13 +30,22 @@ This helper creates a [StaticBody3D] child node with a [ConcavePolygonShape3D] collision shape calculated from the mesh geometry. It's mainly used for testing. + + + + + + + Returns the [Material] that will be used by the [Mesh] when drawing. This can return the [member GeometryInstance3D.material_override], the surface override [Material] defined in this [MeshInstance3D], or the surface [Material] defined in the [Mesh]. For example, if [member GeometryInstance3D.material_override] is used, all surfaces will return the override material. + + - Returns the [Material] for a surface of the [Mesh] resource. + Returns the override [Material] for the specified surface of the [Mesh] resource. @@ -54,7 +63,7 @@ - Sets the [Material] for a surface of the [Mesh] resource. + Sets the override [Material] for the specified surface of the [Mesh] resource. This material is associated with this [MeshInstance3D] rather than with the [Mesh] resource. diff --git a/scene/3d/mesh_instance_3d.cpp b/scene/3d/mesh_instance_3d.cpp index 284bbd70917..30aabf6e008 100644 --- a/scene/3d/mesh_instance_3d.cpp +++ b/scene/3d/mesh_instance_3d.cpp @@ -305,6 +305,23 @@ Ref MeshInstance3D::get_surface_material(int p_surface) const { return materials[p_surface]; } +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(); + + if (mesh.is_null() || p_surface >= mesh->get_surface_count()) { + return Ref(); + } + + 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 e45f68e295f..914148f4279 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 &p_material); Ref get_surface_material(int p_surface) const; + Ref get_active_material(int p_surface) const; Node *create_trimesh_collision_node(); void create_trimesh_collision();