From 60a7e84a5dc2fc252b0c582dd8f877685d28d74a Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 4 Mar 2022 14:17:55 +0100 Subject: [PATCH] Implemented debug collision shape visualization for the terrain. Also fixed the winding order for the isometric mesher's returned shape. --- meshers/default/terrain_2d_mesher_default.cpp | 26 ++++++++++++- .../isometric/terrain_2d_mesher_isometric.cpp | 4 +- world/default/terrain_2d_chunk_default.cpp | 37 +++++++++++++++++++ world/default/terrain_2d_chunk_default.h | 3 ++ world/terrain_2d_chunk.cpp | 6 +++ world/terrain_2d_chunk.h | 4 ++ 6 files changed, 76 insertions(+), 4 deletions(-) diff --git a/meshers/default/terrain_2d_mesher_default.cpp b/meshers/default/terrain_2d_mesher_default.cpp index 5ed575d..617c6d4 100644 --- a/meshers/default/terrain_2d_mesher_default.cpp +++ b/meshers/default/terrain_2d_mesher_default.cpp @@ -22,8 +22,8 @@ SOFTWARE. #include "terrain_2d_mesher_default.h" -#include "../../world/default/terrain_2d_chunk_default.h" #include "../../library/terrain_2d_material_cache.h" +#include "../../world/default/terrain_2d_chunk_default.h" #include "../../defines.h" @@ -73,6 +73,11 @@ void Terrain2DMesherDefault::create_tile_colliders(Ref ch) { Physics2DServer::get_singleton()->body_set_state(body_rid, Physics2DServer::BODY_STATE_TRANSFORM, chunk->get_transform()); } +#if TOOLS_ENABLED + chunk->_debug_terrain_collider_transforms.clear(); +#endif + Physics2DServer::get_singleton()->body_clear_shapes(body_rid); + Physics2DServer::get_singleton()->body_set_mode(body_rid, Physics2DServer::BODY_MODE_STATIC); Physics2DServer::get_singleton()->body_attach_object_instance_id(body_rid, get_instance_id()); //TODO @@ -92,7 +97,18 @@ void Terrain2DMesherDefault::create_tile_colliders(Ref ch) { mcache = _library->material_cache_get(chunk->material_cache_key_get()); } - int texture_scale = get_texture_scale(); +#if TOOLS_ENABLED + SceneTree *st = SceneTree::get_singleton(); + + bool debug_shapes = false; + if (st) { + if (Engine::get_singleton()->is_editor_hint()) { + //debug_shapes = show_collision; + } else { + debug_shapes = st->is_debugging_collisions_hint(); + } + } +#endif int margin_start = chunk->get_margin_start(); //z_size + margin_start is fine, x, and z are in data space. @@ -108,6 +124,12 @@ void Terrain2DMesherDefault::create_tile_colliders(Ref ch) { if ((flags & Terrain2DChunkDefault::FLAG_CHANNEL_COLLIDER) != 0) { Transform2D coll_transform(0, mesh_transform_terrain.xform(Vector2((x)*cell_size_x, y * cell_size_y))); +#if TOOLS_ENABLED + if (debug_shapes) { + chunk->_debug_terrain_collider_transforms.push_back(coll_transform); + } +#endif + Physics2DServer::get_singleton()->body_add_shape(body_rid, tile_collider_shape->get_rid(), coll_transform); } } diff --git a/meshers/isometric/terrain_2d_mesher_isometric.cpp b/meshers/isometric/terrain_2d_mesher_isometric.cpp index 538147b..64861fa 100644 --- a/meshers/isometric/terrain_2d_mesher_isometric.cpp +++ b/meshers/isometric/terrain_2d_mesher_isometric.cpp @@ -635,10 +635,10 @@ Ref Terrain2DMesherIsometric::create_terrain_tile_collider_shape(Refmesh_transform_terrain_get(); points.push_back(mesh_transform_terrain.xform(Vector2(0, 0))); - points.push_back(mesh_transform_terrain.xform(Vector2(_cell_size_x, 0))); points.push_back(mesh_transform_terrain.xform(Vector2(0, _cell_size_y))); points.push_back(mesh_transform_terrain.xform(Vector2(_cell_size_x, _cell_size_y))); - + points.push_back(mesh_transform_terrain.xform(Vector2(_cell_size_x, 0))); + sh->set_points(points); return sh; diff --git a/world/default/terrain_2d_chunk_default.cpp b/world/default/terrain_2d_chunk_default.cpp index 5721c96..f9c7984 100644 --- a/world/default/terrain_2d_chunk_default.cpp +++ b/world/default/terrain_2d_chunk_default.cpp @@ -727,6 +727,43 @@ void Terrain2DChunkDefault::_draw() { //Note: the transform parameter is not implemented in gles2 VisualServer::get_singleton()->canvas_item_add_mesh(get_canvas_item(0), prop_mesh_rid, Transform2D(), Color(1, 1, 1, 1), prop_texture_rid, RID()); } + +#if TOOLS_ENABLED + SceneTree *st = SceneTree::get_singleton(); + + bool debug_shapes = false; + if (st) { + if (Engine::get_singleton()->is_editor_hint()) { + //debug_shapes = show_collision; + } else { + debug_shapes = st->is_debugging_collisions_hint(); + } + } + + if (debug_shapes) { + if (_debug_canvas_item == RID()) { + _debug_canvas_item = VisualServer::get_singleton()->canvas_item_create(); + + if (_voxel_world) { + VisualServer::get_singleton()->canvas_item_set_parent(_debug_canvas_item, get_voxel_world()->get_canvas_item()); + } + + VisualServer::get_singleton()->canvas_item_set_transform(_debug_canvas_item, get_transform()); + VisualServer::get_singleton()->canvas_item_set_z_index(_debug_canvas_item, 1); + } + + VisualServer::get_singleton()->canvas_item_clear(_debug_canvas_item); + + Color debug_collision_color = st->get_debug_collisions_color(); + + Ref shape = get_default_tile_shape(); + + for (int i = 0; i < _debug_terrain_collider_transforms.size(); ++i) { + VisualServer::get_singleton()->canvas_item_add_set_transform(_debug_canvas_item, _debug_terrain_collider_transforms[i]); + shape->draw(_debug_canvas_item, debug_collision_color); + } + } +#endif } //Lights diff --git a/world/default/terrain_2d_chunk_default.h b/world/default/terrain_2d_chunk_default.h index c75c8f6..3f9afd2 100644 --- a/world/default/terrain_2d_chunk_default.h +++ b/world/default/terrain_2d_chunk_default.h @@ -202,6 +202,9 @@ public: //Todo make this a generic thing Vector _mesh_transforms; RID _2d_body_rid; +#if TOOLS_ENABLED + Vector _debug_terrain_collider_transforms; +#endif protected: virtual void _channel_setup(); diff --git a/world/terrain_2d_chunk.cpp b/world/terrain_2d_chunk.cpp index dc55dba..ee0580d 100644 --- a/world/terrain_2d_chunk.cpp +++ b/world/terrain_2d_chunk.cpp @@ -1325,6 +1325,12 @@ void Terrain2DChunk::_world_transform_changed() { if (get_canvas_item_count() > 0) { VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(0), t); } + +#if TOOLS_ENABLED + if (_debug_canvas_item.is_valid()) { + VisualServer::get_singleton()->canvas_item_set_transform(_debug_canvas_item, t); + } +#endif } /* diff --git a/world/terrain_2d_chunk.h b/world/terrain_2d_chunk.h index c6e4f01..82fcf70 100644 --- a/world/terrain_2d_chunk.h +++ b/world/terrain_2d_chunk.h @@ -364,6 +364,10 @@ public: Terrain2DChunk(); ~Terrain2DChunk(); +#if TOOLS_ENABLED + RID _debug_canvas_item; +#endif + protected: virtual void _enter_tree(); virtual void _exit_tree();