I'm loading my images using load()
like this:
get_node("%CustomSprite").texture=load(img_path)
but they're being loaded in blurry, I saw this video to fix it,
so is there any way to reimport images via gdscript?
or any work around to make the image sharp?
CodePudding user response:
First of all, I'll point out that if you are using load
, that texture is already imported.
Now, let us see. I'll give you three solutions, from more to less involved. All tested in Godot 3.5.
Re-import the texture
If you are writing an EditorPlugin
, re-import makes some sense. Sadly, I'm not aware of a direct way to do it.
Instead, you can open the file with .import
extension that Godot creates for the asset (it contains the import configuration) with ConfigFile
(it is basically an INI file).
var config := ConfigFile.new()
config.load(img_path ".import")
In the .import
file you can find what are the imported resources (I mean, the resource files in Godot binary format which are located in the .import
folder). To get the list, read the "dest_files"
of the "deps"
section.
var dest_files:Array = config.get_value("deps", "dest_files")
Since you want to force a re-import you can delete them:
var dir := Directory.new()
for dest_file in dest_files:
dir.remove(dest_file)
Or move them to trash:
for dest_file in dest_files:
OS.move_to_trash(ProjectSettings.globalize_path(dest_file))
Now, you want to change the settings so they are not blurry. I believe, in particular you want to set the value "flags/filter"
of the "params"
section to false
.
config.set_value("params", "flags/filter", false)
Ah, you need to save it!
config.save(img_path ".import")
And now tell Godot to re-scan the files calling scan
from the EditorFileSystem
class (you get an instance calling get_editor_interface().get_resource_filesystem()
from your EditorPlugin
).
get_editor_interface().get_resource_filesystem().scan()
It should re-import resource (generate again the files in the ".import"
folder).
Before you load the resource again, know that scanning the file system is not synchronous. So you want to yield on it.
yield(get_editor_interface().get_resource_filesystem(), "filesystem_changed")
One more thing. Even though you are re-importing the resource. It might be in cache. So, load it with ResourceLoader.load
and specify to bypass the cache:
var my_texture = ResourceLoader.load(img_path, "", true)
Creating a texture form an image file
Please notice that re-importing a resource modifies the project.
If you have an image file and want a texture from it, you can read the image file and skip the whole resource import system.
Let us read the contents of the image file:
var file := File.new()
file.open(img_path, File.READ)
var buffer := file.get_buffer(file.get_len())
file.close()
And now, let us create an Image
object from that (I'm assuming it is a png):
var my_image := Image.new()
my_image.load_png_from_buffer(buffer)
And now a texture from it:
var my_texture := ImageTexture.new()
my_texture.create_from_image(my_image)
About making sure it is not blurry, you want to set the flags
property. In particular remove FLAG_FILTER
.
If you are working with VisualServer
, you might want to use texture_create_from_image
instead:
var texture_rid := VisualServer.texture_create_from_image(my_image)
And for the flags, you can use VisualServer.texture_set_flags
. Again, remove FLAG_FILTER
to make it not blurry.
Just change texture flags
So, at the end you need to make sure FLAG_FILTER
is not set. I believe you should be able to modify it after loading:
var my_texture = load(img_path)
my_texture.flags = 0