Setting texture filter on a viewport texture

Godot Version

4.3

Question

I’m using a viewport texture (2d scene) in a material for 3d (custom shader).In Godot 3.x I used to set my viewport texture's linear filter on/off with something like

node.get_node(“2dViewport”).get_texture().set_flags(Texture.FLAG_REPEAT)
node.get_node(“2dViewport”).get_texture().set_flags(Texture.FLAG_DEFAULT)

This let me change the viewport texture filtering at the press of a button. How can I do this in Godot 4 with gdscript? The subviewport itself has options ‘Canvas Items → Default Texture Filter’ but these do nothing for me.

you should be able to just set it:

viewport.canvas_item_default_texture_filter = DefaultCanvasItemTextureFilter.[...]
1 Like

It’s not doing anything for me

func _on_filter_toggled(toggled_on: bool) → void:
if toggled_on:
print(“filter linear”)
%viewport1.canvas_item_default_texture_filter = Viewport.DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR
else:
print(“filter nearest”)
%viewport1.canvas_item_default_texture_filter = Viewport.DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST

The only way I can change it is by setting it as a hint in the shader, but then I can’t change it with a button : /

Filtering has been moved to the Shader itself as uniform hints In your case it would be something like: uniform sampler2D viewport_texture: filter_linear; for example.

1 Like

That can’t be changed runtime anymore then…?

You can swap the shader.

Right. That’s um, ideal. So the options on the subviewport, do they just not do anything?

That’s for drawing into viewport.

Filtering is property of a sampler, not the texture itself, so it makes more sense to decouple it from the texture, which is what they did in v 4.

Make two versions of the shader and swap. If you need many shaders, move the sampler uniform declaration to a shader include.