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

Godot Version

4.3beta2

Question

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.

Thanks

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 = ShaderMaterial.new()
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.

I could smooch you wet lip style right now. I dont know how a solution this unbelievably simple was this hard to find. Every result when searching this up was for things almost entirely unrelated, and you are probably the only person on the internet who has actually given an answer to this. You have saved me hours of time manually going through countless nodes in a scene and hitting “Make unique” on the material.