props_2d/material_cache/prop_2d_material_cache_pcm.cpp

200 lines
6.5 KiB
C++
Raw Normal View History

2021-11-23 16:53:47 +01:00
/*
Copyright (c) 2019-2022 Péter Magyar
2021-11-23 16:53:47 +01:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
2022-02-21 22:02:46 +01:00
#include "prop_2d_material_cache_pcm.h"
2021-11-23 16:53:47 +01:00
#include "../../texture_packer/texture_packer.h"
2022-02-21 22:02:46 +01:00
#include "../singleton/prop_2d_cache.h"
2021-11-23 16:53:47 +01:00
#include "scene/resources/texture.h"
2022-02-21 22:12:25 +01:00
int Prop2DMaterialCachePCM::get_texture_flags() const {
2021-11-23 16:53:47 +01:00
return _packer->get_texture_flags();
}
2022-02-21 22:12:25 +01:00
void Prop2DMaterialCachePCM::set_texture_flags(const int flags) {
2021-11-23 16:53:47 +01:00
_packer->set_texture_flags(flags);
}
2022-02-21 22:12:25 +01:00
int Prop2DMaterialCachePCM::get_max_atlas_size() const {
2021-11-23 16:53:47 +01:00
return _packer->get_max_atlas_size();
}
2022-02-21 22:12:25 +01:00
void Prop2DMaterialCachePCM::set_max_atlas_size(const int size) {
2021-11-23 16:53:47 +01:00
_packer->set_max_atlas_size(size);
}
2022-02-21 22:12:25 +01:00
bool Prop2DMaterialCachePCM::get_keep_original_atlases() const {
2021-11-23 16:53:47 +01:00
return _packer->get_keep_original_atlases();
}
2022-02-21 22:12:25 +01:00
void Prop2DMaterialCachePCM::set_keep_original_atlases(const bool value) {
2021-11-23 16:53:47 +01:00
_packer->set_keep_original_atlases(value);
}
2022-02-21 22:12:25 +01:00
Color Prop2DMaterialCachePCM::get_background_color() const {
2021-11-23 16:53:47 +01:00
return _packer->get_background_color();
}
2022-02-21 22:12:25 +01:00
void Prop2DMaterialCachePCM::set_background_color(const Color &color) {
2021-11-23 16:53:47 +01:00
_packer->set_background_color(color);
}
2022-02-21 22:12:25 +01:00
int Prop2DMaterialCachePCM::get_margin() const {
2021-11-23 16:53:47 +01:00
return _packer->get_margin();
}
2022-02-21 22:12:25 +01:00
void Prop2DMaterialCachePCM::set_margin(const int margin) {
2021-11-23 16:53:47 +01:00
_packer->set_margin(margin);
}
2022-02-21 22:12:25 +01:00
Ref<AtlasTexture> Prop2DMaterialCachePCM::texture_get_atlas_tex(const Ref<Texture> &texture) {
2021-11-23 16:53:47 +01:00
if (!_packer->contains_texture(texture)) {
return Ref<AtlasTexture>();
}
return _packer->get_texture(texture);
}
2022-02-21 22:12:25 +01:00
Rect2 Prop2DMaterialCachePCM::texture_get_uv_rect(const Ref<Texture> &texture) {
2021-11-23 16:53:47 +01:00
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 = tex->get_width();
float h = tex->get_height();
2021-11-23 16:53:47 +01:00
region.position = Size2(region.position.x / w, region.position.y / h);
region.size = Size2(region.size.x / w, region.size.y / h);
return region;
}
Ref<Texture> Prop2DMaterialCachePCM::texture_get_merged() {
return _merged_texture;
}
2022-02-21 22:12:25 +01:00
void Prop2DMaterialCachePCM::refresh_rects() {
2021-11-23 16:53:47 +01:00
bool texture_added = false;
for (int i = 0; i < _textures.size(); i++) {
Ref<Texture> tex = _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();
ERR_FAIL_COND(_packer->get_texture_count() == 0);
_merged_texture = _packer->get_generated_texture(0);
2021-11-23 16:53:47 +01:00
setup_material_albedo(_merged_texture);
2021-11-23 16:53:47 +01:00
}
_initialized = true;
}
2022-02-21 22:12:25 +01:00
void Prop2DMaterialCachePCM::initial_setup_default() {
Prop2DMaterialCache::initial_setup_default();
2021-11-23 16:53:47 +01:00
2022-02-21 22:12:25 +01:00
Prop2DCache *pc = Prop2DCache::get_singleton();
2021-11-23 16:53:47 +01:00
set_texture_flags(pc->get_texture_flags());
set_max_atlas_size(pc->get_max_atlas_size());
set_keep_original_atlases(pc->get_keep_original_atlases());
set_background_color(pc->get_background_color());
set_margin(pc->get_margin());
}
2022-02-21 22:12:25 +01:00
void Prop2DMaterialCachePCM::_setup_material_albedo(Ref<Texture> texture) {
Ref<Material> m = material_get();
2021-11-23 16:53:47 +01:00
Ref<ShaderMaterial> shmat = m;
2021-11-23 16:53:47 +01:00
if (shmat.is_valid()) {
shmat->set_shader_param("texture_albedo", texture);
2021-11-23 16:53:47 +01:00
}
}
2022-02-21 22:12:25 +01:00
Prop2DMaterialCachePCM::Prop2DMaterialCachePCM() {
2021-11-23 16:53:47 +01:00
_packer.instance();
#if GODOT4
#warning implement
#else
_packer->set_texture_flags(Texture::FLAG_MIPMAPS | Texture::FLAG_FILTER);
#endif
_packer->set_max_atlas_size(1024);
_packer->set_keep_original_atlases(false);
_packer->set_margin(0);
}
2022-02-21 22:12:25 +01:00
Prop2DMaterialCachePCM::~Prop2DMaterialCachePCM() {
2021-11-23 16:53:47 +01:00
_packer->clear();
_packer.unref();
}
2022-02-21 22:12:25 +01:00
void Prop2DMaterialCachePCM::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_texture_flags"), &Prop2DMaterialCachePCM::get_texture_flags);
ClassDB::bind_method(D_METHOD("set_texture_flags", "flags"), &Prop2DMaterialCachePCM::set_texture_flags);
2021-11-23 16:53:47 +01:00
ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_flags", PROPERTY_HINT_FLAGS, "Mipmaps,Repeat,Filter,Anisotropic Linear,Convert to Linear,Mirrored Repeat,Video Surface"), "set_texture_flags", "get_texture_flags");
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("get_max_atlas_size"), &Prop2DMaterialCachePCM::get_max_atlas_size);
ClassDB::bind_method(D_METHOD("set_max_atlas_size", "size"), &Prop2DMaterialCachePCM::set_max_atlas_size);
2021-11-23 16:53:47 +01:00
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_atlas_size"), "set_max_atlas_size", "get_max_atlas_size");
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("get_keep_original_atlases"), &Prop2DMaterialCachePCM::get_keep_original_atlases);
ClassDB::bind_method(D_METHOD("set_keep_original_atlases", "value"), &Prop2DMaterialCachePCM::set_keep_original_atlases);
2021-11-23 16:53:47 +01:00
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_original_atlases"), "set_keep_original_atlases", "get_keep_original_atlases");
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("get_background_color"), &Prop2DMaterialCachePCM::get_background_color);
ClassDB::bind_method(D_METHOD("set_background_color", "color"), &Prop2DMaterialCachePCM::set_background_color);
2021-11-23 16:53:47 +01:00
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "background_color"), "set_background_color", "get_background_color");
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("get_margin"), &Prop2DMaterialCachePCM::get_margin);
ClassDB::bind_method(D_METHOD("set_margin", "size"), &Prop2DMaterialCachePCM::set_margin);
2021-11-23 16:53:47 +01:00
ADD_PROPERTY(PropertyInfo(Variant::INT, "margin"), "set_margin", "get_margin");
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("_setup_material_albedo", "texture"), &Prop2DMaterialCachePCM::_setup_material_albedo);
2021-11-23 16:53:47 +01:00
}