Added convenience method for just allocating one mesh and switched to it everywhere. Now meshes are actually visible.

This commit is contained in:
Relintai 2022-02-24 09:40:21 +01:00
parent 2a778660c9
commit d47a87934b
5 changed files with 68 additions and 28 deletions

View File

@ -262,7 +262,6 @@ void Terrain2DChunkDefault::meshes_create(const int mesh_index, const int mesh_c
ERR_FAIL_COND(m.has(MESH_TYPE_INDEX_MESH)); ERR_FAIL_COND(m.has(MESH_TYPE_INDEX_MESH));
Array am; Array am;
Array ami;
for (int i = 0; i < mesh_count; ++i) { for (int i = 0; i < mesh_count; ++i) {
RID mesh_rid = VS::get_singleton()->mesh_create(); RID mesh_rid = VS::get_singleton()->mesh_create();
@ -296,6 +295,41 @@ void Terrain2DChunkDefault::meshes_free(const int mesh_index) {
m.erase(MESH_TYPE_INDEX_MESH); m.erase(MESH_TYPE_INDEX_MESH);
} }
void Terrain2DChunkDefault::mesh_create(const int mesh_index) {
ERR_FAIL_COND(_voxel_world == NULL);
ERR_FAIL_COND(!get_library().is_valid());
if (!_rids.has(mesh_index))
_rids[mesh_index] = Dictionary();
Dictionary m = _rids[mesh_index];
ERR_FAIL_COND(m.has(MESH_TYPE_INDEX_MESH));
RID mesh_rid = VS::get_singleton()->mesh_create();
m[MESH_TYPE_INDEX_MESH] = mesh_rid;
_rids[mesh_index] = m;
}
void Terrain2DChunkDefault::mesh_free(const int mesh_index) {
if (!_rids.has(mesh_index))
return;
Dictionary m = _rids[mesh_index];
RID rid;
if (m.has(MESH_TYPE_INDEX_MESH)) {
RID r = m[MESH_TYPE_INDEX_MESH];
if (r != rid) {
VS::get_singleton()->free(r);
}
}
m.erase(MESH_TYPE_INDEX_MESH);
}
void Terrain2DChunkDefault::colliders_create(const int mesh_index, const int layer_mask) { void Terrain2DChunkDefault::colliders_create(const int mesh_index, const int layer_mask) {
/* /*
ERR_FAIL_COND(_voxel_world == NULL); ERR_FAIL_COND(_voxel_world == NULL);
@ -405,6 +439,7 @@ void Terrain2DChunkDefault::colliders_free(const int mesh_index) {
void Terrain2DChunkDefault::free_index(const int mesh_index) { void Terrain2DChunkDefault::free_index(const int mesh_index) {
meshes_free(mesh_index); meshes_free(mesh_index);
mesh_free(mesh_index);
colliders_free(mesh_index); colliders_free(mesh_index);
} }
@ -842,6 +877,8 @@ void Terrain2DChunkDefault::_bind_methods() {
ClassDB::bind_method(D_METHOD("meshes_create", "mesh_index", "mesh_count"), &Terrain2DChunkDefault::meshes_create); ClassDB::bind_method(D_METHOD("meshes_create", "mesh_index", "mesh_count"), &Terrain2DChunkDefault::meshes_create);
ClassDB::bind_method(D_METHOD("meshes_free", "mesh_index"), &Terrain2DChunkDefault::meshes_free); ClassDB::bind_method(D_METHOD("meshes_free", "mesh_index"), &Terrain2DChunkDefault::meshes_free);
ClassDB::bind_method(D_METHOD("mesh_create", "mesh_index"), &Terrain2DChunkDefault::mesh_create);
ClassDB::bind_method(D_METHOD("mesh_free", "mesh_index"), &Terrain2DChunkDefault::mesh_free);
ClassDB::bind_method(D_METHOD("create_colliders", "mesh_index", "layer_mask"), &Terrain2DChunkDefault::colliders_create, DEFVAL(1)); ClassDB::bind_method(D_METHOD("create_colliders", "mesh_index", "layer_mask"), &Terrain2DChunkDefault::colliders_create, DEFVAL(1));
ClassDB::bind_method(D_METHOD("free_colliders", "mesh_index"), &Terrain2DChunkDefault::colliders_free); ClassDB::bind_method(D_METHOD("free_colliders", "mesh_index"), &Terrain2DChunkDefault::colliders_free);

View File

@ -128,6 +128,8 @@ public:
void meshes_create(const int mesh_index, const int mesh_count); void meshes_create(const int mesh_index, const int mesh_count);
void meshes_free(const int mesh_index); void meshes_free(const int mesh_index);
void mesh_create(const int mesh_index);
void mesh_free(const int mesh_index);
void colliders_create(const int mesh_index, const int layer_mask = 1); void colliders_create(const int mesh_index, const int layer_mask = 1);
void colliders_create_area(const int mesh_index, const int layer_mask = 1); void colliders_create_area(const int mesh_index, const int layer_mask = 1);

View File

@ -321,20 +321,16 @@ void Terrain2DProp2DJob::phase_steps() {
//set up the meshes //set up the meshes
if (should_do()) { if (should_do()) {
RID mesh_rid = chunk->mesh_rid_get_index(Terrain2DChunkDefault::MESH_INDEX_PROP, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH, 0); RID mesh_rid = chunk->mesh_rid_get(Terrain2DChunkDefault::MESH_INDEX_PROP, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH);
if (mesh_rid == RID()) { if (mesh_rid == RID()) {
//need to allocate the meshes //need to allocate the meshes
//allocate //allocate
chunk->meshes_create(Terrain2DChunkDefault::MESH_INDEX_PROP, 1); chunk->mesh_create(Terrain2DChunkDefault::MESH_INDEX_PROP);
} else { } else {
//we have the meshes, just clear mesh_rid = chunk->mesh_rid_get(Terrain2DChunkDefault::MESH_INDEX_PROP, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH);
int count = chunk->mesh_rid_get_count(Terrain2DChunkDefault::MESH_INDEX_PROP, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH);
for (int i = 0; i < count; ++i) {
mesh_rid = chunk->mesh_rid_get_index(Terrain2DChunkDefault::MESH_INDEX_PROP, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH, i);
if (VS::get_singleton()->mesh_get_surface_count(mesh_rid) > 0) if (VS::get_singleton()->mesh_get_surface_count(mesh_rid) > 0)
#if !GODOT4 #if !GODOT4
@ -344,7 +340,6 @@ void Terrain2DProp2DJob::phase_steps() {
#endif #endif
} }
} }
}
step_type_normal(); step_type_normal();
@ -362,7 +357,7 @@ void Terrain2DProp2DJob::step_type_normal() {
temp_mesh_arr = _prop_mesher->build_mesh(); temp_mesh_arr = _prop_mesher->build_mesh();
RID mesh_rid = chunk->mesh_rid_get_index(Terrain2DChunkDefault::MESH_INDEX_PROP, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH, 0); RID mesh_rid = chunk->mesh_rid_get(Terrain2DChunkDefault::MESH_INDEX_PROP, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH);
VS::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr); VS::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr);
} }

View File

@ -226,19 +226,19 @@ void Terrain2DTerrain2DJob::phase_terrain_mesh() {
//set up the meshes //set up the meshes
if (should_do()) { if (should_do()) {
RID mesh_rid = chunk->mesh_rid_get_index(Terrain2DChunkDefault::MESH_INDEX_TERRAIN, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH, 0); RID mesh_rid = chunk->mesh_rid_get(Terrain2DChunkDefault::MESH_INDEX_TERRAIN, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH);
if (mesh_rid == RID()) { if (mesh_rid == RID()) {
//need to allocate the meshes //need to allocate the meshes
//allocate //allocate
chunk->meshes_create(Terrain2DChunkDefault::MESH_INDEX_TERRAIN, 1); chunk->mesh_create(Terrain2DChunkDefault::MESH_INDEX_TERRAIN);
} else { } else {
//we have the meshes, just clear //we have the meshes, just clear
int count = chunk->mesh_rid_get_count(Terrain2DChunkDefault::MESH_INDEX_TERRAIN, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH); int count = chunk->mesh_rid_get_count(Terrain2DChunkDefault::MESH_INDEX_TERRAIN, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH);
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
mesh_rid = chunk->mesh_rid_get_index(Terrain2DChunkDefault::MESH_INDEX_TERRAIN, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH, i); mesh_rid = chunk->mesh_rid_get(Terrain2DChunkDefault::MESH_INDEX_TERRAIN, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH);
if (VS::get_singleton()->mesh_get_surface_count(mesh_rid) > 0) if (VS::get_singleton()->mesh_get_surface_count(mesh_rid) > 0)
#if !GODOT4 #if !GODOT4
@ -261,13 +261,13 @@ void Terrain2DTerrain2DJob::phase_terrain_mesh() {
} }
} }
RID mesh_rid = chunk->mesh_rid_get_index(Terrain2DChunkDefault::MESH_INDEX_LIQUID, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH, 0); RID mesh_rid = chunk->mesh_rid_get(Terrain2DChunkDefault::MESH_INDEX_LIQUID, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH);
if (should_do()) { if (should_do()) {
if (mesh_rid == RID()) { if (mesh_rid == RID()) {
chunk->meshes_create(Terrain2DChunkDefault::MESH_INDEX_LIQUID, 1); chunk->mesh_create(Terrain2DChunkDefault::MESH_INDEX_LIQUID);
mesh_rid = chunk->mesh_rid_get_index(Terrain2DChunkDefault::MESH_INDEX_LIQUID, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH, 0); mesh_rid = chunk->mesh_rid_get(Terrain2DChunkDefault::MESH_INDEX_LIQUID, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH);
} }
if (VS::get_singleton()->mesh_get_surface_count(mesh_rid) > 0) if (VS::get_singleton()->mesh_get_surface_count(mesh_rid) > 0)
@ -364,7 +364,7 @@ void Terrain2DTerrain2DJob::step_type_normal() {
temp_mesh_arr = _mesher->build_mesh(); temp_mesh_arr = _mesher->build_mesh();
RID mesh_rid = chunk->mesh_rid_get_index(Terrain2DChunkDefault::MESH_INDEX_TERRAIN, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH, 0); RID mesh_rid = chunk->mesh_rid_get(Terrain2DChunkDefault::MESH_INDEX_TERRAIN, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH);
VS::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr); VS::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr);
} }

View File

@ -277,6 +277,8 @@ void Terrain2DWorld::chunk_add(Ref<Terrain2DChunk> chunk, const int x, const int
} }
emit_signal("chunk_added", chunk); emit_signal("chunk_added", chunk);
update();
} }
bool Terrain2DWorld::chunk_has(const int x, const int z) const { bool Terrain2DWorld::chunk_has(const int x, const int z) const {
return _chunks.has(IntPos(x, z)); return _chunks.has(IntPos(x, z));
@ -319,6 +321,7 @@ Ref<Terrain2DChunk> Terrain2DWorld::chunk_remove(const int x, const int z) {
_chunks.erase(pos); _chunks.erase(pos);
emit_signal("chunk_removed", chunk); emit_signal("chunk_removed", chunk);
update();
return chunk; return chunk;
} }
@ -340,6 +343,7 @@ Ref<Terrain2DChunk> Terrain2DWorld::chunk_remove_index(const int index) {
//_generating.erase(chunk); //_generating.erase(chunk);
emit_signal("chunk_removed", chunk); emit_signal("chunk_removed", chunk);
update();
return chunk; return chunk;
} }
@ -376,6 +380,8 @@ void Terrain2DWorld::chunks_clear() {
//never remove from this here //never remove from this here
//_generating.clear(); //_generating.clear();
update();
} }
Ref<Terrain2DChunk> Terrain2DWorld::chunk_get_or_create(int x, int z) { Ref<Terrain2DChunk> Terrain2DWorld::chunk_get_or_create(int x, int z) {