Moved the mesh store api from the isometric mesher to the base mesher.

This commit is contained in:
Relintai 2022-03-02 14:51:18 +01:00
parent 674659c01e
commit 4ee15cfa5c
4 changed files with 153 additions and 156 deletions

View File

@ -738,144 +738,6 @@ void Terrain2DMesherIsometric::mesh_base(Ref<Terrain2DChunkDefault> 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<Vector2> array;
array.resize(md.vertices.size());
#if !GODOT4
PoolVector<Vector2>::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<Color> array;
array.resize(md.vertices.size());
#if !GODOT4
PoolVector<Color>::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<Vector2> array;
array.resize(md.vertices.size());
#if !GODOT4
PoolVector<Vector2>::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<int> array;
array.resize(md.indices.size());
#if !GODOT4
PoolVector<int>::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);
}

View File

@ -46,24 +46,11 @@ public:
void add_chunk_normal(Ref<Terrain2DChunkDefault> chunk);
void mesh_base(Ref<Terrain2DChunkDefault> 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<Vertex> vertices;
PoolVector<int> indices;
};
Vector<Terrain2DMesherIsometricMesh> _meshes;
};
#endif

View File

@ -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<Vector2> array;
array.resize(md.vertices.size());
#if !GODOT4
PoolVector<Vector2>::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<Color> array;
array.resize(md.vertices.size());
#if !GODOT4
PoolVector<Color>::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<Vector2> array;
array.resize(md.vertices.size());
#if !GODOT4
PoolVector<Vector2>::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<int> array;
array.resize(md.indices.size());
#if !GODOT4
PoolVector<int>::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<Terrain2DChunk> 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);
}

View File

@ -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<Vector2> get_vertices() const;
void set_vertices(const PoolVector<Vector2> &values);
int get_vertex_count() const;
@ -209,6 +214,13 @@ protected:
float _ao_strength;
float _base_light_value;
Rect2 _uv_margin;
struct Terrain2DMesherStoredMesh {
PoolVector<Vertex> vertices;
PoolVector<int> indices;
};
Vector<Terrain2DMesherStoredMesh> _stored_meshes;
};
#endif