1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-05 12:10:55 +00:00

Change ShapeCast3D and RayCast3D debug to use RenderingServer directly

Changes ShapeCast3D and RayCast3D debug to use RenderingServer directly.
This commit is contained in:
smix8
2023-10-01 00:16:42 +02:00
parent f5dbbf7fd0
commit f556d7a54b
4 changed files with 83 additions and 57 deletions

View File

@@ -41,7 +41,7 @@ void RayCast3D::set_target_position(const Vector3 &p_point) {
if (is_inside_tree()) { if (is_inside_tree()) {
_update_debug_shape_vertices(); _update_debug_shape_vertices();
} }
} else if (debug_shape) { } else if (debug_instance.is_valid()) {
_update_debug_shape(); _update_debug_shape();
} }
} }
@@ -186,11 +186,17 @@ void RayCast3D::_notification(int p_what) {
set_physics_process_internal(false); set_physics_process_internal(false);
} }
if (debug_shape) { if (debug_instance.is_valid()) {
_clear_debug_shape(); _clear_debug_shape();
} }
} break; } break;
case NOTIFICATION_VISIBILITY_CHANGED: {
if (is_inside_tree() && debug_instance.is_valid()) {
RenderingServer::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree());
}
} break;
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
if (!enabled) { if (!enabled) {
break; break;
@@ -200,6 +206,9 @@ void RayCast3D::_notification(int p_what) {
_update_raycast_state(); _update_raycast_state();
if (prev_collision_state != collided && get_tree()->is_debugging_collisions_hint()) { if (prev_collision_state != collided && get_tree()->is_debugging_collisions_hint()) {
_update_debug_shape_material(true); _update_debug_shape_material(true);
if (is_inside_tree() && debug_instance.is_valid()) {
RenderingServer::get_singleton()->instance_set_transform(debug_instance, get_global_transform());
}
} }
} break; } break;
} }
@@ -416,7 +425,7 @@ void RayCast3D::set_debug_shape_thickness(const int p_debug_shape_thickness) {
if (is_inside_tree()) { if (is_inside_tree()) {
_update_debug_shape_vertices(); _update_debug_shape_vertices();
} }
} else if (debug_shape) { } else if (debug_instance.is_valid()) {
_update_debug_shape(); _update_debug_shape();
} }
} }
@@ -448,13 +457,13 @@ const Color &RayCast3D::get_debug_shape_custom_color() const {
void RayCast3D::_create_debug_shape() { void RayCast3D::_create_debug_shape() {
_update_debug_shape_material(); _update_debug_shape_material();
Ref<ArrayMesh> mesh = memnew(ArrayMesh); if (!debug_instance.is_valid()) {
debug_instance = RenderingServer::get_singleton()->instance_create();
}
MeshInstance3D *mi = memnew(MeshInstance3D); if (debug_mesh.is_null()) {
mi->set_mesh(mesh); debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
}
add_child(mi);
debug_shape = mi;
} }
void RayCast3D::_update_debug_shape_material(bool p_check_collision) { void RayCast3D::_update_debug_shape_material(bool p_check_collision) {
@@ -494,19 +503,17 @@ void RayCast3D::_update_debug_shape() {
return; return;
} }
if (!debug_shape) { if (!debug_instance.is_valid()) {
_create_debug_shape(); _create_debug_shape();
} }
MeshInstance3D *mi = static_cast<MeshInstance3D *>(debug_shape); if (!debug_instance.is_valid() || debug_mesh.is_null()) {
Ref<ArrayMesh> mesh = mi->get_mesh();
if (!mesh.is_valid()) {
return; return;
} }
_update_debug_shape_vertices(); _update_debug_shape_vertices();
mesh->clear_surfaces(); debug_mesh->clear_surfaces();
Array a; Array a;
a.resize(Mesh::ARRAY_MAX); a.resize(Mesh::ARRAY_MAX);
@@ -516,32 +523,36 @@ void RayCast3D::_update_debug_shape() {
if (!debug_line_vertices.is_empty()) { if (!debug_line_vertices.is_empty()) {
a[Mesh::ARRAY_VERTEX] = debug_line_vertices; a[Mesh::ARRAY_VERTEX] = debug_line_vertices;
mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a, Array(), Dictionary(), flags); debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a, Array(), Dictionary(), flags);
mesh->surface_set_material(surface_count, debug_material); debug_mesh->surface_set_material(surface_count, debug_material);
++surface_count; ++surface_count;
} }
if (!debug_shape_vertices.is_empty()) { if (!debug_shape_vertices.is_empty()) {
a[Mesh::ARRAY_VERTEX] = debug_shape_vertices; a[Mesh::ARRAY_VERTEX] = debug_shape_vertices;
mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLE_STRIP, a, Array(), Dictionary(), flags); debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLE_STRIP, a, Array(), Dictionary(), flags);
mesh->surface_set_material(surface_count, debug_material); debug_mesh->surface_set_material(surface_count, debug_material);
++surface_count; ++surface_count;
} }
RenderingServer::get_singleton()->instance_set_base(debug_instance, debug_mesh->get_rid());
if (is_inside_tree()) {
RenderingServer::get_singleton()->instance_set_scenario(debug_instance, get_world_3d()->get_scenario());
RenderingServer::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree());
RenderingServer::get_singleton()->instance_set_transform(debug_instance, get_global_transform());
}
} }
void RayCast3D::_clear_debug_shape() { void RayCast3D::_clear_debug_shape() {
if (!debug_shape) { ERR_FAIL_NULL(RenderingServer::get_singleton());
return; if (debug_instance.is_valid()) {
RenderingServer::get_singleton()->free(debug_instance);
debug_instance = RID();
} }
if (debug_mesh.is_valid()) {
MeshInstance3D *mi = static_cast<MeshInstance3D *>(debug_shape); RenderingServer::get_singleton()->free(debug_mesh->get_rid());
if (mi->is_inside_tree()) { debug_mesh = Ref<ArrayMesh>();
mi->queue_free();
} else {
memdelete(mi);
} }
debug_shape = nullptr;
} }
RayCast3D::RayCast3D() { RayCast3D::RayCast3D() {

View File

@@ -53,7 +53,6 @@ class RayCast3D : public Node3D {
uint32_t collision_mask = 1; uint32_t collision_mask = 1;
bool exclude_parent_body = true; bool exclude_parent_body = true;
Node *debug_shape = nullptr;
Ref<Material> debug_material; Ref<Material> debug_material;
Color debug_shape_custom_color = Color(0.0, 0.0, 0.0); Color debug_shape_custom_color = Color(0.0, 0.0, 0.0);
int debug_shape_thickness = 2; int debug_shape_thickness = 2;
@@ -72,6 +71,9 @@ class RayCast3D : public Node3D {
bool hit_from_inside = false; bool hit_from_inside = false;
bool hit_back_faces = true; bool hit_back_faces = true;
RID debug_instance;
Ref<ArrayMesh> debug_mesh;
protected: protected:
void _notification(int p_what); void _notification(int p_what);
void _update_raycast_state(); void _update_raycast_state();

View File

@@ -64,11 +64,17 @@ void ShapeCast3D::_notification(int p_what) {
set_physics_process_internal(false); set_physics_process_internal(false);
} }
if (debug_shape) { if (debug_instance.is_valid()) {
_clear_debug_shape(); _clear_debug_shape();
} }
} break; } break;
case NOTIFICATION_VISIBILITY_CHANGED: {
if (is_inside_tree() && debug_instance.is_valid()) {
RenderingServer::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree());
}
} break;
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
if (!enabled) { if (!enabled) {
break; break;
@@ -86,6 +92,9 @@ void ShapeCast3D::_notification(int p_what) {
if (prev_collision_state == collided && !collided) { if (prev_collision_state == collided && !collided) {
_update_debug_shape(); _update_debug_shape();
} }
if (is_inside_tree() && debug_instance.is_valid()) {
RenderingServer::get_singleton()->instance_set_transform(debug_instance, get_global_transform());
}
} }
} break; } break;
} }
@@ -217,7 +226,7 @@ void ShapeCast3D::set_target_position(const Vector3 &p_point) {
if (is_inside_tree()) { if (is_inside_tree()) {
_update_debug_shape_vertices(); _update_debug_shape_vertices();
} }
} else if (debug_shape) { } else if (debug_instance.is_valid()) {
_update_debug_shape(); _update_debug_shape();
} }
} }
@@ -532,13 +541,13 @@ const Color &ShapeCast3D::get_debug_shape_custom_color() const {
void ShapeCast3D::_create_debug_shape() { void ShapeCast3D::_create_debug_shape() {
_update_debug_shape_material(); _update_debug_shape_material();
Ref<ArrayMesh> mesh = memnew(ArrayMesh); if (!debug_instance.is_valid()) {
debug_instance = RenderingServer::get_singleton()->instance_create();
}
MeshInstance3D *mi = memnew(MeshInstance3D); if (debug_mesh.is_null()) {
mi->set_mesh(mesh); debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
}
add_child(mi);
debug_shape = mi;
} }
void ShapeCast3D::_update_debug_shape_material(bool p_check_collision) { void ShapeCast3D::_update_debug_shape_material(bool p_check_collision) {
@@ -578,7 +587,7 @@ void ShapeCast3D::_update_debug_shape() {
return; return;
} }
if (!debug_shape) { if (!debug_instance.is_valid()) {
_create_debug_shape(); _create_debug_shape();
} }
@@ -588,13 +597,11 @@ void ShapeCast3D::_update_debug_shape() {
return; return;
} }
MeshInstance3D *mi = static_cast<MeshInstance3D *>(debug_shape); if (!debug_instance.is_valid() || debug_mesh.is_null()) {
Ref<ArrayMesh> mesh = mi->get_mesh();
if (!mesh.is_valid()) {
return; return;
} }
mesh->clear_surfaces(); debug_mesh->clear_surfaces();
Array a; Array a;
a.resize(Mesh::ARRAY_MAX); a.resize(Mesh::ARRAY_MAX);
@@ -604,30 +611,34 @@ void ShapeCast3D::_update_debug_shape() {
if (!debug_shape_vertices.is_empty()) { if (!debug_shape_vertices.is_empty()) {
a[Mesh::ARRAY_VERTEX] = debug_shape_vertices; a[Mesh::ARRAY_VERTEX] = debug_shape_vertices;
mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a, Array(), Dictionary(), flags); debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a, Array(), Dictionary(), flags);
mesh->surface_set_material(surface_count, debug_material); debug_mesh->surface_set_material(surface_count, debug_material);
++surface_count; ++surface_count;
} }
if (!debug_line_vertices.is_empty()) { if (!debug_line_vertices.is_empty()) {
a[Mesh::ARRAY_VERTEX] = debug_line_vertices; a[Mesh::ARRAY_VERTEX] = debug_line_vertices;
mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a, Array(), Dictionary(), flags); debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a, Array(), Dictionary(), flags);
mesh->surface_set_material(surface_count, debug_material); debug_mesh->surface_set_material(surface_count, debug_material);
++surface_count; ++surface_count;
} }
RenderingServer::get_singleton()->instance_set_base(debug_instance, debug_mesh->get_rid());
if (is_inside_tree()) {
RenderingServer::get_singleton()->instance_set_scenario(debug_instance, get_world_3d()->get_scenario());
RenderingServer::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree());
RenderingServer::get_singleton()->instance_set_transform(debug_instance, get_global_transform());
}
} }
void ShapeCast3D::_clear_debug_shape() { void ShapeCast3D::_clear_debug_shape() {
if (!debug_shape) { ERR_FAIL_NULL(RenderingServer::get_singleton());
return; if (debug_instance.is_valid()) {
RenderingServer::get_singleton()->free(debug_instance);
debug_instance = RID();
} }
if (debug_mesh.is_valid()) {
MeshInstance3D *mi = static_cast<MeshInstance3D *>(debug_shape); RenderingServer::get_singleton()->free(debug_mesh->get_rid());
if (mi->is_inside_tree()) { debug_mesh = Ref<ArrayMesh>();
mi->queue_free();
} else {
memdelete(mi);
} }
debug_shape = nullptr;
} }

View File

@@ -55,7 +55,6 @@ class ShapeCast3D : public Node3D {
bool collide_with_areas = false; bool collide_with_areas = false;
bool collide_with_bodies = true; bool collide_with_bodies = true;
Node *debug_shape = nullptr;
Ref<Material> debug_material; Ref<Material> debug_material;
Color debug_shape_custom_color = Color(0.0, 0.0, 0.0); Color debug_shape_custom_color = Color(0.0, 0.0, 0.0);
Vector<Vector3> debug_shape_vertices; Vector<Vector3> debug_shape_vertices;
@@ -76,6 +75,9 @@ class ShapeCast3D : public Node3D {
Array _get_collision_result() const; Array _get_collision_result() const;
RID debug_instance;
Ref<ArrayMesh> debug_mesh;
protected: protected:
void _notification(int p_what); void _notification(int p_what);
void _update_shapecast_state(); void _update_shapecast_state();