Merge pull request #77326 from BastiaanOlij/fix_material_dependencies

Properly clear material slots on mesh instance when material is freed
This commit is contained in:
Rémi Verschelde 2023-05-22 22:36:48 +02:00
commit c3c1909429
No known key found for this signature in database
GPG key ID: C3336907360768E1
2 changed files with 24 additions and 1 deletions

View file

@ -34,7 +34,6 @@
#include "core/object/worker_thread_pool.h"
#include "core/os/os.h"
#include "rendering_server_default.h"
#include "rendering_server_globals.h"
#include <new>

View file

@ -42,6 +42,7 @@
#include "servers/rendering/renderer_scene_occlusion_cull.h"
#include "servers/rendering/renderer_scene_render.h"
#include "servers/rendering/rendering_method.h"
#include "servers/rendering/rendering_server_globals.h"
#include "servers/rendering/storage/utilities.h"
#include "servers/xr/xr_interface.h"
@ -516,6 +517,29 @@ public:
} else if (p_dependency == instance->skeleton) {
singleton->instance_attach_skeleton(instance->self, RID());
} else {
// It's possible the same material is used in multiple slots,
// so we check whether we need to clear them all.
if (p_dependency == instance->material_override) {
singleton->instance_geometry_set_material_override(instance->self, RID());
}
if (p_dependency == instance->material_overlay) {
singleton->instance_geometry_set_material_overlay(instance->self, RID());
}
for (int i = 0; i < instance->materials.size(); i++) {
if (p_dependency == instance->materials[i]) {
singleton->instance_set_surface_override_material(instance->self, i, RID());
}
}
if (instance->base_type == RS::INSTANCE_PARTICLES) {
RID particle_material = RSG::particles_storage->particles_get_process_material(instance->base);
if (p_dependency == particle_material) {
RSG::particles_storage->particles_set_process_material(instance->base, RID());
}
}
// Even if no change is made we still need to call `_instance_queue_update`.
// This dependency could also be a result of the freed material being used
// by the mesh this mesh instance uses.
singleton->_instance_queue_update(instance, false, true);
}
}