How to add a shader and a mask to a sprite using gdscript?

Godot Version



I’m trying to add a shader and mask to a sprite in gdscript.

The code below adds the shader ok, but I’m stuck how to add the mask next?

var material_thismask = load("res://Shaders/Test2.gdshader")
$Control2/Final3.material =
$Control2/Final3.material.set("shader", material_thismask)

This is the shader code itself:

shader_type canvas_item;

uniform sampler2D mask;

void fragment( )
    vec4 mask_color = texture(mask, UV).rgba;
    vec4 sprite_color = texture(TEXTURE, UV).rgba;
    if (mask_color.a == 0.0)
        sprite_color.a = 0.0;
    COLOR = sprite_color;

I need to pass an image through to the Mask. Looking at inspector the hint suggests “shader_parameter/mask” but honestly I’m stuck.

I tried this but no luck:

$Control2/Final3.material.set_shader_parameter("mask", full_path)

I can complete this task ok manually in inspector but I’m doing this for a lot of sprites (cannot predict in advance how many) so want to do it all in code. Thanks!

UPDATE - solved it eventually:

var piece_mask = load(full_path)
$Control2/Final3.material.set_shader_parameter(mask, piece_mask)