diff --git a/library/terraman_library.cpp b/library/terraman_library.cpp index 6722556..fd1e4c9 100644 --- a/library/terraman_library.cpp +++ b/library/terraman_library.cpp @@ -69,12 +69,11 @@ Ref TerramanLibrary::material_lod_get(const int index) { return _materials[index]; } -int TerramanLibrary::material_cache_get_key(const Ref &chunk) { - return call("_material_cache_get_key", chunk); +void TerramanLibrary::material_cache_get_key(const Ref &chunk) { + call("_material_cache_get_key", chunk); } -int TerramanLibrary::_material_cache_get_key(Ref chunk) { - return 0; +void TerramanLibrary::_material_cache_get_key(Ref chunk) { } Ref TerramanLibrary::material_cache_get(const int key) { @@ -144,12 +143,11 @@ Ref TerramanLibrary::liquid_material_lod_get(const int index) { return _liquid_materials[index]; } -int TerramanLibrary::liquid_material_cache_get_key(const Ref &chunk) { - return call("_liquid_material_cache_get_key", chunk); +void TerramanLibrary::liquid_material_cache_get_key(const Ref &chunk) { + call("_liquid_material_cache_get_key", chunk); } -int TerramanLibrary::_liquid_material_cache_get_key(Ref chunk) { - return 0; +void TerramanLibrary::_liquid_material_cache_get_key(Ref chunk) { } Ref TerramanLibrary::liquid_material_cache_get(const int key) { @@ -219,12 +217,11 @@ Ref TerramanLibrary::prop_material_lod_get(const int index) { return _prop_materials[index]; } -int TerramanLibrary::prop_material_cache_get_key(const Ref &chunk) { - return call("_prop_material_cache_get_key", chunk); +void TerramanLibrary::prop_material_cache_get_key(const Ref &chunk) { + call("_prop_material_cache_get_key", chunk); } -int TerramanLibrary::_prop_material_cache_get_key(Ref chunk) { - return 0; +void TerramanLibrary::_prop_material_cache_get_key(Ref chunk) { } Ref TerramanLibrary::prop_material_cache_get(const int key) { @@ -349,7 +346,7 @@ void TerramanLibrary::_bind_methods() { BIND_VMETHOD(MethodInfo("_setup_material_albedo", PropertyInfo(Variant::INT, "material_index"), PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"))); - BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::INT, "ret"), "_material_cache_get_key")); + BIND_VMETHOD(MethodInfo("_material_cache_get_key", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "TerraChunk"))); BIND_VMETHOD(MethodInfo("_material_cache_get", PropertyInfo(Variant::OBJECT, "key", PROPERTY_HINT_RESOURCE_TYPE, "TerraMaterialCache"))); ClassDB::bind_method(D_METHOD("material_get", "index"), &TerramanLibrary::material_get); @@ -370,7 +367,7 @@ void TerramanLibrary::_bind_methods() { ClassDB::bind_method(D_METHOD("materials_set"), &TerramanLibrary::materials_set); ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "materials", PROPERTY_HINT_NONE, "17/17:Material", PROPERTY_USAGE_DEFAULT, "Material"), "materials_set", "materials_get"); - BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::INT, "ret"), "_liquid_material_cache_get_key")); + BIND_VMETHOD(MethodInfo("_liquid_material_cache_get_key", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "TerraChunk"))); BIND_VMETHOD(MethodInfo("_liquid_material_cache_get", PropertyInfo(Variant::OBJECT, "key", PROPERTY_HINT_RESOURCE_TYPE, "TerraMaterialCache"))); ClassDB::bind_method(D_METHOD("liquid_material_get", "index"), &TerramanLibrary::liquid_material_get); @@ -391,7 +388,7 @@ void TerramanLibrary::_bind_methods() { ClassDB::bind_method(D_METHOD("liquid_materials_set"), &TerramanLibrary::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"); - BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::INT, "ret"), "_prop_material_cache_get_key")); + BIND_VMETHOD(MethodInfo("_prop_material_cache_get_key", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "TerraChunk"))); BIND_VMETHOD(MethodInfo("_prop_material_cache_get", PropertyInfo(Variant::OBJECT, "key", PROPERTY_HINT_RESOURCE_TYPE, "TerraMaterialCache"))); ClassDB::bind_method(D_METHOD("prop_material_get", "index"), &TerramanLibrary::prop_material_get); diff --git a/library/terraman_library.h b/library/terraman_library.h index bc53100..298bb44 100644 --- a/library/terraman_library.h +++ b/library/terraman_library.h @@ -72,8 +72,8 @@ public: Ref material_get(const int index); Ref material_lod_get(const int index); - int material_cache_get_key(const Ref &chunk); - virtual int _material_cache_get_key(Ref chunk); + void material_cache_get_key(const Ref &chunk); + virtual void _material_cache_get_key(Ref chunk); Ref material_cache_get(const int key); virtual Ref _material_cache_get(const int key); @@ -89,8 +89,8 @@ public: Ref liquid_material_get(const int index); Ref liquid_material_lod_get(const int index); - int liquid_material_cache_get_key(const Ref &chunk); - virtual int _liquid_material_cache_get_key(Ref chunk); + void liquid_material_cache_get_key(const Ref &chunk); + virtual void _liquid_material_cache_get_key(Ref chunk); Ref liquid_material_cache_get(const int key); virtual Ref _liquid_material_cache_get(const int key); @@ -106,8 +106,8 @@ public: Ref prop_material_get(const int index); Ref prop_material_lod_get(const int index); - int prop_material_cache_get_key(const Ref &chunk); - virtual int _prop_material_cache_get_key(Ref chunk); + void prop_material_cache_get_key(const Ref &chunk); + virtual void _prop_material_cache_get_key(Ref chunk); Ref prop_material_cache_get(const int key); virtual Ref _prop_material_cache_get(const int key); diff --git a/library/terraman_library_merger_pcm.cpp b/library/terraman_library_merger_pcm.cpp index c216718..850056f 100644 --- a/library/terraman_library_merger_pcm.cpp +++ b/library/terraman_library_merger_pcm.cpp @@ -31,17 +31,41 @@ SOFTWARE. #include "../../props/props/prop_data_prop.h" #endif +#include "terra_material_cache_pcm.h" + #if MESH_DATA_RESOURCE_PRESENT #include "../../mesh_data_resource/props/prop_data_mesh_data.h" #endif #include "../defines.h" +#include "../world/default/terra_chunk_default.h" bool TerramanLibraryMergerPCM::_supports_caching() { return true; } +void TerramanLibraryMergerPCM::_material_cache_get_key(Ref chunk) { + + uint8_t *data = chunk->channel_get(TerraChunkDefault::DEFAULT_CHANNEL_TYPE); + + if (!data) { + chunk->material_cache_key_set(0); + chunk->material_cache_key_has_set(true); + + return; + } + + PoolIntArray surfaces; + + + + return; +} + +Ref TerramanLibraryMergerPCM::_material_cache_get(const int key) { + return Ref(); +} int TerramanLibraryMergerPCM::get_texture_flags() const { return _packer->get_texture_flags(); @@ -319,7 +343,6 @@ void TerramanLibraryMergerPCM::_setup_material_albedo(const int material_index, } for (int i = 0; i < count; ++i) { - switch (material_index) { case MATERIAL_INDEX_TERRAS: mat = material_get(i); diff --git a/library/terraman_library_merger_pcm.h b/library/terraman_library_merger_pcm.h index 7f5e2de..c0983f4 100644 --- a/library/terraman_library_merger_pcm.h +++ b/library/terraman_library_merger_pcm.h @@ -43,6 +43,7 @@ SOFTWARE. class TerraSurfaceSimple; class TerraMesher; class PackedScene; +class TerraMaterialCachePCM; //pcm = per chunk material class TerramanLibraryMergerPCM : public TerramanLibrary { @@ -50,6 +51,8 @@ class TerramanLibraryMergerPCM : public TerramanLibrary { public: bool _supports_caching(); + void _material_cache_get_key(Ref chunk); + Ref _material_cache_get(const int key); int get_texture_flags() const; void set_texture_flags(const int flags); @@ -107,6 +110,8 @@ protected: static void _bind_methods(); + Map > _material_cache; + Vector > _voxel_surfaces; #ifdef PROPS_PRESENT Vector > _props; diff --git a/meshers/terra_mesher.cpp b/meshers/terra_mesher.cpp index 46f17d6..9434578 100644 --- a/meshers/terra_mesher.cpp +++ b/meshers/terra_mesher.cpp @@ -126,13 +126,6 @@ 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; } @@ -908,7 +901,6 @@ TerraMesher::TerraMesher(const Ref &library) { _format = 0; _texture_scale = 1; - _terrarin_material_key = 0; } TerraMesher::TerraMesher() { @@ -921,7 +913,6 @@ TerraMesher::TerraMesher() { _channel_index_type = 0; _channel_index_isolevel = 0; _texture_scale = 1; - _terrarin_material_key = 0; } TerraMesher::~TerraMesher() { @@ -963,10 +954,6 @@ 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 dd4b8a8..45b301c 100644 --- a/meshers/terra_mesher.h +++ b/meshers/terra_mesher.h @@ -116,9 +116,6 @@ 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); @@ -208,8 +205,6 @@ 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 02ba9e4..940805e 100644 --- a/world/jobs/terra_terrarin_job.cpp +++ b/world/jobs/terra_terrarin_job.cpp @@ -99,11 +99,7 @@ void TerraTerrarinJob::phase_library_setup() { } if (lib->supports_caching()) { - _terrain_material_key = lib->material_cache_get_key(_chunk); - - if (_mesher.is_valid()) { - _mesher->set_terrain_material_key(_terrain_material_key); - } + lib->material_cache_get_key(_chunk); //if (_liquid_mesher.is_valid()) { // _liquid_mesher->set_terrain_material_key(_terrarin_material_key); @@ -484,7 +480,7 @@ void TerraTerrarinJob::step_type_normal() { Ref lmat; if (chunk->get_library()->supports_caching()) { - lmat = chunk->get_library()->material_cache_get(_terrain_material_key)->material_lod_get(_current_mesh); + lmat = chunk->get_library()->material_cache_get(_chunk->material_cache_key_get())->material_lod_get(_current_mesh); } else { lmat = chunk->get_library()->material_lod_get(_current_mesh); } @@ -525,7 +521,7 @@ void TerraTerrarinJob::step_type_normal_lod() { Ref lmat; if (chunk->get_library()->supports_caching()) { - lmat = chunk->get_library()->material_cache_get(_terrain_material_key)->material_lod_get(_current_mesh); + lmat = chunk->get_library()->material_cache_get(_chunk->material_cache_key_get())->material_lod_get(_current_mesh); } else { lmat = chunk->get_library()->material_lod_get(_current_mesh); } @@ -549,7 +545,7 @@ void TerraTerrarinJob::step_type_drop_uv2() { Ref lmat; if (chunk->get_library()->supports_caching()) { - lmat = chunk->get_library()->material_cache_get(_terrain_material_key)->material_lod_get(_current_mesh); + lmat = chunk->get_library()->material_cache_get(_chunk->material_cache_key_get())->material_lod_get(_current_mesh); } else { lmat = chunk->get_library()->material_lod_get(_current_mesh); } @@ -573,7 +569,7 @@ void TerraTerrarinJob::step_type_merge_verts() { Ref lmat; if (chunk->get_library()->supports_caching()) { - lmat = chunk->get_library()->material_cache_get(_terrain_material_key)->material_lod_get(_current_mesh); + lmat = chunk->get_library()->material_cache_get(_chunk->material_cache_key_get())->material_lod_get(_current_mesh); } else { lmat = chunk->get_library()->material_lod_get(_current_mesh); } @@ -609,7 +605,7 @@ void TerraTerrarinJob::step_type_bake_texture() { Ref lmat; if (chunk->get_library()->supports_caching()) { - lmat = chunk->get_library()->material_cache_get(_terrain_material_key)->material_lod_get(_current_mesh); + lmat = chunk->get_library()->material_cache_get(_chunk->material_cache_key_get())->material_lod_get(_current_mesh); } else { lmat = chunk->get_library()->material_lod_get(_current_mesh); } @@ -644,7 +640,7 @@ void TerraTerrarinJob::step_type_simplify_mesh() { Ref lmat; if (chunk->get_library()->supports_caching()) { - lmat = chunk->get_library()->material_cache_get(_terrain_material_key)->material_lod_get(_current_mesh); + lmat = chunk->get_library()->material_cache_get(_chunk->material_cache_key_get())->material_lod_get(_current_mesh); } else { lmat = chunk->get_library()->material_lod_get(_current_mesh); } diff --git a/world/jobs/terra_terrarin_job.h b/world/jobs/terra_terrarin_job.h index fac4006..3c57ffd 100644 --- a/world/jobs/terra_terrarin_job.h +++ b/world/jobs/terra_terrarin_job.h @@ -87,8 +87,6 @@ protected: PoolVector temp_arr_collider; PoolVector temp_arr_collider_liquid; Array temp_mesh_arr; - - int _terrain_material_key; }; #endif diff --git a/world/terra_chunk.cpp b/world/terra_chunk.cpp index 13cf73c..903c717 100644 --- a/world/terra_chunk.cpp +++ b/world/terra_chunk.cpp @@ -171,6 +171,20 @@ _FORCE_INLINE_ void TerraChunk::set_margin_end(const int value) { _margin_end = value; } +int TerraChunk::material_cache_key_get() const { + return _material_cache_key; +} +void TerraChunk::material_cache_key_set(const int value) { + _material_cache_key = value; +} + +bool TerraChunk::material_cache_key_has() const { + return _material_cache_key_has; +} +void TerraChunk::material_cache_key_has_set(const bool value) { + _material_cache_key_has = value; +} + Ref TerraChunk::get_library() { return _library; } @@ -965,19 +979,16 @@ void TerraChunk::set_transform(const Transform &transform) { } Transform TerraChunk::get_global_transform() const { - ERR_FAIL_COND_V(!get_voxel_world(), Transform()); return get_voxel_world()->get_global_transform() * _transform; } Vector3 TerraChunk::to_local(Vector3 p_global) const { - return get_global_transform().affine_inverse().xform(p_global); } Vector3 TerraChunk::to_global(Vector3 p_local) const { - return get_global_transform().xform(p_local); } @@ -1008,6 +1019,9 @@ TerraChunk::TerraChunk() { _margin_start = 0; _margin_end = 0; + _material_cache_key = 0; + _material_cache_key_has = false; + _current_job = -1; _world_height = 256; @@ -1287,6 +1301,14 @@ void TerraChunk::_bind_methods() { ClassDB::bind_method(D_METHOD("set_margin_end"), &TerraChunk::set_margin_end); ADD_PROPERTY(PropertyInfo(Variant::INT, "margin_end"), "set_margin_end", "get_margin_end"); + ClassDB::bind_method(D_METHOD("material_cache_key_get"), &TerraChunk::material_cache_key_get); + ClassDB::bind_method(D_METHOD("material_cache_key_set"), &TerraChunk::material_cache_key_set); + ADD_PROPERTY(PropertyInfo(Variant::INT, "material_cache_key"), "material_cache_key_set", "material_cache_key_get"); + + ClassDB::bind_method(D_METHOD("material_cache_key_has"), &TerraChunk::material_cache_key_has); + ClassDB::bind_method(D_METHOD("material_cache_key_has_set"), &TerraChunk::material_cache_key_has_set); + ADD_PROPERTY(PropertyInfo(Variant::INT, "material_cache_key_has"), "material_cache_key_has_set", "material_cache_key_has"); + ClassDB::bind_method(D_METHOD("get_library"), &TerraChunk::get_library); ClassDB::bind_method(D_METHOD("set_library", "value"), &TerraChunk::set_library); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "TerramanLibrary"), "set_library", "get_library"); diff --git a/world/terra_chunk.h b/world/terra_chunk.h index 7d720ad..0da7fe6 100644 --- a/world/terra_chunk.h +++ b/world/terra_chunk.h @@ -26,15 +26,15 @@ SOFTWARE. #include "core/version.h" #if VERSION_MAJOR > 3 +#include "core/config/engine.h" #include "core/io/resource.h" #include "core/string/ustring.h" -#include "core/config/engine.h" #include "core/variant/array.h" #else +#include "core/array.h" +#include "core/engine.h" #include "core/resource.h" #include "core/ustring.h" -#include "core/engine.h" -#include "core/array.h" #endif #include "../defines.h" @@ -135,6 +135,12 @@ public: void set_margin_start(const int value); void set_margin_end(const int value); + int material_cache_key_get() const; + void material_cache_key_set(const int value); + + bool material_cache_key_has() const; + void material_cache_key_has_set(const bool value); + Ref get_library(); void set_library(const Ref &value); @@ -356,6 +362,9 @@ protected: int _margin_start; int _margin_end; + int _material_cache_key; + bool _material_cache_key_has; + float _world_height; Vector _channels; @@ -363,11 +372,11 @@ protected: float _voxel_scale; int _current_job; - Vector > _jobs; + Vector> _jobs; Ref _library; - Vector > _voxel_structures; + Vector> _voxel_structures; #if PROPS_PRESENT Vector _props;