| 1 | /**************************************************************************/ |
| 2 | /* resource_importer_texture_atlas.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "resource_importer_texture_atlas.h" |
| 32 | |
| 33 | #include "atlas_import_failed.xpm" |
| 34 | #include "core/config/project_settings.h" |
| 35 | #include "core/io/image_loader.h" |
| 36 | #include "core/io/resource_saver.h" |
| 37 | #include "core/math/geometry_2d.h" |
| 38 | #include "editor/import/editor_atlas_packer.h" |
| 39 | #include "scene/resources/atlas_texture.h" |
| 40 | #include "scene/resources/bit_map.h" |
| 41 | #include "scene/resources/image_texture.h" |
| 42 | #include "scene/resources/mesh.h" |
| 43 | #include "scene/resources/mesh_texture.h" |
| 44 | |
| 45 | String ResourceImporterTextureAtlas::get_importer_name() const { |
| 46 | return "texture_atlas"; |
| 47 | } |
| 48 | |
| 49 | String ResourceImporterTextureAtlas::get_visible_name() const { |
| 50 | return "TextureAtlas"; |
| 51 | } |
| 52 | |
| 53 | void ResourceImporterTextureAtlas::get_recognized_extensions(List<String> *p_extensions) const { |
| 54 | ImageLoader::get_recognized_extensions(p_extensions); |
| 55 | } |
| 56 | |
| 57 | String ResourceImporterTextureAtlas::get_save_extension() const { |
| 58 | return "res"; |
| 59 | } |
| 60 | |
| 61 | String ResourceImporterTextureAtlas::get_resource_type() const { |
| 62 | return "Texture2D"; |
| 63 | } |
| 64 | |
| 65 | bool ResourceImporterTextureAtlas::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const { |
| 66 | if (p_option == "crop_to_region" && int(p_options["import_mode"]) != IMPORT_MODE_REGION) { |
| 67 | return false; |
| 68 | } else if (p_option == "trim_alpha_border_from_region" && int(p_options["import_mode"]) != IMPORT_MODE_REGION) { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | int ResourceImporterTextureAtlas::get_preset_count() const { |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | String ResourceImporterTextureAtlas::get_preset_name(int p_idx) const { |
| 80 | return String(); |
| 81 | } |
| 82 | |
| 83 | void ResourceImporterTextureAtlas::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const { |
| 84 | r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "atlas_file", PROPERTY_HINT_SAVE_FILE, "*.png"), "")); |
| 85 | r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "import_mode", PROPERTY_HINT_ENUM, "Region,Mesh2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), 0)); |
| 86 | r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "crop_to_region"), false)); |
| 87 | r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "trim_alpha_border_from_region"), true)); |
| 88 | } |
| 89 | |
| 90 | String ResourceImporterTextureAtlas::get_option_group_file() const { |
| 91 | return "atlas_file"; |
| 92 | } |
| 93 | |
| 94 | Error ResourceImporterTextureAtlas::import(ResourceUID::ID p_source_id, const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) { |
| 95 | /* If this happens, it's because the atlas_file field was not filled, so just import a broken texture */ |
| 96 | |
| 97 | //use an xpm because it's size independent, the editor images are vector and size dependent |
| 98 | //it's a simple hack |
| 99 | Ref<Image> broken = memnew(Image((const char **)atlas_import_failed_xpm)); |
| 100 | ResourceSaver::save(ImageTexture::create_from_image(broken), p_save_path + ".tex"); |
| 101 | |
| 102 | return OK; |
| 103 | } |
| 104 | |
| 105 | // FIXME: Rasterization has issues, see https://github.com/godotengine/godot/issues/68350#issuecomment-1305610290 |
| 106 | static void _plot_triangle(Vector2i *p_vertices, const Vector2i &p_offset, bool p_transposed, Ref<Image> p_image, const Ref<Image> &p_src_image) { |
| 107 | int width = p_image->get_width(); |
| 108 | int height = p_image->get_height(); |
| 109 | int src_width = p_src_image->get_width(); |
| 110 | int src_height = p_src_image->get_height(); |
| 111 | |
| 112 | int x[3]; |
| 113 | int y[3]; |
| 114 | |
| 115 | for (int j = 0; j < 3; j++) { |
| 116 | x[j] = p_vertices[j].x; |
| 117 | y[j] = p_vertices[j].y; |
| 118 | } |
| 119 | |
| 120 | // sort the points vertically |
| 121 | if (y[1] > y[2]) { |
| 122 | SWAP(x[1], x[2]); |
| 123 | SWAP(y[1], y[2]); |
| 124 | } |
| 125 | if (y[0] > y[1]) { |
| 126 | SWAP(x[0], x[1]); |
| 127 | SWAP(y[0], y[1]); |
| 128 | } |
| 129 | if (y[1] > y[2]) { |
| 130 | SWAP(x[1], x[2]); |
| 131 | SWAP(y[1], y[2]); |
| 132 | } |
| 133 | |
| 134 | double dx_far = double(x[2] - x[0]) / (y[2] - y[0] + 1); |
| 135 | double dx_upper = double(x[1] - x[0]) / (y[1] - y[0] + 1); |
| 136 | double dx_low = double(x[2] - x[1]) / (y[2] - y[1] + 1); |
| 137 | double xf = x[0]; |
| 138 | double xt = x[0] + dx_upper; // if y[0] == y[1], special case |
| 139 | int max_y = MIN(y[2], p_transposed ? (width - p_offset.x - 1) : (height - p_offset.y - 1)); |
| 140 | for (int yi = y[0]; yi < max_y; yi++) { |
| 141 | if (yi >= 0) { |
| 142 | for (int xi = (xf > 0 ? int(xf) : 0); xi < (xt <= src_width ? xt : src_width); xi++) { |
| 143 | int px = xi, py = yi; |
| 144 | int sx = px, sy = py; |
| 145 | sx = CLAMP(sx, 0, src_width - 1); |
| 146 | sy = CLAMP(sy, 0, src_height - 1); |
| 147 | Color color = p_src_image->get_pixel(sx, sy); |
| 148 | if (p_transposed) { |
| 149 | SWAP(px, py); |
| 150 | } |
| 151 | px += p_offset.x; |
| 152 | py += p_offset.y; |
| 153 | |
| 154 | //may have been cropped, so don't blit what is not visible? |
| 155 | if (px < 0 || px >= width) { |
| 156 | continue; |
| 157 | } |
| 158 | if (py < 0 || py >= height) { |
| 159 | continue; |
| 160 | } |
| 161 | p_image->set_pixel(px, py, color); |
| 162 | } |
| 163 | |
| 164 | for (int xi = (xf < src_width ? int(xf) : src_width - 1); xi >= (xt > 0 ? xt : 0); xi--) { |
| 165 | int px = xi, py = yi; |
| 166 | int sx = px, sy = py; |
| 167 | sx = CLAMP(sx, 0, src_width - 1); |
| 168 | sy = CLAMP(sy, 0, src_height - 1); |
| 169 | Color color = p_src_image->get_pixel(sx, sy); |
| 170 | if (p_transposed) { |
| 171 | SWAP(px, py); |
| 172 | } |
| 173 | px += p_offset.x; |
| 174 | py += p_offset.y; |
| 175 | |
| 176 | //may have been cropped, so don't blit what is not visible? |
| 177 | if (px < 0 || px >= width) { |
| 178 | continue; |
| 179 | } |
| 180 | if (py < 0 || py >= height) { |
| 181 | continue; |
| 182 | } |
| 183 | p_image->set_pixel(px, py, color); |
| 184 | } |
| 185 | } |
| 186 | xf += dx_far; |
| 187 | if (yi < y[1]) { |
| 188 | xt += dx_upper; |
| 189 | } else { |
| 190 | xt += dx_low; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | Error ResourceImporterTextureAtlas::import_group_file(const String &p_group_file, const HashMap<String, HashMap<StringName, Variant>> &p_source_file_options, const HashMap<String, String> &p_base_paths) { |
| 196 | ERR_FAIL_COND_V(p_source_file_options.is_empty(), ERR_BUG); //should never happen |
| 197 | |
| 198 | Vector<EditorAtlasPacker::Chart> charts; |
| 199 | Vector<PackData> pack_data_files; |
| 200 | |
| 201 | pack_data_files.resize(p_source_file_options.size()); |
| 202 | |
| 203 | int idx = 0; |
| 204 | for (const KeyValue<String, HashMap<StringName, Variant>> &E : p_source_file_options) { |
| 205 | PackData &pack_data = pack_data_files.write[idx]; |
| 206 | const String &source = E.key; |
| 207 | const HashMap<StringName, Variant> &options = E.value; |
| 208 | |
| 209 | Ref<Image> image; |
| 210 | image.instantiate(); |
| 211 | Error err = ImageLoader::load_image(source, image); |
| 212 | ERR_CONTINUE(err != OK); |
| 213 | |
| 214 | pack_data.image = image; |
| 215 | |
| 216 | int mode = options["import_mode"]; |
| 217 | |
| 218 | if (mode == IMPORT_MODE_REGION) { |
| 219 | pack_data.is_mesh = false; |
| 220 | pack_data.is_cropped = options["crop_to_region"]; |
| 221 | |
| 222 | EditorAtlasPacker::Chart chart; |
| 223 | |
| 224 | Rect2i used_rect = Rect2i(Vector2i(), image->get_size()); |
| 225 | if (options["trim_alpha_border_from_region"]) { |
| 226 | // Clip a region from the image. |
| 227 | used_rect = image->get_used_rect(); |
| 228 | } |
| 229 | pack_data.region = used_rect; |
| 230 | |
| 231 | chart.vertices.push_back(used_rect.position); |
| 232 | chart.vertices.push_back(used_rect.position + Vector2i(used_rect.size.x, 0)); |
| 233 | chart.vertices.push_back(used_rect.position + Vector2i(used_rect.size.x, used_rect.size.y)); |
| 234 | chart.vertices.push_back(used_rect.position + Vector2i(0, used_rect.size.y)); |
| 235 | EditorAtlasPacker::Chart::Face f; |
| 236 | f.vertex[0] = 0; |
| 237 | f.vertex[1] = 1; |
| 238 | f.vertex[2] = 2; |
| 239 | chart.faces.push_back(f); |
| 240 | f.vertex[0] = 0; |
| 241 | f.vertex[1] = 2; |
| 242 | f.vertex[2] = 3; |
| 243 | chart.faces.push_back(f); |
| 244 | chart.can_transpose = false; |
| 245 | pack_data.chart_vertices.push_back(chart.vertices); |
| 246 | pack_data.chart_pieces.push_back(charts.size()); |
| 247 | charts.push_back(chart); |
| 248 | |
| 249 | } else { |
| 250 | pack_data.is_mesh = true; |
| 251 | |
| 252 | Ref<BitMap> bit_map; |
| 253 | bit_map.instantiate(); |
| 254 | bit_map->create_from_image_alpha(image); |
| 255 | Vector<Vector<Vector2>> polygons = bit_map->clip_opaque_to_polygons(Rect2(Vector2(), image->get_size())); |
| 256 | |
| 257 | for (int j = 0; j < polygons.size(); j++) { |
| 258 | EditorAtlasPacker::Chart chart; |
| 259 | chart.vertices = polygons[j]; |
| 260 | chart.can_transpose = true; |
| 261 | |
| 262 | Vector<int> poly = Geometry2D::triangulate_polygon(polygons[j]); |
| 263 | for (int i = 0; i < poly.size(); i += 3) { |
| 264 | EditorAtlasPacker::Chart::Face f; |
| 265 | f.vertex[0] = poly[i + 0]; |
| 266 | f.vertex[1] = poly[i + 1]; |
| 267 | f.vertex[2] = poly[i + 2]; |
| 268 | chart.faces.push_back(f); |
| 269 | } |
| 270 | |
| 271 | pack_data.chart_pieces.push_back(charts.size()); |
| 272 | charts.push_back(chart); |
| 273 | |
| 274 | pack_data.chart_vertices.push_back(polygons[j]); |
| 275 | } |
| 276 | } |
| 277 | idx++; |
| 278 | } |
| 279 | |
| 280 | const int max_width = (int)GLOBAL_GET("editor/import/atlas_max_width"); |
| 281 | |
| 282 | //pack the charts |
| 283 | int atlas_width, atlas_height; |
| 284 | EditorAtlasPacker::chart_pack(charts, atlas_width, atlas_height, max_width); |
| 285 | |
| 286 | if (atlas_height > max_width * 2) { |
| 287 | WARN_PRINT(vformat(TTR("%s: Atlas texture significantly larger on one axis (%d), consider changing the `editor/import/atlas_max_width` Project Setting to allow a wider texture, making the result more even in size."), p_group_file, atlas_height)); |
| 288 | } |
| 289 | |
| 290 | //blit the atlas |
| 291 | Ref<Image> new_atlas = Image::create_empty(atlas_width, atlas_height, false, Image::FORMAT_RGBA8); |
| 292 | |
| 293 | for (int i = 0; i < pack_data_files.size(); i++) { |
| 294 | PackData &pack_data = pack_data_files.write[i]; |
| 295 | |
| 296 | for (int j = 0; j < pack_data.chart_pieces.size(); j++) { |
| 297 | const EditorAtlasPacker::Chart &chart = charts[pack_data.chart_pieces[j]]; |
| 298 | for (int k = 0; k < chart.faces.size(); k++) { |
| 299 | Vector2i positions[3]; |
| 300 | for (int l = 0; l < 3; l++) { |
| 301 | int vertex_idx = chart.faces[k].vertex[l]; |
| 302 | positions[l] = Vector2i(chart.vertices[vertex_idx]); |
| 303 | } |
| 304 | |
| 305 | _plot_triangle(positions, Vector2i(chart.final_offset), chart.transposed, new_atlas, pack_data.image); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | //save the atlas |
| 311 | |
| 312 | new_atlas->save_png(p_group_file); |
| 313 | |
| 314 | //update cache if existing, else create |
| 315 | Ref<Texture2D> cache; |
| 316 | cache = ResourceCache::get_ref(p_group_file); |
| 317 | if (cache.is_null()) { |
| 318 | Ref<ImageTexture> res_cache = ImageTexture::create_from_image(new_atlas); |
| 319 | res_cache->set_path(p_group_file); |
| 320 | cache = res_cache; |
| 321 | } |
| 322 | |
| 323 | //save the images |
| 324 | idx = 0; |
| 325 | for (const KeyValue<String, HashMap<StringName, Variant>> &E : p_source_file_options) { |
| 326 | PackData &pack_data = pack_data_files.write[idx]; |
| 327 | |
| 328 | Ref<Texture2D> texture; |
| 329 | |
| 330 | if (!pack_data.is_mesh) { |
| 331 | Vector2 offset = charts[pack_data.chart_pieces[0]].vertices[0] + charts[pack_data.chart_pieces[0]].final_offset; |
| 332 | |
| 333 | //region |
| 334 | Ref<AtlasTexture> atlas_texture; |
| 335 | atlas_texture.instantiate(); |
| 336 | atlas_texture->set_atlas(cache); |
| 337 | atlas_texture->set_region(Rect2(offset, pack_data.region.size)); |
| 338 | |
| 339 | if (!pack_data.is_cropped) { |
| 340 | atlas_texture->set_margin(Rect2(pack_data.region.position, pack_data.image->get_size() - pack_data.region.size)); |
| 341 | } |
| 342 | |
| 343 | texture = atlas_texture; |
| 344 | } else { |
| 345 | Ref<ArrayMesh> mesh; |
| 346 | mesh.instantiate(); |
| 347 | |
| 348 | for (int i = 0; i < pack_data.chart_pieces.size(); i++) { |
| 349 | const EditorAtlasPacker::Chart &chart = charts[pack_data.chart_pieces[i]]; |
| 350 | Vector<Vector2> vertices; |
| 351 | Vector<int> indices; |
| 352 | Vector<Vector2> uvs; |
| 353 | int vc = chart.vertices.size(); |
| 354 | int fc = chart.faces.size(); |
| 355 | vertices.resize(vc); |
| 356 | uvs.resize(vc); |
| 357 | indices.resize(fc * 3); |
| 358 | |
| 359 | { |
| 360 | Vector2 *vw = vertices.ptrw(); |
| 361 | int *iw = indices.ptrw(); |
| 362 | Vector2 *uvw = uvs.ptrw(); |
| 363 | |
| 364 | for (int j = 0; j < vc; j++) { |
| 365 | vw[j] = chart.vertices[j]; |
| 366 | Vector2 uv = chart.vertices[j]; |
| 367 | if (chart.transposed) { |
| 368 | SWAP(uv.x, uv.y); |
| 369 | } |
| 370 | uv += chart.final_offset; |
| 371 | uv /= new_atlas->get_size(); //normalize uv to 0-1 range |
| 372 | uvw[j] = uv; |
| 373 | } |
| 374 | |
| 375 | for (int j = 0; j < fc; j++) { |
| 376 | iw[j * 3 + 0] = chart.faces[j].vertex[0]; |
| 377 | iw[j * 3 + 1] = chart.faces[j].vertex[1]; |
| 378 | iw[j * 3 + 2] = chart.faces[j].vertex[2]; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | Array arrays; |
| 383 | arrays.resize(Mesh::ARRAY_MAX); |
| 384 | arrays[Mesh::ARRAY_VERTEX] = vertices; |
| 385 | arrays[Mesh::ARRAY_TEX_UV] = uvs; |
| 386 | arrays[Mesh::ARRAY_INDEX] = indices; |
| 387 | |
| 388 | mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays); |
| 389 | } |
| 390 | |
| 391 | Ref<MeshTexture> mesh_texture; |
| 392 | mesh_texture.instantiate(); |
| 393 | mesh_texture->set_base_texture(cache); |
| 394 | mesh_texture->set_image_size(pack_data.image->get_size()); |
| 395 | mesh_texture->set_mesh(mesh); |
| 396 | |
| 397 | texture = mesh_texture; |
| 398 | } |
| 399 | |
| 400 | String save_path = p_base_paths[E.key] + ".res"; |
| 401 | ResourceSaver::save(texture, save_path); |
| 402 | idx++; |
| 403 | } |
| 404 | |
| 405 | return OK; |
| 406 | } |