Change default texture filtering options through code

Godot Version

4.2.2

I try to add a button in options that would change the default texture filtering from nearest to linear for the game to look adequate on a 720p resolution but I honestly have no idea where to start

I’m not sure that’s possible but I could be wrong. Since the texture filtering is used when you import a texture to your project. Changing this would require you change the setting and then re-import all the textures in your project for the change to take effect. I don’t think this is possible to do at runtime. Possibly with a plugin and/or a require a game restart.

You can change the default texture filtering using the ProjectSettings class with the set_setting() method like this:

ProjectSettings.set_setting("rendering/textures/canvas_textures/default_texture_filter",  1)
ProjectSettings.save()

The only real solution I can see is to store keep two versions of every texture in your game, one for each filtering and hot-swap every texture in your game depending on the resolution setting, but I don’t think that’s feasible. Maybe someone else has a better solution.

yeah, so far I’ve came to the same solution as you actually which idk if it would even export properly, maybe there’s a shader or something for it

1 Like

This was in Godot 3, in Godot 4 this option is not a import setting anymore, instead you set the viewport filtering option.

For change that in runtime you can set the get_viewport().canvas_item_default_texture_filter property (see more: Viewport — Godot Engine (stable) documentation in English )

1 Like

Well look at that! Thanks for clearing that up :+1:

1 Like

Btw you can also set that in the project settings, but keep in mind the project settings is only read at the game start, so doesn’t work to change in runtime.

1 Like

hell yeah, thank you so much, you are my savior, it worked,

I’ve set it up to a signal from a button in options like this:

func _on_filtering_toggled(toggled_on):
	if toggled_on == true:
		get_viewport().canvas_item_default_texture_filter = Viewport.DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR
	if toggled_on == false:
		get_viewport().canvas_item_default_texture_filter = Viewport.DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST
1 Like