Resetting shader values when using tweens causes errors
Asked Answered
S

4

0

I have a shader:

shader_type canvas_item;
uniform float progress: hint_range(0.0, 1.0) = 0.0;

void fragment() {
	COLOR.a = step(UV.x, progress);	
}

that is supposed to reveal an image via a tween when a button is pressed. Hence, I am controlling the shader via this code: (the code runs in the parent of the node with the shader)

extends Node2D

func _process(delta):
	if Input.is_action_just_pressed("ui_accept"):
		var tween = create_tween()
		tween.tween_property($Icon, "material:shader_parameter/progress",1,0.5).set_trans(Tween.TRANS_QUAD)
		tween.connect('finished', reset)

func reset():
	$Icon.material.set_shader_parameter('progress',0)

After the animation is done I want to image to disappear and be ready to reappear when a button is pressed again. For that purpose I added the reset function that gets triggered when the tween is done.

That last part is the problem: When I run the shader via the tween for the first time it works just fine but after that it doesn't work anymore even though the value of progress was reset to 0. Am I missing something? I don't really get why it doesn't work anymore.

Sawfly answered 18/10, 2023 at 12:21 Comment(0)
P
0

Sawfly If progress is 0 then step function in the shader will always return 1. You need to set progress to 1 if you want step to always return 0... or swap the arguments when calling step.

Personality answered 18/10, 2023 at 12:37 Comment(0)
S
0

But the default value is 0 and it works on the first go. Only after I try to reset it do I get the weird behaviour.

Sawfly answered 18/10, 2023 at 12:58 Comment(0)
P
0

Sawfly You're right, the shader is fine. The problem is with set_shader_parameter(). Try:

$Icon.material.set_shader_parameter('progress', 0.0)
Personality answered 18/10, 2023 at 14:15 Comment(0)
S
0

ohhhh so I need 0.0 to make sure it is using floating point values, that is useful to know

In case some else is reading it in the future, another solution would be the tween_method

func _process(_delta):
	if Input.is_action_just_pressed("ui_accept"):
		var tween: Tween = create_tween()
		tween.tween_method(reveal, 0.0, 1.0, 0.5).

func reveal(value: float):
	$Icon.material.set_shader_parameter('progress',value)

And thank you Maros, you're so incredibly helpful <3

Sawfly answered 19/10, 2023 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.