Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

"the vanishing of ethan carter" grass movement

Discussion in 'Shaders' started by imagoFX, Oct 14, 2014.

  1. imagoFX

    imagoFX

    Joined:
    Sep 19, 2011
    Posts:
    81
    Hi all,
    I'm trying to create a grass shader with realistic wind movement like in this clip:

    I can't get the movement to flow like that. Any help would be appreciated!
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    The base for something like that is this:
    Code (csharp):
    1.  
    2. position.xz += 0.1 * sin(position.zx + time);
    3.  
    You will need add that it only affects the top and you might need to scale some things.
     
  3. imagoFX

    imagoFX

    Joined:
    Sep 19, 2011
    Posts:
    81
    Thanks but that gives a very unnatural wave, the movement in the clip is much more complex. I have tried mixing a few sin waves with different frequencies but it looks... meh.
     
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    have a peek in TerrainEngine.cginc - you can find unity's sources here: http://unity3d.com/unity/download/archive

    It contains many interesting sources you can pull apart such as:
    Code (CSharp):
    1.  
    2. fixed4 TerrainWaveGrass (inout float4 vertex, float waveAmount, fixed4 color)
    3. {
    4.     float4 _waveXSize = float4(0.012, 0.02, 0.06, 0.024) * _WaveAndDistance.y;
    5.     float4 _waveZSize = float4 (0.006, .02, 0.02, 0.05) * _WaveAndDistance.y;
    6.     float4 waveSpeed = float4 (0.3, .5, .4, 1.2) * 4;
    7.  
    8.     float4 _waveXmove = float4(0.012, 0.02, -0.06, 0.048) * 2;
    9.     float4 _waveZmove = float4 (0.006, .02, -0.02, 0.1);
    10.  
    11.     float4 waves;
    12.     waves = vertex.x * _waveXSize;
    13.     waves += vertex.z * _waveZSize;
    14.  
    15.     // Add in time to model them over time
    16.     waves += _WaveAndDistance.x * waveSpeed;
    17.  
    18.     float4 s, c;
    19.     waves = frac (waves);
    20.     FastSinCos (waves, s,c);
    21.  
    22.     s = s * s;
    23.    
    24.     s = s * s;
    25.  
    26.     float lighting = dot (s, normalize (float4 (1,1,.4,.2))) * .7;
    27.  
    28.     s = s * waveAmount;
    29.  
    30.     float3 waveMove = float3 (0,0,0);
    31.     waveMove.x = dot (s, _waveXmove);
    32.     waveMove.z = dot (s, _waveZmove);
    33.  
    34.     vertex.xz -= waveMove.xz * _WaveAndDistance.z;
    35.    
    36.     // apply color animation
    37.    
    38.     // fix for dx11/etc warning
    39.     fixed3 waveColor = lerp (fixed3(0.5,0.5,0.5), _WavingTint.rgb, fixed3(lighting,lighting,lighting));
    40.    
    41.     // Fade the grass out before detail distance.
    42.     // Saturate because Radeon HD drivers on OS X 10.4.10 don't saturate vertex colors properly.
    43.     float3 offset = vertex.xyz - _CameraPosition.xyz;
    44.     color.a = saturate (2 * (_WaveAndDistance.w - dot (offset, offset)) * _CameraPosition.w);
    45.    
    46.     return fixed4(2 * waveColor * color.rgb, color.a);
    47. }
    You'll need to refer to the above include to get that code to run though.
     
  5. imagoFX

    imagoFX

    Joined:
    Sep 19, 2011
    Posts:
    81
    @hippocoder Looks interesting, I will check it out. Thanks!