From f077cb218ead5c33f1c9220a4518e51a763daef5 Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 23 Feb 2022 23:20:41 +0100 Subject: [PATCH] Removed multiple (lod) material support. --- library/terrain_2d_library.cpp | 217 +++------------------- library/terrain_2d_library.h | 45 +---- library/terrain_2d_library_merger.cpp | 61 +----- library/terrain_2d_library_merger_pcm.cpp | 86 ++------- library/terrain_2d_material_cache.cpp | 74 +------- library/terrain_2d_material_cache.h | 14 +- library/terrain_2d_material_cache_pcm.cpp | 19 +- meshers/terrain_2d_mesher.cpp | 3 - world/jobs/terrain_2d_prop_job.cpp | 12 -- world/jobs/terrain_2d_terrain_job.cpp | 20 -- 10 files changed, 66 insertions(+), 485 deletions(-) diff --git a/library/terrain_2d_library.cpp b/library/terrain_2d_library.cpp index f0d4ef6..0d34790 100644 --- a/library/terrain_2d_library.cpp +++ b/library/terrain_2d_library.cpp @@ -160,25 +160,6 @@ bool Terrain2DLibrary::_supports_caching() { } //Materials -Ref Terrain2DLibrary::material_get(const int index) { - ERR_FAIL_INDEX_V(index, _materials.size(), Ref()); - - return _materials[index]; -} - -Ref Terrain2DLibrary::material_lod_get(const int index) { - ERR_FAIL_COND_V_MSG(_materials.size() == 0, Ref(), "Error! You should to add at least one material to Terrain2DLibrary! (By default it will use up to 5. See VoxelWorldDefault::_create_chunk().)"); - - if (index < 0) { - return _materials[0]; - } - - if (index >= _materials.size()) { - return _materials[_materials.size() - 1]; - } - - return _materials[index]; -} void Terrain2DLibrary::material_cache_get_key(Ref chunk) { CALL(_material_cache_get_key, chunk); @@ -201,65 +182,15 @@ void Terrain2DLibrary::material_cache_unref(const int key) { void Terrain2DLibrary::_material_cache_unref(const int key) { } -void Terrain2DLibrary::material_add(const Ref &value) { - ERR_FAIL_COND(!value.is_valid()); - - _materials.push_back(value); +Ref Terrain2DLibrary::material_get() { + return _material; } -void Terrain2DLibrary::material_set(const int index, const Ref &value) { - ERR_FAIL_INDEX(index, _materials.size()); - - _materials.set(index, value); -} - -void Terrain2DLibrary::material_remove(const int index) { - _materials.VREMOVE(index); -} - -int Terrain2DLibrary::material_get_num() const { - return _materials.size(); -} - -void Terrain2DLibrary::materials_clear() { - _materials.clear(); -} - -Vector Terrain2DLibrary::materials_get() { - VARIANT_ARRAY_GET(_materials); -} - -void Terrain2DLibrary::materials_set(const Vector &materials) { - _materials.clear(); - - for (int i = 0; i < materials.size(); i++) { - Ref material = Ref(materials[i]); - - _materials.push_back(material); - } +void Terrain2DLibrary::material_set(const Ref &value) { + _material = value; } //Liquid Materials -Ref Terrain2DLibrary::liquid_material_get(const int index) { - ERR_FAIL_INDEX_V(index, _liquid_materials.size(), Ref()); - - return _liquid_materials[index]; -} - -Ref Terrain2DLibrary::liquid_material_lod_get(const int index) { - ERR_FAIL_COND_V_MSG(_liquid_materials.size() == 0, Ref(), "Error! You should to add at least one liquid to Terrain2DLibrary! (By default it will use up to 5. See VoxelWorldDefault::_create_chunk().)"); - - if (index < 0) { - return _liquid_materials[0]; - } - - if (index >= _liquid_materials.size()) { - return _liquid_materials[_liquid_materials.size() - 1]; - } - - return _liquid_materials[index]; -} - void Terrain2DLibrary::liquid_material_cache_get_key(Ref chunk) { CALL(_liquid_material_cache_get_key, chunk); } @@ -281,64 +212,15 @@ void Terrain2DLibrary::liquid_material_cache_unref(const int key) { void Terrain2DLibrary::_liquid_material_cache_unref(const int key) { } -void Terrain2DLibrary::liquid_material_add(const Ref &value) { - ERR_FAIL_COND(!value.is_valid()); - - _liquid_materials.push_back(value); +Ref Terrain2DLibrary::liquid_material_get() { + return _liquid_material; } -void Terrain2DLibrary::liquid_material_set(const int index, const Ref &value) { - ERR_FAIL_INDEX(index, _liquid_materials.size()); - - _liquid_materials.set(index, value); -} - -void Terrain2DLibrary::liquid_material_remove(const int index) { - _liquid_materials.VREMOVE(index); -} - -int Terrain2DLibrary::liquid_material_get_num() const { - return _liquid_materials.size(); -} - -void Terrain2DLibrary::liquid_materials_clear() { - _liquid_materials.clear(); -} - -Vector Terrain2DLibrary::liquid_materials_get() { - VARIANT_ARRAY_GET(_liquid_materials); -} - -void Terrain2DLibrary::liquid_materials_set(const Vector &materials) { - _liquid_materials.clear(); - - for (int i = 0; i < materials.size(); i++) { - Ref material = Ref(materials[i]); - - _liquid_materials.push_back(material); - } +void Terrain2DLibrary::liquid_material_set(const Ref &value) { + _liquid_material = value; } //Prop2D Materials -Ref Terrain2DLibrary::prop_material_get(const int index) { - ERR_FAIL_INDEX_V(index, _prop_materials.size(), Ref()); - - return _prop_materials[index]; -} - -Ref Terrain2DLibrary::prop_material_lod_get(const int index) { - ERR_FAIL_COND_V_MSG(_prop_materials.size() == 0, Ref(), "Error! You should to add at least one prop material to Terrain2DLibrary! (By default it will use up to 5. See VoxelWorldDefault::_create_chunk().)"); - - if (index < 0) { - return _prop_materials[0]; - } - - if (index >= _prop_materials.size()) { - return _prop_materials[_prop_materials.size() - 1]; - } - - return _prop_materials[index]; -} void Terrain2DLibrary::prop_material_cache_get_key(Ref chunk) { CALL(_prop_material_cache_get_key, chunk); @@ -361,36 +243,12 @@ void Terrain2DLibrary::prop_material_cache_unref(const int key) { void Terrain2DLibrary::_prop_material_cache_unref(const int key) { } -void Terrain2DLibrary::prop_material_add(const Ref &value) { - ERR_FAIL_COND(!value.is_valid()); - - _prop_materials.push_back(value); +Ref Terrain2DLibrary::prop_material_get() { + return _prop_material; } -void Terrain2DLibrary::prop_material_set(const int index, const Ref &value) { - ERR_FAIL_INDEX(index, _prop_materials.size()); - - _prop_materials.set(index, value); -} - -void Terrain2DLibrary::prop_material_remove(const int index) { - _prop_materials.VREMOVE(index); -} - -int Terrain2DLibrary::prop_material_get_num() const { - return _prop_materials.size(); -} - -void Terrain2DLibrary::prop_materials_clear() { - _prop_materials.clear(); -} - -Vector Terrain2DLibrary::prop_materials_get() { - VARIANT_ARRAY_GET(_prop_materials); -} - -void Terrain2DLibrary::prop_materials_set(const Vector &materials) { - VARIANT_ARRAY_SET(materials, _prop_materials, Material); +void Terrain2DLibrary::prop_material_set(const Ref &value) { + _prop_material = value; } //Surfaces @@ -464,9 +322,9 @@ Terrain2DLibrary::Terrain2DLibrary() { } Terrain2DLibrary::~Terrain2DLibrary() { - _materials.clear(); - _liquid_materials.clear(); - _prop_materials.clear(); + _material.unref(); + _liquid_material.unref(); + _prop_material.unref(); } #if VERSION_MAJOR >= 4 @@ -513,9 +371,6 @@ void Terrain2DLibrary::_bind_methods() { GDVIRTUAL_BIND(_material_cache_unref, "key"); #endif - ClassDB::bind_method(D_METHOD("material_get", "index"), &Terrain2DLibrary::material_get); - ClassDB::bind_method(D_METHOD("material_lod_get", "index"), &Terrain2DLibrary::material_lod_get); - ClassDB::bind_method(D_METHOD("material_cache_get_key", "chunk"), &Terrain2DLibrary::material_cache_get_key); ClassDB::bind_method(D_METHOD("_material_cache_get_key", "chunk"), &Terrain2DLibrary::_material_cache_get_key); ClassDB::bind_method(D_METHOD("material_cache_get", "key"), &Terrain2DLibrary::material_cache_get); @@ -523,15 +378,9 @@ void Terrain2DLibrary::_bind_methods() { ClassDB::bind_method(D_METHOD("material_cache_unref", "key"), &Terrain2DLibrary::material_cache_unref); ClassDB::bind_method(D_METHOD("_material_cache_unref", "key"), &Terrain2DLibrary::_material_cache_unref); - ClassDB::bind_method(D_METHOD("material_add", "value"), &Terrain2DLibrary::material_add); - ClassDB::bind_method(D_METHOD("material_set", "index", "value"), &Terrain2DLibrary::material_set); - ClassDB::bind_method(D_METHOD("material_remove", "index"), &Terrain2DLibrary::material_remove); - ClassDB::bind_method(D_METHOD("material_get_num"), &Terrain2DLibrary::material_get_num); - ClassDB::bind_method(D_METHOD("materials_clear"), &Terrain2DLibrary::materials_clear); - - ClassDB::bind_method(D_METHOD("materials_get"), &Terrain2DLibrary::materials_get); - ClassDB::bind_method(D_METHOD("materials_set"), &Terrain2DLibrary::materials_set); - ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "materials", PROPERTY_HINT_NONE, "17/17:Material", PROPERTY_USAGE_DEFAULT, "Material"), "materials_set", "materials_get"); + ClassDB::bind_method(D_METHOD("material_get"), &Terrain2DLibrary::material_get); + ClassDB::bind_method(D_METHOD("material_set", "value"), &Terrain2DLibrary::material_set); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "material_set", "material_get"); #if VERSION_MAJOR < 4 BIND_VMETHOD(MethodInfo("_liquid_material_cache_get_key", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "Terrain2DChunk"))); @@ -543,9 +392,6 @@ void Terrain2DLibrary::_bind_methods() { GDVIRTUAL_BIND(_liquid_material_cache_unref, "key"); #endif - ClassDB::bind_method(D_METHOD("liquid_material_get", "index"), &Terrain2DLibrary::liquid_material_get); - ClassDB::bind_method(D_METHOD("liquid_material_lod_get", "index"), &Terrain2DLibrary::liquid_material_lod_get); - ClassDB::bind_method(D_METHOD("liquid_material_cache_get_key", "chunk"), &Terrain2DLibrary::liquid_material_cache_get_key); ClassDB::bind_method(D_METHOD("_liquid_material_cache_get_key", "chunk"), &Terrain2DLibrary::_liquid_material_cache_get_key); ClassDB::bind_method(D_METHOD("liquid_material_cache_get", "key"), &Terrain2DLibrary::liquid_material_cache_get); @@ -553,15 +399,9 @@ void Terrain2DLibrary::_bind_methods() { ClassDB::bind_method(D_METHOD("liquid_material_cache_unref", "key"), &Terrain2DLibrary::liquid_material_cache_unref); ClassDB::bind_method(D_METHOD("_liquid_material_cache_unref", "key"), &Terrain2DLibrary::_liquid_material_cache_unref); - ClassDB::bind_method(D_METHOD("liquid_material_add", "value"), &Terrain2DLibrary::liquid_material_add); - ClassDB::bind_method(D_METHOD("liquid_material_set", "index", "value"), &Terrain2DLibrary::liquid_material_set); - ClassDB::bind_method(D_METHOD("liquid_material_remove", "index"), &Terrain2DLibrary::liquid_material_remove); - ClassDB::bind_method(D_METHOD("liquid_material_get_num"), &Terrain2DLibrary::liquid_material_get_num); - ClassDB::bind_method(D_METHOD("liquid_materials_clear"), &Terrain2DLibrary::liquid_materials_clear); - - ClassDB::bind_method(D_METHOD("liquid_materials_get"), &Terrain2DLibrary::liquid_materials_get); - ClassDB::bind_method(D_METHOD("liquid_materials_set"), &Terrain2DLibrary::liquid_materials_set); - ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "liquid_materials", PROPERTY_HINT_NONE, "17/17:Material", PROPERTY_USAGE_DEFAULT, "Material"), "liquid_materials_set", "liquid_materials_get"); + ClassDB::bind_method(D_METHOD("liquid_material_get"), &Terrain2DLibrary::liquid_material_get); + ClassDB::bind_method(D_METHOD("liquid_material_set", "value"), &Terrain2DLibrary::liquid_material_set); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "liquid_material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "liquid_material_set", "liquid_material_get"); #if VERSION_MAJOR < 4 BIND_VMETHOD(MethodInfo("_prop_material_cache_get_key", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "Terrain2DChunk"))); @@ -573,9 +413,6 @@ void Terrain2DLibrary::_bind_methods() { GDVIRTUAL_BIND(_prop_material_cache_unref, "key"); #endif - ClassDB::bind_method(D_METHOD("prop_material_get", "index"), &Terrain2DLibrary::prop_material_get); - ClassDB::bind_method(D_METHOD("prop_material_lod_get", "index"), &Terrain2DLibrary::prop_material_lod_get); - ClassDB::bind_method(D_METHOD("prop_material_cache_get_key", "chunk"), &Terrain2DLibrary::prop_material_cache_get_key); ClassDB::bind_method(D_METHOD("_prop_material_cache_get_key", "chunk"), &Terrain2DLibrary::_prop_material_cache_get_key); ClassDB::bind_method(D_METHOD("prop_material_cache_get", "key"), &Terrain2DLibrary::prop_material_cache_get); @@ -583,15 +420,9 @@ void Terrain2DLibrary::_bind_methods() { ClassDB::bind_method(D_METHOD("prop_material_cache_unref", "key"), &Terrain2DLibrary::prop_material_cache_unref); ClassDB::bind_method(D_METHOD("_prop_material_cache_unref", "key"), &Terrain2DLibrary::_prop_material_cache_unref); - ClassDB::bind_method(D_METHOD("prop_material_add", "value"), &Terrain2DLibrary::prop_material_add); - ClassDB::bind_method(D_METHOD("prop_material_set", "index", "value"), &Terrain2DLibrary::prop_material_set); - ClassDB::bind_method(D_METHOD("prop_material_remove", "index"), &Terrain2DLibrary::prop_material_remove); - ClassDB::bind_method(D_METHOD("prop_material_get_num"), &Terrain2DLibrary::prop_material_get_num); - ClassDB::bind_method(D_METHOD("prop_materials_clear"), &Terrain2DLibrary::prop_materials_clear); - - ClassDB::bind_method(D_METHOD("prop_materials_get"), &Terrain2DLibrary::prop_materials_get); - ClassDB::bind_method(D_METHOD("prop_materials_set"), &Terrain2DLibrary::prop_materials_set); - ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "prop_materials", PROPERTY_HINT_NONE, "17/17:Material", PROPERTY_USAGE_DEFAULT, "Material"), "prop_materials_set", "prop_materials_get"); + ClassDB::bind_method(D_METHOD("prop_material_get"), &Terrain2DLibrary::prop_material_get); + ClassDB::bind_method(D_METHOD("prop_material_set" "value"), &Terrain2DLibrary::prop_material_set); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "prop_material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "prop_material_set", "prop_material_get"); ClassDB::bind_method(D_METHOD("terra_surface_get", "index"), &Terrain2DLibrary::terra_surface_get); ClassDB::bind_method(D_METHOD("terra_surface_add", "value"), &Terrain2DLibrary::terra_surface_add); diff --git a/library/terrain_2d_library.h b/library/terrain_2d_library.h index aa2b092..8a753af 100644 --- a/library/terrain_2d_library.h +++ b/library/terrain_2d_library.h @@ -88,9 +88,6 @@ public: bool supports_caching(); virtual bool _supports_caching(); - Ref material_get(const int index); - Ref material_lod_get(const int index); - void material_cache_get_key(Ref chunk); virtual void _material_cache_get_key(Ref chunk); Ref material_cache_get(const int key); @@ -98,17 +95,8 @@ public: void material_cache_unref(const int key); virtual void _material_cache_unref(const int key); - void material_add(const Ref &value); - void material_set(const int index, const Ref &value); - void material_remove(const int index); - int material_get_num() const; - void materials_clear(); - - Vector materials_get(); - void materials_set(const Vector &materials); - - Ref liquid_material_get(const int index); - Ref liquid_material_lod_get(const int index); + Ref material_get(); + void material_set(const Ref &value); void liquid_material_cache_get_key(Ref chunk); virtual void _liquid_material_cache_get_key(Ref chunk); @@ -117,17 +105,8 @@ public: void liquid_material_cache_unref(const int key); virtual void _liquid_material_cache_unref(const int key); - void liquid_material_add(const Ref &value); - void liquid_material_set(const int index, const Ref &value); - void liquid_material_remove(const int index); - int liquid_material_get_num() const; - void liquid_materials_clear(); - - Vector liquid_materials_get(); - void liquid_materials_set(const Vector &materials); - - Ref prop_material_get(const int index); - Ref prop_material_lod_get(const int index); + Ref liquid_material_get(); + void liquid_material_set(const Ref &value); void prop_material_cache_get_key(Ref chunk); virtual void _prop_material_cache_get_key(Ref chunk); @@ -136,14 +115,8 @@ public: void prop_material_cache_unref(const int key); virtual void _prop_material_cache_unref(const int key); - void prop_material_add(const Ref &value); - void prop_material_set(const int index, const Ref &value); - void prop_material_remove(const int index); - int prop_material_get_num() const; - void prop_materials_clear(); - - Vector prop_materials_get(); - void prop_materials_set(const Vector &materials); + Ref prop_material_get(); + void prop_material_set(const Ref &value); virtual Ref terra_surface_get(const int index); virtual void terra_surface_add(Ref value); @@ -200,9 +173,9 @@ protected: static void _bind_methods(); bool _initialized; - Vector> _materials; - Vector> _liquid_materials; - Vector> _prop_materials; + Ref _material; + Ref _liquid_material; + Ref _prop_material; }; #endif // TERRAIN_2D_LIBRARY_H diff --git a/library/terrain_2d_library_merger.cpp b/library/terrain_2d_library_merger.cpp index b34bbb4..f9417b1 100644 --- a/library/terrain_2d_library_merger.cpp +++ b/library/terrain_2d_library_merger.cpp @@ -296,71 +296,22 @@ void Terrain2DLibraryMerger::refresh_rects() { } void Terrain2DLibraryMerger::_setup_material_albedo(const int material_index, const Ref &texture) { - Ref mat; - - int count = 0; + Ref shmat; switch (material_index) { case MATERIAL_INDEX_TERRAIN: - count = material_get_num(); + shmat = material_get(); break; case MATERIAL_INDEX_LIQUID: - count = liquid_material_get_num(); + shmat = liquid_material_get(); break; case MATERIAL_INDEX_PROP: - count = prop_material_get_num(); + shmat = prop_material_get(); break; } - for (int i = 0; i < count; ++i) { - switch (material_index) { - case MATERIAL_INDEX_TERRAIN: - mat = material_get(i); - break; - case MATERIAL_INDEX_LIQUID: - mat = liquid_material_get(i); - break; - case MATERIAL_INDEX_PROP: - mat = prop_material_get(i); - break; - } - - Ref spmat; - - switch (material_index) { - case MATERIAL_INDEX_TERRAIN: - spmat = material_get(i); - break; - case MATERIAL_INDEX_LIQUID: - spmat = liquid_material_get(i); - break; - case MATERIAL_INDEX_PROP: - spmat = prop_material_get(i); - break; - } - - if (spmat.is_valid()) { - spmat->set_texture(SpatialMaterial::TEXTURE_ALBEDO, texture); - return; - } - - Ref shmat; - - switch (material_index) { - case MATERIAL_INDEX_TERRAIN: - shmat = material_get(i); - break; - case MATERIAL_INDEX_LIQUID: - shmat = liquid_material_get(i); - break; - case MATERIAL_INDEX_PROP: - shmat = prop_material_get(i); - break; - } - - if (shmat.is_valid()) { - shmat->set_shader_param("texture_albedo", texture); - } + if (shmat.is_valid()) { + shmat->set_shader_param("texture_albedo", texture); } } diff --git a/library/terrain_2d_library_merger_pcm.cpp b/library/terrain_2d_library_merger_pcm.cpp index 91a0508..856341c 100644 --- a/library/terrain_2d_library_merger_pcm.cpp +++ b/library/terrain_2d_library_merger_pcm.cpp @@ -172,16 +172,9 @@ void Terrain2DLibraryMergerPCM::_material_cache_get_key(Ref chun cache->surface_add(nms); } - for (int i = 0; i < _materials.size(); ++i) { - Ref m = _materials[i]; - - if (!m.is_valid()) { - continue; - } - - Ref nm = m->duplicate(); - - cache->material_add(nm); + if (_material.is_valid()) { + Ref nm = _material->duplicate(); + cache->material_set(nm); } _material_cache[hash] = cache; @@ -338,16 +331,10 @@ void Terrain2DLibraryMergerPCM::_prop_material_cache_get_key(Ref cache->set_background_color(get_background_color()); cache->set_margin(get_margin()); - for (int i = 0; i < _prop_materials.size(); ++i) { - Ref m = _prop_materials[i]; + if (_prop_material.is_valid()) { + Ref nm = _prop_material->duplicate(); - if (!m.is_valid()) { - continue; - } - - Ref nm = m->duplicate(); - - cache->material_add(nm); + cache->material_set(nm); } /* @@ -684,71 +671,22 @@ void Terrain2DLibraryMergerPCM::refresh_rects() { } void Terrain2DLibraryMergerPCM::_setup_material_albedo(const int material_index, const Ref &texture) { - Ref mat; - - int count = 0; + Ref shmat; switch (material_index) { case MATERIAL_INDEX_TERRAIN: - count = material_get_num(); + shmat = material_get(); break; case MATERIAL_INDEX_LIQUID: - count = liquid_material_get_num(); + shmat = liquid_material_get(); break; case MATERIAL_INDEX_PROP: - count = prop_material_get_num(); + shmat = prop_material_get(); break; } - for (int i = 0; i < count; ++i) { - switch (material_index) { - case MATERIAL_INDEX_TERRAIN: - mat = material_get(i); - break; - case MATERIAL_INDEX_LIQUID: - mat = liquid_material_get(i); - break; - case MATERIAL_INDEX_PROP: - mat = prop_material_get(i); - break; - } - - Ref spmat; - - switch (material_index) { - case MATERIAL_INDEX_TERRAIN: - spmat = material_get(i); - break; - case MATERIAL_INDEX_LIQUID: - spmat = liquid_material_get(i); - break; - case MATERIAL_INDEX_PROP: - spmat = prop_material_get(i); - break; - } - - if (spmat.is_valid()) { - spmat->set_texture(SpatialMaterial::TEXTURE_ALBEDO, texture); - return; - } - - Ref shmat; - - switch (material_index) { - case MATERIAL_INDEX_TERRAIN: - shmat = material_get(i); - break; - case MATERIAL_INDEX_LIQUID: - shmat = liquid_material_get(i); - break; - case MATERIAL_INDEX_PROP: - shmat = prop_material_get(i); - break; - } - - if (shmat.is_valid()) { - shmat->set_shader_param("texture_albedo", texture); - } + if (shmat.is_valid()) { + shmat->set_shader_param("texture_albedo", texture); } } diff --git a/library/terrain_2d_material_cache.cpp b/library/terrain_2d_material_cache.cpp index e07d9a0..33e4c38 100644 --- a/library/terrain_2d_material_cache.cpp +++ b/library/terrain_2d_material_cache.cpp @@ -54,62 +54,12 @@ void Terrain2DMaterialCache::dec_ref_count() { } //Materials -Ref Terrain2DMaterialCache::material_get(const int index) { - ERR_FAIL_INDEX_V(index, _materials.size(), Ref(NULL)); - - return _materials[index]; +Ref Terrain2DMaterialCache::material_get() { + return _material; } -Ref Terrain2DMaterialCache::material_lod_get(const int index) { - ERR_FAIL_COND_V(_materials.size() == 0, Ref(NULL)); - - if (index < 0) { - return _materials[0]; - } - - if (index >= _materials.size()) { - return _materials[_materials.size() - 1]; - } - - return _materials[index]; -} - -void Terrain2DMaterialCache::material_add(const Ref &value) { - ERR_FAIL_COND(!value.is_valid()); - - _materials.push_back(value); -} - -void Terrain2DMaterialCache::material_set(const int index, const Ref &value) { - ERR_FAIL_INDEX(index, _materials.size()); - - _materials.set(index, value); -} - -void Terrain2DMaterialCache::material_remove(const int index) { - _materials.VREMOVE(index); -} - -int Terrain2DMaterialCache::material_get_num() const { - return _materials.size(); -} - -void Terrain2DMaterialCache::materials_clear() { - _materials.clear(); -} - -Vector Terrain2DMaterialCache::materials_get() { - VARIANT_ARRAY_GET(_materials); -} - -void Terrain2DMaterialCache::materials_set(const Vector &materials) { - _materials.clear(); - - for (int i = 0; i < materials.size(); i++) { - Ref material = Ref(materials[i]); - - _materials.push_back(material); - } +void Terrain2DMaterialCache::material_set(const Ref &value) { + _material = value; } //Surfaces @@ -271,7 +221,7 @@ Terrain2DMaterialCache::Terrain2DMaterialCache() { } Terrain2DMaterialCache::~Terrain2DMaterialCache() { - _materials.clear(); + _material.unref(); _surfaces.clear(); } @@ -292,17 +242,9 @@ void Terrain2DMaterialCache::_bind_methods() { GDVIRTUAL_BIND(_setup_material_albedo, "texture"); #endif - ClassDB::bind_method(D_METHOD("material_get", "index"), &Terrain2DMaterialCache::material_get); - ClassDB::bind_method(D_METHOD("material_lod_get", "index"), &Terrain2DMaterialCache::material_lod_get); - ClassDB::bind_method(D_METHOD("material_add", "value"), &Terrain2DMaterialCache::material_add); - ClassDB::bind_method(D_METHOD("material_set", "index", "value"), &Terrain2DMaterialCache::material_set); - ClassDB::bind_method(D_METHOD("material_remove", "index"), &Terrain2DMaterialCache::material_remove); - ClassDB::bind_method(D_METHOD("material_get_num"), &Terrain2DMaterialCache::material_get_num); - ClassDB::bind_method(D_METHOD("materials_clear"), &Terrain2DMaterialCache::materials_clear); - - ClassDB::bind_method(D_METHOD("materials_get"), &Terrain2DMaterialCache::materials_get); - ClassDB::bind_method(D_METHOD("materials_set"), &Terrain2DMaterialCache::materials_set); - ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "materials", PROPERTY_HINT_NONE, "17/17:Material", PROPERTY_USAGE_DEFAULT, "Material"), "materials_set", "materials_get"); + ClassDB::bind_method(D_METHOD("material_get"), &Terrain2DMaterialCache::material_get); + ClassDB::bind_method(D_METHOD("material_set", "value"), &Terrain2DMaterialCache::material_set); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "material_set", "material_get"); ClassDB::bind_method(D_METHOD("surface_get", "index"), &Terrain2DMaterialCache::surface_get); ClassDB::bind_method(D_METHOD("surface_id_get", "index"), &Terrain2DMaterialCache::surface_id_get); diff --git a/library/terrain_2d_material_cache.h b/library/terrain_2d_material_cache.h index d1a14a7..e24ed53 100644 --- a/library/terrain_2d_material_cache.h +++ b/library/terrain_2d_material_cache.h @@ -56,16 +56,8 @@ public: void inc_ref_count(); void dec_ref_count(); - Ref material_get(const int index); - Ref material_lod_get(const int index); - void material_add(const Ref &value); - void material_set(const int index, const Ref &value); - void material_remove(const int index); - int material_get_num() const; - void materials_clear(); - - Vector materials_get(); - void materials_set(const Vector &materials); + Ref material_get(); + void material_set(const Ref &value); virtual Ref surface_get(const int index); virtual Ref surface_id_get(const int id); @@ -107,7 +99,7 @@ protected: bool _initialized; Vector> _surfaces; - Vector> _materials; + Ref _material; Vector> _additional_textures; int _ref_count; diff --git a/library/terrain_2d_material_cache_pcm.cpp b/library/terrain_2d_material_cache_pcm.cpp index adc2cdb..bdc4150 100644 --- a/library/terrain_2d_material_cache_pcm.cpp +++ b/library/terrain_2d_material_cache_pcm.cpp @@ -158,23 +158,12 @@ void Terrain2DMaterialCachePCM::refresh_rects() { } void Terrain2DMaterialCachePCM::_setup_material_albedo(Ref texture) { - int count = material_get_num(); + Ref m = material_get(); - for (int i = 0; i < count; ++i) { - Ref m = material_get(i); + Ref shmat = m; - Ref spmat = m; - - if (spmat.is_valid()) { - spmat->set_texture(SpatialMaterial::TEXTURE_ALBEDO, texture); - return; - } - - Ref shmat = m; - - if (shmat.is_valid()) { - shmat->set_shader_param("texture_albedo", texture); - } + if (shmat.is_valid()) { + shmat->set_shader_param("texture_albedo", texture); } } diff --git a/meshers/terrain_2d_mesher.cpp b/meshers/terrain_2d_mesher.cpp index a32fd64..3ad4873 100644 --- a/meshers/terrain_2d_mesher.cpp +++ b/meshers/terrain_2d_mesher.cpp @@ -252,9 +252,6 @@ void Terrain2DMesher::build_mesh_into(RID mesh) { Array arr = build_mesh(); VS::get_singleton()->mesh_add_surface_from_arrays(mesh, VisualServer::PRIMITIVE_TRIANGLES, arr); - - if (_material.is_valid()) - VS::get_singleton()->mesh_surface_set_material(mesh, 0, _library->material_lod_get(0)->get_rid()); } void Terrain2DMesher::remove_doubles() { diff --git a/world/jobs/terrain_2d_prop_job.cpp b/world/jobs/terrain_2d_prop_job.cpp index 0806c1c..80f4446 100644 --- a/world/jobs/terrain_2d_prop_job.cpp +++ b/world/jobs/terrain_2d_prop_job.cpp @@ -364,18 +364,6 @@ void Terrain2DProp2DJob::step_type_normal() { RID mesh_rid = chunk->mesh_rid_get_index(Terrain2DChunkDefault::MESH_INDEX_PROP, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH, 0); VS::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr); - - Ref lmat; - - if (chunk->prop_material_cache_key_has()) { - lmat = chunk->get_library()->prop_material_cache_get(_chunk->prop_material_cache_key_get())->material_lod_get(0); - } else { - lmat = chunk->get_library()->prop_material_lod_get(0); - } - - if (lmat.is_valid()) { - VisualServer::get_singleton()->mesh_surface_set_material(mesh_rid, 0, lmat->get_rid()); - } } Terrain2DProp2DJob::Terrain2DProp2DJob() { diff --git a/world/jobs/terrain_2d_terrain_job.cpp b/world/jobs/terrain_2d_terrain_job.cpp index 5a54dba..a291adb 100644 --- a/world/jobs/terrain_2d_terrain_job.cpp +++ b/world/jobs/terrain_2d_terrain_job.cpp @@ -297,14 +297,6 @@ void Terrain2DTerrain2DJob::phase_terrain_mesh() { // if (should_do()) { VS::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr); - - if (chunk->get_library()->liquid_material_lod_get(0).is_valid()) - VS::get_singleton()->mesh_surface_set_material(mesh_rid, 0, chunk->get_library()->liquid_material_lod_get(0)->get_rid()); - - // if (should_return()) { - // return; - // } - //} } reset_stages(); @@ -389,18 +381,6 @@ void Terrain2DTerrain2DJob::step_type_normal() { RID mesh_rid = chunk->mesh_rid_get_index(Terrain2DChunkDefault::MESH_INDEX_TERRAIN, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH, 0); VS::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr); - - Ref lmat; - - if (chunk->material_cache_key_has()) { - lmat = chunk->get_library()->material_cache_get(_chunk->material_cache_key_get())->material_lod_get(0); - } else { - lmat = chunk->get_library()->material_lod_get(0); - } - - if (lmat.is_valid()) { - VisualServer::get_singleton()->mesh_surface_set_material(mesh_rid, 0, lmat->get_rid()); - } } Terrain2DTerrain2DJob::Terrain2DTerrain2DJob() {