How to add a shader to a Sprite2D (all by GDScript)?

Godot Version



How can you add a custom shader to a Sprite2D via GDScript?

I was able to create the Sprite2D, assign a texture and add it to the scene, but I’m unsure how to assign my Fragment-Shader to the Sprite2D-Object.


You create a ShaderMaterial and set the shader to your shader and then assign the material of the node with the material you vreay

How do I write this in GDSript, when ‘my_spr’ contains my Sprite2D and ‘my_sha’ my shader resource?

var my_mat =
my_mat.shader = my_sha
my_spr.material = my_mat

Works like a charm. Thanks.

Another one in this respect: How do you set a filter-method of Sprite2D ‘my_spr’ via GDScript (in the Inspektor it goes by CanvasItem/Texture/Filter)?

Does the Inspektor show you, what kind of ‘commands’ you’re triggering when using it?

You set it with a hint:

uniform sampler2D my_tex : filter_nearest

For example, see here

It works when you pass one specific texture from GDScript to the shader, but how do you set f.e. the filter to filter_nearest, for any texture (TEXTURE) the shader is attached to? When I pass an array of specific textures, I also have to go through each one in the shader, which is probably not the way to go.