Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Standard water - speed

Discussion in 'Shaders' started by Karwoch, Mar 11, 2018.

  1. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    110
    Im creating a game where water speed gives illusion of ship in motion (high water speed), and that works great. But when ship is hit, and I change speed of water - waves suddenly change its position, as changing water speed changes also its position... Is there any remedy for that problem? Can I calculate same position of water textures for different speeds? And there is actually no tiling position of those wave textures, so I don`t know if it is even possible.
     
  2. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    276
    I'm assuming you're doing the offset via speed in the shader? In which case the Time variable is "Time from the start of the app" so changing speed is effectively going "backwards in time", that's why you're seeing that jump, not just less offset.

    You'll want to manage your UV offset in C# more, and pass an X/Y offset to the shader, not a speed and direction.
     
  3. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    110
    Yea, that explains why changing speed looks like fast forward. So probably it is doable, but calculating right UV offset for actual time... this will be challenging. Thanks, I have really much to think about now.
     
  4. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    276
    Calculate your speed like normal. Add this frames speed value to the x and y uv offsets based on the direction you want to appear to be traveling. It's the same concept you've already done, just rearranged a bit.
     
  5. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    110
    Surely I will try that! Thank You!

    Now I just need to figure out how to access UV coordinates in water shader. Long time since I last do anything with shaders.
     
    Last edited: Mar 13, 2018
  6. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    110
    Finally got it working, no shader coding just waters script, here is code for anyone interested:

    Code (CSharp):
    1.             double t = Time.timeSinceLevelLoad / 20.0 * waterSpeedMultilier;
    2.  
    3.             Vector4 offsetClamped = new Vector4(
    4.                 (float)Math.IEEERemainder(waveSpeed.x * waveScale4.x * t, 1.0),
    5.                 (float)Math.IEEERemainder(waveSpeed.y * waveScale4.y * t, 1.0),
    6.                 (float)Math.IEEERemainder(waveSpeed.z * waveScale4.z * t, 1.0),
    7.                 (float)Math.IEEERemainder(waveSpeed.w * waveScale4.w * t, 1.0)
    8.                 );
    9.  
    10.  
    11.             if (waterSpeedMultilier != waterSpeedMultilierLastFrame)
    12.             {
    13.                 t = timeSinceLoadLastFrame / 20.0 * waterSpeedMultilier;
    14.  
    15.                 Vector4 offsetClampedDifference = new Vector4(
    16.                 (float)Math.IEEERemainder(waveSpeed.x * waveScale4.x * t, 1.0),
    17.                 (float)Math.IEEERemainder(waveSpeed.y * waveScale4.y * t, 1.0),
    18.                 (float)Math.IEEERemainder(waveSpeed.z * waveScale4.z * t, 1.0),
    19.                 (float)Math.IEEERemainder(waveSpeed.w * waveScale4.w * t, 1.0)
    20.                 );
    21.  
    22.                 offSetModifier = offSetLastFrame - offsetClampedDifference;
    23.             }
    24.  
    25.             offsetClamped += offSetModifier;
    26.  
    27.             waterSpeedMultilierLastFrame = waterSpeedMultilier;
    28.             timeSinceLoadLastFrame = Time.timeSinceLevelLoad;
    29.  
    30.             offSetLastFrame = offsetClamped;
    31.  
    32.             mat.SetVector("_WaveOffset", offsetClamped);
    33.             mat.SetVector("_WaveScale4", waveScale4);