From a22be7208cd568b2a3835339ec654a03edd564a9 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 8 Mar 2020 23:40:22 +0100 Subject: [PATCH] The transvoxel mesher now won't allocate small arrays constantly during generation. --- .../voxel_mesher_transvoxel.cpp | 13 ++++--------- .../transvoxel_uv_mesher/voxel_mesher_transvoxel.h | 3 ++- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/meshers/transvoxel_uv_mesher/voxel_mesher_transvoxel.cpp b/meshers/transvoxel_uv_mesher/voxel_mesher_transvoxel.cpp index c07a28c..cabe150 100644 --- a/meshers/transvoxel_uv_mesher/voxel_mesher_transvoxel.cpp +++ b/meshers/transvoxel_uv_mesher/voxel_mesher_transvoxel.cpp @@ -34,9 +34,7 @@ void VoxelMesherTransvoxel::set_texture_scale(const int value) { _texture_scale = value; } -int *VoxelMesherTransvoxel::get_voxel_type_array(VoxelChunk *chunk, const int x, const int y, const int z, const int size) { - int *arr = memnew_arr(int, 8); - +void VoxelMesherTransvoxel::get_voxel_type_array(int *arr, VoxelChunk *chunk, const int x, const int y, const int z, const int size) { arr[0] = chunk->get_voxel(x, y, z, VoxelChunk::DEFAULT_CHANNEL_TYPE); arr[1] = chunk->get_voxel(x, y + size, z, VoxelChunk::DEFAULT_CHANNEL_TYPE); arr[2] = chunk->get_voxel(x, y, z + size, VoxelChunk::DEFAULT_CHANNEL_TYPE); @@ -45,8 +43,6 @@ int *VoxelMesherTransvoxel::get_voxel_type_array(VoxelChunk *chunk, const int x, arr[5] = chunk->get_voxel(x + size, y + size, z, VoxelChunk::DEFAULT_CHANNEL_TYPE); arr[6] = chunk->get_voxel(x + size, y, z + size, VoxelChunk::DEFAULT_CHANNEL_TYPE); arr[7] = chunk->get_voxel(x + size, y + size, z + size, VoxelChunk::DEFAULT_CHANNEL_TYPE); - - return arr; } int VoxelMesherTransvoxel::get_case_code_from_arr(const int *data) { int case_code = 0; @@ -157,6 +153,8 @@ void VoxelMesherTransvoxel::_add_chunk(Node *p_chunk) { chunk->generate_ao(); + int type_arr[8]; + int x_size = chunk->get_size_x(); int y_size = chunk->get_size_y(); int z_size = chunk->get_size_z(); @@ -167,11 +165,10 @@ void VoxelMesherTransvoxel::_add_chunk(Node *p_chunk) { for (int z = 0; z < z_size; z += lod_size) { for (int x = 0; x < x_size; x += lod_size) { - int *type_arr = get_voxel_type_array(chunk, x, y, z, lod_size); + get_voxel_type_array(type_arr, chunk, x, y, z, lod_size); int case_code = get_case_code_from_arr(type_arr); if (case_code == 0 or case_code == 255) { - memdelete_arr(type_arr); continue; } @@ -395,8 +392,6 @@ void VoxelMesherTransvoxel::_add_chunk(Node *p_chunk) { add_uv2(temp_uv2s[i]); add_vertex(vert_pos); } - - memdelete_arr(type_arr); } } } diff --git a/meshers/transvoxel_uv_mesher/voxel_mesher_transvoxel.h b/meshers/transvoxel_uv_mesher/voxel_mesher_transvoxel.h index ac7d432..9f561fb 100644 --- a/meshers/transvoxel_uv_mesher/voxel_mesher_transvoxel.h +++ b/meshers/transvoxel_uv_mesher/voxel_mesher_transvoxel.h @@ -68,7 +68,8 @@ public: int get_texture_scale() const; void set_texture_scale(const int value); - int *get_voxel_type_array(VoxelChunk *chunk, const int x, const int y, const int z, const int size = 1); + //arr should have a size of 8 + void get_voxel_type_array(int *arr, VoxelChunk *chunk, const int x, const int y, const int z, const int size = 1); int get_case_code_from_arr(const int *data); int get_case_code(VoxelChunk *chunk, const int x, const int y, const int z, const int size = 1); int get_voxel_type(VoxelChunk *chunk, const int x, const int y, const int z, const int size = 1);