editor_texture_import_plugin.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  1. /*************************************************************************/
  2. /* editor_texture_import_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "editor_texture_import_plugin.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_settings.h"
  33. #include "editor_atlas.h"
  34. #include "globals.h"
  35. #include "io/image_loader.h"
  36. #include "io/marshalls.h"
  37. #include "io/resource_saver.h"
  38. #include "scene/gui/button_group.h"
  39. #include "scene/gui/check_button.h"
  40. #include "scene/gui/margin_container.h"
  41. #include "scene/io/resource_format_image.h"
  42. #include "thirdparty/misc/md5.h"
  43. static const char *flag_names[] = {
  44. ("Streaming Format"),
  45. ("Fix Border Alpha"),
  46. ("Alpha Bit Hint"),
  47. ("Compress Extra (PVRTC2)"),
  48. ("No MipMaps"),
  49. ("Repeat"),
  50. ("Filter (Magnifying)"),
  51. ("Premultiply Alpha"),
  52. ("Convert SRGB->Linear"),
  53. ("Convert NormalMap to XY"),
  54. ("Use Anisotropy"),
  55. NULL
  56. };
  57. #if 0 // not used
  58. static const char *flag_short_names[]={
  59. "Stream",
  60. "FixBorder",
  61. "AlphBit",
  62. "ExtComp",
  63. "NoMipMap",
  64. "Repeat",
  65. "Filter",
  66. "PMAlpha",
  67. "ToLinear",
  68. "ToRG",
  69. "Anisoropic",
  70. NULL
  71. };
  72. #endif
  73. void EditorImportTextureOptions::set_format(EditorTextureImportPlugin::ImageFormat p_format) {
  74. updating = true;
  75. format->select(p_format);
  76. if (p_format == EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_DISK_LOSSY) {
  77. quality_vb->show();
  78. } else {
  79. quality_vb->hide();
  80. }
  81. updating = false;
  82. }
  83. EditorTextureImportPlugin::ImageFormat EditorImportTextureOptions::get_format() const {
  84. return (EditorTextureImportPlugin::ImageFormat)format->get_selected();
  85. }
  86. void EditorImportTextureOptions::set_flags(uint32_t p_flags) {
  87. updating = true;
  88. for (int i = 0; i < items.size(); i++) {
  89. items[i]->set_checked(0, p_flags & (1 << i));
  90. }
  91. updating = false;
  92. }
  93. void EditorImportTextureOptions::set_quality(float p_quality) {
  94. quality->set_val(p_quality);
  95. }
  96. float EditorImportTextureOptions::get_quality() const {
  97. return quality->get_val();
  98. }
  99. uint32_t EditorImportTextureOptions::get_flags() const {
  100. uint32_t f = 0;
  101. for (int i = 0; i < items.size(); i++) {
  102. if (items[i]->is_checked(0))
  103. f |= (1 << i);
  104. }
  105. return f;
  106. }
  107. void EditorImportTextureOptions::_changedp(int p_value) {
  108. _changed();
  109. }
  110. void EditorImportTextureOptions::_changed() {
  111. if (updating)
  112. return;
  113. if (format->get_selected() == EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_DISK_LOSSY) {
  114. quality_vb->show();
  115. } else {
  116. quality_vb->hide();
  117. }
  118. emit_signal("changed");
  119. }
  120. void EditorImportTextureOptions::_bind_methods() {
  121. ObjectTypeDB::bind_method("_changed", &EditorImportTextureOptions::_changed);
  122. ObjectTypeDB::bind_method("_changedp", &EditorImportTextureOptions::_changedp);
  123. ADD_SIGNAL(MethodInfo("changed"));
  124. }
  125. void EditorImportTextureOptions::_notification(int p_what) {
  126. if (p_what == NOTIFICATION_ENTER_TREE) {
  127. flags->connect("item_edited", this, "_changed");
  128. format->connect("item_selected", this, "_changedp");
  129. }
  130. }
  131. void EditorImportTextureOptions::show_2d_notice() {
  132. //notice_for_2d->show();
  133. }
  134. EditorImportTextureOptions::EditorImportTextureOptions() {
  135. add_constant_override("separation", 3);
  136. updating = false;
  137. format = memnew(OptionButton);
  138. format->add_item(TTR("Uncompressed"), EditorTextureImportPlugin::IMAGE_FORMAT_UNCOMPRESSED);
  139. format->add_item(TTR("Compress Lossless (PNG)"), EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_DISK_LOSSLESS);
  140. format->add_item(TTR("Compress Lossy (WebP)"), EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_DISK_LOSSY);
  141. format->add_item(TTR("Compress (VRAM)"), EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_RAM);
  142. add_margin_child(TTR("Texture Format"), format);
  143. quality_vb = memnew(VBoxContainer);
  144. HBoxContainer *quality_hb = memnew(HBoxContainer);
  145. HSlider *hs = memnew(HSlider);
  146. hs->set_h_size_flags(SIZE_EXPAND_FILL);
  147. hs->set_stretch_ratio(0.8);
  148. quality_hb->add_child(hs);
  149. quality_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  150. SpinBox *sb = memnew(SpinBox);
  151. sb->set_h_size_flags(SIZE_EXPAND_FILL);
  152. sb->set_stretch_ratio(0.2);
  153. quality_hb->add_child(sb);
  154. sb->share(hs);
  155. hs->set_min(0);
  156. hs->set_max(1.0);
  157. hs->set_step(0.01);
  158. hs->set_val(0.7);
  159. quality = hs;
  160. quality_vb->add_margin_child(TTR("Texture Compression Quality (WebP):"), quality_hb);
  161. add_child(quality_vb);
  162. flags = memnew(Tree);
  163. flags->set_hide_root(true);
  164. TreeItem *root = flags->create_item();
  165. const char **fname = flag_names;
  166. while (*fname) {
  167. TreeItem *ti = flags->create_item(root);
  168. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  169. ti->set_text(0, *fname);
  170. ti->set_editable(0, true);
  171. items.push_back(ti);
  172. fname++;
  173. }
  174. add_margin_child(TTR("Texture Options"), flags, true);
  175. }
  176. ///////////////////////////////////////////////////////////
  177. class EditorTextureImportDialog : public ConfirmationDialog {
  178. OBJ_TYPE(EditorTextureImportDialog, ConfirmationDialog);
  179. HBoxContainer *mode_hb;
  180. CheckBox *mode_check[EditorTextureImportPlugin::MODE_MAX];
  181. EditorImportTextureOptions *texture_options;
  182. EditorTextureImportPlugin::Mode mode;
  183. //EditorNode *editor;
  184. LineEdit *import_path;
  185. LineEdit *save_path;
  186. EditorFileDialog *file_select;
  187. EditorFileDialog *save_file_select;
  188. EditorDirDialog *save_select;
  189. OptionButton *texture_action;
  190. ConfirmationDialog *error_dialog;
  191. CheckButton *crop_source;
  192. SpinBox *size;
  193. MarginContainer *size_mc;
  194. Label *size_label;
  195. Label *source_label;
  196. Label *notice_for_2d;
  197. EditorTextureImportPlugin *plugin;
  198. void _mode_changed(int p_mode);
  199. void _choose_files(const Vector<String> &p_path);
  200. void _choose_file(const String &p_path);
  201. void _choose_save_dir(const String &p_path);
  202. void _browse();
  203. void _browse_target();
  204. void _import();
  205. protected:
  206. void _notification(int p_what);
  207. static void _bind_methods();
  208. public:
  209. void setup_multiple_import_3d(const Vector<String> &p_path, const String &p_dest) {
  210. _mode_changed(EditorTextureImportPlugin::MODE_TEXTURE_3D);
  211. _choose_files(p_path);
  212. _choose_save_dir(p_dest);
  213. }
  214. void add_sources_and_dest(const Vector<String> &p_path, const String &p_dest) {
  215. _choose_files(p_path);
  216. _choose_save_dir(p_dest);
  217. }
  218. Error import(const String &p_from, const String &p_to, const String &p_preset);
  219. void popup_import(const String &p_from = String());
  220. EditorTextureImportDialog(EditorTextureImportPlugin *p_plugin = NULL);
  221. };
  222. /////////////////////////////////////////////////////////
  223. void EditorTextureImportDialog::_choose_files(const Vector<String> &p_path) {
  224. String files;
  225. for (int i = 0; i < p_path.size(); i++) {
  226. if (i > 0)
  227. files += ",";
  228. files += p_path[i];
  229. }
  230. /*
  231. if (p_path.size()) {
  232. String srctex=p_path[0];
  233. String ipath = EditorImportDB::get_singleton()->find_source_path(srctex);
  234. if (ipath!="")
  235. save_path->set_text(ipath.get_base_dir());
  236. }*/
  237. import_path->set_text(files);
  238. }
  239. void EditorTextureImportDialog::_choose_file(const String &p_path) {
  240. import_path->set_text(p_path);
  241. }
  242. void EditorTextureImportDialog::_choose_save_dir(const String &p_path) {
  243. save_path->set_text(p_path);
  244. }
  245. void EditorTextureImportDialog::_import() {
  246. // ImportMonitorBlock imb;
  247. Vector<String> files = import_path->get_text().split(",");
  248. if (!files.size()) {
  249. error_dialog->set_text(TTR("Please specify some files!"));
  250. error_dialog->popup_centered(Size2(200, 100) * EDSCALE);
  251. return;
  252. }
  253. String dst_path = save_path->get_text();
  254. if (save_path->get_text().strip_edges() == "") {
  255. error_dialog->set_text(TTR("Target path is empty."));
  256. error_dialog->popup_centered_minsize();
  257. return;
  258. }
  259. if (!save_path->get_text().begins_with("res://")) {
  260. error_dialog->set_text(TTR("Target path must be a complete resource path."));
  261. error_dialog->popup_centered_minsize();
  262. return;
  263. }
  264. if (mode != EditorTextureImportPlugin::MODE_ATLAS && mode != EditorTextureImportPlugin::MODE_LARGE && !DirAccess::exists(save_path->get_text())) {
  265. error_dialog->set_text(TTR("Target path must exist."));
  266. error_dialog->popup_centered_minsize();
  267. return;
  268. }
  269. if (mode == EditorTextureImportPlugin::MODE_ATLAS) { //atlas
  270. if (files.size() == 0) {
  271. error_dialog->set_text(TTR("At least one file needed for Atlas."));
  272. error_dialog->popup_centered(Size2(200, 100) * EDSCALE);
  273. return;
  274. }
  275. String dst_file = dst_path;
  276. //dst_file=dst_file.basename()+".tex";
  277. Ref<ResourceImportMetadata> imd = memnew(ResourceImportMetadata);
  278. //imd->set_editor();
  279. for (int i = 0; i < files.size(); i++) {
  280. imd->add_source(EditorImportPlugin::validate_source_path(files[i]));
  281. }
  282. imd->set_option("format", texture_options->get_format());
  283. imd->set_option("flags", texture_options->get_flags());
  284. imd->set_option("quality", texture_options->get_quality());
  285. imd->set_option("atlas", true);
  286. imd->set_option("atlas_size", int(size->get_val()));
  287. imd->set_option("large", false);
  288. imd->set_option("crop", crop_source->is_pressed());
  289. imd->set_option("mode", mode);
  290. Error err = plugin->import(dst_file, imd);
  291. if (err) {
  292. error_dialog->set_text(TTR("Error importing:") + " " + dst_file.get_file());
  293. error_dialog->popup_centered(Size2(200, 100) * EDSCALE);
  294. return;
  295. }
  296. } else if (mode == EditorTextureImportPlugin::MODE_LARGE) { //large
  297. if (files.size() != 1) {
  298. error_dialog->set_text(TTR("Only one file is required for large texture."));
  299. error_dialog->popup_centered(Size2(200, 100) * EDSCALE);
  300. return;
  301. }
  302. String dst_file = dst_path;
  303. //dst_file=dst_file.basename()+".tex";
  304. Ref<ResourceImportMetadata> imd = memnew(ResourceImportMetadata);
  305. //imd->set_editor();
  306. for (int i = 0; i < files.size(); i++) {
  307. imd->add_source(EditorImportPlugin::validate_source_path(files[i]));
  308. }
  309. imd->set_option("format", texture_options->get_format());
  310. imd->set_option("flags", texture_options->get_flags());
  311. imd->set_option("quality", texture_options->get_quality());
  312. imd->set_option("atlas", false);
  313. imd->set_option("large", true);
  314. imd->set_option("large_cell_size", int(size->get_val()));
  315. imd->set_option("crop", crop_source->is_pressed());
  316. imd->set_option("mode", mode);
  317. Error err = plugin->import(dst_file, imd);
  318. if (err) {
  319. error_dialog->set_text(TTR("Error importing:") + " " + dst_file.get_file());
  320. error_dialog->popup_centered(Size2(200, 100) * EDSCALE);
  321. return;
  322. }
  323. } else {
  324. for (int i = 0; i < files.size(); i++) {
  325. String dst_file = dst_path.plus_file(files[i].get_file());
  326. dst_file = dst_file.basename() + ".tex";
  327. Ref<ResourceImportMetadata> imd = memnew(ResourceImportMetadata);
  328. //imd->set_editor();
  329. imd->add_source(EditorImportPlugin::validate_source_path(files[i]));
  330. imd->set_option("format", texture_options->get_format());
  331. imd->set_option("flags", texture_options->get_flags());
  332. imd->set_option("quality", texture_options->get_quality());
  333. imd->set_option("atlas", false);
  334. imd->set_option("large", false);
  335. imd->set_option("mode", mode);
  336. Error err = plugin->import(dst_file, imd);
  337. if (err) {
  338. error_dialog->set_text(TTR("Error importing:") + " " + dst_file.get_file());
  339. error_dialog->popup_centered(Size2(200, 100) * EDSCALE);
  340. return;
  341. }
  342. }
  343. }
  344. hide();
  345. }
  346. void EditorTextureImportDialog::_browse() {
  347. file_select->popup_centered_ratio();
  348. }
  349. void EditorTextureImportDialog::_browse_target() {
  350. if (mode == EditorTextureImportPlugin::MODE_ATLAS || mode == EditorTextureImportPlugin::MODE_LARGE) {
  351. save_file_select->popup_centered_ratio();
  352. } else {
  353. save_select->popup_centered_ratio();
  354. }
  355. }
  356. void EditorTextureImportDialog::popup_import(const String &p_from) {
  357. popup_centered(Size2(600, 500) * EDSCALE);
  358. if (p_from != "") {
  359. Ref<ResourceImportMetadata> rimd = ResourceLoader::load_import_metadata(p_from);
  360. ERR_FAIL_COND(!rimd.is_valid());
  361. if (rimd->has_option("mode")) {
  362. //new imported stuff uses this option
  363. _mode_changed(rimd->get_option("mode"));
  364. } else {
  365. //this one is for compatibility, will have to guess it
  366. if (rimd->has_option("atlas") && rimd->get_option("atlas")) {
  367. _mode_changed(EditorTextureImportPlugin::MODE_ATLAS);
  368. } else if (rimd->has_option("large") && rimd->get_option("large")) {
  369. _mode_changed(EditorTextureImportPlugin::MODE_LARGE);
  370. } else {
  371. //guess by usage of mipmaps..?
  372. _mode_changed(EditorTextureImportPlugin::MODE_TEXTURE_2D);
  373. }
  374. }
  375. if (mode == EditorTextureImportPlugin::MODE_ATLAS || mode == EditorTextureImportPlugin::MODE_LARGE)
  376. save_path->set_text(p_from);
  377. else
  378. save_path->set_text(p_from.get_base_dir());
  379. texture_options->set_format(EditorTextureImportPlugin::ImageFormat(int(rimd->get_option("format"))));
  380. texture_options->set_flags(rimd->get_option("flags"));
  381. texture_options->set_quality(rimd->get_option("quality"));
  382. String src = "";
  383. for (int i = 0; i < rimd->get_source_count(); i++) {
  384. if (i > 0)
  385. src += ",";
  386. src += EditorImportPlugin::expand_source_path(rimd->get_source_path(i));
  387. }
  388. import_path->set_text(src);
  389. }
  390. }
  391. void EditorTextureImportDialog::_notification(int p_what) {
  392. if (p_what == NOTIFICATION_ENTER_TREE) {
  393. List<String> extensions;
  394. ImageLoader::get_recognized_extensions(&extensions);
  395. // ResourceLoader::get_recognized_extensions_for_type("PackedTexture",&extensions);
  396. file_select->clear_filters();
  397. for (int i = 0; i < extensions.size(); i++) {
  398. file_select->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  399. }
  400. }
  401. }
  402. Error EditorTextureImportDialog::import(const String &p_from, const String &p_to, const String &p_preset) {
  403. import_path->set_text(p_from);
  404. save_path->set_text(p_to);
  405. _import();
  406. return OK;
  407. }
  408. void EditorTextureImportDialog::_mode_changed(int p_mode) {
  409. mode = EditorTextureImportPlugin::Mode(p_mode);
  410. for (int i = 0; i < EditorTextureImportPlugin::MODE_MAX; i++) {
  411. mode_check[i]->set_pressed(i == mode);
  412. }
  413. if (p_mode == EditorTextureImportPlugin::MODE_ATLAS) {
  414. size_label->set_text(TTR("Max Texture Size:"));
  415. size->set_val(2048);
  416. crop_source->show();
  417. size_label->show();
  418. size->show();
  419. texture_options->set_flags(EditorTextureImportPlugin::IMAGE_FLAG_FIX_BORDER_ALPHA | EditorTextureImportPlugin::IMAGE_FLAG_NO_MIPMAPS | EditorTextureImportPlugin::IMAGE_FLAG_FILTER);
  420. texture_options->set_quality(0.7);
  421. texture_options->set_format(EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_DISK_LOSSY);
  422. set_title(TTR("Import Textures for Atlas (2D)"));
  423. } else {
  424. crop_source->hide();
  425. }
  426. if (p_mode == EditorTextureImportPlugin::MODE_LARGE) {
  427. size_label->set_text(TTR("Cell Size:"));
  428. size->set_val(256);
  429. size_label->show();
  430. size->show();
  431. file_select->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  432. save_file_select->add_filter("*.ltex;" + TTR("Large Texture"));
  433. texture_options->set_flags(EditorTextureImportPlugin::IMAGE_FLAG_FIX_BORDER_ALPHA | EditorTextureImportPlugin::IMAGE_FLAG_NO_MIPMAPS | EditorTextureImportPlugin::IMAGE_FLAG_FILTER);
  434. texture_options->set_quality(0.7);
  435. texture_options->set_format(EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_DISK_LOSSLESS);
  436. set_title(TTR("Import Large Textures (2D)"));
  437. source_label->set_text(TTR("Source Texture"));
  438. } else {
  439. file_select->set_mode(EditorFileDialog::MODE_OPEN_FILES);
  440. save_file_select->add_filter("*.tex;" + TTR("Base Atlas Texture"));
  441. source_label->set_text(TTR("Source Texture(s)"));
  442. }
  443. if (p_mode == EditorTextureImportPlugin::MODE_TEXTURE_2D) {
  444. size_label->hide();
  445. size->hide();
  446. texture_options->set_flags(EditorTextureImportPlugin::IMAGE_FLAG_NO_MIPMAPS | EditorTextureImportPlugin::IMAGE_FLAG_FIX_BORDER_ALPHA | EditorTextureImportPlugin::IMAGE_FLAG_FILTER);
  447. texture_options->set_quality(0.7);
  448. texture_options->set_format(EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_DISK_LOSSY);
  449. notice_for_2d->show();
  450. set_title(TTR("Import Textures for 2D"));
  451. } else {
  452. notice_for_2d->hide();
  453. }
  454. if (p_mode == EditorTextureImportPlugin::MODE_TEXTURE_3D) {
  455. size_label->hide();
  456. size->hide();
  457. //texture_options->set_flags(EditorTextureImportPlugin::IMAGE_FLAG_);
  458. //texture_options->set_flags(EditorTextureImportPlugin::IMAGE_FLAG_NO_MIPMAPS);
  459. texture_options->set_flags(EditorTextureImportPlugin::IMAGE_FLAG_FIX_BORDER_ALPHA | EditorTextureImportPlugin::IMAGE_FLAG_FILTER | EditorTextureImportPlugin::IMAGE_FLAG_REPEAT);
  460. texture_options->set_format(EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_RAM);
  461. set_title(TTR("Import Textures for 3D"));
  462. }
  463. }
  464. void EditorTextureImportDialog::_bind_methods() {
  465. ObjectTypeDB::bind_method("_choose_files", &EditorTextureImportDialog::_choose_files);
  466. ObjectTypeDB::bind_method("_choose_file", &EditorTextureImportDialog::_choose_file);
  467. ObjectTypeDB::bind_method("_choose_save_dir", &EditorTextureImportDialog::_choose_save_dir);
  468. ObjectTypeDB::bind_method("_import", &EditorTextureImportDialog::_import);
  469. ObjectTypeDB::bind_method("_browse", &EditorTextureImportDialog::_browse);
  470. ObjectTypeDB::bind_method("_browse_target", &EditorTextureImportDialog::_browse_target);
  471. ObjectTypeDB::bind_method("_mode_changed", &EditorTextureImportDialog::_mode_changed);
  472. // ADD_SIGNAL( MethodInfo("imported",PropertyInfo(Variant::OBJECT,"scene")) );
  473. }
  474. EditorTextureImportDialog::EditorTextureImportDialog(EditorTextureImportPlugin *p_plugin) {
  475. plugin = p_plugin;
  476. set_title(TTR("Import Textures"));
  477. mode_hb = memnew(HBoxContainer);
  478. add_child(mode_hb);
  479. set_child_rect(mode_hb);
  480. VBoxContainer *vbcg = memnew(VBoxContainer);
  481. mode_hb->add_child(vbcg);
  482. mode_hb->add_constant_override("separation", 15);
  483. ButtonGroup *bg = memnew(ButtonGroup);
  484. vbcg->add_margin_child("Import Mode", bg);
  485. for (int i = 0; i < EditorTextureImportPlugin::MODE_MAX; i++) {
  486. String mode_name[EditorTextureImportPlugin::MODE_MAX] = {
  487. TTR("2D Texture"),
  488. TTR("3D Texture"),
  489. TTR("Atlas Texture"),
  490. TTR("Large Texture")
  491. };
  492. mode_check[i] = memnew(CheckBox);
  493. bg->add_child(mode_check[i]);
  494. mode_check[i]->set_text(mode_name[i]);
  495. mode_check[i]->connect("pressed", this, "_mode_changed", varray(i));
  496. }
  497. VBoxContainer *vbc = memnew(VBoxContainer);
  498. mode_hb->add_child(vbc);
  499. vbc->set_h_size_flags(SIZE_EXPAND_FILL);
  500. vbc->add_constant_override("separation", 4);
  501. notice_for_2d = memnew(Label);
  502. notice_for_2d->set_text(TTR("NOTICE: Importing 2D textures is not mandatory. Just copy png/jpg files to the project."));
  503. //notice_for_2d->set_custom_minimum_size(Size2(0,50));
  504. notice_for_2d->set_autowrap(true);
  505. notice_for_2d->hide();
  506. vbcg->add_child(notice_for_2d);
  507. notice_for_2d->set_v_size_flags(SIZE_EXPAND_FILL);
  508. notice_for_2d->set_valign(Label::VALIGN_BOTTOM);
  509. VBoxContainer *source_vb = memnew(VBoxContainer);
  510. MarginContainer *source_mc = vbc->add_margin_child(TTR("Source Texture(s):"), source_vb);
  511. source_label = vbc->get_child(source_mc->get_index() - 1)->cast_to<Label>();
  512. HBoxContainer *hbc = memnew(HBoxContainer);
  513. source_vb->add_child(hbc);
  514. import_path = memnew(LineEdit);
  515. import_path->set_h_size_flags(SIZE_EXPAND_FILL);
  516. hbc->add_child(import_path);
  517. crop_source = memnew(CheckButton);
  518. crop_source->set_pressed(true);
  519. source_vb->add_child(crop_source);
  520. crop_source->set_text(TTR("Crop empty space."));
  521. Button *import_choose = memnew(Button);
  522. import_choose->set_text(" .. ");
  523. hbc->add_child(import_choose);
  524. import_choose->connect("pressed", this, "_browse");
  525. hbc = memnew(HBoxContainer);
  526. vbc->add_margin_child(TTR("Target Path:"), hbc);
  527. size = memnew(SpinBox);
  528. size->set_min(128);
  529. size->set_max(16384);
  530. size->set_val(256);
  531. size_mc = vbc->add_margin_child(TTR("Cell Size:"), size);
  532. size_label = vbc->get_child(size_mc->get_index() - 1)->cast_to<Label>();
  533. save_path = memnew(LineEdit);
  534. save_path->set_h_size_flags(SIZE_EXPAND_FILL);
  535. hbc->add_child(save_path);
  536. Button *save_choose = memnew(Button);
  537. save_choose->set_text(" .. ");
  538. hbc->add_child(save_choose);
  539. save_choose->connect("pressed", this, "_browse_target");
  540. file_select = memnew(EditorFileDialog);
  541. file_select->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  542. add_child(file_select);
  543. file_select->connect("files_selected", this, "_choose_files");
  544. file_select->connect("file_selected", this, "_choose_file");
  545. save_file_select = memnew(EditorFileDialog);
  546. save_file_select->set_access(EditorFileDialog::ACCESS_RESOURCES);
  547. add_child(save_file_select);
  548. save_file_select->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  549. save_file_select->clear_filters();
  550. save_file_select->connect("file_selected", this, "_choose_save_dir");
  551. save_select = memnew(EditorDirDialog);
  552. add_child(save_select);
  553. // save_select->set_mode(EditorFileDialog::MODE_OPEN_DIR);
  554. save_select->connect("dir_selected", this, "_choose_save_dir");
  555. get_ok()->connect("pressed", this, "_import");
  556. get_ok()->set_text(TTR("Import"));
  557. //move stuff up
  558. //for(int i=0;i<4;i++)
  559. // vbc->move_child( vbc->get_child( vbc->get_child_count() -1), 0);
  560. error_dialog = memnew(ConfirmationDialog);
  561. add_child(error_dialog);
  562. error_dialog->get_ok()->set_text(TTR("Accept"));
  563. // error_dialog->get_cancel()->hide();
  564. set_hide_on_ok(false);
  565. texture_options = memnew(EditorImportTextureOptions);
  566. vbc->add_child(texture_options);
  567. texture_options->set_v_size_flags(SIZE_EXPAND_FILL);
  568. _mode_changed(EditorTextureImportPlugin::MODE_TEXTURE_3D);
  569. // GLOBAL_DEF("import/shared_textures","res://");
  570. // Globals::get_singleton()->set_custom_property_info("import/shared_textures",PropertyInfo(Variant::STRING,"import/shared_textures",PROPERTY_HINT_DIR));
  571. }
  572. ///////////////////////////////////////////////////////////
  573. String EditorTextureImportPlugin::get_name() const {
  574. return "texture";
  575. #if 0 //old names, kept for compatibility reference
  576. switch(mode) {
  577. case MODE_TEXTURE_2D: {
  578. return "texture_2d";
  579. } break;
  580. case MODE_TEXTURE_3D: {
  581. return "texture_3d";
  582. } break;
  583. case MODE_ATLAS: {
  584. return "texture_atlas";
  585. } break;
  586. case MODE_LARGE: {
  587. return "texture_large";
  588. } break;
  589. }
  590. return "";
  591. #endif
  592. }
  593. String EditorTextureImportPlugin::get_visible_name() const {
  594. return TTR("Texture");
  595. }
  596. void EditorTextureImportPlugin::import_dialog(const String &p_from) {
  597. dialog->popup_import(p_from);
  598. }
  599. void EditorTextureImportPlugin::compress_image(EditorExportPlatform::ImageCompression p_mode, Image &image, bool p_smaller) {
  600. switch (p_mode) {
  601. case EditorExportPlatform::IMAGE_COMPRESSION_NONE: {
  602. //do absolutely nothing
  603. } break;
  604. case EditorExportPlatform::IMAGE_COMPRESSION_INDEXED: {
  605. //quantize
  606. image.quantize();
  607. } break;
  608. case EditorExportPlatform::IMAGE_COMPRESSION_BC: {
  609. // for maximum compatibility, BC shall always use mipmaps and be PO2
  610. image.resize_to_po2();
  611. if (image.get_mipmaps() == 0)
  612. image.generate_mipmaps();
  613. image.compress(Image::COMPRESS_BC);
  614. /*
  615. if (has_alpha) {
  616. if (flags&IMAGE_FLAG_ALPHA_BIT) {
  617. image.convert(Image::FORMAT_BC3);
  618. } else {
  619. image.convert(Image::FORMAT_BC2);
  620. }
  621. } else {
  622. image.convert(Image::FORMAT_BC1);
  623. }*/
  624. } break;
  625. case EditorExportPlatform::IMAGE_COMPRESSION_PVRTC:
  626. case EditorExportPlatform::IMAGE_COMPRESSION_PVRTC_SQUARE: {
  627. // for maximum compatibility (hi apple!), PVRT shall always
  628. // use mipmaps, be PO2 and square
  629. if (image.get_mipmaps() == 0)
  630. image.generate_mipmaps();
  631. image.resize_to_po2(true);
  632. if (p_smaller) {
  633. image.compress(Image::COMPRESS_PVRTC2);
  634. //image.convert(has_alpha ? Image::FORMAT_PVRTC2_ALPHA : Image::FORMAT_PVRTC2);
  635. } else {
  636. image.compress(Image::COMPRESS_PVRTC4);
  637. //image.convert(has_alpha ? Image::FORMAT_PVRTC4_ALPHA : Image::FORMAT_PVRTC4);
  638. }
  639. } break;
  640. case EditorExportPlatform::IMAGE_COMPRESSION_ETC1: {
  641. image.resize_to_po2(); //square or not?
  642. if (image.get_mipmaps() == 0)
  643. image.generate_mipmaps();
  644. if (!image.detect_alpha()) {
  645. //ETC1 is only opaque
  646. image.compress(Image::COMPRESS_ETC);
  647. }
  648. } break;
  649. case EditorExportPlatform::IMAGE_COMPRESSION_ETC2: {
  650. } break;
  651. }
  652. }
  653. Error EditorTextureImportPlugin::import(const String &p_path, const Ref<ResourceImportMetadata> &p_from) {
  654. return import2(p_path, p_from, EditorExportPlatform::IMAGE_COMPRESSION_BC, false);
  655. }
  656. Error EditorTextureImportPlugin::_process_texture_data(Ref<ImageTexture> &texture, int format, float quality, int flags, EditorExportPlatform::ImageCompression p_compr, int tex_flags, float shrink) {
  657. if (format == IMAGE_FORMAT_COMPRESS_DISK_LOSSLESS || format == IMAGE_FORMAT_COMPRESS_DISK_LOSSY) {
  658. Image image = texture->get_data();
  659. ERR_FAIL_COND_V(image.empty(), ERR_INVALID_DATA);
  660. bool has_alpha = image.detect_alpha();
  661. if (!has_alpha && image.get_format() == Image::FORMAT_RGBA) {
  662. image.convert(Image::FORMAT_RGB);
  663. }
  664. if (image.get_format() == Image::FORMAT_RGBA && flags & IMAGE_FLAG_FIX_BORDER_ALPHA) {
  665. image.fix_alpha_edges();
  666. }
  667. if (image.get_format() == Image::FORMAT_RGBA && flags & IMAGE_FLAG_PREMULT_ALPHA) {
  668. image.premultiply_alpha();
  669. }
  670. if (flags & IMAGE_FLAG_CONVERT_NORMAL_TO_XY) {
  671. image.normalmap_to_xy();
  672. }
  673. //if ((image.get_format()==Image::FORMAT_RGB || image.get_format()==Image::FORMAT_RGBA) && flags&IMAGE_FLAG_CONVERT_TO_LINEAR) {
  674. // image.srgb_to_linear();
  675. //}
  676. if (shrink > 1) {
  677. int orig_w = image.get_width();
  678. int orig_h = image.get_height();
  679. image.resize(orig_w / shrink, orig_h / shrink, Image::INTERPOLATE_CUBIC);
  680. texture->create_from_image(image, tex_flags);
  681. texture->set_size_override(Size2(orig_w, orig_h));
  682. } else {
  683. texture->create_from_image(image, tex_flags);
  684. }
  685. if (format == IMAGE_FORMAT_COMPRESS_DISK_LOSSLESS) {
  686. texture->set_storage(ImageTexture::STORAGE_COMPRESS_LOSSLESS);
  687. } else {
  688. texture->set_storage(ImageTexture::STORAGE_COMPRESS_LOSSY);
  689. }
  690. texture->set_lossy_storage_quality(quality);
  691. } else {
  692. Image image = texture->get_data();
  693. ERR_FAIL_COND_V(image.empty(), ERR_INVALID_DATA);
  694. bool has_alpha = image.detect_alpha();
  695. if (!has_alpha && image.get_format() == Image::FORMAT_RGBA) {
  696. image.convert(Image::FORMAT_RGB);
  697. }
  698. if (image.get_format() == Image::FORMAT_RGBA && flags & IMAGE_FLAG_FIX_BORDER_ALPHA) {
  699. image.fix_alpha_edges();
  700. }
  701. if (image.get_format() == Image::FORMAT_RGBA && flags & IMAGE_FLAG_PREMULT_ALPHA) {
  702. image.premultiply_alpha();
  703. }
  704. if (flags & IMAGE_FLAG_CONVERT_NORMAL_TO_XY) {
  705. image.normalmap_to_xy();
  706. }
  707. //if ((image.get_format()==Image::FORMAT_RGB || image.get_format()==Image::FORMAT_RGBA) && flags&IMAGE_FLAG_CONVERT_TO_LINEAR) {
  708. //
  709. // print_line("CONVERT BECAUSE: "+itos(flags));
  710. // image.srgb_to_linear();
  711. //}
  712. int orig_w = image.get_width();
  713. int orig_h = image.get_height();
  714. if (shrink > 1) {
  715. image.resize(orig_w / shrink, orig_h / shrink, Image::INTERPOLATE_CUBIC);
  716. texture->create_from_image(image, tex_flags);
  717. texture->set_size_override(Size2(orig_w, orig_h));
  718. }
  719. if (!(flags & IMAGE_FLAG_NO_MIPMAPS)) {
  720. image.generate_mipmaps();
  721. }
  722. if (format != IMAGE_FORMAT_UNCOMPRESSED) {
  723. compress_image(p_compr, image, flags & IMAGE_FLAG_COMPRESS_EXTRA);
  724. }
  725. texture->create_from_image(image, tex_flags);
  726. if (shrink > 1 || (format != IMAGE_FORMAT_UNCOMPRESSED && (image.get_width() != orig_w || image.get_height() != orig_h))) {
  727. texture->set_size_override(Size2(orig_w, orig_h));
  728. }
  729. //uint32_t save_flags=ResourceSaver::FLAG_COMPRESS;
  730. }
  731. return OK;
  732. }
  733. Error EditorTextureImportPlugin::import2(const String &p_path, const Ref<ResourceImportMetadata> &p_from, EditorExportPlatform::ImageCompression p_compr, bool p_external) {
  734. ERR_FAIL_COND_V(p_from->get_source_count() == 0, ERR_INVALID_PARAMETER);
  735. Ref<ResourceImportMetadata> from = p_from;
  736. Ref<ImageTexture> texture;
  737. Vector<Ref<AtlasTexture> > atlases;
  738. bool atlas = from->get_option("atlas");
  739. bool large = from->get_option("large");
  740. int flags = from->get_option("flags");
  741. int format = from->get_option("format");
  742. float quality = from->get_option("quality");
  743. uint32_t tex_flags = 0;
  744. if (flags & EditorTextureImportPlugin::IMAGE_FLAG_REPEAT)
  745. tex_flags |= Texture::FLAG_REPEAT;
  746. if (flags & EditorTextureImportPlugin::IMAGE_FLAG_FILTER)
  747. tex_flags |= Texture::FLAG_FILTER;
  748. if (!(flags & EditorTextureImportPlugin::IMAGE_FLAG_NO_MIPMAPS))
  749. tex_flags |= Texture::FLAG_MIPMAPS;
  750. if (flags & EditorTextureImportPlugin::IMAGE_FLAG_CONVERT_TO_LINEAR)
  751. tex_flags |= Texture::FLAG_CONVERT_TO_LINEAR;
  752. if (flags & EditorTextureImportPlugin::IMAGE_FLAG_USE_ANISOTROPY)
  753. tex_flags |= Texture::FLAG_ANISOTROPIC_FILTER;
  754. print_line("path: " + p_path + " flags: " + itos(tex_flags));
  755. float shrink = 1;
  756. if (from->has_option("shrink"))
  757. shrink = from->get_option("shrink");
  758. if (large) {
  759. ERR_FAIL_COND_V(from->get_source_count() != 1, ERR_INVALID_PARAMETER);
  760. String src_path = EditorImportPlugin::expand_source_path(from->get_source_path(0));
  761. int cell_size = from->get_option("large_cell_size");
  762. ERR_FAIL_COND_V(cell_size < 128 || cell_size > 16384, ERR_CANT_OPEN);
  763. EditorProgress pg("ltex", TTR("Import Large Texture"), 3);
  764. pg.step(TTR("Load Source Image"), 0);
  765. Image img;
  766. Error err = ImageLoader::load_image(src_path, &img);
  767. if (err) {
  768. return err;
  769. }
  770. pg.step(TTR("Slicing"), 1);
  771. Map<Vector2, Image> pieces;
  772. for (int i = 0; i < img.get_width(); i += cell_size) {
  773. int w = MIN(img.get_width() - i, cell_size);
  774. for (int j = 0; j < img.get_height(); j += cell_size) {
  775. int h = MIN(img.get_height() - j, cell_size);
  776. Image piece(w, h, 0, img.get_format());
  777. piece.blit_rect(img, Rect2(i, j, w, h), Point2(0, 0));
  778. if (!piece.is_invisible()) {
  779. pieces[Vector2(i, j)] = piece;
  780. //print_line("ADDING PIECE AT "+Vector2(i,j));
  781. }
  782. }
  783. }
  784. Ref<LargeTexture> existing;
  785. if (ResourceCache::has(p_path)) {
  786. existing = ResourceCache::get(p_path);
  787. }
  788. if (existing.is_valid()) {
  789. existing->clear();
  790. } else {
  791. existing = Ref<LargeTexture>(memnew(LargeTexture));
  792. }
  793. existing->set_size(Size2(img.get_width(), img.get_height()));
  794. pg.step(TTR("Inserting"), 2);
  795. for (Map<Vector2, Image>::Element *E = pieces.front(); E; E = E->next()) {
  796. Ref<ImageTexture> imgtex = Ref<ImageTexture>(memnew(ImageTexture));
  797. imgtex->create_from_image(E->get(), tex_flags);
  798. _process_texture_data(imgtex, format, quality, flags, p_compr, tex_flags, shrink);
  799. existing->add_piece(E->key(), imgtex);
  800. }
  801. if (!p_external) {
  802. from->set_editor(get_name());
  803. from->set_source_md5(0, FileAccess::get_md5(src_path));
  804. existing->set_path(p_path);
  805. existing->set_import_metadata(from);
  806. }
  807. pg.step(TTR("Saving"), 3);
  808. err = ResourceSaver::save(p_path, existing);
  809. if (err != OK) {
  810. EditorNode::add_io_error(TTR("Couldn't save large texture:") + " " + p_path);
  811. return err;
  812. }
  813. return OK;
  814. } else if (atlas) {
  815. //prepare atlas!
  816. Vector<Image> sources;
  817. Vector<Image> tsources;
  818. bool alpha = false;
  819. bool crop = from->get_option("crop");
  820. EditorProgress ep("make_atlas", TTR("Build Atlas For:") + " " + p_path.get_file(), from->get_source_count() + 3);
  821. print_line("sources: " + itos(from->get_source_count()));
  822. for (int i = 0; i < from->get_source_count(); i++) {
  823. String path = EditorImportPlugin::expand_source_path(from->get_source_path(i));
  824. String md5 = FileAccess::get_md5(path);
  825. from->set_source_md5(i, FileAccess::get_md5(path));
  826. ep.step(TTR("Loading Image:") + " " + path, i);
  827. print_line("source path: " + path + " md5 " + md5);
  828. Image src;
  829. Error err = ImageLoader::load_image(path, &src);
  830. if (err) {
  831. EditorNode::add_io_error(TTR("Couldn't load image:") + " " + path);
  832. return err;
  833. }
  834. if (src.detect_alpha())
  835. alpha = true;
  836. tsources.push_back(src);
  837. }
  838. ep.step(TTR("Converting Images"), sources.size());
  839. Map<uint64_t, int> source_md5;
  840. Map<int, List<int> > source_map;
  841. for (int i = 0; i < tsources.size(); i++) {
  842. Image src = tsources[i];
  843. if (alpha) {
  844. src.convert(Image::FORMAT_RGBA);
  845. } else {
  846. src.convert(Image::FORMAT_RGB);
  847. }
  848. DVector<uint8_t> data = src.get_data();
  849. MD5_CTX md5;
  850. DVector<uint8_t>::Read r = data.read();
  851. MD5Init(&md5);
  852. int len = data.size();
  853. for (int j = 0; j < len; j++) {
  854. uint8_t b = r[j];
  855. b >>= 2; //to aid in comparing
  856. MD5Update(&md5, (unsigned char *)&b, 1);
  857. }
  858. MD5Final(&md5);
  859. uint64_t *cmp = (uint64_t *)md5.digest; //less bits, but still useful for this
  860. tsources[i] = Image(); //clear
  861. if (source_md5.has(*cmp)) {
  862. int sidx = source_md5[*cmp];
  863. source_map[sidx].push_back(i);
  864. print_line("REUSING " + from->get_source_path(i));
  865. } else {
  866. int sidx = sources.size();
  867. source_md5[*cmp] = sidx;
  868. sources.push_back(src);
  869. List<int> sm;
  870. sm.push_back(i);
  871. source_map[sidx] = sm;
  872. }
  873. }
  874. //texturepacker is not really good for optimizing, so..
  875. //will at some point likely replace with my own
  876. //first, will find the nearest to a square packing
  877. int border = 1;
  878. Vector<Size2i> src_sizes;
  879. Vector<Rect2> crops;
  880. ep.step(TTR("Cropping Images"), sources.size() + 1);
  881. for (int j = 0; j < sources.size(); j++) {
  882. Size2i s;
  883. if (crop) {
  884. Rect2 crop = sources[j].get_used_rect();
  885. print_line("CROP: " + crop);
  886. s = crop.size;
  887. crops.push_back(crop);
  888. } else {
  889. s = Size2i(sources[j].get_width(), sources[j].get_height());
  890. }
  891. s += Size2i(border * 2, border * 2);
  892. src_sizes.push_back(s); //add a line to constraint width
  893. }
  894. Vector<Point2i> dst_positions;
  895. Size2i dst_size;
  896. EditorAtlas::fit(src_sizes, dst_positions, dst_size);
  897. print_line("size that worked: " + itos(dst_size.width) + "," + itos(dst_size.height));
  898. ep.step(TTR("Blitting Images"), sources.size() + 2);
  899. bool blit_to_po2 = tex_flags & Texture::FLAG_MIPMAPS;
  900. int atlas_w = dst_size.width;
  901. int atlas_h = dst_size.height;
  902. if (blit_to_po2) {
  903. atlas_w = next_power_of_2(dst_size.width);
  904. atlas_h = next_power_of_2(dst_size.height);
  905. }
  906. Image atlas;
  907. atlas.create(atlas_w, atlas_h, 0, alpha ? Image::FORMAT_RGBA : Image::FORMAT_RGB);
  908. atlases.resize(from->get_source_count());
  909. for (int i = 0; i < sources.size(); i++) {
  910. int x = dst_positions[i].x;
  911. int y = dst_positions[i].y;
  912. Size2 sz = Size2(sources[i].get_width(), sources[i].get_height());
  913. Rect2 region;
  914. Rect2 margin;
  915. if (crop && sz != crops[i].size) {
  916. Rect2 rect = crops[i];
  917. rect.size = sz - rect.size;
  918. region = Rect2(x + border, y + border, crops[i].size.width, crops[i].size.height);
  919. margin = rect;
  920. atlas.blit_rect(sources[i], crops[i], Point2(x + border, y + border));
  921. } else {
  922. region = Rect2(x + border, y + border, sz.x, sz.y);
  923. atlas.blit_rect(sources[i], Rect2(0, 0, sources[i].get_width(), sources[i].get_height()), Point2(x + border, y + border));
  924. }
  925. ERR_CONTINUE(!source_map.has(i));
  926. for (List<int>::Element *E = source_map[i].front(); E; E = E->next()) {
  927. String apath;
  928. String spath = from->get_source_path(E->get()).get_file();
  929. if (p_external) {
  930. apath = p_path.get_base_dir().plus_file(spath.basename() + "." + from->get_source_path(E->get()).md5_text() + ".atex");
  931. } else {
  932. apath = p_path.get_base_dir().plus_file(spath.basename() + ".atex");
  933. }
  934. Ref<AtlasTexture> at;
  935. if (ResourceCache::has(apath)) {
  936. at = Ref<AtlasTexture>(ResourceCache::get(apath)->cast_to<AtlasTexture>());
  937. } else {
  938. at = Ref<AtlasTexture>(memnew(AtlasTexture));
  939. }
  940. at->set_region(region);
  941. at->set_margin(margin);
  942. at->set_path(apath);
  943. atlases[E->get()] = at;
  944. }
  945. }
  946. if (ResourceCache::has(p_path)) {
  947. texture = Ref<ImageTexture>(ResourceCache::get(p_path)->cast_to<ImageTexture>());
  948. } else {
  949. texture = Ref<ImageTexture>(memnew(ImageTexture));
  950. }
  951. texture->create_from_image(atlas, tex_flags);
  952. } else {
  953. ERR_FAIL_COND_V(from->get_source_count() != 1, ERR_INVALID_PARAMETER);
  954. String src_path = EditorImportPlugin::expand_source_path(from->get_source_path(0));
  955. if (ResourceCache::has(p_path)) {
  956. Resource *r = ResourceCache::get(p_path);
  957. texture = Ref<ImageTexture>(r->cast_to<ImageTexture>());
  958. Image img;
  959. Error err = img.load(src_path);
  960. ERR_FAIL_COND_V(err != OK, ERR_CANT_OPEN);
  961. texture->create_from_image(img);
  962. } else {
  963. texture = ResourceLoader::load(src_path, "ImageTexture");
  964. }
  965. ERR_FAIL_COND_V(texture.is_null(), ERR_CANT_OPEN);
  966. if (!p_external)
  967. from->set_source_md5(0, FileAccess::get_md5(src_path));
  968. }
  969. if (!p_external) {
  970. from->set_editor(get_name());
  971. texture->set_path(p_path);
  972. texture->set_import_metadata(from);
  973. }
  974. if (atlas) {
  975. if (p_external) {
  976. //used by exporter
  977. Array rects(true);
  978. for (int i = 0; i < atlases.size(); i++) {
  979. rects.push_back(atlases[i]->get_region());
  980. rects.push_back(atlases[i]->get_margin());
  981. }
  982. from->set_option("rects", rects);
  983. } else {
  984. //used by importer
  985. for (int i = 0; i < atlases.size(); i++) {
  986. String apath = atlases[i]->get_path();
  987. atlases[i]->set_atlas(texture);
  988. Error err = ResourceSaver::save(apath, atlases[i]);
  989. if (err) {
  990. EditorNode::add_io_error(TTR("Couldn't save atlas image:") + " " + apath);
  991. return err;
  992. }
  993. //from->set_source_md5(i,FileAccess::get_md5(apath));
  994. }
  995. }
  996. }
  997. bool compress = false;
  998. #if 1
  999. _process_texture_data(texture, format, quality, flags, p_compr, tex_flags, shrink);
  1000. #else
  1001. if (format == IMAGE_FORMAT_COMPRESS_DISK_LOSSLESS || format == IMAGE_FORMAT_COMPRESS_DISK_LOSSY) {
  1002. Image image = texture->get_data();
  1003. ERR_FAIL_COND_V(image.empty(), ERR_INVALID_DATA);
  1004. bool has_alpha = image.detect_alpha();
  1005. if (!has_alpha && image.get_format() == Image::FORMAT_RGBA) {
  1006. image.convert(Image::FORMAT_RGB);
  1007. }
  1008. if (image.get_format() == Image::FORMAT_RGBA && flags & IMAGE_FLAG_FIX_BORDER_ALPHA) {
  1009. image.fix_alpha_edges();
  1010. }
  1011. if (image.get_format() == Image::FORMAT_RGBA && flags & IMAGE_FLAG_PREMULT_ALPHA) {
  1012. image.premultiply_alpha();
  1013. }
  1014. if (flags & IMAGE_FLAG_CONVERT_NORMAL_TO_XY) {
  1015. image.normalmap_to_xy();
  1016. }
  1017. //if ((image.get_format()==Image::FORMAT_RGB || image.get_format()==Image::FORMAT_RGBA) && flags&IMAGE_FLAG_CONVERT_TO_LINEAR) {
  1018. // image.srgb_to_linear();
  1019. //}
  1020. if (shrink > 1) {
  1021. int orig_w = image.get_width();
  1022. int orig_h = image.get_height();
  1023. image.resize(orig_w / shrink, orig_h / shrink);
  1024. texture->create_from_image(image, tex_flags);
  1025. texture->set_size_override(Size2(orig_w, orig_h));
  1026. } else {
  1027. texture->create_from_image(image, tex_flags);
  1028. }
  1029. if (format == IMAGE_FORMAT_COMPRESS_DISK_LOSSLESS) {
  1030. texture->set_storage(ImageTexture::STORAGE_COMPRESS_LOSSLESS);
  1031. } else {
  1032. texture->set_storage(ImageTexture::STORAGE_COMPRESS_LOSSY);
  1033. }
  1034. texture->set_lossy_storage_quality(quality);
  1035. } else {
  1036. Image image = texture->get_data();
  1037. ERR_FAIL_COND_V(image.empty(), ERR_INVALID_DATA);
  1038. bool has_alpha = image.detect_alpha();
  1039. if (!has_alpha && image.get_format() == Image::FORMAT_RGBA) {
  1040. image.convert(Image::FORMAT_RGB);
  1041. }
  1042. if (image.get_format() == Image::FORMAT_RGBA && flags & IMAGE_FLAG_FIX_BORDER_ALPHA) {
  1043. image.fix_alpha_edges();
  1044. }
  1045. if (image.get_format() == Image::FORMAT_RGBA && flags & IMAGE_FLAG_PREMULT_ALPHA) {
  1046. image.premultiply_alpha();
  1047. }
  1048. if (flags & IMAGE_FLAG_CONVERT_NORMAL_TO_XY) {
  1049. image.normalmap_to_xy();
  1050. }
  1051. //if ((image.get_format()==Image::FORMAT_RGB || image.get_format()==Image::FORMAT_RGBA) && flags&IMAGE_FLAG_CONVERT_TO_LINEAR) {
  1052. //
  1053. // print_line("CONVERT BECAUSE: "+itos(flags));
  1054. // image.srgb_to_linear();
  1055. //}
  1056. int orig_w = image.get_width();
  1057. int orig_h = image.get_height();
  1058. if (shrink > 1) {
  1059. image.resize(orig_w / shrink, orig_h / shrink);
  1060. texture->create_from_image(image, tex_flags);
  1061. texture->set_size_override(Size2(orig_w, orig_h));
  1062. }
  1063. if (!(flags & IMAGE_FLAG_NO_MIPMAPS)) {
  1064. image.generate_mipmaps();
  1065. }
  1066. if (format != IMAGE_FORMAT_UNCOMPRESSED) {
  1067. compress_image(p_compr, image, flags & IMAGE_FLAG_COMPRESS_EXTRA);
  1068. }
  1069. texture->create_from_image(image, tex_flags);
  1070. if (shrink > 1 || (format != IMAGE_FORMAT_UNCOMPRESSED && (image.get_width() != orig_w || image.get_height() != orig_h))) {
  1071. texture->set_size_override(Size2(orig_w, orig_h));
  1072. }
  1073. compress = true;
  1074. }
  1075. #endif
  1076. uint32_t save_flags = 0;
  1077. if (compress)
  1078. save_flags = ResourceSaver::FLAG_COMPRESS;
  1079. Error err = ResourceSaver::save(p_path, texture, save_flags);
  1080. if (err != OK) {
  1081. EditorNode::add_io_error(TTR("Couldn't save converted texture:") + " " + p_path);
  1082. return err;
  1083. }
  1084. return OK;
  1085. }
  1086. Vector<uint8_t> EditorTextureImportPlugin::custom_export(const String &p_path, const Ref<EditorExportPlatform> &p_platform) {
  1087. Ref<ResourceImportMetadata> rimd = ResourceLoader::load_import_metadata(p_path);
  1088. if (rimd.is_null()) {
  1089. StringName group = EditorImportExport::get_singleton()->image_get_export_group(p_path);
  1090. if (group != StringName()) {
  1091. //handled by export group
  1092. rimd = Ref<ResourceImportMetadata>(memnew(ResourceImportMetadata));
  1093. int group_format = 0;
  1094. float group_lossy_quality = EditorImportExport::get_singleton()->image_export_group_get_lossy_quality(group);
  1095. int group_shrink = EditorImportExport::get_singleton()->image_export_group_get_shrink(group);
  1096. group_shrink *= EditorImportExport::get_singleton()->get_export_image_shrink();
  1097. switch (EditorImportExport::get_singleton()->image_export_group_get_image_action(group)) {
  1098. case EditorImportExport::IMAGE_ACTION_NONE: {
  1099. switch (EditorImportExport::get_singleton()->get_export_image_action()) {
  1100. case EditorImportExport::IMAGE_ACTION_NONE: {
  1101. group_format = EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_DISK_LOSSLESS; //?
  1102. } break; //use default
  1103. case EditorImportExport::IMAGE_ACTION_COMPRESS_DISK: {
  1104. group_format = EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_DISK_LOSSY;
  1105. } break; //use default
  1106. case EditorImportExport::IMAGE_ACTION_COMPRESS_RAM: {
  1107. group_format = EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_RAM;
  1108. } break; //use default
  1109. }
  1110. group_lossy_quality = EditorImportExport::get_singleton()->get_export_image_quality();
  1111. } break; //use default
  1112. case EditorImportExport::IMAGE_ACTION_COMPRESS_DISK: {
  1113. group_format = EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_DISK_LOSSY;
  1114. } break; //use default
  1115. case EditorImportExport::IMAGE_ACTION_COMPRESS_RAM: {
  1116. group_format = EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_RAM;
  1117. } break; //use default
  1118. case EditorImportExport::IMAGE_ACTION_KEEP: {
  1119. return Vector<uint8_t>();
  1120. } break; //use default
  1121. }
  1122. String validated_path = EditorImportPlugin::validate_source_path(p_path);
  1123. int flags = texture_flags_to_export_flags(ResourceFormatLoaderImage::load_image_flags(validated_path));
  1124. flags |= IMAGE_FLAG_FIX_BORDER_ALPHA;
  1125. print_line("group format" + itos(group_format));
  1126. rimd->set_option("format", group_format);
  1127. rimd->set_option("flags", flags);
  1128. rimd->set_option("quality", group_lossy_quality);
  1129. rimd->set_option("atlas", false);
  1130. rimd->set_option("shrink", group_shrink);
  1131. rimd->add_source(validated_path, FileAccess::get_md5(p_path));
  1132. } else if (EditorImportExport::get_singleton()->get_image_formats().has(p_path.extension().to_lower()) && EditorImportExport::get_singleton()->get_export_image_action() != EditorImportExport::IMAGE_ACTION_NONE) {
  1133. //handled by general image export settings
  1134. rimd = Ref<ResourceImportMetadata>(memnew(ResourceImportMetadata));
  1135. switch (EditorImportExport::get_singleton()->get_export_image_action()) {
  1136. case EditorImportExport::IMAGE_ACTION_COMPRESS_DISK: rimd->set_option("format", IMAGE_FORMAT_COMPRESS_DISK_LOSSY); break;
  1137. case EditorImportExport::IMAGE_ACTION_COMPRESS_RAM: rimd->set_option("format", IMAGE_FORMAT_COMPRESS_RAM); break;
  1138. }
  1139. String validated_path = EditorImportPlugin::validate_source_path(p_path);
  1140. int flags = texture_flags_to_export_flags(ResourceFormatLoaderImage::load_image_flags(validated_path));
  1141. flags |= IMAGE_FLAG_FIX_BORDER_ALPHA;
  1142. rimd->set_option("shrink", EditorImportExport::get_singleton()->get_export_image_shrink());
  1143. rimd->set_option("flags", flags);
  1144. rimd->set_option("quality", EditorImportExport::get_singleton()->get_export_image_quality());
  1145. rimd->set_option("atlas", false);
  1146. rimd->add_source(validated_path, FileAccess::get_md5(p_path));
  1147. } else {
  1148. return Vector<uint8_t>();
  1149. }
  1150. }
  1151. int fmt = rimd->get_option("format");
  1152. if (fmt != IMAGE_FORMAT_COMPRESS_RAM && fmt != IMAGE_FORMAT_COMPRESS_DISK_LOSSY) {
  1153. print_line("no compress ram or lossy");
  1154. return Vector<uint8_t>(); //pointless to do anything, since no need to reconvert
  1155. }
  1156. uint32_t flags = rimd->get_option("flags");
  1157. uint8_t shrink = rimd->has_option("shrink") ? rimd->get_option("shrink") : Variant(1);
  1158. uint8_t format = rimd->get_option("format");
  1159. uint8_t comp = (format == EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_RAM) ? uint8_t(p_platform->get_image_compression()) : uint8_t(255);
  1160. MD5_CTX ctx;
  1161. uint8_t f4[4];
  1162. encode_uint32(flags, &f4[0]);
  1163. MD5Init(&ctx);
  1164. String gp = Globals::get_singleton()->globalize_path(p_path);
  1165. CharString cs = gp.utf8();
  1166. MD5Update(&ctx, (unsigned char *)cs.get_data(), cs.length());
  1167. MD5Update(&ctx, f4, 4);
  1168. MD5Update(&ctx, &format, 1);
  1169. MD5Update(&ctx, &comp, 1);
  1170. MD5Update(&ctx, &shrink, 1);
  1171. MD5Final(&ctx);
  1172. uint64_t sd = 0;
  1173. String smd5;
  1174. String md5 = String::md5(ctx.digest);
  1175. print_line(p_path + " MD5: " + md5 + " FLAGS: " + itos(flags));
  1176. String tmp_path = EditorSettings::get_singleton()->get_settings_path().plus_file("tmp/");
  1177. bool valid = false;
  1178. {
  1179. //if existing, make sure it's valid
  1180. FileAccessRef f = FileAccess::open(tmp_path + "imgexp-" + md5 + ".txt", FileAccess::READ);
  1181. if (f) {
  1182. uint64_t d = f->get_line().strip_edges().to_int64();
  1183. sd = FileAccess::get_modified_time(p_path);
  1184. if (d == sd) {
  1185. valid = true;
  1186. } else {
  1187. String cmd5 = f->get_line().strip_edges();
  1188. smd5 = FileAccess::get_md5(p_path);
  1189. if (cmd5 == smd5) {
  1190. valid = true;
  1191. }
  1192. }
  1193. }
  1194. }
  1195. if (!valid) {
  1196. //cache failed, convert
  1197. Error err = import2(tmp_path + "imgexp-" + md5 + ".tex", rimd, p_platform->get_image_compression(), true);
  1198. ERR_FAIL_COND_V(err != OK, Vector<uint8_t>());
  1199. FileAccessRef f = FileAccess::open(tmp_path + "imgexp-" + md5 + ".txt", FileAccess::WRITE);
  1200. if (sd == 0)
  1201. sd = FileAccess::get_modified_time(p_path);
  1202. if (smd5 == String())
  1203. smd5 = FileAccess::get_md5(p_path);
  1204. f->store_line(String::num(sd));
  1205. f->store_line(smd5);
  1206. f->store_line(gp); //source path for reference
  1207. }
  1208. Vector<uint8_t> ret;
  1209. FileAccessRef f = FileAccess::open(tmp_path + "imgexp-" + md5 + ".tex", FileAccess::READ);
  1210. ERR_FAIL_COND_V(!f, ret);
  1211. ret.resize(f->get_len());
  1212. f->get_buffer(ret.ptr(), ret.size());
  1213. return ret;
  1214. }
  1215. uint32_t EditorTextureImportPlugin::texture_flags_to_export_flags(uint32_t p_tex_flags) const {
  1216. uint32_t flags = 0;
  1217. if (!(p_tex_flags & Texture::FLAG_MIPMAPS)) {
  1218. flags |= IMAGE_FLAG_NO_MIPMAPS;
  1219. }
  1220. if (p_tex_flags & Texture::FLAG_REPEAT) {
  1221. flags |= IMAGE_FLAG_REPEAT;
  1222. }
  1223. if (p_tex_flags & Texture::FLAG_FILTER) {
  1224. flags |= IMAGE_FLAG_FILTER;
  1225. }
  1226. if (p_tex_flags & Texture::FLAG_ANISOTROPIC_FILTER) {
  1227. flags |= IMAGE_FLAG_USE_ANISOTROPY;
  1228. }
  1229. if (p_tex_flags & Texture::FLAG_CONVERT_TO_LINEAR) {
  1230. flags |= IMAGE_FLAG_CONVERT_TO_LINEAR;
  1231. }
  1232. /* // no correspondence yet
  1233. if (p_tex_flags&Texture::TEXTURE_FLAG_MIRRORED_REPEAT) {
  1234. flags|=;
  1235. }*/
  1236. return flags;
  1237. }
  1238. void EditorTextureImportPlugin::import_from_drop(const Vector<String> &p_drop, const String &p_dest_path) {
  1239. Vector<String> valid;
  1240. List<String> valid_extensions;
  1241. ImageLoader::get_recognized_extensions(&valid_extensions);
  1242. for (int i = 0; i < p_drop.size(); i++) {
  1243. String extension = p_drop[i].extension().to_lower();
  1244. for (List<String>::Element *E = valid_extensions.front(); E; E = E->next()) {
  1245. if (E->get() == extension) {
  1246. valid.push_back(p_drop[i]);
  1247. break;
  1248. }
  1249. }
  1250. }
  1251. if (valid.size()) {
  1252. dialog->popup_import();
  1253. dialog->setup_multiple_import_3d(valid, p_dest_path);
  1254. }
  1255. }
  1256. void EditorTextureImportPlugin::reimport_multiple_files(const Vector<String> &p_list) {
  1257. Vector<String> valid;
  1258. for (int i = 0; i < p_list.size(); i++) {
  1259. Ref<ResourceImportMetadata> rimd = ResourceLoader::load_import_metadata(p_list[i]);
  1260. String type = rimd->get_editor();
  1261. if (type == "texture" || type.begins_with("texture_")) {
  1262. if ((rimd->has_option("atlas") && rimd->get_option("atlas")) || (rimd->has_option("large") && rimd->get_option("large"))) {
  1263. continue;
  1264. }
  1265. valid.push_back(p_list[i]);
  1266. }
  1267. }
  1268. if (valid.size()) {
  1269. dialog->popup_import(valid[0]);
  1270. Vector<String> sources;
  1271. for (int i = 0; i < valid.size(); i++) {
  1272. int idx;
  1273. EditorFileSystemDirectory *efsd = EditorFileSystem::get_singleton()->find_file(valid[i], &idx);
  1274. if (efsd) {
  1275. for (int j = 0; j < efsd->get_source_count(idx); j++) {
  1276. String file = expand_source_path(efsd->get_source_file(idx, j));
  1277. if (sources.find(file) == -1) {
  1278. sources.push_back(file);
  1279. }
  1280. }
  1281. }
  1282. }
  1283. if (sources.size()) {
  1284. dialog->add_sources_and_dest(sources, valid[0].get_base_dir());
  1285. }
  1286. }
  1287. }
  1288. bool EditorTextureImportPlugin::can_reimport_multiple_files() const {
  1289. return true;
  1290. }
  1291. EditorTextureImportPlugin *EditorTextureImportPlugin::singleton = NULL;
  1292. EditorTextureImportPlugin::EditorTextureImportPlugin(EditorNode *p_editor) {
  1293. singleton = this;
  1294. editor = p_editor;
  1295. dialog = memnew(EditorTextureImportDialog(this));
  1296. editor->get_gui_base()->add_child(dialog);
  1297. }
  1298. ////////////////////////////
  1299. Vector<uint8_t> EditorTextureExportPlugin::custom_export(String &p_path, const Ref<EditorExportPlatform> &p_platform) {
  1300. Ref<ResourceImportMetadata> rimd = ResourceLoader::load_import_metadata(p_path);
  1301. if (rimd.is_valid()) {
  1302. if (rimd->get_editor() != "") {
  1303. int compression = rimd->get_option("format");
  1304. if (compression != EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_RAM)
  1305. return Vector<uint8_t>(); //only useful for RAM compression to reconvert
  1306. Ref<EditorImportPlugin> pl = EditorImportExport::get_singleton()->get_import_plugin_by_name(rimd->get_editor());
  1307. if (pl.is_valid()) {
  1308. Vector<uint8_t> ce = pl->custom_export(p_path, p_platform);
  1309. if (ce.size())
  1310. return ce;
  1311. }
  1312. }
  1313. } else if (EditorImportExport::get_singleton()->image_get_export_group(p_path)) {
  1314. Ref<EditorImportPlugin> pl = EditorImportExport::get_singleton()->get_import_plugin_by_name("texture");
  1315. if (pl.is_valid()) {
  1316. Vector<uint8_t> ce = pl->custom_export(p_path, p_platform);
  1317. if (ce.size()) {
  1318. p_path = p_path.basename() + ".converted.tex";
  1319. return ce;
  1320. }
  1321. }
  1322. } else if (EditorImportExport::get_singleton()->get_export_image_action() != EditorImportExport::IMAGE_ACTION_NONE) {
  1323. String xt = p_path.extension().to_lower();
  1324. if (EditorImportExport::get_singleton()->get_image_formats().has(xt)) { //should check for more I guess?
  1325. Ref<EditorImportPlugin> pl = EditorImportExport::get_singleton()->get_import_plugin_by_name("texture");
  1326. if (pl.is_valid()) {
  1327. Vector<uint8_t> ce = pl->custom_export(p_path, p_platform);
  1328. if (ce.size()) {
  1329. p_path = p_path.basename() + ".converted.tex";
  1330. return ce;
  1331. }
  1332. }
  1333. }
  1334. }
  1335. return Vector<uint8_t>();
  1336. }
  1337. EditorTextureExportPlugin::EditorTextureExportPlugin() {
  1338. }