diff --git a/meshers/terra_mesher.cpp b/meshers/terra_mesher.cpp index d9b8101..46f17d6 100644 --- a/meshers/terra_mesher.cpp +++ b/meshers/terra_mesher.cpp @@ -30,7 +30,6 @@ SOFTWARE. #include "../world/terra_chunk.h" bool TerraMesher::Vertex::operator==(const Vertex &p_vertex) const { - if (vertex != p_vertex.vertex) return false; @@ -66,7 +65,6 @@ bool TerraMesher::Vertex::operator==(const Vertex &p_vertex) const { } uint32_t TerraMesher::VertexHasher::hash(const Vertex &p_vtx) { - uint32_t h = hash_djb2_buffer((const uint8_t *)&p_vtx.vertex, sizeof(real_t) * 3); h = hash_djb2_buffer((const uint8_t *)&p_vtx.normal, sizeof(real_t) * 3, h); h = hash_djb2_buffer((const uint8_t *)&p_vtx.binormal, sizeof(real_t) * 3, h); @@ -128,6 +126,13 @@ void TerraMesher::set_material(const Ref &material) { _material = material; } +int TerraMesher::get_terrain_material_key() { + return _terrarin_material_key; +} +void TerraMesher::set_terrain_material_key(const int key) { + _terrarin_material_key = key; +} + float TerraMesher::get_ao_strength() const { return _ao_strength; } @@ -295,7 +300,6 @@ void TerraMesher::build_mesh_into(RID mesh) { } void TerraMesher::generate_normals(bool p_flip) { - _format = _format | VisualServer::ARRAY_FORMAT_NORMAL; for (int i = 0; i < _indices.size(); i += 3) { @@ -582,11 +586,9 @@ PoolVector TerraMesher::build_collider() const { return face_points; if (_indices.size() == 0) { - int len = (_vertices.size() / 4); for (int i = 0; i < len; ++i) { - face_points.push_back(_vertices.get(i * 4).vertex); face_points.push_back(_vertices.get((i * 4) + 2).vertex); face_points.push_back(_vertices.get((i * 4) + 1).vertex); @@ -607,7 +609,7 @@ PoolVector TerraMesher::build_collider() const { return face_points; } -void TerraMesher::bake_lights(MeshInstance *node, Vector > &lights) { +void TerraMesher::bake_lights(MeshInstance *node, Vector> &lights) { ERR_FAIL_COND(node == NULL); Color darkColor(0, 0, 0, 1); @@ -906,6 +908,7 @@ TerraMesher::TerraMesher(const Ref &library) { _format = 0; _texture_scale = 1; + _terrarin_material_key = 0; } TerraMesher::TerraMesher() { @@ -918,6 +921,7 @@ TerraMesher::TerraMesher() { _channel_index_type = 0; _channel_index_isolevel = 0; _texture_scale = 1; + _terrarin_material_key = 0; } TerraMesher::~TerraMesher() { @@ -959,6 +963,10 @@ void TerraMesher::_bind_methods() { ClassDB::bind_method(D_METHOD("set_material", "value"), &TerraMesher::set_material); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "set_material", "get_material"); + ClassDB::bind_method(D_METHOD("get_terrain_material_key"), &TerraMesher::get_terrain_material_key); + ClassDB::bind_method(D_METHOD("set_terrain_material_key", "key"), &TerraMesher::set_terrain_material_key); + ADD_PROPERTY(PropertyInfo(Variant::INT, "terrain_material_key"), "set_terrain_material_key", "get_terrain_material_key"); + ClassDB::bind_method(D_METHOD("get_voxel_scale"), &TerraMesher::get_voxel_scale); ClassDB::bind_method(D_METHOD("set_voxel_scale", "value"), &TerraMesher::set_voxel_scale); ADD_PROPERTY(PropertyInfo(Variant::REAL, "voxel_scale"), "set_voxel_scale", "get_voxel_scale"); diff --git a/meshers/terra_mesher.h b/meshers/terra_mesher.h index 45b301c..dd4b8a8 100644 --- a/meshers/terra_mesher.h +++ b/meshers/terra_mesher.h @@ -116,6 +116,9 @@ public: Ref get_material(); void set_material(const Ref &material); + int get_terrain_material_key(); + void set_terrain_material_key(const int key); + float get_ao_strength() const; void set_ao_strength(const float value); @@ -205,6 +208,8 @@ protected: int _texture_scale; + int _terrarin_material_key; + PoolVector _vertices; PoolVector _indices; diff --git a/world/jobs/terra_terrarin_job.cpp b/world/jobs/terra_terrarin_job.cpp index 2f0350e..302d646 100644 --- a/world/jobs/terra_terrarin_job.cpp +++ b/world/jobs/terra_terrarin_job.cpp @@ -85,6 +85,41 @@ void TerraTerrarinJob::phase_setup() { next_phase(); } +void TerraTerrarinJob::phase_library_setup() { + if (should_return()) { + return; + } + + Ref lib = _chunk->get_library(); + + if (!lib.is_valid()) { + next_phase(); + return; + } + + if (lib->supports_caching()) { + _terrain_material_key = lib->material_cached_get_key(_chunk); + + if (_mesher.is_valid()) { + _mesher->set_terrain_material_key(_terrain_material_key); + } + + //if (_liquid_mesher.is_valid()) { + // _liquid_mesher->set_terrain_material_key(_terrarin_material_key); + //} + } + + //if (lib->supports_caching()) { + // _terrain_material_key = lib->material_cached_get_key(_chunk); + + //if (_liquid_mesher.is_valid()) { + // _liquid_mesher->set_terrain_material_key(_terrarin_material_key); + //} + //} + + next_phase(); +} + void TerraTerrarinJob::phase_terrarin_mesh_setup() { if (should_return()) { return; @@ -222,7 +257,6 @@ void TerraTerrarinJob::phase_terrarin_mesh() { //set up the meshes if (should_do()) { - RID mesh_rid = chunk->mesh_rid_get_index(TerraChunkDefault::MESH_INDEX_TERRARIN, TerraChunkDefault::MESH_TYPE_INDEX_MESH, 0); if (mesh_rid == RID()) { @@ -380,14 +414,16 @@ void TerraTerrarinJob::_execute_phase() { if (_phase == 0) { phase_setup(); } else if (_phase == 1) { - phase_terrarin_mesh_setup(); + phase_library_setup(); } else if (_phase == 2) { + phase_terrarin_mesh_setup(); + } else if (_phase == 3) { phase_collider(); - } else if (_phase == 4) { - phase_terrarin_mesh(); } else if (_phase == 5) { + phase_terrarin_mesh(); + } else if (_phase == 6) { phase_finalize(); - } else if (_phase > 5) { + } else if (_phase > 6) { set_complete(true); //So threadpool knows it's done next_job(); ERR_FAIL_MSG("TerraTerrarinJob: _phase is too high!"); @@ -426,7 +462,7 @@ void TerraTerrarinJob::_reset() { } void TerraTerrarinJob::_physics_process(float delta) { - if (_phase == 3) + if (_phase == 4) phase_physics_process(); } @@ -483,7 +519,6 @@ void TerraTerrarinJob::step_type_normal_lod() { } void TerraTerrarinJob::step_type_drop_uv2() { - Ref chunk = _chunk; RID mesh_rid = chunk->mesh_rid_get_index(TerraChunkDefault::MESH_INDEX_TERRARIN, TerraChunkDefault::MESH_TYPE_INDEX_MESH, _current_mesh); @@ -499,7 +534,6 @@ void TerraTerrarinJob::step_type_drop_uv2() { } void TerraTerrarinJob::step_type_merge_verts() { - Array temp_mesh_arr2 = merge_mesh_array(temp_mesh_arr); temp_mesh_arr = temp_mesh_arr2; @@ -543,7 +577,6 @@ void TerraTerrarinJob::step_type_bake_texture() { } void TerraTerrarinJob::step_type_simplify_mesh() { - #ifdef MESH_UTILS_PRESENT Ref chunk = _chunk; diff --git a/world/jobs/terra_terrarin_job.h b/world/jobs/terra_terrarin_job.h index c71c675..fac4006 100644 --- a/world/jobs/terra_terrarin_job.h +++ b/world/jobs/terra_terrarin_job.h @@ -52,6 +52,7 @@ public: int get_jobs_step_count() const; void phase_setup(); + void phase_library_setup(); void phase_terrarin_mesh_setup(); void phase_collider(); void phase_physics_proces(); @@ -86,6 +87,8 @@ protected: PoolVector temp_arr_collider; PoolVector temp_arr_collider_liquid; Array temp_mesh_arr; + + int _terrain_material_key; }; #endif