texture_packer/texture_packer.cpp

214 lines
5.4 KiB
C++
Raw Normal View History

2019-10-21 21:11:11 +02:00
#include "texture_packer.h"
2019-10-20 21:19:00 +02:00
2019-10-21 21:11:11 +02:00
Ref<AtlasTexture> TexturePacker::add_texture(Ref<Texture> texture) {
2019-10-21 18:33:12 +02:00
for (int i = 0; i < _rects.size(); ++i) {
rect_xywhf *r = _rects.get(i);
if (r->original_texture == texture)
return r->atlas_texture;
}
Ref<AtlasTexture> tex;
tex.instance();
//Temp setup, so the texture is usable even while the atlases are generating.
tex->set_atlas(texture);
tex->set_region(Rect2(0, 0, texture->get_width(), texture->get_height()));
rect_xywhf *rect = memnew(rect_xywhf);
rect->original_texture = texture;
rect->atlas_texture = tex;
rect->w = texture->get_width();
rect->h = texture->get_height();
_rects.push_back(rect);
return tex;
}
2019-10-21 21:11:11 +02:00
Ref<Texture> TexturePacker::get_original_texture(int index) {
2019-10-21 18:33:12 +02:00
ERR_FAIL_INDEX_V(index, _rects.size(), Ref<Texture>());
return _rects.get(index)->original_texture;
}
2019-10-21 21:11:11 +02:00
Ref<AtlasTexture> TexturePacker::get_texture(int index) {
2019-10-21 18:33:12 +02:00
ERR_FAIL_INDEX_V(index, _rects.size(), Ref<AtlasTexture>());
return _rects.get(index)->atlas_texture;
}
2019-10-21 21:11:11 +02:00
void TexturePacker::remove_texture_index(int index) {
2019-10-21 18:33:12 +02:00
ERR_FAIL_INDEX(index, _rects.size());
rect_xywhf *r = _rects.get(index);
_rects.remove(index);
r->atlas_texture.unref();
r->atlas_texture.unref();
memdelete(r);
}
2019-10-21 21:11:11 +02:00
void TexturePacker::remove_texture(Ref<Texture> texture) {
2019-10-21 18:33:12 +02:00
for (int i = 0; i < _rects.size(); ++i) {
rect_xywhf *r = _rects.get(i);
if (r->original_texture == texture) {
_rects.remove(i);
return;
}
}
}
2019-10-21 21:11:11 +02:00
int TexturePacker::get_texture_count() {
2019-10-21 18:33:12 +02:00
return _rects.size();
}
2019-10-21 21:11:11 +02:00
Ref<ImageTexture> TexturePacker::get_generated_texture(int index) {
2019-10-21 20:57:39 +02:00
ERR_FAIL_INDEX_V(index, _generated_textures.size(), Ref<ImageTexture>());
return _generated_textures.get(index);
}
2019-10-21 21:11:11 +02:00
int TexturePacker::get_generated_texture_count() {
2019-10-21 20:57:39 +02:00
return _generated_textures.size();
}
2019-10-21 21:11:11 +02:00
void TexturePacker::merge() {
2019-10-21 18:33:12 +02:00
_bins.clear();
2019-10-21 20:57:39 +02:00
if (pack(_rects.ptr(), _rects.size(), 1024, false, _bins)) {
_generated_textures.clear();
2019-10-21 20:57:39 +02:00
_generated_textures.resize(_bins.size());
2019-10-21 20:57:39 +02:00
for (int i = 0; i < _bins.size(); ++i) {
2019-10-21 18:33:12 +02:00
bin b = _bins[i];
PoolByteArray data;
data.resize(b.size.w * b.size.h * 4);
2019-10-21 20:57:39 +02:00
// so it's transparent by default
for (int j = 0; j < data.size(); ++j) {
data.set(j, 0);
}
2019-10-21 18:33:12 +02:00
for (int j = 0; j < b.rects.size(); ++j) {
rect_xywhf *r = b.rects[j];
Ref<Texture> otext = r->original_texture;
2019-10-21 20:57:39 +02:00
ERR_CONTINUE(!otext.is_valid());
2019-10-21 18:33:12 +02:00
Ref<Image> img = otext->get_data();
2019-10-21 20:57:39 +02:00
ERR_CONTINUE(!img.is_valid());
2019-10-21 18:33:12 +02:00
PoolByteArray image_data = img->get_data();
2019-10-21 20:57:39 +02:00
int indx = 0;
for (int y = 0; y < r->h; ++y) {
int start_indx = r->y + y * (b.size.w) * 4 + (r->x * 4);
int row_width = r->w * 4;
for (int x = 0; x < row_width; ++x) {
data.set(start_indx + x, image_data[indx]);
++indx;
}
}
2019-10-21 18:33:12 +02:00
}
2019-10-21 20:57:39 +02:00
Ref<Image> image;
image.instance();
image->create(b.size.w, b.size.h, false, Image::FORMAT_RGBA8, data);
2019-10-21 18:33:12 +02:00
2019-10-21 20:57:39 +02:00
Ref<ImageTexture> texture;
texture.instance();
texture->create_from_image(image, 0);
_generated_textures.set(i, texture);
2019-10-21 18:33:12 +02:00
for (int j = 0; j < b.rects.size(); ++j) {
rect_xywhf *r = b.rects[j];
Ref<AtlasTexture> at = r->atlas_texture;
2019-10-21 20:57:39 +02:00
at->set_atlas(texture);
2019-10-21 18:33:12 +02:00
at->set_region(Rect2(r->x, r->y, r->w, r->h));
}
}
}
}
2019-10-21 21:11:11 +02:00
String TexturePacker::test() {
String res = "";
const int RECTS = 200;
const bool ALLOW_FLIP = false;
rect_xywhf rects[RECTS], *ptr_rects[RECTS];
for (int i = 0; i < RECTS; ++i) {
rects[i] = rect_xywhf(0, 0, 7 + rand() % 50, 7 + rand() % 50);
ptr_rects[i] = rects + i;
}
std::vector<bin> bins;
if (pack(ptr_rects, RECTS, 400, ALLOW_FLIP, bins)) {
res += "bins: " + String::num(bins.size()) + "\n";
for (int i = 0; i < bins.size(); ++i) {
res += "bin: " + String::num(i) + ", rects: " + String::num(bins[i].size.w) + " " + String::num(bins[i].size.h) + " " + String::num(bins[i].rects.size()) + "\n";
for (int r = 0; r < bins[i].rects.size(); ++r) {
rect_xywhf *rect = bins[i].rects[r];
res += "rect " + String::num(r) + ": x: " + String::num(rect->x) + ", y: " + String::num(rect->y) + ", w: " + String::num(rect->w) + ", h: " + String::num(rect->h) + ", was flipped: " + (rect->flipped ? "yes" : " no") + "\n";
}
}
} else {
res += "failed: there's a rectangle with width/height bigger than max_size!\n";
}
return res;
}
2019-10-20 21:19:00 +02:00
2019-10-21 21:11:11 +02:00
TexturePacker::TexturePacker() {
2019-10-20 21:19:00 +02:00
}
2019-10-21 21:11:11 +02:00
TexturePacker::~TexturePacker() {
2019-10-21 18:33:12 +02:00
_bins.clear();
for (int i = 0; i < _rects.size(); ++i) {
rect_xywhf *r = _rects.get(i);
r->atlas_texture.unref();
r->original_texture.unref();
memdelete(r);
}
_rects.clear();
2019-10-20 21:19:00 +02:00
}
2019-10-21 21:11:11 +02:00
void TexturePacker::_bind_methods() {
ClassDB::bind_method(D_METHOD("test"), &TexturePacker::test);
2019-10-21 20:57:39 +02:00
2019-10-21 21:11:11 +02:00
ClassDB::bind_method(D_METHOD("add_texture", "texture"), &TexturePacker::add_texture);
ClassDB::bind_method(D_METHOD("get_texture", "index"), &TexturePacker::get_texture);
ClassDB::bind_method(D_METHOD("get_original_texture", "index"), &TexturePacker::get_original_texture);
ClassDB::bind_method(D_METHOD("remove_texture_index", "index"), &TexturePacker::remove_texture_index);
ClassDB::bind_method(D_METHOD("remove_texture", "texture"), &TexturePacker::remove_texture);
ClassDB::bind_method(D_METHOD("get_texture_count"), &TexturePacker::get_texture_count);
2019-10-21 20:57:39 +02:00
2019-10-21 21:11:11 +02:00
ClassDB::bind_method(D_METHOD("get_generated_texture", "index"), &TexturePacker::get_generated_texture);
ClassDB::bind_method(D_METHOD("get_generated_texture_count"), &TexturePacker::get_generated_texture_count);
2019-10-21 20:57:39 +02:00
2019-10-21 21:11:11 +02:00
ClassDB::bind_method(D_METHOD("merge"), &TexturePacker::merge);
2019-10-20 21:19:00 +02:00
}