From 6cf36fcd05c77798773f82132e9776343b87f7f2 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 8 Aug 2021 11:43:02 +0200 Subject: [PATCH] Implement additional textures login for TerraMaterialCachePCM. --- library/terra_material_cache_pcm.cpp | 52 ++++++++++++++++++++++++++++ library/terra_material_cache_pcm.h | 4 +++ 2 files changed, 56 insertions(+) diff --git a/library/terra_material_cache_pcm.cpp b/library/terra_material_cache_pcm.cpp index 104ce6d..8c48dc1 100644 --- a/library/terra_material_cache_pcm.cpp +++ b/library/terra_material_cache_pcm.cpp @@ -62,6 +62,47 @@ void TerraMaterialCachePCM::set_margin(const int margin) { _packer->set_margin(margin); } +Ref TerraMaterialCachePCM::additional_texture_get_atlas_tex(const Ref &texture) { + if (!_packer->contains_texture(texture)) { + return Ref(); + } + + return _packer->get_texture(texture); +} +Rect2 TerraMaterialCachePCM::additional_texture_get_uv_rect(const Ref &texture) { + if (!texture.is_valid()) { + return Rect2(0, 0, 1, 1); + } + + Ref at = _packer->get_texture(texture); + + if (!at.is_valid()) { + return Rect2(0, 0, 1, 1); + } + + Rect2 region = at->get_region(); + + Ref tex = at->get_atlas(); + + if (!tex.is_valid()) { + return Rect2(0, 0, 1, 1); + } + + Ref image = tex->get_data(); + + if (!image.is_valid()) { + return Rect2(0, 0, 1, 1); + } + + float w = image->get_width(); + float h = image->get_height(); + + region.position = Size2(region.position.x / w, region.position.y / h); + region.size = Size2(region.size.x / w, region.size.y / h); + + return region; +} + void TerraMaterialCachePCM::refresh_rects() { bool texture_added = false; for (int i = 0; i < _surfaces.size(); i++) { @@ -84,6 +125,17 @@ void TerraMaterialCachePCM::refresh_rects() { } } + for (int i = 0; i < _additional_textures.size(); i++) { + Ref tex = _additional_textures.get(i); + + ERR_CONTINUE(!tex.is_valid()); + + if (!_packer->contains_texture(tex)) { + _packer->add_texture(tex); + texture_added = true; + } + } + if (texture_added) { _packer->merge(); diff --git a/library/terra_material_cache_pcm.h b/library/terra_material_cache_pcm.h index 7578ac2..49f3f7c 100644 --- a/library/terra_material_cache_pcm.h +++ b/library/terra_material_cache_pcm.h @@ -44,6 +44,7 @@ SOFTWARE. class TerraSurface; class TexturePacker; +class PropData; class TerraMaterialCachePCM : public TerraMaterialCache { GDCLASS(TerraMaterialCachePCM, TerraMaterialCache); @@ -64,6 +65,9 @@ public: int get_margin() const; void set_margin(const int margin); + Ref additional_texture_get_atlas_tex(const Ref &texture); + Rect2 additional_texture_get_uv_rect(const Ref &texture); + void refresh_rects(); void _setup_material_albedo(Ref texture);