From de13e52b9b077073dfa79d59ece6b2c5cec2c8c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Fri, 30 Sep 2016 10:49:55 +0200 Subject: [PATCH] Respect texture .flags files on export (cherry picked from commit 11cbbeb17ed0f5ff369046c1a194e0a31863f207) --- scene/io/resource_format_image.cpp | 118 ++++++++++-------- scene/io/resource_format_image.h | 2 +- .../editor_texture_import_plugin.cpp | 53 +++++--- .../io_plugins/editor_texture_import_plugin.h | 2 + 4 files changed, 100 insertions(+), 75 deletions(-) diff --git a/scene/io/resource_format_image.cpp b/scene/io/resource_format_image.cpp index f3a0eaa8c40..55ad23ba936 100644 --- a/scene/io/resource_format_image.cpp +++ b/scene/io/resource_format_image.cpp @@ -136,61 +136,7 @@ RES ResourceFormatLoaderImage::load(const String &p_path, const String& p_origin #endif - uint32_t flags=0; - - FileAccess *f2 = FileAccess::open(p_path+".flags",FileAccess::READ); - Map flags_found; - if (f2) { - - while(!f2->eof_reached()) { - String l2 = f2->get_line(); - int eqpos = l2.find("="); - if (eqpos!=-1) { - String flag=l2.substr(0,eqpos).strip_edges(); - String val=l2.substr(eqpos+1,l2.length()).strip_edges().to_lower(); - flags_found[flag]=(val=="true" || val=="1")?true:false; - } - } - memdelete(f2); - } - - - if (flags_found.has("filter")) { - if (flags_found["filter"]) - flags|=Texture::FLAG_FILTER; - } else if (bool(GLOBAL_DEF("image_loader/filter",true))) { - flags|=Texture::FLAG_FILTER; - } - - - if (flags_found.has("gen_mipmaps")) { - if (flags_found["gen_mipmaps"]) - flags|=Texture::FLAG_MIPMAPS; - } else if (bool(GLOBAL_DEF("image_loader/gen_mipmaps",true))) { - flags|=Texture::FLAG_MIPMAPS; - } - - if (flags_found.has("repeat")) { - if (flags_found["repeat"]) - flags|=Texture::FLAG_REPEAT; - } else if (bool(GLOBAL_DEF("image_loader/repeat",true))) { - flags|=Texture::FLAG_REPEAT; - } - - if (flags_found.has("anisotropic")) { - if (flags_found["anisotropic"]) - flags|=Texture::FLAG_ANISOTROPIC_FILTER; - } - - if (flags_found.has("tolinear")) { - if (flags_found["tolinear"]) - flags|=Texture::FLAG_CONVERT_TO_LINEAR; - } - - if (flags_found.has("mirroredrepeat")) { - if (flags_found["mirroredrepeat"]) - flags|=Texture::FLAG_MIRRORED_REPEAT; - } + uint32_t flags=load_image_flags(p_path); if (debug_load_times) begtime=OS::get_singleton()->get_ticks_usec(); @@ -214,6 +160,68 @@ RES ResourceFormatLoaderImage::load(const String &p_path, const String& p_origin } +uint32_t ResourceFormatLoaderImage::load_image_flags(const String &p_path) { + + + FileAccess *f2 = FileAccess::open(p_path+".flags",FileAccess::READ); + Map flags_found; + if (f2) { + + while(!f2->eof_reached()) { + String l2 = f2->get_line(); + int eqpos = l2.find("="); + if (eqpos!=-1) { + String flag=l2.substr(0,eqpos).strip_edges(); + String val=l2.substr(eqpos+1,l2.length()).strip_edges().to_lower(); + flags_found[flag]=(val=="true" || val=="1")?true:false; + } + } + memdelete(f2); + } + + + uint32_t flags=0; + + if (flags_found.has("filter")) { + if (flags_found["filter"]) + flags|=Texture::FLAG_FILTER; + } else if (bool(GLOBAL_DEF("image_loader/filter",true))) { + flags|=Texture::FLAG_FILTER; + } + + + if (flags_found.has("gen_mipmaps")) { + if (flags_found["gen_mipmaps"]) + flags|=Texture::FLAG_MIPMAPS; + } else if (bool(GLOBAL_DEF("image_loader/gen_mipmaps",true))) { + flags|=Texture::FLAG_MIPMAPS; + } + + if (flags_found.has("repeat")) { + if (flags_found["repeat"]) + flags|=Texture::FLAG_REPEAT; + } else if (bool(GLOBAL_DEF("image_loader/repeat",true))) { + flags|=Texture::FLAG_REPEAT; + } + + if (flags_found.has("anisotropic")) { + if (flags_found["anisotropic"]) + flags|=Texture::FLAG_ANISOTROPIC_FILTER; + } + + if (flags_found.has("tolinear")) { + if (flags_found["tolinear"]) + flags|=Texture::FLAG_CONVERT_TO_LINEAR; + } + + if (flags_found.has("mirroredrepeat")) { + if (flags_found["mirroredrepeat"]) + flags|=Texture::FLAG_MIRRORED_REPEAT; + } + + return flags; +} + bool ResourceFormatLoaderImage::handles_type(const String& p_type) const { return ObjectTypeDB::is_type(p_type,"Texture") || ObjectTypeDB::is_type(p_type,"CubeMap"); diff --git a/scene/io/resource_format_image.h b/scene/io/resource_format_image.h index 6388aa641f2..cce6ffab836 100644 --- a/scene/io/resource_format_image.h +++ b/scene/io/resource_format_image.h @@ -39,8 +39,8 @@ class ResourceFormatLoaderImage : public ResourceFormatLoader { bool debug_load_times; int max_texture_size; public: - virtual RES load(const String &p_path,const String& p_original_path="",Error *r_error=NULL); + static uint32_t load_image_flags(const String &p_path); virtual void get_recognized_extensions(List *p_extensions) const; virtual bool handles_type(const String& p_type) const; virtual String get_resource_type(const String &p_path) const; diff --git a/tools/editor/io_plugins/editor_texture_import_plugin.cpp b/tools/editor/io_plugins/editor_texture_import_plugin.cpp index 60642999f26..2935ea8fe24 100644 --- a/tools/editor/io_plugins/editor_texture_import_plugin.cpp +++ b/tools/editor/io_plugins/editor_texture_import_plugin.cpp @@ -38,6 +38,7 @@ #include "scene/gui/check_button.h" #include "scene/gui/button_group.h" #include "scene/gui/margin_container.h" +#include "scene/io/resource_format_image.h" static const char *flag_names[]={ ("Streaming Format"), @@ -1589,16 +1590,9 @@ Vector EditorTextureImportPlugin::custom_export(const String& p_path, c } break; //use default } + String validated_path=EditorImportPlugin::validate_source_path(p_path); - int flags=0; - - if (Globals::get_singleton()->get("image_loader/filter")) - flags|=IMAGE_FLAG_FILTER; - if (!Globals::get_singleton()->get("image_loader/gen_mipmaps")) - flags|=IMAGE_FLAG_NO_MIPMAPS; - if (!Globals::get_singleton()->get("image_loader/repeat")) - flags|=IMAGE_FLAG_REPEAT; - + int flags=texture_flags_to_export_flags(ResourceFormatLoaderImage::load_image_flags(validated_path)); flags|=IMAGE_FLAG_FIX_BORDER_ALPHA; print_line("group format"+itos(group_format)); @@ -1607,7 +1601,7 @@ Vector EditorTextureImportPlugin::custom_export(const String& p_path, c rimd->set_option("quality",group_lossy_quality); rimd->set_option("atlas",false); rimd->set_option("shrink",group_shrink); - rimd->add_source(EditorImportPlugin::validate_source_path(p_path),FileAccess::get_md5(p_path)); + rimd->add_source(validated_path,FileAccess::get_md5(p_path)); } 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) { //handled by general image export settings @@ -1619,22 +1613,16 @@ Vector EditorTextureImportPlugin::custom_export(const String& p_path, c case EditorImportExport::IMAGE_ACTION_COMPRESS_RAM: rimd->set_option("format",IMAGE_FORMAT_COMPRESS_RAM); break; } - int flags=0; - - if (Globals::get_singleton()->get("image_loader/filter")) - flags|=IMAGE_FLAG_FILTER; - if (!Globals::get_singleton()->get("image_loader/gen_mipmaps")) - flags|=IMAGE_FLAG_NO_MIPMAPS; - if (!Globals::get_singleton()->get("image_loader/repeat")) - flags|=IMAGE_FLAG_REPEAT; + String validated_path=EditorImportPlugin::validate_source_path(p_path); + int flags=texture_flags_to_export_flags(ResourceFormatLoaderImage::load_image_flags(validated_path)); flags|=IMAGE_FLAG_FIX_BORDER_ALPHA; rimd->set_option("shrink",EditorImportExport::get_singleton()->get_export_image_shrink()); rimd->set_option("flags",flags); rimd->set_option("quality",EditorImportExport::get_singleton()->get_export_image_quality()); rimd->set_option("atlas",false); - rimd->add_source(EditorImportPlugin::validate_source_path(p_path),FileAccess::get_md5(p_path)); + rimd->add_source(validated_path,FileAccess::get_md5(p_path)); } else { return Vector(); @@ -1726,6 +1714,33 @@ Vector EditorTextureImportPlugin::custom_export(const String& p_path, c return ret; } +uint32_t EditorTextureImportPlugin::texture_flags_to_export_flags(uint32_t p_tex_flags) const { + + uint32_t flags=0; + + if (!(p_tex_flags&Texture::FLAG_MIPMAPS)) { + flags|=IMAGE_FLAG_NO_MIPMAPS; + } + if (p_tex_flags&Texture::FLAG_REPEAT) { + flags|=IMAGE_FLAG_REPEAT; + } + if (p_tex_flags&Texture::FLAG_FILTER) { + flags|=IMAGE_FLAG_FILTER; + } + if (p_tex_flags&Texture::FLAG_ANISOTROPIC_FILTER) { + flags|=IMAGE_FLAG_USE_ANISOTROPY; + } + if (p_tex_flags&Texture::FLAG_CONVERT_TO_LINEAR) { + flags|=IMAGE_FLAG_CONVERT_TO_LINEAR; + } + /* // no correspondence yet + if (p_tex_flags&Texture::TEXTURE_FLAG_MIRRORED_REPEAT) { + flags|=; + }*/ + + return flags; +} + void EditorTextureImportPlugin::import_from_drop(const Vector& p_drop,const String& p_dest_path) { Vector valid; diff --git a/tools/editor/io_plugins/editor_texture_import_plugin.h b/tools/editor/io_plugins/editor_texture_import_plugin.h index 5c8abd10a47..22c10a1a3ac 100644 --- a/tools/editor/io_plugins/editor_texture_import_plugin.h +++ b/tools/editor/io_plugins/editor_texture_import_plugin.h @@ -72,6 +72,8 @@ private: Error _process_texture_data(Ref &texture, int format, float quality, int flags,EditorExportPlatform::ImageCompression p_compr,int tex_flags,float shrink); void compress_image(EditorExportPlatform::ImageCompression p_mode,Image& image,bool p_smaller); + + uint32_t texture_flags_to_export_flags(uint32_t p_tex_flags) const; public: