How to tween texture

Godot Version

4.1

Question

Hi, everyone. I’m trying to create a transition between two textures using tween. My code changes the texture after 1 second as I defined. However, the code doesn’t do it smoothly. It’s just a sudden change after 1 second. I’m doing it like this:

extends Area2D

var target_texture: Texture


func _ready():
	target_texture = preload("res://icon_selected.png")
	
	$Sprite2D.material.set_shader_parameter("weight", 0)
	$Sprite2D.material.set_shader_parameter("target_texture", target_texture)
	

func _on_mouse_entered():
	var tween = create_tween()
	tween.tween_property($Sprite2D.material, "shader_parameter/weight", 1.0, 1).set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_SINE)


func _on_mouse_exited():

	var tween = create_tween()
	tween.tween_property($Sprite2D.material, "shader_parameter/weight", 0, 1).set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_SINE)
	

Shader:

shader_type canvas_item;
uniform sampler2D target_texture;
uniform float weight: hint_range(0, 1);

void fragment()
{
    vec4 color_a = texture(TEXTURE, UV);
    vec4 color_b = texture(target_texture, UV);
    COLOR = mix(color_a, color_b, weight);
	
}

I’ve just had the exact same experience, and I think it’s because you’ve got your weight var in gdscript as an int - change both of the 0s to 0.0 instead and it should work.

1 Like