11class_name VTFLoader extends RefCounted
2+ enum SRGBConversionMethod {
3+ DISABLED ,
4+ DURING_IMPORT ,
5+ PROCESS_IN_SHADER
6+ }
7+
28enum ImageFormat {
39 IMAGE_FORMAT_RGBA8888 ,
410 IMAGE_FORMAT_ABGR8888 ,
@@ -203,7 +209,7 @@ static func create(path: String, duration: float = 0):
203209
204210func done (): file .close ();
205211
206- func compile_texture ():
212+ func compile_texture (srgb_conversion_method : SRGBConversionMethod ):
207213 if width == 0 or height == 0 :
208214 push_error ("Corrupted file: {0} " .format ([file .get_path ()]));
209215 return null ;
@@ -215,11 +221,11 @@ func compile_texture():
215221 tex .frames = frames ;
216222
217223 for frame in range (0 , frames ):
218- tex .set_frame_texture (frame , _read_frame (frame ));
224+ tex .set_frame_texture (frame , _read_frame (frame , srgb_conversion_method ));
219225 tex .set_frame_duration (frame , frame_duration );
220226
221227 else :
222- tex = _read_frame (0 );
228+ tex = _read_frame (0 , srgb_conversion_method );
223229
224230 if not tex :
225231 push_error ("Texture not loaded: {0} " .format ([path ]));
@@ -230,13 +236,12 @@ func compile_texture():
230236static var normal_conversion_shader : Shader ;
231237static var shader_material : ShaderMaterial ;
232238
233- func _read_frame (frame ):
239+ func _read_frame (frame , srgb_conversion_method : SRGBConversionMethod ):
234240 var data = PackedByteArray ();
235241 var byteRead = 0 ;
236242 var is_dxt_1 = hires_image_format == ImageFormat .IMAGE_FORMAT_DXT1 ;
237243 var format = format_map [str (hires_image_format )];
238244 var use_mipmaps = not (flags & Flags .TEXTUREFLAGS_NOMIP );
239-
240245 frame = frames - 1 - frame ;
241246
242247 for i in range (mipmap_count ):
@@ -251,7 +256,11 @@ func _read_frame(frame):
251256
252257 byteRead += mip_size + mip_size * (frames - 1 );
253258
254- var img = Image .create_from_data (width , height , use_mipmaps , format , data );
259+ var img := Image .create_from_data (width , height , use_mipmaps , format , data );
260+
261+ if srgb_conversion_method == SRGBConversionMethod .DURING_IMPORT :
262+ img .decompress ();
263+ img .compress (Image .COMPRESS_S3TC );
255264
256265 alpha = flags & Flags .TEXTUREFLAGS_ONEBITALPHA or flags & Flags .TEXTUREFLAGS_EIGHTBITALPHA ;
257266
@@ -266,7 +275,6 @@ func _init(path, duration):
266275 self .frame_duration = duration ;
267276
268277 file = FileAccess .open (path , FileAccess .READ );
269- prints ('VTFFLAGS' , flags );
270278
271279static func get_texture (texture : String ):
272280 texture = texture .to_lower ();
0 commit comments