Search Unity

Standard water - speed

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

  1. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    111
    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:
    375
    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:
    111
    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:
    375
    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:
    111
    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:
    111
    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);