From 4ee15cfa5c1c47129bbb06a8290344f3a76311d5 Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 2 Mar 2022 14:51:18 +0100 Subject: [PATCH] Moved the mesh store api from the isometric mesher to the base mesher. --- .../isometric/terrain_2d_mesher_isometric.cpp | 143 ------------------ .../isometric/terrain_2d_mesher_isometric.h | 13 -- meshers/terrain_2d_mesher.cpp | 141 +++++++++++++++++ meshers/terrain_2d_mesher.h | 12 ++ 4 files changed, 153 insertions(+), 156 deletions(-) diff --git a/meshers/isometric/terrain_2d_mesher_isometric.cpp b/meshers/isometric/terrain_2d_mesher_isometric.cpp index 5b0698c..9d95f25 100644 --- a/meshers/isometric/terrain_2d_mesher_isometric.cpp +++ b/meshers/isometric/terrain_2d_mesher_isometric.cpp @@ -738,144 +738,6 @@ void Terrain2DMesherIsometric::mesh_base(Ref chunk) { } } -void Terrain2DMesherIsometric::reset() { - Terrain2DMesher::reset(); - - _meshes.resize(0); -} - -void Terrain2DMesherIsometric::next_mesh() { - Terrain2DMesherIsometricMesh m; - - m.vertices = _vertices; - m.indices = _indices; - - _vertices.resize(0); - _indices.resize(0); - - _meshes.push_back(m); -} - -int Terrain2DMesherIsometric::get_mesh_count() const { - return _meshes.size(); -} - -Array Terrain2DMesherIsometric::build_mesh_index(const int index) { - ERR_FAIL_INDEX_V(index, _meshes.size(), Array()); - - Array a; - a.resize(VisualServer::ARRAY_MAX); - - const Terrain2DMesherIsometricMesh &md = _meshes[index]; - - if (md.vertices.size() == 0) { - //Nothing to do - return a; - } - - { - PoolVector array; - array.resize(md.vertices.size()); -#if !GODOT4 - PoolVector::Write w = array.write(); -#endif - - for (int i = 0; i < md.vertices.size(); ++i) { -#if !GODOT4 - w[i] = md.vertices[i].vertex; -#else - array.set(i, md.vertices[i].vertex); -#endif - } - -#if !GODOT4 - w.release(); -#endif - - a[VisualServer::ARRAY_VERTEX] = array; - } - - if ((_format & VisualServer::ARRAY_FORMAT_COLOR) != 0) { - PoolVector array; - array.resize(md.vertices.size()); -#if !GODOT4 - PoolVector::Write w = array.write(); -#endif - - for (int i = 0; i < md.vertices.size(); ++i) { -#if !GODOT4 - w[i] = md.vertices[i].color; -#else - array.set(i, md.vertices[i].color); -#endif - } - -#if !GODOT4 - w.release(); -#endif - a[VisualServer::ARRAY_COLOR] = array; - } - - if ((_format & VisualServer::ARRAY_FORMAT_TEX_UV) != 0) { - PoolVector array; - array.resize(md.vertices.size()); -#if !GODOT4 - PoolVector::Write w = array.write(); -#endif - - for (int i = 0; i < md.vertices.size(); ++i) { -#if !GODOT4 - w[i] = md.vertices[i].uv; -#else - array.set(i, md.vertices[i].uv); -#endif - } - -#if !GODOT4 - w.release(); -#endif - - a[VisualServer::ARRAY_TEX_UV] = array; - } - - if (md.indices.size() > 0) { - PoolVector array; - array.resize(md.indices.size()); -#if !GODOT4 - PoolVector::Write w = array.write(); -#endif - - for (int i = 0; i < md.indices.size(); ++i) { -#if !GODOT4 - w[i] = md.indices[i]; -#else - array.set(i, md.indices[i]); -#endif - } - -#if !GODOT4 - w.release(); -#endif - a[VisualServer::ARRAY_INDEX] = array; - } - - return a; -} -void Terrain2DMesherIsometric::build_mesh_index_into(const int index, RID mesh) { - ERR_FAIL_COND(mesh == RID()); - - VS::get_singleton()->mesh_clear(mesh); - - if (_vertices.size() == 0) { - //Nothing to do - return; - } - - Array arr = build_mesh_index(index); - - VS::get_singleton()->mesh_add_surface_from_arrays(mesh, VisualServer::PRIMITIVE_TRIANGLES, arr); -} - Terrain2DMesherIsometric::Terrain2DMesherIsometric() { } @@ -884,9 +746,4 @@ Terrain2DMesherIsometric::~Terrain2DMesherIsometric() { void Terrain2DMesherIsometric::_bind_methods() { ClassDB::bind_method(D_METHOD("_add_chunk", "buffer"), &Terrain2DMesherIsometric::_add_chunk); - - ClassDB::bind_method(D_METHOD("next_mesh"), &Terrain2DMesherIsometric::next_mesh); - ClassDB::bind_method(D_METHOD("get_mesh_count"), &Terrain2DMesherIsometric::get_mesh_count); - ClassDB::bind_method(D_METHOD("build_mesh_index", "index"), &Terrain2DMesherIsometric::build_mesh_index); - ClassDB::bind_method(D_METHOD("build_mesh_index_into", "index", "mesh"), &Terrain2DMesherIsometric::build_mesh_index_into); } diff --git a/meshers/isometric/terrain_2d_mesher_isometric.h b/meshers/isometric/terrain_2d_mesher_isometric.h index ba9e1ac..5385e6f 100644 --- a/meshers/isometric/terrain_2d_mesher_isometric.h +++ b/meshers/isometric/terrain_2d_mesher_isometric.h @@ -46,24 +46,11 @@ public: void add_chunk_normal(Ref chunk); void mesh_base(Ref chunk); - void reset(); - void next_mesh(); - int get_mesh_count() const; - Array build_mesh_index(const int index); - void build_mesh_index_into(const int index, RID mesh); - Terrain2DMesherIsometric(); ~Terrain2DMesherIsometric(); protected: static void _bind_methods(); - - struct Terrain2DMesherIsometricMesh { - PoolVector vertices; - PoolVector indices; - }; - - Vector _meshes; }; #endif diff --git a/meshers/terrain_2d_mesher.cpp b/meshers/terrain_2d_mesher.cpp index d3e9e2c..2e58bbc 100644 --- a/meshers/terrain_2d_mesher.cpp +++ b/meshers/terrain_2d_mesher.cpp @@ -345,6 +345,140 @@ void Terrain2DMesher::remove_doubles_hashed() { //print_error("after " + String::num(_vertices.size()) + " " + String::num(duration.count())); } + +void Terrain2DMesher::store_mesh() { + Terrain2DMesherStoredMesh m; + + m.vertices = _vertices; + m.indices = _indices; + + _vertices.resize(0); + _indices.resize(0); + + _stored_meshes.push_back(m); +} + +int Terrain2DMesher::get_stored_mesh_count() const { + return _stored_meshes.size(); +} + +Array Terrain2DMesher::build_stored_mesh(const int index) { + ERR_FAIL_INDEX_V(index, _stored_meshes.size(), Array()); + + Array a; + a.resize(VisualServer::ARRAY_MAX); + + const Terrain2DMesherStoredMesh &md = _stored_meshes[index]; + + if (md.vertices.size() == 0) { + //Nothing to do + return a; + } + + { + PoolVector array; + array.resize(md.vertices.size()); +#if !GODOT4 + PoolVector::Write w = array.write(); +#endif + + for (int i = 0; i < md.vertices.size(); ++i) { +#if !GODOT4 + w[i] = md.vertices[i].vertex; +#else + array.set(i, md.vertices[i].vertex); +#endif + } + +#if !GODOT4 + w.release(); +#endif + + a[VisualServer::ARRAY_VERTEX] = array; + } + + if ((_format & VisualServer::ARRAY_FORMAT_COLOR) != 0) { + PoolVector array; + array.resize(md.vertices.size()); +#if !GODOT4 + PoolVector::Write w = array.write(); +#endif + + for (int i = 0; i < md.vertices.size(); ++i) { +#if !GODOT4 + w[i] = md.vertices[i].color; +#else + array.set(i, md.vertices[i].color); +#endif + } + +#if !GODOT4 + w.release(); +#endif + a[VisualServer::ARRAY_COLOR] = array; + } + + if ((_format & VisualServer::ARRAY_FORMAT_TEX_UV) != 0) { + PoolVector array; + array.resize(md.vertices.size()); +#if !GODOT4 + PoolVector::Write w = array.write(); +#endif + + for (int i = 0; i < md.vertices.size(); ++i) { +#if !GODOT4 + w[i] = md.vertices[i].uv; +#else + array.set(i, md.vertices[i].uv); +#endif + } + +#if !GODOT4 + w.release(); +#endif + + a[VisualServer::ARRAY_TEX_UV] = array; + } + + if (md.indices.size() > 0) { + PoolVector array; + array.resize(md.indices.size()); +#if !GODOT4 + PoolVector::Write w = array.write(); +#endif + + for (int i = 0; i < md.indices.size(); ++i) { +#if !GODOT4 + w[i] = md.indices[i]; +#else + array.set(i, md.indices[i]); +#endif + } + +#if !GODOT4 + w.release(); +#endif + a[VisualServer::ARRAY_INDEX] = array; + } + + return a; +} +void Terrain2DMesher::build_stored_mesh_into(const int index, RID mesh) { + ERR_FAIL_COND(mesh == RID()); + + VS::get_singleton()->mesh_clear(mesh); + + if (_vertices.size() == 0) { + //Nothing to do + return; + } + + Array arr = build_stored_mesh(index); + + VS::get_singleton()->mesh_add_surface_from_arrays(mesh, VisualServer::PRIMITIVE_TRIANGLES, arr); +} + + void Terrain2DMesher::reset() { _vertices.resize(0); _indices.resize(0); @@ -353,6 +487,8 @@ void Terrain2DMesher::reset() { _last_uv = Vector2(); _last_bones.clear(); _last_weights.clear(); + + _stored_meshes.resize(0); } void Terrain2DMesher::add_chunk(Ref chunk) { @@ -853,4 +989,9 @@ void Terrain2DMesher::_bind_methods() { ClassDB::bind_method(D_METHOD("remove_doubles"), &Terrain2DMesher::remove_doubles); ClassDB::bind_method(D_METHOD("remove_doubles_hashed"), &Terrain2DMesher::remove_doubles_hashed); + + ClassDB::bind_method(D_METHOD("store_mesh"), &Terrain2DMesher::store_mesh); + ClassDB::bind_method(D_METHOD("get_stored_mesh_count"), &Terrain2DMesher::get_stored_mesh_count); + ClassDB::bind_method(D_METHOD("build_stored_mesh", "index"), &Terrain2DMesher::build_stored_mesh); + ClassDB::bind_method(D_METHOD("build_stored_mesh_into", "index", "mesh"), &Terrain2DMesher::build_stored_mesh_into); } diff --git a/meshers/terrain_2d_mesher.h b/meshers/terrain_2d_mesher.h index 34ab99c..a1f01cb 100644 --- a/meshers/terrain_2d_mesher.h +++ b/meshers/terrain_2d_mesher.h @@ -149,6 +149,11 @@ public: void remove_doubles(); void remove_doubles_hashed(); + void store_mesh(); + int get_stored_mesh_count() const; + Array build_stored_mesh(const int index); + void build_stored_mesh_into(const int index, RID mesh); + PoolVector get_vertices() const; void set_vertices(const PoolVector &values); int get_vertex_count() const; @@ -209,6 +214,13 @@ protected: float _ao_strength; float _base_light_value; Rect2 _uv_margin; + + struct Terrain2DMesherStoredMesh { + PoolVector vertices; + PoolVector indices; + }; + + Vector _stored_meshes; }; #endif