2017-03-05 14:47:28 +00:00
/**************************************************************************/
/* resource_importer_texture.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
2018-01-04 23:50:27 +00:00
2017-02-03 03:08:50 +00:00
# include "resource_importer_texture.h"
2017-03-05 14:47:28 +00:00
2022-02-12 01:46:22 +00:00
# include "core/config/project_settings.h"
2018-09-11 16:13:45 +00:00
# include "core/io/config_file.h"
# include "core/io/image_loader.h"
2019-09-27 02:16:44 +00:00
# include "core/version.h"
2017-03-05 13:21:25 +00:00
# include "editor/editor_file_system.h"
2017-09-13 07:13:23 +00:00
# include "editor/editor_node.h"
2022-08-22 19:07:02 +00:00
# include "editor/editor_scale.h"
# include "editor/editor_settings.h"
2023-06-20 01:03:51 +00:00
# include "editor/import/resource_importer_texture_settings.h"
2023-07-11 20:29:09 +00:00
# include "scene/resources/compressed_texture.h"
2017-09-13 07:13:23 +00:00
2022-03-05 15:43:38 +00:00
void ResourceImporterTexture : : _texture_reimport_roughness ( const Ref < CompressedTexture2D > & p_tex , const String & p_normal_path , RS : : TextureDetectRoughnessChannel p_channel ) {
2021-12-17 15:56:41 +00:00
ERR_FAIL_COND ( p_tex . is_null ( ) ) ;
2020-02-26 10:28:13 +00:00
MutexLock lock ( singleton - > mutex ) ;
2017-02-06 03:38:39 +00:00
StringName path = p_tex - > get_path ( ) ;
if ( ! singleton - > make_flags . has ( path ) ) {
2019-06-11 18:43:37 +00:00
singleton - > make_flags [ path ] = MakeInfo ( ) ;
2017-02-06 03:38:39 +00:00
}
2019-06-11 18:43:37 +00:00
singleton - > make_flags [ path ] . flags | = MAKE_ROUGHNESS_FLAG ;
singleton - > make_flags [ path ] . channel_for_roughness = p_channel ;
singleton - > make_flags [ path ] . normal_path_for_roughness = p_normal_path ;
2017-02-06 03:38:39 +00:00
}
2022-03-05 15:43:38 +00:00
void ResourceImporterTexture : : _texture_reimport_3d ( const Ref < CompressedTexture2D > & p_tex ) {
2021-12-17 15:56:41 +00:00
ERR_FAIL_COND ( p_tex . is_null ( ) ) ;
2020-02-26 10:28:13 +00:00
MutexLock lock ( singleton - > mutex ) ;
2017-02-06 03:38:39 +00:00
StringName path = p_tex - > get_path ( ) ;
if ( ! singleton - > make_flags . has ( path ) ) {
2019-06-11 18:43:37 +00:00
singleton - > make_flags [ path ] = MakeInfo ( ) ;
2017-02-06 03:38:39 +00:00
}
2019-06-11 18:43:37 +00:00
singleton - > make_flags [ path ] . flags | = MAKE_3D_FLAG ;
2017-02-06 03:38:39 +00:00
}
2022-03-05 15:43:38 +00:00
void ResourceImporterTexture : : _texture_reimport_normal ( const Ref < CompressedTexture2D > & p_tex ) {
2021-12-17 15:56:41 +00:00
ERR_FAIL_COND ( p_tex . is_null ( ) ) ;
2020-02-26 10:28:13 +00:00
MutexLock lock ( singleton - > mutex ) ;
2017-06-17 00:47:28 +00:00
StringName path = p_tex - > get_path ( ) ;
if ( ! singleton - > make_flags . has ( path ) ) {
2019-06-11 18:43:37 +00:00
singleton - > make_flags [ path ] = MakeInfo ( ) ;
2017-06-17 00:47:28 +00:00
}
2019-06-11 18:43:37 +00:00
singleton - > make_flags [ path ] . flags | = MAKE_NORMAL_FLAG ;
2017-06-17 00:47:28 +00:00
}
2017-02-06 03:38:39 +00:00
void ResourceImporterTexture : : update_imports ( ) {
if ( EditorFileSystem : : get_singleton ( ) - > is_scanning ( ) | | EditorFileSystem : : get_singleton ( ) - > is_importing ( ) ) {
2019-05-19 10:34:40 +00:00
return ; // do nothing for now
2017-02-06 03:38:39 +00:00
}
2020-02-26 10:28:13 +00:00
MutexLock lock ( mutex ) ;
2017-02-06 03:38:39 +00:00
Vector < String > to_reimport ;
2020-02-26 10:28:13 +00:00
{
2020-12-15 12:04:21 +00:00
if ( make_flags . is_empty ( ) ) {
2020-02-26 10:28:13 +00:00
return ;
}
2017-02-06 03:38:39 +00:00
2021-08-09 20:13:42 +00:00
for ( const KeyValue < StringName , MakeInfo > & E : make_flags ) {
2020-02-26 10:28:13 +00:00
Ref < ConfigFile > cf ;
2021-06-17 22:03:09 +00:00
cf . instantiate ( ) ;
2021-08-09 20:13:42 +00:00
String src_path = String ( E . key ) + " .import " ;
2017-02-06 03:38:39 +00:00
2020-02-26 10:28:13 +00:00
Error err = cf - > load ( src_path ) ;
ERR_CONTINUE ( err ! = OK ) ;
2019-06-11 18:43:37 +00:00
2020-02-26 10:28:13 +00:00
bool changed = false ;
2017-02-06 03:38:39 +00:00
2022-06-20 13:30:19 +00:00
if ( E . value . flags & MAKE_NORMAL_FLAG & & int ( cf - > get_value ( " params " , " compress/normal_map " ) ) = = 0 ) {
2022-06-13 18:16:14 +00:00
print_line ( vformat ( TTR ( " %s: Texture detected as used as a normal map in 3D. Enabling red-green texture compression to reduce memory usage (blue channel is discarded). " ) , String ( E . key ) ) ) ;
2020-02-26 10:28:13 +00:00
cf - > set_value ( " params " , " compress/normal_map " , 1 ) ;
changed = true ;
}
2017-06-17 00:47:28 +00:00
2021-08-09 20:13:42 +00:00
if ( E . value . flags & MAKE_ROUGHNESS_FLAG & & int ( cf - > get_value ( " params " , " roughness/mode " ) ) = = 0 ) {
2022-06-13 18:16:14 +00:00
print_line ( vformat ( TTR ( " %s: Texture detected as used as a roughness map in 3D. Enabling roughness limiter based on the detected associated normal map at %s. " ) , String ( E . key ) , E . value . normal_path_for_roughness ) ) ;
2021-08-09 20:13:42 +00:00
cf - > set_value ( " params " , " roughness/mode " , E . value . channel_for_roughness + 2 ) ;
cf - > set_value ( " params " , " roughness/src_normal " , E . value . normal_path_for_roughness ) ;
2020-02-26 10:28:13 +00:00
changed = true ;
2019-09-27 02:16:44 +00:00
}
2017-02-06 03:38:39 +00:00
2022-06-20 13:30:19 +00:00
if ( E . value . flags & MAKE_3D_FLAG & & bool ( cf - > get_value ( " params " , " detect_3d/compress_to " ) ) ) {
const int compress_to = cf - > get_value ( " params " , " detect_3d/compress_to " ) ;
String compress_string ;
cf - > set_value ( " params " , " detect_3d/compress_to " , 0 ) ;
if ( compress_to = = 1 ) {
cf - > set_value ( " params " , " compress/mode " , COMPRESS_VRAM_COMPRESSED ) ;
compress_string = " VRAM Compressed (S3TC/ETC/BPTC) " ;
} else if ( compress_to = = 2 ) {
cf - > set_value ( " params " , " compress/mode " , COMPRESS_BASIS_UNIVERSAL ) ;
compress_string = " Basis Universal " ;
}
print_line ( vformat ( TTR ( " %s: Texture detected as used in 3D. Enabling mipmap generation and setting the texture compression mode to %s. " ) , String ( E . key ) , compress_string ) ) ;
cf - > set_value ( " params " , " mipmaps/generate " , true ) ;
changed = true ;
}
2020-02-26 10:28:13 +00:00
if ( changed ) {
cf - > save ( src_path ) ;
2021-08-09 20:13:42 +00:00
to_reimport . push_back ( E . key ) ;
2020-02-26 10:28:13 +00:00
}
}
2017-02-06 03:38:39 +00:00
2020-02-26 10:28:13 +00:00
make_flags . clear ( ) ;
}
2017-02-06 03:38:39 +00:00
if ( to_reimport . size ( ) ) {
EditorFileSystem : : get_singleton ( ) - > reimport_files ( to_reimport ) ;
}
}
2017-02-01 12:45:45 +00:00
String ResourceImporterTexture : : get_importer_name ( ) const {
return " texture " ;
}
String ResourceImporterTexture : : get_visible_name ( ) const {
2019-06-11 18:43:37 +00:00
return " Texture2D " ;
2017-02-01 12:45:45 +00:00
}
2020-05-14 12:29:06 +00:00
2017-02-01 12:45:45 +00:00
void ResourceImporterTexture : : get_recognized_extensions ( List < String > * p_extensions ) const {
ImageLoader : : get_recognized_extensions ( p_extensions ) ;
}
2020-05-14 12:29:06 +00:00
2017-02-01 12:45:45 +00:00
String ResourceImporterTexture : : get_save_extension ( ) const {
2022-03-05 15:43:38 +00:00
return " ctex " ;
2017-02-01 12:45:45 +00:00
}
String ResourceImporterTexture : : get_resource_type ( ) const {
2022-03-05 15:43:38 +00:00
return " CompressedTexture2D " ;
2017-02-01 12:45:45 +00:00
}
2022-05-13 13:04:37 +00:00
bool ResourceImporterTexture : : get_option_visibility ( const String & p_path , const String & p_option , const HashMap < StringName , Variant > & p_options ) const {
2023-01-25 11:17:11 +00:00
if ( p_option = = " compress/high_quality " | | p_option = = " compress/hdr_compression " ) {
2017-04-26 15:49:08 +00:00
int compress_mode = int ( p_options [ " compress/mode " ] ) ;
2023-01-25 11:17:11 +00:00
if ( compress_mode ! = COMPRESS_VRAM_COMPRESSED ) {
return false ;
}
} else if ( p_option = = " compress/lossy_quality " ) {
int compress_mode = int ( p_options [ " compress/mode " ] ) ;
if ( compress_mode ! = COMPRESS_LOSSY ) {
2017-04-26 15:49:08 +00:00
return false ;
}
2018-08-27 16:47:13 +00:00
} else if ( p_option = = " compress/hdr_mode " ) {
2018-08-22 22:31:12 +00:00
int compress_mode = int ( p_options [ " compress/mode " ] ) ;
2019-09-27 02:16:44 +00:00
if ( compress_mode < COMPRESS_VRAM_COMPRESSED ) {
2018-08-22 22:31:12 +00:00
return false ;
}
2022-03-26 23:21:07 +00:00
} else if ( p_option = = " compress/normal_map " ) {
int compress_mode = int ( p_options [ " compress/mode " ] ) ;
if ( compress_mode = = COMPRESS_LOSSLESS ) {
return false ;
}
2019-09-27 02:16:44 +00:00
} else if ( p_option = = " mipmaps/limit " ) {
return p_options [ " mipmaps/generate " ] ;
2017-04-26 15:49:08 +00:00
}
2017-02-01 12:45:45 +00:00
return true ;
}
int ResourceImporterTexture : : get_preset_count ( ) const {
2020-06-14 13:08:09 +00:00
return 3 ;
2017-02-01 12:45:45 +00:00
}
2020-05-14 12:29:06 +00:00
2017-02-01 12:45:45 +00:00
String ResourceImporterTexture : : get_preset_name ( int p_idx ) const {
static const char * preset_names [ ] = {
2022-04-27 05:50:54 +00:00
TTRC ( " 2D/3D (Auto-Detect) " ) ,
TTRC ( " 2D " ) ,
TTRC ( " 3D " ) ,
2017-02-01 12:45:45 +00:00
} ;
2022-04-27 05:50:54 +00:00
return TTRGET ( preset_names [ p_idx ] ) ;
2017-02-01 12:45:45 +00:00
}
2021-11-14 17:02:38 +00:00
void ResourceImporterTexture : : get_import_options ( const String & p_path , List < ImportOption > * r_options , int p_preset ) const {
2019-09-27 02:16:44 +00:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : INT , " compress/mode " , PROPERTY_HINT_ENUM , " Lossless,Lossy,VRAM Compressed,VRAM Uncompressed,Basis Universal " , PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED ) , p_preset = = PRESET_3D ? 2 : 0 ) ) ;
2023-01-25 11:17:11 +00:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : BOOL , " compress/high_quality " ) , false ) ) ;
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : FLOAT , " compress/lossy_quality " , PROPERTY_HINT_RANGE , " 0,1,0.01 " ) , 0.7 ) ) ;
2020-05-01 12:34:23 +00:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : INT , " compress/hdr_compression " , PROPERTY_HINT_ENUM , " Disabled,Opaque Only,Always " ) , 1 ) ) ;
2017-06-17 00:47:28 +00:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : INT , " compress/normal_map " , PROPERTY_HINT_ENUM , " Detect,Enable,Disabled " ) , 0 ) ) ;
2019-06-11 18:43:37 +00:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : INT , " compress/channel_pack " , PROPERTY_HINT_ENUM , " sRGB Friendly,Optimized " ) , 0 ) ) ;
2019-09-27 02:16:44 +00:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : BOOL , " mipmaps/generate " ) , ( p_preset = = PRESET_3D ? true : false ) ) ) ;
2019-09-27 12:05:22 +00:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : INT , " mipmaps/limit " , PROPERTY_HINT_RANGE , " -1,256 " ) , - 1 ) ) ;
2019-06-11 18:43:37 +00:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : INT , " roughness/mode " , PROPERTY_HINT_ENUM , " Detect,Disabled,Red,Green,Blue,Alpha,Gray " ) , 0 ) ) ;
2022-01-17 11:39:57 +00:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : STRING , " roughness/src_normal " , PROPERTY_HINT_FILE , " *.bmp,*.dds,*.exr,*.jpeg,*.jpg,*.hdr,*.png,*.svg,*.tga,*.webp " ) , " " ) ) ;
2019-06-26 13:08:25 +00:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : BOOL , " process/fix_alpha_border " ) , p_preset ! = PRESET_3D ) ) ;
2017-06-25 11:01:15 +00:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : BOOL , " process/premult_alpha " ) , false ) ) ;
2020-05-31 17:27:29 +00:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : BOOL , " process/normal_map_invert_y " ) , false ) ) ;
2022-02-12 01:39:10 +00:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : BOOL , " process/hdr_as_srgb " ) , false ) ) ;
2022-02-16 00:43:44 +00:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : BOOL , " process/hdr_clamp_exposure " ) , false ) ) ;
2019-09-14 03:37:42 +00:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : INT , " process/size_limit " , PROPERTY_HINT_RANGE , " 0,4096,1 " ) , 0 ) ) ;
2019-09-27 12:05:22 +00:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : INT , " detect_3d/compress_to " , PROPERTY_HINT_ENUM , " Disabled,VRAM Compressed,Basis Universal " ) , ( p_preset = = PRESET_DETECT ) ? 1 : 0 ) ) ;
2022-01-16 20:18:00 +00:00
2022-10-08 14:01:00 +00:00
// Do path based customization only if a path was passed.
if ( p_path . is_empty ( ) | | p_path . get_extension ( ) = = " svg " ) {
2022-01-16 20:18:00 +00:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : FLOAT , " svg/scale " , PROPERTY_HINT_RANGE , " 0.001,100,0.001 " ) , 1.0 ) ) ;
2022-08-22 19:07:02 +00:00
// Editor use only, applies to SVG.
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : BOOL , " editor/scale_with_editor_scale " ) , false ) ) ;
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : BOOL , " editor/convert_colors_with_editor_theme " ) , false ) ) ;
2022-01-16 20:18:00 +00:00
}
2017-02-01 12:45:45 +00:00
}
2022-03-23 09:08:58 +00:00
void ResourceImporterTexture : : save_to_ctex_format ( Ref < FileAccess > f , const Ref < Image > & p_image , CompressMode p_compress_mode , Image : : UsedChannels p_channels , Image : : CompressMode p_compress_format , float p_lossy_quality ) {
2017-02-01 12:45:45 +00:00
switch ( p_compress_mode ) {
case COMPRESS_LOSSLESS : {
2022-10-18 14:43:37 +00:00
bool lossless_force_png = GLOBAL_GET ( " rendering/textures/lossless_compression/force_png " ) | |
2021-08-13 20:17:55 +00:00
! Image : : _webp_mem_loader_func ; // WebP module disabled.
2021-04-12 16:56:50 +00:00
bool use_webp = ! lossless_force_png & & p_image - > get_width ( ) < = 16383 & & p_image - > get_height ( ) < = 16383 ; // WebP has a size limit
2022-03-05 15:43:38 +00:00
f - > store_32 ( use_webp ? CompressedTexture2D : : DATA_FORMAT_WEBP : CompressedTexture2D : : DATA_FORMAT_PNG ) ;
2019-09-27 02:16:44 +00:00
f - > store_16 ( p_image - > get_width ( ) ) ;
f - > store_16 ( p_image - > get_height ( ) ) ;
f - > store_32 ( p_image - > get_mipmap_count ( ) ) ;
f - > store_32 ( p_image - > get_format ( ) ) ;
2017-02-01 12:45:45 +00:00
2019-09-27 02:16:44 +00:00
for ( int i = 0 ; i < p_image - > get_mipmap_count ( ) + 1 ; i + + ) {
2021-04-12 16:56:50 +00:00
Vector < uint8_t > data ;
if ( use_webp ) {
data = Image : : webp_lossless_packer ( p_image - > get_image_from_mipmap ( i ) ) ;
} else {
data = Image : : png_packer ( p_image - > get_image_from_mipmap ( i ) ) ;
}
2017-02-01 12:45:45 +00:00
int data_len = data . size ( ) ;
f - > store_32 ( data_len ) ;
2020-02-17 21:06:54 +00:00
const uint8_t * r = data . ptr ( ) ;
f - > store_buffer ( r , data_len ) ;
2017-02-01 12:45:45 +00:00
}
} break ;
case COMPRESS_LOSSY : {
2022-03-05 15:43:38 +00:00
f - > store_32 ( CompressedTexture2D : : DATA_FORMAT_WEBP ) ;
2019-09-27 02:16:44 +00:00
f - > store_16 ( p_image - > get_width ( ) ) ;
f - > store_16 ( p_image - > get_height ( ) ) ;
f - > store_32 ( p_image - > get_mipmap_count ( ) ) ;
f - > store_32 ( p_image - > get_format ( ) ) ;
2017-02-01 12:45:45 +00:00
2019-09-27 02:16:44 +00:00
for ( int i = 0 ; i < p_image - > get_mipmap_count ( ) + 1 ; i + + ) {
2021-04-12 16:56:50 +00:00
Vector < uint8_t > data = Image : : webp_lossy_packer ( p_image - > get_image_from_mipmap ( i ) , p_lossy_quality ) ;
2017-02-01 12:45:45 +00:00
int data_len = data . size ( ) ;
f - > store_32 ( data_len ) ;
2020-02-17 21:06:54 +00:00
const uint8_t * r = data . ptr ( ) ;
f - > store_buffer ( r , data_len ) ;
2017-02-01 12:45:45 +00:00
}
} break ;
2019-09-27 02:16:44 +00:00
case COMPRESS_VRAM_COMPRESSED : {
2017-05-17 10:36:47 +00:00
Ref < Image > image = p_image - > duplicate ( ) ;
2017-05-27 00:49:49 +00:00
2023-01-25 11:17:11 +00:00
image - > compress_from_channels ( p_compress_format , p_channels ) ;
2017-02-01 12:45:45 +00:00
2022-03-05 15:43:38 +00:00
f - > store_32 ( CompressedTexture2D : : DATA_FORMAT_IMAGE ) ;
2019-09-27 02:16:44 +00:00
f - > store_16 ( image - > get_width ( ) ) ;
f - > store_16 ( image - > get_height ( ) ) ;
f - > store_32 ( image - > get_mipmap_count ( ) ) ;
f - > store_32 ( image - > get_format ( ) ) ;
2017-02-01 12:45:45 +00:00
2020-02-17 21:06:54 +00:00
Vector < uint8_t > data = image - > get_data ( ) ;
2017-02-01 12:45:45 +00:00
int dl = data . size ( ) ;
2020-02-17 21:06:54 +00:00
const uint8_t * r = data . ptr ( ) ;
f - > store_buffer ( r , dl ) ;
2017-02-01 12:45:45 +00:00
} break ;
2019-09-27 02:16:44 +00:00
case COMPRESS_VRAM_UNCOMPRESSED : {
2022-03-05 15:43:38 +00:00
f - > store_32 ( CompressedTexture2D : : DATA_FORMAT_IMAGE ) ;
2019-09-27 02:16:44 +00:00
f - > store_16 ( p_image - > get_width ( ) ) ;
f - > store_16 ( p_image - > get_height ( ) ) ;
f - > store_32 ( p_image - > get_mipmap_count ( ) ) ;
f - > store_32 ( p_image - > get_format ( ) ) ;
2017-02-01 12:45:45 +00:00
2020-02-17 21:06:54 +00:00
Vector < uint8_t > data = p_image - > get_data ( ) ;
2017-02-01 12:45:45 +00:00
int dl = data . size ( ) ;
2020-02-17 21:06:54 +00:00
const uint8_t * r = data . ptr ( ) ;
2017-02-01 12:45:45 +00:00
2020-02-17 21:06:54 +00:00
f - > store_buffer ( r , dl ) ;
2017-02-01 12:45:45 +00:00
} break ;
2019-09-27 02:16:44 +00:00
case COMPRESS_BASIS_UNIVERSAL : {
2022-03-05 15:43:38 +00:00
f - > store_32 ( CompressedTexture2D : : DATA_FORMAT_BASIS_UNIVERSAL ) ;
2019-09-27 02:16:44 +00:00
f - > store_16 ( p_image - > get_width ( ) ) ;
f - > store_16 ( p_image - > get_height ( ) ) ;
f - > store_32 ( p_image - > get_mipmap_count ( ) ) ;
f - > store_32 ( p_image - > get_format ( ) ) ;
2022-07-19 00:58:27 +00:00
Vector < uint8_t > data = Image : : basis_universal_packer ( p_image , p_channels ) ;
int data_len = data . size ( ) ;
f - > store_32 ( data_len ) ;
const uint8_t * r = data . ptr ( ) ;
f - > store_buffer ( r , data_len ) ;
2019-09-27 02:16:44 +00:00
} break ;
2017-02-01 12:45:45 +00:00
}
2019-09-27 02:16:44 +00:00
}
2022-03-10 03:22:36 +00:00
void ResourceImporterTexture : : _save_ctex ( const Ref < Image > & p_image , const String & p_to_path , CompressMode p_compress_mode , float p_lossy_quality , Image : : CompressMode p_vram_compression , bool p_mipmaps , bool p_streamable , bool p_detect_3d , bool p_detect_roughness , bool p_detect_normal , bool p_force_normal , bool p_srgb_friendly , bool p_force_po2_for_compressed , uint32_t p_limit_mipmap , const Ref < Image > & p_normal , Image : : RoughnessChannel p_roughness_channel ) {
2022-03-23 09:08:58 +00:00
Ref < FileAccess > f = FileAccess : : open ( p_to_path , FileAccess : : WRITE ) ;
ERR_FAIL_COND ( f . is_null ( ) ) ;
2019-09-27 02:16:44 +00:00
f - > store_8 ( ' G ' ) ;
f - > store_8 ( ' S ' ) ;
f - > store_8 ( ' T ' ) ;
f - > store_8 ( ' 2 ' ) ; //godot streamable texture 2D
//format version
2022-03-05 15:43:38 +00:00
f - > store_32 ( CompressedTexture2D : : FORMAT_VERSION ) ;
2019-09-27 02:16:44 +00:00
//texture may be resized later, so original size must be saved first
f - > store_32 ( p_image - > get_width ( ) ) ;
f - > store_32 ( p_image - > get_height ( ) ) ;
uint32_t flags = 0 ;
2020-05-14 14:41:43 +00:00
if ( p_streamable ) {
2022-03-05 15:43:38 +00:00
flags | = CompressedTexture2D : : FORMAT_BIT_STREAM ;
2020-05-14 14:41:43 +00:00
}
if ( p_mipmaps ) {
2022-03-05 15:43:38 +00:00
flags | = CompressedTexture2D : : FORMAT_BIT_HAS_MIPMAPS ; //mipmaps bit
2020-05-14 14:41:43 +00:00
}
if ( p_detect_3d ) {
2022-03-05 15:43:38 +00:00
flags | = CompressedTexture2D : : FORMAT_BIT_DETECT_3D ;
2020-05-14 14:41:43 +00:00
}
if ( p_detect_roughness ) {
2022-03-05 15:43:38 +00:00
flags | = CompressedTexture2D : : FORMAT_BIT_DETECT_ROUGNESS ;
2020-05-14 14:41:43 +00:00
}
if ( p_detect_normal ) {
2022-03-05 15:43:38 +00:00
flags | = CompressedTexture2D : : FORMAT_BIT_DETECT_NORMAL ;
2020-05-14 14:41:43 +00:00
}
2019-09-27 02:16:44 +00:00
f - > store_32 ( flags ) ;
f - > store_32 ( p_limit_mipmap ) ;
2020-03-11 17:59:18 +00:00
//reserved for future use
2019-09-27 02:16:44 +00:00
f - > store_32 ( 0 ) ;
f - > store_32 ( 0 ) ;
f - > store_32 ( 0 ) ;
/*
print_line ( " streamable " + itos ( p_streamable ) ) ;
print_line ( " mipmaps " + itos ( p_mipmaps ) ) ;
print_line ( " detect_3d " + itos ( p_detect_3d ) ) ;
print_line ( " roughness " + itos ( p_detect_roughness ) ) ;
print_line ( " normal " + itos ( p_detect_normal ) ) ;
*/
if ( ( p_compress_mode = = COMPRESS_LOSSLESS | | p_compress_mode = = COMPRESS_LOSSY ) & & p_image - > get_format ( ) > Image : : FORMAT_RGBA8 ) {
p_compress_mode = COMPRESS_VRAM_UNCOMPRESSED ; //these can't go as lossy
}
Ref < Image > image = p_image - > duplicate ( ) ;
2022-07-19 00:58:27 +00:00
if ( p_force_po2_for_compressed & & p_mipmaps & & ( ( p_compress_mode = = COMPRESS_BASIS_UNIVERSAL ) | | ( p_compress_mode = = COMPRESS_VRAM_COMPRESSED ) ) ) {
2019-09-27 02:16:44 +00:00
image - > resize_to_po2 ( ) ;
}
if ( p_mipmaps & & ( ! image - > has_mipmaps ( ) | | p_force_normal ) ) {
image - > generate_mipmaps ( p_force_normal ) ;
}
if ( ! p_mipmaps ) {
image - > clear_mipmaps ( ) ;
}
2020-01-26 23:09:40 +00:00
if ( image - > has_mipmaps ( ) & & p_normal . is_valid ( ) ) {
image - > generate_mipmap_roughness ( p_roughness_channel , p_normal ) ;
}
2019-09-27 02:16:44 +00:00
Image : : CompressSource csource = Image : : COMPRESS_SOURCE_GENERIC ;
if ( p_force_normal ) {
csource = Image : : COMPRESS_SOURCE_NORMAL ;
} else if ( p_srgb_friendly ) {
csource = Image : : COMPRESS_SOURCE_SRGB ;
}
Image : : UsedChannels used_channels = image - > detect_used_channels ( csource ) ;
2022-03-10 03:22:36 +00:00
save_to_ctex_format ( f , image , p_compress_mode , used_channels , p_vram_compression , p_lossy_quality ) ;
2017-02-01 12:45:45 +00:00
}
2023-05-26 16:48:23 +00:00
void ResourceImporterTexture : : _save_editor_meta ( const Dictionary & p_metadata , const String & p_to_path ) {
Ref < FileAccess > f = FileAccess : : open ( p_to_path , FileAccess : : WRITE ) ;
ERR_FAIL_COND ( f . is_null ( ) ) ;
f - > store_var ( p_metadata ) ;
}
Dictionary ResourceImporterTexture : : _load_editor_meta ( const String & p_path ) const {
Ref < FileAccess > f = FileAccess : : open ( p_path , FileAccess : : READ ) ;
2023-08-01 08:28:36 +00:00
ERR_FAIL_COND_V_MSG ( f . is_null ( ) , Dictionary ( ) , vformat ( " Missing required editor-specific import metadata for a texture (please reimport it using the 'Import' tab): '%s' " , p_path ) ) ;
2023-05-26 16:48:23 +00:00
return f - > get_var ( ) ;
}
2022-05-13 13:04:37 +00:00
Error ResourceImporterTexture : : import ( 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 ) {
2022-08-23 10:25:14 +00:00
// Parse import options.
int32_t loader_flags = ImageFormatLoader : : FLAG_NONE ;
// Compression.
2019-09-27 02:16:44 +00:00
CompressMode compress_mode = CompressMode ( int ( p_options [ " compress/mode " ] ) ) ;
2022-02-12 01:39:10 +00:00
const float lossy = p_options [ " compress/lossy_quality " ] ;
const int pack_channels = p_options [ " compress/channel_pack " ] ;
2022-08-23 10:25:14 +00:00
const int normal = p_options [ " compress/normal_map " ] ;
const int hdr_compression = p_options [ " compress/hdr_compression " ] ;
2023-01-25 11:17:11 +00:00
const int high_quality = p_options [ " compress/high_quality " ] ;
2022-08-23 10:25:14 +00:00
// Mipmaps.
2022-02-12 01:39:10 +00:00
const bool mipmaps = p_options [ " mipmaps/generate " ] ;
const uint32_t mipmap_limit = mipmaps ? uint32_t ( p_options [ " mipmaps/limit " ] ) : uint32_t ( - 1 ) ;
2022-08-23 10:25:14 +00:00
// Roughness.
const int roughness = p_options [ " roughness/mode " ] ;
const String normal_map = p_options [ " roughness/src_normal " ] ;
// Processing.
2022-02-12 01:39:10 +00:00
const bool fix_alpha_border = p_options [ " process/fix_alpha_border " ] ;
const bool premult_alpha = p_options [ " process/premult_alpha " ] ;
const bool normal_map_invert_y = p_options [ " process/normal_map_invert_y " ] ;
2022-02-12 01:37:14 +00:00
// Support for texture streaming is not implemented yet.
const bool stream = false ;
2022-02-12 01:39:10 +00:00
const int size_limit = p_options [ " process/size_limit " ] ;
const bool hdr_as_srgb = p_options [ " process/hdr_as_srgb " ] ;
2022-08-23 10:25:14 +00:00
if ( hdr_as_srgb ) {
loader_flags | = ImageFormatLoader : : FLAG_FORCE_LINEAR ;
}
2022-02-16 00:43:44 +00:00
const bool hdr_clamp_exposure = p_options [ " process/hdr_clamp_exposure " ] ;
2022-08-23 10:25:14 +00:00
2022-01-16 20:18:00 +00:00
float scale = 1.0 ;
2022-08-23 10:25:14 +00:00
// SVG-specific options.
2022-01-16 20:18:00 +00:00
if ( p_options . has ( " svg/scale " ) ) {
scale = p_options [ " svg/scale " ] ;
}
2020-01-26 23:09:40 +00:00
2022-08-22 19:07:02 +00:00
// Editor-specific options.
bool use_editor_scale = p_options . has ( " editor/scale_with_editor_scale " ) & & p_options [ " editor/scale_with_editor_scale " ] ;
bool convert_editor_colors = p_options . has ( " editor/convert_colors_with_editor_theme " ) & & p_options [ " editor/convert_colors_with_editor_theme " ] ;
// Start importing images.
List < Ref < Image > > images_imported ;
// Load the normal image.
2020-01-26 23:09:40 +00:00
Ref < Image > normal_image ;
2020-03-31 11:50:25 +00:00
Image : : RoughnessChannel roughness_channel = Image : : ROUGHNESS_CHANNEL_R ;
2020-01-26 23:09:40 +00:00
if ( mipmaps & & roughness > 1 & & FileAccess : : exists ( normal_map ) ) {
2021-06-17 22:03:09 +00:00
normal_image . instantiate ( ) ;
2020-01-26 23:09:40 +00:00
if ( ImageLoader : : load_image ( normal_map , normal_image ) = = OK ) {
roughness_channel = Image : : RoughnessChannel ( roughness - 2 ) ;
}
}
2022-08-23 10:25:14 +00:00
2022-08-22 19:07:02 +00:00
// Load the main image.
2017-05-17 10:36:47 +00:00
Ref < Image > image ;
2021-06-17 22:03:09 +00:00
image . instantiate ( ) ;
2022-08-23 10:25:14 +00:00
Error err = ImageLoader : : load_image ( p_source_file , image , nullptr , loader_flags , scale ) ;
2020-05-14 14:41:43 +00:00
if ( err ! = OK ) {
2017-02-01 12:45:45 +00:00
return err ;
2020-05-14 14:41:43 +00:00
}
2022-08-22 19:07:02 +00:00
images_imported . push_back ( image ) ;
2017-02-01 12:45:45 +00:00
2022-08-22 19:07:02 +00:00
// Load the editor-only image.
Ref < Image > editor_image ;
bool import_editor_image = use_editor_scale | | convert_editor_colors ;
if ( import_editor_image ) {
float editor_scale = scale ;
if ( use_editor_scale ) {
editor_scale = scale * EDSCALE ;
}
2017-02-01 12:45:45 +00:00
2022-08-22 19:07:02 +00:00
int32_t editor_loader_flags = loader_flags ;
if ( convert_editor_colors ) {
editor_loader_flags | = ImageFormatLoader : : FLAG_CONVERT_COLORS ;
2017-02-01 12:45:45 +00:00
}
2018-07-03 13:55:50 +00:00
2022-08-22 19:07:02 +00:00
editor_image . instantiate ( ) ;
err = ImageLoader : : load_image ( p_source_file , editor_image , nullptr , editor_loader_flags , editor_scale ) ;
if ( err ! = OK ) {
WARN_PRINT ( " Failed to import an image resource for editor use from ' " + p_source_file + " ' " ) ;
} else {
images_imported . push_back ( editor_image ) ;
2018-07-03 13:55:50 +00:00
}
2017-02-01 12:45:45 +00:00
}
2022-08-22 19:07:02 +00:00
for ( Ref < Image > & target_image : images_imported ) {
// Apply the size limit.
if ( size_limit > 0 & & ( target_image - > get_width ( ) > size_limit | | target_image - > get_height ( ) > size_limit ) ) {
if ( target_image - > get_width ( ) > = target_image - > get_height ( ) ) {
int new_width = size_limit ;
int new_height = target_image - > get_height ( ) * new_width / target_image - > get_width ( ) ;
2017-02-01 12:45:45 +00:00
2022-08-22 19:07:02 +00:00
target_image - > resize ( new_width , new_height , Image : : INTERPOLATE_CUBIC ) ;
} else {
int new_height = size_limit ;
int new_width = target_image - > get_width ( ) * new_height / target_image - > get_height ( ) ;
2017-02-01 12:45:45 +00:00
2022-08-22 19:07:02 +00:00
target_image - > resize ( new_width , new_height , Image : : INTERPOLATE_CUBIC ) ;
}
2018-08-17 16:28:07 +00:00
2022-08-22 19:07:02 +00:00
if ( normal = = 1 ) {
target_image - > normalize ( ) ;
2018-08-17 16:28:07 +00:00
}
}
2022-08-22 19:07:02 +00:00
// Fix alpha border.
if ( fix_alpha_border ) {
target_image - > fix_alpha_edges ( ) ;
}
2022-02-16 00:43:44 +00:00
2022-08-22 19:07:02 +00:00
// Premultiply the alpha.
if ( premult_alpha ) {
target_image - > premultiply_alpha ( ) ;
}
2022-02-16 00:43:44 +00:00
2022-08-22 19:07:02 +00:00
// Invert the green channel of the image to flip the normal map it contains.
if ( normal_map_invert_y ) {
// Inverting the green channel can be used to flip a normal map's direction.
// There's no standard when it comes to normal map Y direction, so this is
// sometimes needed when using a normal map exported from another program.
// See <http://wiki.polycount.com/wiki/Normal_Map_Technical_Details#Common_Swizzle_Coordinates>.
const int height = target_image - > get_height ( ) ;
const int width = target_image - > get_width ( ) ;
for ( int i = 0 ; i < width ; i + + ) {
for ( int j = 0 ; j < height ; j + + ) {
const Color color = target_image - > get_pixel ( i , j ) ;
target_image - > set_pixel ( i , j , Color ( color . r , 1 - color . g , color . b ) ) ;
2022-02-16 00:43:44 +00:00
}
2022-08-22 19:07:02 +00:00
}
}
2022-02-16 00:43:44 +00:00
2022-08-22 19:07:02 +00:00
// Clamp HDR exposure.
if ( hdr_clamp_exposure ) {
// Clamp HDR exposure following Filament's tonemapping formula.
// This can be used to reduce fireflies in environment maps or reduce the influence
// of the sun from an HDRI panorama on environment lighting (when a DirectionalLight3D is used instead).
const int height = target_image - > get_height ( ) ;
const int width = target_image - > get_width ( ) ;
// These values are chosen arbitrarily and seem to produce good results with 4,096 samples.
const float linear = 4096.0 ;
const float compressed = 16384.0 ;
for ( int i = 0 ; i < width ; i + + ) {
for ( int j = 0 ; j < height ; j + + ) {
const Color color = target_image - > get_pixel ( i , j ) ;
const float luma = color . get_luminance ( ) ;
Color clamped_color ;
if ( luma < = linear ) {
clamped_color = color ;
} else {
clamped_color = ( color / luma ) * ( ( linear * linear - compressed * luma ) / ( 2 * linear - compressed - luma ) ) ;
}
target_image - > set_pixel ( i , j , clamped_color ) ;
}
2022-02-16 00:43:44 +00:00
}
}
}
2019-09-27 02:16:44 +00:00
if ( compress_mode = = COMPRESS_BASIS_UNIVERSAL & & image - > get_format ( ) > = Image : : FORMAT_RF ) {
2022-08-22 19:07:02 +00:00
// Basis universal does not support float formats, fallback.
2019-09-27 02:16:44 +00:00
compress_mode = COMPRESS_VRAM_COMPRESSED ;
}
bool detect_3d = int ( p_options [ " detect_3d/compress_to " ] ) > 0 ;
2019-06-11 18:43:37 +00:00
bool detect_roughness = roughness = = 0 ;
2017-06-17 00:47:28 +00:00
bool detect_normal = normal = = 0 ;
bool force_normal = normal = = 1 ;
2019-06-11 18:43:37 +00:00
bool srgb_friendly_pack = pack_channels = = 0 ;
2017-02-01 12:45:45 +00:00
2022-08-22 19:07:02 +00:00
Array formats_imported ;
2019-09-27 02:16:44 +00:00
if ( compress_mode = = COMPRESS_VRAM_COMPRESSED ) {
2022-08-22 19:07:02 +00:00
// Must import in all formats, in order of priority (so platform choses the best supported one. IE, etc2 over etc).
// Android, GLES 2.x
2017-09-05 23:17:26 +00:00
2022-01-18 12:39:55 +00:00
const bool is_hdr = ( image - > get_format ( ) > = Image : : FORMAT_RF & & image - > get_format ( ) < = Image : : FORMAT_RGBE9995 ) ;
2023-06-20 01:03:51 +00:00
const bool can_s3tc_bptc = ResourceImporterTextureSettings : : should_import_s3tc_bptc ( ) ;
const bool can_etc2_astc = ResourceImporterTextureSettings : : should_import_etc2_astc ( ) ;
2017-09-05 23:17:26 +00:00
2023-01-25 11:17:11 +00:00
// Add list of formats imported
if ( can_s3tc_bptc ) {
formats_imported . push_back ( " s3tc_bptc " ) ;
}
if ( can_etc2_astc ) {
formats_imported . push_back ( " etc2_astc " ) ;
2020-05-01 12:34:23 +00:00
}
2018-08-28 03:05:04 +00:00
2020-05-01 12:34:23 +00:00
bool can_compress_hdr = hdr_compression > 0 ;
bool has_alpha = image - > detect_alpha ( ) ! = Image : : ALPHA_NONE ;
2023-01-25 11:17:11 +00:00
bool use_uncompressed = false ;
2020-05-01 12:34:23 +00:00
2023-01-25 11:17:11 +00:00
if ( is_hdr ) {
2020-05-01 12:34:23 +00:00
if ( has_alpha ) {
2022-08-22 19:07:02 +00:00
// Can compress HDR, but HDR with alpha is not compressible.
2020-05-01 12:34:23 +00:00
if ( hdr_compression = = 2 ) {
2022-08-22 19:07:02 +00:00
// But user selected to compress HDR anyway, so force an alpha-less format.
2020-05-01 12:34:23 +00:00
if ( image - > get_format ( ) = = Image : : FORMAT_RGBAF ) {
image - > convert ( Image : : FORMAT_RGBF ) ;
} else if ( image - > get_format ( ) = = Image : : FORMAT_RGBAH ) {
image - > convert ( Image : : FORMAT_RGBH ) ;
}
} else {
can_compress_hdr = false ;
2018-08-28 03:05:04 +00:00
}
2020-05-01 12:34:23 +00:00
}
2018-08-28 03:05:04 +00:00
2022-01-18 12:39:55 +00:00
if ( ! can_compress_hdr ) {
2022-08-22 19:07:02 +00:00
// Fallback to RGBE99995.
2022-01-18 12:39:55 +00:00
if ( image - > get_format ( ) ! = Image : : FORMAT_RGBE9995 ) {
image - > convert ( Image : : FORMAT_RGBE9995 ) ;
2023-01-25 11:17:11 +00:00
use_uncompressed = true ;
2018-08-28 03:05:04 +00:00
}
2020-05-01 12:34:23 +00:00
}
2018-08-28 03:05:04 +00:00
}
2018-08-22 02:56:04 +00:00
2023-01-25 11:17:11 +00:00
if ( use_uncompressed ) {
_save_ctex ( image , p_save_path + " .ctex " , COMPRESS_VRAM_UNCOMPRESSED , lossy , Image : : COMPRESS_S3TC /*this is ignored */ , mipmaps , stream , detect_3d , detect_roughness , detect_normal , force_normal , srgb_friendly_pack , false , mipmap_limit , normal_image , roughness_channel ) ;
} else {
if ( can_s3tc_bptc ) {
Image : : CompressMode image_compress_mode ;
String image_compress_format ;
if ( high_quality | | is_hdr ) {
image_compress_mode = Image : : COMPRESS_BPTC ;
image_compress_format = " bptc " ;
} else {
image_compress_mode = Image : : COMPRESS_S3TC ;
image_compress_format = " s3tc " ;
}
_save_ctex ( image , p_save_path + " . " + image_compress_format + " .ctex " , compress_mode , lossy , image_compress_mode , mipmaps , stream , detect_3d , detect_roughness , detect_normal , force_normal , srgb_friendly_pack , false , mipmap_limit , normal_image , roughness_channel ) ;
r_platform_variants - > push_back ( image_compress_format ) ;
2022-01-18 12:39:55 +00:00
}
2017-06-17 00:47:28 +00:00
2023-01-25 11:17:11 +00:00
if ( can_etc2_astc ) {
Image : : CompressMode image_compress_mode ;
String image_compress_format ;
if ( high_quality | | is_hdr ) {
image_compress_mode = Image : : COMPRESS_ASTC ;
image_compress_format = " astc " ;
} else {
image_compress_mode = Image : : COMPRESS_ETC2 ;
image_compress_format = " etc2 " ;
}
_save_ctex ( image , p_save_path + " . " + image_compress_format + " .ctex " , compress_mode , lossy , image_compress_mode , mipmaps , stream , detect_3d , detect_roughness , detect_normal , force_normal , srgb_friendly_pack , false , mipmap_limit , normal_image , roughness_channel ) ;
r_platform_variants - > push_back ( image_compress_format ) ;
}
2017-09-05 23:17:26 +00:00
}
2017-02-01 12:45:45 +00:00
} else {
2022-08-22 19:07:02 +00:00
// Import normally.
2022-03-10 03:22:36 +00:00
_save_ctex ( image , p_save_path + " .ctex " , compress_mode , lossy , Image : : COMPRESS_S3TC /*this is ignored */ , mipmaps , stream , detect_3d , detect_roughness , detect_normal , force_normal , srgb_friendly_pack , false , mipmap_limit , normal_image , roughness_channel ) ;
2017-02-01 12:45:45 +00:00
}
2022-08-22 19:07:02 +00:00
if ( editor_image . is_valid ( ) ) {
_save_ctex ( editor_image , p_save_path + " .editor.ctex " , compress_mode , lossy , Image : : COMPRESS_S3TC /*this is ignored */ , mipmaps , stream , detect_3d , detect_roughness , detect_normal , force_normal , srgb_friendly_pack , false , mipmap_limit , normal_image , roughness_channel ) ;
2023-05-26 16:48:23 +00:00
// Generate and save editor-specific metadata, which we cannot save to the .import file.
Dictionary editor_meta ;
if ( use_editor_scale ) {
editor_meta [ " editor_scale " ] = EDSCALE ;
}
if ( convert_editor_colors ) {
editor_meta [ " editor_dark_theme " ] = EditorSettings : : get_singleton ( ) - > is_dark_theme ( ) ;
}
_save_editor_meta ( editor_meta , p_save_path + " .editor.meta " ) ;
2022-08-22 19:07:02 +00:00
}
2019-02-26 21:43:37 +00:00
if ( r_metadata ) {
2022-09-29 09:53:28 +00:00
Dictionary meta ;
meta [ " vram_texture " ] = compress_mode = = COMPRESS_VRAM_COMPRESSED ;
2019-02-26 21:43:37 +00:00
if ( formats_imported . size ( ) ) {
2022-09-29 09:53:28 +00:00
meta [ " imported_formats " ] = formats_imported ;
2019-02-26 21:43:37 +00:00
}
2022-08-22 19:07:02 +00:00
if ( editor_image . is_valid ( ) ) {
2022-09-29 09:53:28 +00:00
meta [ " has_editor_variant " ] = true ;
2022-08-22 19:07:02 +00:00
}
2022-09-29 09:53:28 +00:00
* r_metadata = meta ;
2019-02-26 21:43:37 +00:00
}
2023-05-26 16:48:23 +00:00
2017-02-01 12:45:45 +00:00
return OK ;
}
2019-02-26 21:43:37 +00:00
const char * ResourceImporterTexture : : compression_formats [ ] = {
2023-01-25 11:17:11 +00:00
" s3tc_bptc " ,
" etc2_astc " ,
2020-04-01 23:20:12 +00:00
nullptr
2019-02-26 21:43:37 +00:00
} ;
String ResourceImporterTexture : : get_import_settings_string ( ) const {
String s ;
int index = 0 ;
while ( compression_formats [ index ] ) {
2021-02-17 16:44:49 +00:00
String setting_path = " rendering/textures/vram_compression/import_ " + String ( compression_formats [ index ] ) ;
2022-10-18 14:43:37 +00:00
bool test = GLOBAL_GET ( setting_path ) ;
2019-02-26 21:43:37 +00:00
if ( test ) {
s + = String ( compression_formats [ index ] ) ;
}
index + + ;
}
return s ;
}
bool ResourceImporterTexture : : are_import_settings_valid ( const String & p_path ) const {
2022-09-29 09:53:28 +00:00
Dictionary meta = ResourceFormatImporter : : get_singleton ( ) - > get_resource_metadata ( p_path ) ;
2019-02-26 21:43:37 +00:00
2022-09-29 09:53:28 +00:00
if ( meta . has ( " has_editor_variant " ) ) {
2023-05-26 16:48:23 +00:00
String imported_path = ResourceFormatImporter : : get_singleton ( ) - > get_internal_resource_path ( p_path ) ;
String editor_meta_path = imported_path . replace ( " .editor.ctex " , " .editor.meta " ) ;
Dictionary editor_meta = _load_editor_meta ( editor_meta_path ) ;
if ( editor_meta . has ( " editor_scale " ) & & ( float ) editor_meta [ " editor_scale " ] ! = EDSCALE ) {
2022-08-22 19:07:02 +00:00
return false ;
}
2023-05-26 16:48:23 +00:00
if ( editor_meta . has ( " editor_dark_theme " ) & & ( bool ) editor_meta [ " editor_dark_theme " ] ! = EditorSettings : : get_singleton ( ) - > is_dark_theme ( ) ) {
2022-08-22 19:07:02 +00:00
return false ;
}
}
2022-09-29 09:53:28 +00:00
if ( ! meta . has ( " vram_texture " ) ) {
2019-02-26 21:43:37 +00:00
return false ;
}
2022-09-29 09:53:28 +00:00
bool vram = meta [ " vram_texture " ] ;
2019-02-26 21:43:37 +00:00
if ( ! vram ) {
2022-08-22 19:07:02 +00:00
return true ; // Do not care about non-VRAM.
2019-02-26 21:43:37 +00:00
}
2023-05-26 16:48:23 +00:00
// Will become invalid if formats are missing to import.
2019-02-26 21:43:37 +00:00
Vector < String > formats_imported ;
2022-09-29 09:53:28 +00:00
if ( meta . has ( " imported_formats " ) ) {
formats_imported = meta [ " imported_formats " ] ;
2019-02-26 21:43:37 +00:00
}
int index = 0 ;
bool valid = true ;
while ( compression_formats [ index ] ) {
2021-02-17 16:44:49 +00:00
String setting_path = " rendering/textures/vram_compression/import_ " + String ( compression_formats [ index ] ) ;
2023-01-25 11:17:11 +00:00
if ( ProjectSettings : : get_singleton ( ) - > has_setting ( setting_path ) ) {
bool test = GLOBAL_GET ( setting_path ) ;
if ( test ) {
if ( ! formats_imported . has ( compression_formats [ index ] ) ) {
valid = false ;
break ;
}
2019-02-26 21:43:37 +00:00
}
2023-01-25 11:17:11 +00:00
} else {
WARN_PRINT ( " Setting for imported format not found: " + setting_path ) ;
2019-02-26 21:43:37 +00:00
}
index + + ;
}
return valid ;
}
2020-04-01 23:20:12 +00:00
ResourceImporterTexture * ResourceImporterTexture : : singleton = nullptr ;
2017-02-06 03:38:39 +00:00
2023-08-07 15:00:46 +00:00
ResourceImporterTexture : : ResourceImporterTexture ( bool p_singleton ) {
// This should only be set through the EditorNode.
if ( p_singleton ) {
2023-07-28 09:52:09 +00:00
singleton = this ;
}
2023-08-07 15:00:46 +00:00
2022-03-05 15:43:38 +00:00
CompressedTexture2D : : request_3d_callback = _texture_reimport_3d ;
CompressedTexture2D : : request_roughness_callback = _texture_reimport_roughness ;
CompressedTexture2D : : request_normal_callback = _texture_reimport_normal ;
2017-02-01 12:45:45 +00:00
}
2017-02-06 03:38:39 +00:00
ResourceImporterTexture : : ~ ResourceImporterTexture ( ) {
2023-07-28 09:52:09 +00:00
if ( singleton = = this ) {
singleton = nullptr ;
}
2017-02-06 03:38:39 +00:00
}