0 votes

I've got a little shader effect - just a sine wave controlling vertex scale so far - and I've got a freq uniform controlling it. I'm using a tween to control this in gdscript.

The obj starts at freq zero (nothing happening), then tweens to freq one. When it's done it fires a signal and tweens back from freq one to zero:

Here's the tween subroutine where I send in the start and stop values:

func run_tween(a, b):
    printt("freq", mat.get_shader_param("freq"))
    var t = $Tween
                            mat, "shader_param/freq", a, b, 1, 
                            Tween.TRANS_BOUNCE, Tween.EASE_IN_OUT)

Here's signal method:

func _on_Tween_tween_all_completed():
    if not odd_even:
        run_tween(1, 0)
    printt("freq", mat.get_shader_param("freq"))
    odd_even = not odd_even

Obviously I have the odd_even bool there to go back and forth.

This prints:

freq    0
freq    1
freq    1
freq    0

Except the frequency is visibly speeding up every time you run it. Same duration, quicker sine wave. It even happens when you lerp without easing.

I'm not changing any other values. Am I missing something obvious here?

Godot version 3.4.4
in Engine by (2,156 points)
edited by

Sorry, I was being an idiot as usual. The shader had TIME in the sine function which obviously grew over time making the frequency higher. Nothing to do with the tween.

Glad you found/fixed the issue - thanks for the follow-up here.

For forum housekeeping, I'd suggest that you copy your comment as an Answer and then select it as Best. That'll show (on the main board) that the question 1) has an answer and 2) the answer resolved the problem - which is potentially important when searching the forum for help...

That's a good idea, let me do that.

1 Answer

0 votes
Best answer

In case anyone else has the same issue. The problem was with the shader code, I had:

void vertex() {
    VERTEX *= sin(freq*TIME) * amp * 0.5 + 1.;

Specifically the sin(freq*TIME) part. TIME grows over, well, time. Normally if you put it inside a sine function it doesn't matter whether TIME is equal to 10 or 20,000 as the rate is the same so the sine wave is the same.

But once I multiplied by freq i changed that. I was bringing the value from 0-10 or 0-10,000 over the same period of time. That meant the animation would get quicker over time depending on how long it's been since the game started.

The solution was just to drop TIME, in my case I scaled it up slightly with a new uniform float which I set to 10.0 to get the effect I wanted.

void vertex() {
    VERTEX *= sin(freq*rate) * amp * 0.5 + 1.;

Now, it always travels between 0 and 10.

by (2,156 points)
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 Frequently asked questions and 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 [email protected] with your username.