From 4dad639cc67b79672445bcbe7ff7c88909618b6e Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 2 Aug 2021 22:23:04 +0200 Subject: [PATCH] Implemented the main caching logic in TerramanLibraryMergerPCM. --- library/terraman_library_merger_pcm.cpp | 102 ++++++++++++++++++++++-- library/terraman_library_merger_pcm.h | 2 + 2 files changed, 97 insertions(+), 7 deletions(-) diff --git a/library/terraman_library_merger_pcm.cpp b/library/terraman_library_merger_pcm.cpp index 850056f..6a6ee2b 100644 --- a/library/terraman_library_merger_pcm.cpp +++ b/library/terraman_library_merger_pcm.cpp @@ -24,6 +24,7 @@ SOFTWARE. #include "scene/resources/packed_scene.h" #include "scene/resources/texture.h" +#include "../../texture_packer/texture_packer.h" #ifdef PROPS_PRESENT #include "../../props/props/prop_data.h" @@ -46,25 +47,112 @@ bool TerramanLibraryMergerPCM::_supports_caching() { } void TerramanLibraryMergerPCM::_material_cache_get_key(Ref chunk) { + uint8_t *ch = chunk->channel_get(TerraChunkDefault::DEFAULT_CHANNEL_TYPE); - uint8_t *data = chunk->channel_get(TerraChunkDefault::DEFAULT_CHANNEL_TYPE); - - if (!data) { + if (!ch) { chunk->material_cache_key_set(0); chunk->material_cache_key_has_set(true); return; } - PoolIntArray surfaces; + Vector surfaces; + uint32_t size = chunk->get_data_size(); + for (uint32_t i = 0; i < size; ++i) { + uint8_t v = ch[i]; - return; + if (v == 0) { + continue; + } + + int ssize = surfaces.size(); + bool found = false; + for (uint8_t j = 0; j < ssize; ++j) { + if (surfaces[j] == v) { + found = true; + break; + } + } + + if (!found) { + surfaces.push_back(v); + } + } + + surfaces.sort(); + + String hstr; + + for (int i = 0; i < surfaces.size(); ++i) { + hstr += String::num(surfaces[i]) + "|"; + } + + print_error("New cache: " + hstr); + + int hash = static_cast(hstr.hash()); + + chunk->material_cache_key_set(hash); + chunk->material_cache_key_has_set(true); + + if (_material_cache.has(hash)) { + return; + } + + Ref cache; + cache.instance(); + + cache->set_texture_flags(get_texture_flags()); + cache->set_max_atlas_size(get_max_atlas_size()); + cache->set_keep_original_atlases(get_keep_original_atlases()); + cache->set_background_color(get_background_color()); + cache->set_margin(get_margin()); + + for (int i = 0; i < surfaces.size(); ++i) { + int s = surfaces[i] - 1; + + if (_voxel_surfaces.size() <= s) { + continue; + } + + Ref ms = _voxel_surfaces[s]; + + if (!ms.is_valid()) { + continue; + } + + Ref nms = ms->duplicate(); + nms->set_library(Ref(this)); + + cache->voxel_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); + } + + cache->refresh_rects(); + + _material_cache[hash] = cache; } Ref TerramanLibraryMergerPCM::_material_cache_get(const int key) { - return Ref(); + // if (!_material_cache.has(key)) { + // return Ref(); + // } + + ERR_FAIL_COND_V(!_material_cache.has(key), Ref()); + + return _material_cache[key]; } int TerramanLibraryMergerPCM::get_texture_flags() const { @@ -174,7 +262,7 @@ void TerramanLibraryMergerPCM::set_voxel_surfaces(const Vector &surface Ref surface = Ref(surfaces[i]); if (surface.is_valid()) { - surface->set_library(this); + surface->set_library(Ref(this)); } _voxel_surfaces.push_back(surface); diff --git a/library/terraman_library_merger_pcm.h b/library/terraman_library_merger_pcm.h index c0983f4..963bfb8 100644 --- a/library/terraman_library_merger_pcm.h +++ b/library/terraman_library_merger_pcm.h @@ -44,6 +44,7 @@ class TerraSurfaceSimple; class TerraMesher; class PackedScene; class TerraMaterialCachePCM; +class TexturePacker; //pcm = per chunk material class TerramanLibraryMergerPCM : public TerramanLibrary { @@ -117,6 +118,7 @@ protected: Vector > _props; #endif + //todo remove these Ref _packer; Ref _prop_packer; };