How to reuse a shader in a scene and retain unique values?
Asked Answered
D

4

0

I want to create a menu screen where the text moves into the window when the scene starts; the movement of the menu items should be done via shaders (or rather via a single shader that I hope to reuse).

Here is the scene setup:
menu scene

and the shader that is applied to both labels:

shader_type canvas_item;

uniform float progress: hint_range(0.0, 1.0);

void vertex() {
	VERTEX.x = VERTEX.x - (1.0 - progress) * 200.0;
}

I am just moving the pixels via a vertex shader. The 1.0 - progress is there so that I can update the shader more easily via a tween.

Speaking of the tween, in the parent node of the scene I have this code:

extends Control

func _ready():
	var tween := create_tween()
	tween.tween_property($MarginContainer/VBoxContainer/Label, "material:shader_parameter/progress", 1.0, 0.5)
	tween.tween_property($MarginContainer/VBoxContainer/Label2, "material:shader_parameter/progress", 1.0, 2.0)

This works but not in the intended way: Both labels are moving at the same time and at the same speed. I know this happens because when I update the shader on the first Label it also updates on Label2. Even leaving the second tween_property method out would still get the same result.

So, is it possible to have one shader applied to multiple nodes and keep the values unique to each node? I know that I can fix it by creating multiple shaders with the same code but that does not feel elegant at all. Is there a better way?

Dingle answered 6/11, 2023 at 11:31 Comment(0)
K
0

Dingle don't use shaders? is there a reason why you are doing it this way?
the problem you are facing happens because both nodes share the same material, so they are sent to the GPU together and share the same uniforms. create a different material with the same shader for each node.

Krutz answered 6/11, 2023 at 20:38 Comment(0)
W
0

There is a better way, but only for spatial shaders that support per-instance uniforms. As far as I know, it's not supported in canvas_item shaders.

Walley answered 6/11, 2023 at 11:48 Comment(0)
D
0

Walley that would have been perfect T_T

Dingle answered 6/11, 2023 at 13:29 Comment(0)
K
0

Dingle don't use shaders? is there a reason why you are doing it this way?
the problem you are facing happens because both nodes share the same material, so they are sent to the GPU together and share the same uniforms. create a different material with the same shader for each node.

Krutz answered 6/11, 2023 at 20:38 Comment(0)
D
0

Krutz It was a really simplified scene to test some shader stuff. Using a different material but the same shader works perfectly, thank you so much!

Dingle answered 7/11, 2023 at 15:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.