More work.

This commit is contained in:
Relintai 2019-10-21 18:33:12 +02:00
parent 5c4aa02eb8
commit 2c7c826558
3 changed files with 151 additions and 24 deletions

View File

@ -1,19 +1,127 @@
#include "merge_texture.h"
Ref<AtlasTexture> MergeTexture::add_texture(Ref<Texture> texture) {
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;
}
Ref<Texture> MergeTexture::get_original_texture(int index) {
ERR_FAIL_INDEX_V(index, _rects.size(), Ref<Texture>());
return _rects.get(index)->original_texture;
}
Ref<AtlasTexture> MergeTexture::get_texture(int index) {
ERR_FAIL_INDEX_V(index, _rects.size(), Ref<AtlasTexture>());
return _rects.get(index)->atlas_texture;
}
void MergeTexture::remove_texture_index(int index) {
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);
}
void MergeTexture::remove_texture(Ref<Texture> texture) {
for (int i = 0; i < _rects.size(); ++i) {
rect_xywhf *r = _rects.get(i);
if (r->original_texture == texture) {
_rects.remove(i);
return;
}
}
}
int MergeTexture::get_texture_count() {
return _rects.size();
}
void MergeTexture::merge() {
const int RECTS = 200;
const bool ALLOW_FLIP = false;
Vector<rect_xywhf *> e;
_bins.clear();
_generated_textures.clear();
//note: add a pointer pointer to the entry into rect_xywhf istead.
for (int i = 0; i < _entries.size(); ++i) {
e.push_back(&(_entries.get(i).rect));
}
if (pack(e.ptr(), RECTS, 400, ALLOW_FLIP, _bins)) {
if (pack(_rects.ptr(), _rects.size(), 400, ALLOW_FLIP, _bins)) {
print_error("ok");
for (int i = 0; i < _bins.size(); ++i) {
bin b = _bins[i];
PoolByteArray data;
data.resize(b.size.w * b.size.h * 4);
data.fill_with(0); // so it's transparent by default
for (int j = 0; j < b.rects.size(); ++j) {
rect_xywhf *r = b.rects[j];
Ref<Texture> otext = r->original_texture;
if (!otext.is_valid()) {
print_error("MergeTexture->merge: !otext.is_valid()");
continue;
}
Ref<Image> img = otext->get_data();
if (!img.is_valid()) {
print_error("MergeTexture->merge: !img.is_valid()");
continue;
}
PoolByteArray image_data = img->get_data();
}
//Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data);
//Ref<Image> image;
//image.instance();
for (int j = 0; j < b.rects.size(); ++j) {
rect_xywhf *r = b.rects[j];
Ref<AtlasTexture> at = r->atlas_texture;
//at->set_atlas(texture);
at->set_region(Rect2(r->x, r->y, r->w, r->h));
}
}
}
}
@ -55,7 +163,19 @@ MergeTexture::MergeTexture() {
}
MergeTexture::~MergeTexture() {
_entries.clear();
_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();
}
void MergeTexture::_bind_methods() {

View File

@ -1,21 +1,27 @@
#ifndef MERGE_TEXTURE_H
#define MERGE_TEXTURE_H
#include <vector>
#include "scene/resources/texture.h"
#include "core/image.h"
#include "core/reference.h"
#include "core/ustring.h"
#include "core/vector.h"
#include "scene/resources/texture.h"
#include <vector>
#include "rectpack2D/pack.h"
class MergeTexture : public ImageTexture {
GDCLASS(MergeTexture, ImageTexture);
//Rename to TexturePacker
class MergeTexture : public Reference {
GDCLASS(MergeTexture, Reference);
public:
void add(Ref<Texture> texture);
void get_texture(int index);
Ref<AtlasTexture> add_texture(Ref<Texture> texture);
Ref<AtlasTexture> get_texture(int index);
Ref<Texture> get_original_texture(int index);
void remove_texture_index(int index);
void remove_texture(Ref<Texture> texture);
int get_texture_count();
void merge();
String test();
@ -23,20 +29,16 @@ public:
MergeTexture();
~MergeTexture();
public:
struct MergeTextureEntry {
Ref<Texture> original_texture;
Ref<AtlasTexture> texture;
rect_xywhf rect;
};
protected:
static void _bind_methods();
private:
bool _keep_original_atlases;
std::vector<bin> _bins;
Vector<MergeTextureEntry> _entries;
Vector<rect_xywhf *> _rects;
Vector<Ref<ImageTexture> > _generated_textures;
};
#endif

View File

@ -1,6 +1,8 @@
#pragma once
#include <vector>
#include "scene/resources/texture.h"
/* of your interest:
1. rect_xywhf - structure representing your rectangle object
@ -71,6 +73,9 @@ struct rect_xywhf : public rect_xywh {
rect_xywhf();
void flip();
bool flipped;
Ref<AtlasTexture> atlas_texture;
Ref<Texture> original_texture;
};
struct bin {