shader parameter works in the inspector but not at run time

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By BusterDublup

hi. I’m new to shaders and i followed a tutorial to make an outline shader with a boolean shader parameter that turns the outline on and off. if i check or uncheck the parameter in the inspector it works but if i try to change the parameter at runtime it doesn’t.

shader_type canvas_item;

uniform vec4 flash_color : hint_color = vec4(1.0);
uniform float flash_modifier : hint_range(0.0, 1.0) = 0.0;

uniform vec4 outline_color : hint_color;
uniform bool outlined;

void fragment(){
	vec4 color = texture(TEXTURE, UV);
	color.rgb = mix(color.rgb, flash_color.rgb, flash_modifier);
	COLOR = color;
	if (outlined) {
		float size_x = 1.0/float(textureSize(TEXTURE, 0).x);
		float size_y = 1.0/float(textureSize(TEXTURE, 0).y);
		vec4 sprite_color = texture(TEXTURE, UV);
		float alpha = -8.0 * sprite_color.a;
		alpha += texture(TEXTURE, UV + vec2(size_x, 0)).a;
		alpha += texture(TEXTURE, UV + vec2(-size_x, 0)).a;
		alpha += texture(TEXTURE, UV + vec2(0, size_y)).a;
		alpha += texture(TEXTURE, UV + vec2(0, -size_y)).a;
		alpha += texture(TEXTURE, UV + vec2(size_x, size_y)).a;
		alpha += texture(TEXTURE, UV + vec2(-size_x, size_y)).a;
		alpha += texture(TEXTURE, UV + vec2(-size_x, -size_y)).a;
		alpha += texture(TEXTURE, UV + vec2(size_x, -size_y)).a;
		vec4 final_color = mix(sprite_color, outline_color, clamp(alpha, 0.0, 1.0));
		COLOR = vec4(final_color.rgb, clamp(abs(alpha) + sprite_color.a, 0.0, 1.0));

i have an area2d on top of the sprite with the shader. im trying to change the outline when i mouse over the sprite.

func _on_Dir_Area2D_mouse_entered() -> void:
	mouse_over = true
	print("area entered ", material.get_shader_param("outlined"))

func _on_Dir_Area2D_mouse_exited() -> void:
	mouse_over = false
	print("area exited ", material.get_shader_param("outlined"))

func set_outline(value: bool):
	material.set_shader_param("outlined", value)

this prints “area entered True” when i mouse over and " area exited False" when i move the mouse away but no outline.

what am i doing wrong? please help.

I’m using a saved material with this sprite that i use on other sprites too and it works on the other sprites. i don’t know if thats somehow affecting it in some way maybe. just thought i should include that info too.

The problem doesn’t seem to be in the code you posted. Is that code in a script on the sprite? Or is it on the Area2D?
My gut feeling is that you’re not modifying the correct material.

Zylann | 2020-09-14 12:59

thanks for the reply. the script is on the parent node of the sprite and area2d. that parent node has the material attached too. the sprite is set to “use parent material” under CanvasItem.

![image of my scene setup][comment1-1]

Imgur: The magic of the Internet

BusterDublup | 2020-09-14 19:10

Does the Area2D also have use parent material enabled? (note, if both sprite and area2d have that option, they dont need to have any material set)

Zylann | 2020-09-14 20:47

area2d does not have use parent material enabled nor does it have a material. just now i tried enabling use parent material on the area2d and it didnt do anything.

BusterDublup | 2020-09-14 20:54

:bust_in_silhouette: Reply From: BusterDublup

i figured out what was wrong. thanks to your tip zylann, i checked the material on the instance of my player scene that was being shown in my main scene and somewhere along the way during testing i attached a different material to that instance without realizing it and that material was the one being called by my code instead of the material in the actual player scene itself like i thought i was being calling.
problem solved.