Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by TurboHermit · Nov 16, 2017 at 12:28 PM · shadermateriallerpspeedlerping

lerp in shader doesn't work as imagined?

Hey, so I'm using lerp in a displacement/tessellation/paralax shader and I'm trying to pulsate between a minimum and maximum float value using _SinTime like this:

 _DisplacementAmount = lerp(_MinDisplacement,_MaxDisplacement, _SinTime.w*_PulseSpeed);

However influencing _SinTime in anyway (for example multiplying by _PulseSpeed), doesn't make the process go faster/slower, but instead increases/decreases the maximum and minimum values. I know Mathf.Lerp in Unity C# uses a 0-1 value to interpolate between two values, but this seems to work in a different manner?

Does anyone know why it does this or what the proper way to use it is?

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 Bunny83 · Nov 16, 2017 at 01:14 PM

Lerp works everywhere the same way. If it doesn't it's not linear interpolation.


The behaviour you see is absolute correct. Lerp requires a value between 0 and 1. When you multiply _SinTime.w by a value you change that range. Of course this doesn't change the speed of the sine wave.


_SinTime contains 4 values which are


 _SinTime.x = Mathf.Sin(Time.time / 8);
 _SinTime.y = Mathf.Sin(Time.time / 4);
 _SinTime.z = Mathf.Sin(Time.time / 2);
 _SinTime.w = Mathf.Sin(Time.time);


If you want to change the frequency of the sine wave you have to change the "time" value inside the sin method. _SinTime just provides 4 predefined values with 4 different constant frequencies (8Hz, 4Hz, 2Hz, 1Hz)

If you want to control the frequency with a parameter you have to do this in your shader:


 DisplacementAmount = lerp(_MinDisplacement,_MaxDisplacement, sin(_Time.y *_PulseSpeed));


Comment
Add comment · Show 2 · 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 Bunny83 · Nov 16, 2017 at 01:22 PM 0
Share

ps: If you fear the "sin" inside the fragment shader could cause performance issues you can do the same that Unity does, set your own precalculated "sine parameter" from script every frame.


Also keep in $$anonymous$$d that "sin" returns a value between "-1" and "1". The "lerp" inside shaders is not clamped to the range 0 - 1. So a value of "-1" would return a value below your _$$anonymous$$inDisplacement. Specifically it would return _$$anonymous$$inDisplacement*2 -_$$anonymous$$axDisplacement

If you just want to bounce between 0 and 1 you have to use

 (0.5 +sin(_Time.y *_PulseSpeed) * 0.5)

avatar image TurboHermit · Nov 16, 2017 at 02:30 PM 0
Share

Works like a charm. However this DOES indicate that it doesn't work the same everywhere, since it's not clamped within the shader (which also explains the behaviour it was showing).

Thanks for the clarification!

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

124 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 avatar image avatar image

Related Questions

Slow down in the same time over different distances 1 Answer

Material doesn't have a color property '_Color' 4 Answers

Blending between Unlit/Color and Standard shaders 1 Answer

How can i get mixed texture from one shader and set it for another object with another shader? 0 Answers

Laser Shader/Material? 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