Implement additional textures login for TerraMaterialCachePCM.

This commit is contained in:
Relintai 2021-08-08 11:43:02 +02:00
parent ab5c1ffb24
commit 6cf36fcd05
2 changed files with 56 additions and 0 deletions

View File

@ -62,6 +62,47 @@ void TerraMaterialCachePCM::set_margin(const int margin) {
_packer->set_margin(margin); _packer->set_margin(margin);
} }
Ref<AtlasTexture> TerraMaterialCachePCM::additional_texture_get_atlas_tex(const Ref<Texture> &texture) {
if (!_packer->contains_texture(texture)) {
return Ref<AtlasTexture>();
}
return _packer->get_texture(texture);
}
Rect2 TerraMaterialCachePCM::additional_texture_get_uv_rect(const Ref<Texture> &texture) {
if (!texture.is_valid()) {
return Rect2(0, 0, 1, 1);
}
Ref<AtlasTexture> at = _packer->get_texture(texture);
if (!at.is_valid()) {
return Rect2(0, 0, 1, 1);
}
Rect2 region = at->get_region();
Ref<Texture> tex = at->get_atlas();
if (!tex.is_valid()) {
return Rect2(0, 0, 1, 1);
}
Ref<Image> 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() { void TerraMaterialCachePCM::refresh_rects() {
bool texture_added = false; bool texture_added = false;
for (int i = 0; i < _surfaces.size(); i++) { 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<Texture> 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) { if (texture_added) {
_packer->merge(); _packer->merge();

View File

@ -44,6 +44,7 @@ SOFTWARE.
class TerraSurface; class TerraSurface;
class TexturePacker; class TexturePacker;
class PropData;
class TerraMaterialCachePCM : public TerraMaterialCache { class TerraMaterialCachePCM : public TerraMaterialCache {
GDCLASS(TerraMaterialCachePCM, TerraMaterialCache); GDCLASS(TerraMaterialCachePCM, TerraMaterialCache);
@ -64,6 +65,9 @@ public:
int get_margin() const; int get_margin() const;
void set_margin(const int margin); void set_margin(const int margin);
Ref<AtlasTexture> additional_texture_get_atlas_tex(const Ref<Texture> &texture);
Rect2 additional_texture_get_uv_rect(const Ref<Texture> &texture);
void refresh_rects(); void refresh_rects();
void _setup_material_albedo(Ref<Texture> texture); void _setup_material_albedo(Ref<Texture> texture);