Skip to content

Commit 8634a8e

Browse files
committed
Fix performance regression when rendering collision shapes
1 parent c6d130a commit 8634a8e

File tree

4 files changed

+32
-52
lines changed

4 files changed

+32
-52
lines changed

modules/gridmap/grid_map.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,12 +805,20 @@ bool GridMap::_octant_update(const OctantKey &p_key) {
805805

806806
#ifndef PHYSICS_3D_DISABLED
807807
if (col_debug.size()) {
808+
SceneTree *st = SceneTree::get_singleton();
809+
810+
Vector<Color> colors;
811+
colors.resize(col_debug.size());
812+
if (st) {
813+
colors.fill(st->get_debug_collisions_color());
814+
}
815+
808816
Array arr;
809817
arr.resize(RS::ARRAY_MAX);
810818
arr[RS::ARRAY_VERTEX] = col_debug;
819+
arr[RS::ARRAY_COLOR] = colors;
811820

812821
RS::get_singleton()->mesh_add_surface_from_arrays(g.collision_debug, RS::PRIMITIVE_LINES, arr);
813-
SceneTree *st = SceneTree::get_singleton();
814822
if (st) {
815823
RS::get_singleton()->mesh_surface_set_material(g.collision_debug, 0, st->get_debug_collision_material()->get_rid());
816824
}

scene/main/scene_tree.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,15 +1008,16 @@ Ref<Material> SceneTree::get_debug_collision_material() {
10081008
return collision_material;
10091009
}
10101010

1011-
Ref<StandardMaterial3D> line_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
1012-
line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
1013-
line_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
1014-
line_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
1015-
line_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
1016-
line_material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
1017-
line_material->set_albedo(get_debug_collisions_color());
1018-
1019-
collision_material = line_material;
1011+
Ref<StandardMaterial3D> material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
1012+
material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
1013+
material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
1014+
material->set_render_priority(StandardMaterial3D::RENDER_PRIORITY_MIN + 1);
1015+
material->set_cull_mode(StandardMaterial3D::CULL_BACK);
1016+
material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
1017+
material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
1018+
material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
1019+
1020+
collision_material = material;
10201021

10211022
return collision_material;
10221023
}

scene/resources/3d/shape_3d.cpp

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -107,63 +107,37 @@ Ref<ArrayMesh> Shape3D::get_debug_mesh() {
107107
debug_mesh_cache.instantiate();
108108

109109
if (!lines.is_empty()) {
110-
//make mesh
111-
Vector<Vector3> array;
112-
array.resize(lines.size());
113-
Vector3 *v = array.ptrw();
114-
115-
Vector<Color> arraycol;
116-
arraycol.resize(lines.size());
117-
Color *c = arraycol.ptrw();
118-
119-
for (int i = 0; i < lines.size(); i++) {
120-
v[i] = lines[i];
121-
c[i] = debug_color;
122-
}
110+
Vector<Color> colors;
111+
colors.resize(lines.size());
112+
colors.fill(debug_color);
123113

124114
Array lines_array;
125115
lines_array.resize(Mesh::ARRAY_MAX);
126-
lines_array[Mesh::ARRAY_VERTEX] = array;
127-
lines_array[Mesh::ARRAY_COLOR] = arraycol;
128-
129-
Ref<StandardMaterial3D> material = get_debug_collision_material();
116+
lines_array[Mesh::ARRAY_VERTEX] = lines;
117+
lines_array[Mesh::ARRAY_COLOR] = colors;
130118

131119
debug_mesh_cache->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, lines_array);
132-
debug_mesh_cache->surface_set_material(0, material);
120+
121+
SceneTree *scene_tree = SceneTree::get_singleton();
122+
if (scene_tree) {
123+
debug_mesh_cache->surface_set_material(0, scene_tree->get_debug_collision_material());
124+
}
133125

134126
if (debug_fill) {
135127
Ref<ArrayMesh> array_mesh = get_debug_arraymesh_faces(debug_color * Color(1.0, 1.0, 1.0, 0.0625));
136128
if (array_mesh.is_valid() && array_mesh->get_surface_count() > 0) {
137129
Array solid_array = array_mesh->surface_get_arrays(0);
138130
debug_mesh_cache->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, solid_array);
139-
debug_mesh_cache->surface_set_material(1, material);
131+
if (scene_tree) {
132+
debug_mesh_cache->surface_set_material(1, scene_tree->get_debug_collision_material());
133+
}
140134
}
141135
}
142136
}
143137

144138
return debug_mesh_cache;
145139
}
146140

147-
Ref<Material> Shape3D::get_debug_collision_material() {
148-
if (collision_material.is_valid()) {
149-
return collision_material;
150-
}
151-
152-
Ref<StandardMaterial3D> material = memnew(StandardMaterial3D);
153-
material->set_albedo(Color(1.0, 1.0, 1.0));
154-
material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
155-
material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
156-
material->set_render_priority(StandardMaterial3D::RENDER_PRIORITY_MIN + 1);
157-
material->set_cull_mode(StandardMaterial3D::CULL_BACK);
158-
material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
159-
material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
160-
material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
161-
162-
collision_material = material;
163-
164-
return collision_material;
165-
}
166-
167141
void Shape3D::_update_shape() {
168142
emit_changed();
169143
debug_mesh_cache.unref();

scene/resources/3d/shape_3d.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ class Shape3D : public Resource {
4444
real_t margin = 0.04;
4545

4646
Ref<ArrayMesh> debug_mesh_cache;
47-
Ref<Material> collision_material;
4847

4948
// Not wrapped in `#ifdef DEBUG_ENABLED` as it is used for rendering.
5049
Color debug_color = Color(0.0, 0.0, 0.0, 0.0);
@@ -59,8 +58,6 @@ class Shape3D : public Resource {
5958
_FORCE_INLINE_ RID get_shape() const { return shape; }
6059
Shape3D(RID p_shape);
6160

62-
Ref<Material> get_debug_collision_material();
63-
6461
virtual void _update_shape();
6562

6663
public:

0 commit comments

Comments
 (0)