0 votes
shader_type canvas_item;

void vertex(){


void fragment(){
    float amk=0.0;

    amk = amk + 0.1;

    COLOR = texture(TEXTURE,UV+amk);

What i want to do is that to make my texture swite right or left continuous.But it works 1 time.
Why doesn't increase 'amk' variable ?

in Engine by (33 points)

1 Answer

+2 votes
Best answer

Because the fragment() function runs for every single pixel of the screen. Also, amk is a local variable, it won't be remembered between two calls anyways.

In shaders there is also no way to assign a member variable, that's not how shaders work. The reason is they run on your graphics card, and executed multiple times in parallel. There is no room for shared state.

Finally, UV + amk is not what you want. UV is a vec2, and amk is a single number. Which means it will probably add the same number to both X and Y, and your texture will scroll in diagonal, not left and right.

What you need to use is either use a uniform parameter which you set from script every frame, or the TIME parameter (which is also a uniform behind the scenes).

shader_type canvas_item;

void fragment(){
    COLOR = texture(TEXTURE, UV + vec2(TIME, 0));

Also make sure your texture has the Repeat flag checked, otherwise the texture won't repeat.

I stronly recommend you read all of this https://docs.godotengine.org/en/stable/tutorials/shading/index.html

by (27,784 points)
selected by

enter image description here

Hi thanks for answer, but the code you wrote work like this,the picture doesn't move continuously.
Also i couldn't find anything about the "TIME" parameter in the docs.
Sorry but i'm totally noob to shaders.

It moves normally in the test project I made for it, one repetition per second. With your code, it barely moves because the multiplier 0.0001 makes the animation so slow it cannot be seen. The "change" you are seeing with that multiplier is irrelevant.

The stretching shown in your screenshot indicates that you haven't enabled repetition in your import settings.
Click on your texture in the File Explorer, then go to the Import tab, Flags -> Repeat -> Enabled.

Yep,did it and works perfecly,thank you !

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.