Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by brossi22 · Jan 21, 2018 at 07:17 AM · shadermathshader programmingsine-wavefrequency

Changing the Play Rate (Frequency) of a sine wave in real-time

I am trying to create a shader similar to the one used in ABZU to animate fish. So far I have this: https://gyazo.com/e5de88a8292bbd6a99ebfa6d69ab2ed8

And here is my code so far:

 v.vertex.x += sin((v.vertex.z + _Time.y * _PlayRate) * _Frequency) * _RollAmplitude;
             if (v.vertex.z > _MaskOffset)
                 v.vertex.x += sin((.05 + _Time.y * _PlayRate) * _Frequency) * _TranslateAmplitude * _MaskOffset;
             else
                 v.vertex.x += sin((v.vertex.z + _Time.y * _PlayRate) * _Frequency) * _TranslateAmplitude * v.vertex.z;

I have the movement down fine, but I want to be able to change the "Play Rate" of the "animation" that the shader is making in real-time based on what the fish's actual speed is. Doing this now, creates a jitter, seen here: https://gyazo.com/c7a1c0c4b0c8ead8cac9523dea55b57c

This gif doesn't do it justice. There is much more jerkiness when changing the play rate variable. I have been trying a bunch of different solutions including using a different variable for time and such, but I can't seem to get it. Many solutions I find are for C# scripts and can't be translated to the shaders well, if at all. Is there any way to get a sooth transition in the shader?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by elenzil · Jan 21, 2018 at 08:16 PM

this is a common issue.

sin(t * rate) will naturally give you significant discontinuities when t is non-zero and rate changes.

i'm not sure about doing this in a shader context, but in general code you should instead keep a separate variable that increments as dt * rate.

so eg

 private float _f = 0.
 ....
 void updateStuff():
   _f += dt * rate.
   displacement = sin(f).

apologies for the brevity & formatting, i'm on mobile

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image brossi22 · Jan 21, 2018 at 08:31 PM 0
Share

Unfortunately, this solution does not work for me. I have tried to implement this countless times and I still cannot get a smooth transition. I have also tried to use delta time ins$$anonymous$$d of time, but that renders my fish almost completely static, except for a little twitch every now and then. I wonder If I am maybe putting the sin(f) in the wrong place? Either way, thank you for your reply.

avatar image elenzil brossi22 · Jan 21, 2018 at 08:50 PM 1
Share

hm. well the key is you have a persistent variable which increments each frame according to dt times rate.

avatar image Bunny83 · Jan 21, 2018 at 10:06 PM 2
Share

I think there are some misunderstandings on both sides here. The code the OP posted is inside a shader while the code elenzil provided is not inside a shader but inside a script. The key is that you should not pass the rate into the shader but ins$$anonymous$$d pass your own "time" to the shader. This time you would update in a script and pass it to the shader. Inside your shader you would get rid of your rate multiplier and use your own time ins$$anonymous$$d of Unity's _Time.y variable.


It would only work that way since you can't update a persistent variable inside a shader.

avatar image brossi22 Bunny83 · Jan 22, 2018 at 12:39 AM 0
Share

Thank you so much @Bunny83 !! That did the trick! Passing a new "time" variable into the shader ins$$anonymous$$d of the "speed". If you could write this out as an answer I will mark you as correct. Thank you again for your help!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

122 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How come edititng "_Glossiness" through script doesn't work? 1 Answer

How to set shader vertex normals to face up 1 Answer

EnableKeyword("_EMISSION") not working 2019.3.13f1 0 Answers

Only render stencil mask when behind maskable object 0 Answers

Silhouette overlay shader 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges