2019-06-11 13:31:23 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* resource_importer_texture_atlas.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2019-06-11 13:31:23 +00: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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2019-04-19 18:54:33 +00:00
|
|
|
#include "resource_importer_texture_atlas.h"
|
|
|
|
|
|
|
|
#include "atlas_import_failed.xpm"
|
2021-06-11 12:51:48 +00:00
|
|
|
#include "core/io/file_access.h"
|
2019-04-19 18:54:33 +00:00
|
|
|
#include "core/io/image_loader.h"
|
|
|
|
#include "core/io/resource_saver.h"
|
2020-05-25 17:20:45 +00:00
|
|
|
#include "core/math/geometry_2d.h"
|
2019-04-19 18:54:33 +00:00
|
|
|
#include "editor/editor_atlas_packer.h"
|
|
|
|
#include "scene/resources/mesh.h"
|
|
|
|
#include "scene/resources/texture.h"
|
|
|
|
|
|
|
|
String ResourceImporterTextureAtlas::get_importer_name() const {
|
|
|
|
return "texture_atlas";
|
|
|
|
}
|
|
|
|
|
|
|
|
String ResourceImporterTextureAtlas::get_visible_name() const {
|
|
|
|
return "TextureAtlas";
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2019-04-19 18:54:33 +00:00
|
|
|
void ResourceImporterTextureAtlas::get_recognized_extensions(List<String> *p_extensions) const {
|
|
|
|
ImageLoader::get_recognized_extensions(p_extensions);
|
|
|
|
}
|
|
|
|
|
|
|
|
String ResourceImporterTextureAtlas::get_save_extension() const {
|
|
|
|
return "res";
|
|
|
|
}
|
|
|
|
|
|
|
|
String ResourceImporterTextureAtlas::get_resource_type() const {
|
2019-06-11 18:43:37 +00:00
|
|
|
return "Texture2D";
|
2019-04-19 18:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ResourceImporterTextureAtlas::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ResourceImporterTextureAtlas::get_preset_count() const {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2019-04-19 18:54:33 +00:00
|
|
|
String ResourceImporterTextureAtlas::get_preset_name(int p_idx) const {
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceImporterTextureAtlas::get_import_options(List<ImportOption> *r_options, int p_preset) const {
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "atlas_file", PROPERTY_HINT_SAVE_FILE, "*.png"), ""));
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "import_mode", PROPERTY_HINT_ENUM, "Region,Mesh2D"), 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
String ResourceImporterTextureAtlas::get_option_group_file() const {
|
|
|
|
return "atlas_file";
|
|
|
|
}
|
|
|
|
|
|
|
|
Error ResourceImporterTextureAtlas::import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
|
|
|
|
/* If this happens, it's because the atlas_file field was not filled, so just import a broken texture */
|
|
|
|
|
|
|
|
//use an xpm because it's size independent, the editor images are vector and size dependent
|
|
|
|
//it's a simple hack
|
|
|
|
Ref<Image> broken = memnew(Image((const char **)atlas_import_failed_xpm));
|
|
|
|
Ref<ImageTexture> broken_texture;
|
|
|
|
broken_texture.instance();
|
|
|
|
broken_texture->create_from_image(broken);
|
|
|
|
|
|
|
|
String target_file = p_save_path + ".tex";
|
|
|
|
|
|
|
|
ResourceSaver::save(target_file, broken_texture);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2020-11-21 07:42:29 +00:00
|
|
|
static void _plot_triangle(Vector2i *vertices, const Vector2i &p_offset, bool p_transposed, Ref<Image> p_image, const Ref<Image> &p_src_image) {
|
2019-04-19 18:54:33 +00:00
|
|
|
int width = p_image->get_width();
|
|
|
|
int height = p_image->get_height();
|
|
|
|
int src_width = p_src_image->get_width();
|
|
|
|
int src_height = p_src_image->get_height();
|
|
|
|
|
|
|
|
int x[3];
|
|
|
|
int y[3];
|
|
|
|
|
|
|
|
for (int j = 0; j < 3; j++) {
|
|
|
|
x[j] = vertices[j].x;
|
|
|
|
y[j] = vertices[j].y;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort the points vertically
|
|
|
|
if (y[1] > y[2]) {
|
|
|
|
SWAP(x[1], x[2]);
|
|
|
|
SWAP(y[1], y[2]);
|
|
|
|
}
|
|
|
|
if (y[0] > y[1]) {
|
|
|
|
SWAP(x[0], x[1]);
|
|
|
|
SWAP(y[0], y[1]);
|
|
|
|
}
|
|
|
|
if (y[1] > y[2]) {
|
|
|
|
SWAP(x[1], x[2]);
|
|
|
|
SWAP(y[1], y[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
double dx_far = double(x[2] - x[0]) / (y[2] - y[0] + 1);
|
|
|
|
double dx_upper = double(x[1] - x[0]) / (y[1] - y[0] + 1);
|
|
|
|
double dx_low = double(x[2] - x[1]) / (y[2] - y[1] + 1);
|
|
|
|
double xf = x[0];
|
|
|
|
double xt = x[0] + dx_upper; // if y[0] == y[1], special case
|
2020-06-03 20:24:49 +00:00
|
|
|
int max_y = MIN(y[2], height - p_offset.y - 1);
|
|
|
|
for (int yi = y[0]; yi <= max_y; yi++) {
|
2019-04-19 18:54:33 +00:00
|
|
|
if (yi >= 0) {
|
|
|
|
for (int xi = (xf > 0 ? int(xf) : 0); xi <= (xt < width ? xt : width - 1); xi++) {
|
|
|
|
int px = xi, py = yi;
|
|
|
|
int sx = px, sy = py;
|
2020-02-26 13:35:57 +00:00
|
|
|
sx = CLAMP(sx, 0, src_width - 1);
|
|
|
|
sy = CLAMP(sy, 0, src_height - 1);
|
2019-04-19 18:54:33 +00:00
|
|
|
Color color = p_src_image->get_pixel(sx, sy);
|
|
|
|
if (p_transposed) {
|
|
|
|
SWAP(px, py);
|
|
|
|
}
|
|
|
|
px += p_offset.x;
|
|
|
|
py += p_offset.y;
|
|
|
|
|
|
|
|
//may have been cropped, so don't blit what is not visible?
|
|
|
|
if (px < 0 || px >= width) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (py < 0 || py >= height) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
p_image->set_pixel(px, py, color);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int xi = (xf < width ? int(xf) : width - 1); xi >= (xt > 0 ? xt : 0); xi--) {
|
|
|
|
int px = xi, py = yi;
|
|
|
|
int sx = px, sy = py;
|
2020-02-26 13:35:57 +00:00
|
|
|
sx = CLAMP(sx, 0, src_width - 1);
|
|
|
|
sy = CLAMP(sy, 0, src_height - 1);
|
2019-04-19 18:54:33 +00:00
|
|
|
Color color = p_src_image->get_pixel(sx, sy);
|
|
|
|
if (p_transposed) {
|
|
|
|
SWAP(px, py);
|
|
|
|
}
|
|
|
|
px += p_offset.x;
|
|
|
|
py += p_offset.y;
|
|
|
|
|
|
|
|
//may have been cropped, so don't blit what is not visible?
|
|
|
|
if (px < 0 || px >= width) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (py < 0 || py >= height) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
p_image->set_pixel(px, py, color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xf += dx_far;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (yi < y[1]) {
|
2019-04-19 18:54:33 +00:00
|
|
|
xt += dx_upper;
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2019-04-19 18:54:33 +00:00
|
|
|
xt += dx_low;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-04-19 18:54:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-17 06:33:00 +00:00
|
|
|
Error ResourceImporterTextureAtlas::import_group_file(const String &p_group_file, const Map<String, Map<StringName, Variant>> &p_source_file_options, const Map<String, String> &p_base_paths) {
|
2019-04-19 18:54:33 +00:00
|
|
|
ERR_FAIL_COND_V(p_source_file_options.size() == 0, ERR_BUG); //should never happen
|
|
|
|
|
|
|
|
Vector<EditorAtlasPacker::Chart> charts;
|
|
|
|
Vector<PackData> pack_data_files;
|
|
|
|
|
|
|
|
pack_data_files.resize(p_source_file_options.size());
|
|
|
|
|
|
|
|
int idx = 0;
|
2020-03-17 06:33:00 +00:00
|
|
|
for (const Map<String, Map<StringName, Variant>>::Element *E = p_source_file_options.front(); E; E = E->next(), idx++) {
|
2019-04-19 18:54:33 +00:00
|
|
|
PackData &pack_data = pack_data_files.write[idx];
|
2019-06-26 13:08:25 +00:00
|
|
|
const String &source = E->key();
|
2019-04-19 18:54:33 +00:00
|
|
|
const Map<StringName, Variant> &options = E->get();
|
|
|
|
|
|
|
|
Ref<Image> image;
|
|
|
|
image.instance();
|
|
|
|
Error err = ImageLoader::load_image(source, image);
|
|
|
|
ERR_CONTINUE(err != OK);
|
|
|
|
|
|
|
|
pack_data.image = image;
|
|
|
|
|
|
|
|
int mode = options["import_mode"];
|
|
|
|
|
|
|
|
if (mode == IMPORT_MODE_REGION) {
|
|
|
|
pack_data.is_mesh = false;
|
|
|
|
|
|
|
|
EditorAtlasPacker::Chart chart;
|
|
|
|
|
|
|
|
//clip a region from the image
|
|
|
|
Rect2 used_rect = image->get_used_rect();
|
|
|
|
pack_data.region = used_rect;
|
|
|
|
|
|
|
|
chart.vertices.push_back(used_rect.position);
|
|
|
|
chart.vertices.push_back(used_rect.position + Vector2(used_rect.size.x, 0));
|
|
|
|
chart.vertices.push_back(used_rect.position + Vector2(used_rect.size.x, used_rect.size.y));
|
|
|
|
chart.vertices.push_back(used_rect.position + Vector2(0, used_rect.size.y));
|
|
|
|
EditorAtlasPacker::Chart::Face f;
|
|
|
|
f.vertex[0] = 0;
|
|
|
|
f.vertex[1] = 1;
|
|
|
|
f.vertex[2] = 2;
|
|
|
|
chart.faces.push_back(f);
|
|
|
|
f.vertex[0] = 0;
|
|
|
|
f.vertex[1] = 2;
|
|
|
|
f.vertex[2] = 3;
|
|
|
|
chart.faces.push_back(f);
|
|
|
|
chart.can_transpose = false;
|
|
|
|
pack_data.chart_vertices.push_back(chart.vertices);
|
|
|
|
pack_data.chart_pieces.push_back(charts.size());
|
|
|
|
charts.push_back(chart);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
pack_data.is_mesh = true;
|
|
|
|
|
|
|
|
Ref<BitMap> bit_map;
|
|
|
|
bit_map.instance();
|
|
|
|
bit_map->create_from_image_alpha(image);
|
2020-03-17 06:33:00 +00:00
|
|
|
Vector<Vector<Vector2>> polygons = bit_map->clip_opaque_to_polygons(Rect2(0, 0, image->get_width(), image->get_height()));
|
2019-04-19 18:54:33 +00:00
|
|
|
|
|
|
|
for (int j = 0; j < polygons.size(); j++) {
|
|
|
|
EditorAtlasPacker::Chart chart;
|
|
|
|
chart.vertices = polygons[j];
|
|
|
|
chart.can_transpose = true;
|
|
|
|
|
2020-05-25 17:20:45 +00:00
|
|
|
Vector<int> poly = Geometry2D::triangulate_polygon(polygons[j]);
|
2019-04-19 18:54:33 +00:00
|
|
|
for (int i = 0; i < poly.size(); i += 3) {
|
|
|
|
EditorAtlasPacker::Chart::Face f;
|
|
|
|
f.vertex[0] = poly[i + 0];
|
|
|
|
f.vertex[1] = poly[i + 1];
|
|
|
|
f.vertex[2] = poly[i + 2];
|
|
|
|
chart.faces.push_back(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
pack_data.chart_pieces.push_back(charts.size());
|
|
|
|
charts.push_back(chart);
|
|
|
|
|
|
|
|
pack_data.chart_vertices.push_back(polygons[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//pack the charts
|
|
|
|
int atlas_width, atlas_height;
|
|
|
|
EditorAtlasPacker::chart_pack(charts, atlas_width, atlas_height);
|
|
|
|
|
|
|
|
//blit the atlas
|
|
|
|
Ref<Image> new_atlas;
|
|
|
|
new_atlas.instance();
|
|
|
|
new_atlas->create(atlas_width, atlas_height, false, Image::FORMAT_RGBA8);
|
|
|
|
|
|
|
|
for (int i = 0; i < pack_data_files.size(); i++) {
|
|
|
|
PackData &pack_data = pack_data_files.write[i];
|
2020-02-17 21:06:54 +00:00
|
|
|
|
2019-04-19 18:54:33 +00:00
|
|
|
for (int j = 0; j < pack_data.chart_pieces.size(); j++) {
|
|
|
|
const EditorAtlasPacker::Chart &chart = charts[pack_data.chart_pieces[j]];
|
|
|
|
for (int k = 0; k < chart.faces.size(); k++) {
|
2020-11-21 07:42:29 +00:00
|
|
|
Vector2i positions[3];
|
2019-04-19 18:54:33 +00:00
|
|
|
for (int l = 0; l < 3; l++) {
|
|
|
|
int vertex_idx = chart.faces[k].vertex[l];
|
2020-11-21 07:42:29 +00:00
|
|
|
positions[l] = Vector2i(chart.vertices[vertex_idx]);
|
2019-04-19 18:54:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-21 07:42:29 +00:00
|
|
|
_plot_triangle(positions, Vector2i(chart.final_offset), chart.transposed, new_atlas, pack_data.image);
|
2019-04-19 18:54:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//save the atlas
|
|
|
|
|
|
|
|
new_atlas->save_png(p_group_file);
|
|
|
|
|
|
|
|
//update cache if existing, else create
|
2019-06-11 18:43:37 +00:00
|
|
|
Ref<Texture2D> cache;
|
2019-04-19 18:54:33 +00:00
|
|
|
if (ResourceCache::has(p_group_file)) {
|
|
|
|
Resource *resptr = ResourceCache::get(p_group_file);
|
|
|
|
cache.reference_ptr(resptr);
|
|
|
|
} else {
|
|
|
|
Ref<ImageTexture> res_cache;
|
|
|
|
res_cache.instance();
|
|
|
|
res_cache->create_from_image(new_atlas);
|
|
|
|
res_cache->set_path(p_group_file);
|
|
|
|
cache = res_cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
//save the images
|
|
|
|
idx = 0;
|
2020-03-17 06:33:00 +00:00
|
|
|
for (const Map<String, Map<StringName, Variant>>::Element *E = p_source_file_options.front(); E; E = E->next(), idx++) {
|
2019-04-19 18:54:33 +00:00
|
|
|
PackData &pack_data = pack_data_files.write[idx];
|
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
Ref<Texture2D> texture;
|
2019-04-19 18:54:33 +00:00
|
|
|
|
|
|
|
if (!pack_data.is_mesh) {
|
|
|
|
Vector2 offset = charts[pack_data.chart_pieces[0]].vertices[0] + charts[pack_data.chart_pieces[0]].final_offset;
|
|
|
|
|
|
|
|
//region
|
|
|
|
Ref<AtlasTexture> atlas_texture;
|
|
|
|
atlas_texture.instance();
|
|
|
|
atlas_texture->set_atlas(cache);
|
|
|
|
atlas_texture->set_region(Rect2(offset, pack_data.region.size));
|
|
|
|
atlas_texture->set_margin(Rect2(pack_data.region.position, Size2(pack_data.image->get_width(), pack_data.image->get_height()) - pack_data.region.size));
|
|
|
|
|
|
|
|
texture = atlas_texture;
|
|
|
|
} else {
|
|
|
|
Ref<ArrayMesh> mesh;
|
|
|
|
mesh.instance();
|
|
|
|
|
|
|
|
for (int i = 0; i < pack_data.chart_pieces.size(); i++) {
|
|
|
|
const EditorAtlasPacker::Chart &chart = charts[pack_data.chart_pieces[i]];
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<Vector2> vertices;
|
|
|
|
Vector<int> indices;
|
|
|
|
Vector<Vector2> uvs;
|
2019-04-19 18:54:33 +00:00
|
|
|
int vc = chart.vertices.size();
|
|
|
|
int fc = chart.faces.size();
|
|
|
|
vertices.resize(vc);
|
|
|
|
uvs.resize(vc);
|
|
|
|
indices.resize(fc * 3);
|
|
|
|
|
|
|
|
{
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector2 *vw = vertices.ptrw();
|
|
|
|
int *iw = indices.ptrw();
|
|
|
|
Vector2 *uvw = uvs.ptrw();
|
2019-04-19 18:54:33 +00:00
|
|
|
|
|
|
|
for (int j = 0; j < vc; j++) {
|
|
|
|
vw[j] = chart.vertices[j];
|
|
|
|
Vector2 uv = chart.vertices[j];
|
|
|
|
if (chart.transposed) {
|
|
|
|
SWAP(uv.x, uv.y);
|
|
|
|
}
|
|
|
|
uv += chart.final_offset;
|
|
|
|
uv /= new_atlas->get_size(); //normalize uv to 0-1 range
|
|
|
|
uvw[j] = uv;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int j = 0; j < fc; j++) {
|
|
|
|
iw[j * 3 + 0] = chart.faces[j].vertex[0];
|
|
|
|
iw[j * 3 + 1] = chart.faces[j].vertex[1];
|
|
|
|
iw[j * 3 + 2] = chart.faces[j].vertex[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Array arrays;
|
|
|
|
arrays.resize(Mesh::ARRAY_MAX);
|
|
|
|
arrays[Mesh::ARRAY_VERTEX] = vertices;
|
|
|
|
arrays[Mesh::ARRAY_TEX_UV] = uvs;
|
|
|
|
arrays[Mesh::ARRAY_INDEX] = indices;
|
|
|
|
|
|
|
|
mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<MeshTexture> mesh_texture;
|
|
|
|
mesh_texture.instance();
|
|
|
|
mesh_texture->set_base_texture(cache);
|
|
|
|
mesh_texture->set_image_size(pack_data.image->get_size());
|
|
|
|
mesh_texture->set_mesh(mesh);
|
|
|
|
|
|
|
|
texture = mesh_texture;
|
|
|
|
//mesh
|
|
|
|
}
|
|
|
|
|
|
|
|
String save_path = p_base_paths[E->key()] + ".res";
|
|
|
|
ResourceSaver::save(save_path, texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResourceImporterTextureAtlas::ResourceImporterTextureAtlas() {
|
|
|
|
}
|