Added a method to the mesher so it can create terrain mesh collider shapes.

This commit is contained in:
Relintai 2022-03-04 00:23:15 +01:00
parent 5567c7da33
commit 487fbc65e5
4 changed files with 36 additions and 1 deletions

View File

@ -25,6 +25,7 @@ SOFTWARE.
#include "core/math/math_funcs.h"
#include "../../library/terrain_2d_material_cache.h"
#include "scene/resources/convex_polygon_shape_2d.h"
void Terrain2DMesherIsometric::_add_chunk(Ref<Terrain2DChunk> p_chunk) {
Ref<Terrain2DChunkDefault> chunk = p_chunk;
@ -588,7 +589,7 @@ void Terrain2DMesherIsometric::mesh_walls(Ref<Terrain2DChunkDefault> chunk) {
if ((flags & Terrain2DChunkDefault::FLAG_CHANNEL_WALL_EAST) != 0) {
int vc = get_vertex_count();
_mesh_transform = Transform2D(0, mesh_transform_terrain.xform(Vector2((x + 1)*cell_size_x, (y ) * cell_size_y)));
_mesh_transform = Transform2D(0, mesh_transform_terrain.xform(Vector2((x + 1) * cell_size_x, (y)*cell_size_y)));
Vector2 verts_wall_east[] = {
Vector2(-cell_size_x, -cell_size_y),
@ -626,6 +627,23 @@ void Terrain2DMesherIsometric::mesh_walls(Ref<Terrain2DChunkDefault> chunk) {
}
}
Ref<Shape2D> Terrain2DMesherIsometric::create_terrain_tile_collider_shape(Ref<Terrain2DChunk> chunk) {
Ref<ConvexPolygonShape2D> sh;
sh.instance();
Vector<Vector2> points;
Transform2D mesh_transform_terrain = chunk->mesh_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)));
sh->set_points(points);
return sh;
}
Terrain2DMesherIsometric::Terrain2DMesherIsometric() {
}

View File

@ -46,6 +46,8 @@ public:
void mesh_base(Ref<Terrain2DChunkDefault> chunk);
void mesh_walls(Ref<Terrain2DChunkDefault> chunk);
Ref<Shape2D> create_terrain_tile_collider_shape(Ref<Terrain2DChunk> chunk);
Terrain2DMesherIsometric();
~Terrain2DMesherIsometric();

View File

@ -28,6 +28,7 @@ SOFTWARE.
#include "../world/default/terrain_2d_chunk_default.h"
#include "../world/terrain_2d_chunk.h"
#include "scene/resources/rectangle_shape_2d.h"
bool Terrain2DMesher::Vertex::operator==(const Vertex &p_vertex) const {
if (vertex != p_vertex.vertex)
@ -514,6 +515,15 @@ AABB Terrain2DMesher::calculate_stored_mesh_aabb(const int index) {
return aabb;
}
Ref<Shape2D> Terrain2DMesher::create_terrain_tile_collider_shape(Ref<Terrain2DChunk> chunk) {
Ref<RectangleShape2D> sh;
sh.instance();
sh->set_extents(Vector2(_cell_size_x, _cell_size_y));
return sh;
}
void Terrain2DMesher::reset() {
_vertices.resize(0);
_indices.resize(0);
@ -1019,6 +1029,7 @@ void Terrain2DMesher::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_indices", "indice"), &Terrain2DMesher::add_indices);
ClassDB::bind_method(D_METHOD("reset"), &Terrain2DMesher::reset);
ClassDB::bind_method(D_METHOD("create_terrain_tile_collider_shape", "chunk"), &Terrain2DMesher::create_terrain_tile_collider_shape);
//ClassDB::bind_method(D_METHOD("calculate_vertex_ambient_occlusion", "meshinstance_path", "radius", "intensity", "sampleCount"), &Terrain2DMesher::calculate_vertex_ambient_occlusion_path);

View File

@ -56,6 +56,8 @@ include_pool_vector
#include "../../mesh_data_resource/mesh_data_resource.h"
#endif
#include "scene/resources/shape_2d.h"
#include "../library/terrain_2d_library.h"
class Terrain2DLibrary;
@ -159,6 +161,8 @@ public:
void build_stored_mesh_into(const int index, RID mesh);
AABB calculate_stored_mesh_aabb(const int index);
virtual Ref<Shape2D> create_terrain_tile_collider_shape(Ref<Terrain2DChunk> chunk);
PoolVector<Vector2> get_vertices() const;
void set_vertices(const PoolVector<Vector2> &values);
int get_vertex_count() const;