Search Unity

Vertex Animation (Dolphin)

Discussion in 'Shaders' started by reduktion, Jan 3, 2014.

  1. reduktion

    reduktion

    Joined:
    Sep 27, 2012
    Posts:
    25
    For my current project (Lowboat) I created some shaders with vertex animations. Since there is not much information on this subject, I thought posting the following shader might be helpful. Of course, any tipps on how to improve it are welcome. :D

    $dolphin2.gif


    Code (csharp):
    1. Shader "Vertex Animations/Dolphin" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.         _Shade ("Shade Cubemap(RGB)", CUBE) = "" { Texgen CubeNormal }
    5.         _AnimationSpeed ("Animation Speed", Range(1, 50)) = 50
    6.         _AnimationAmount ("Animation Amount", Range(0.01, 0.5)) = 0.5
    7.         _AnimationLength ("Animation Length", Range(0.001, 10)) = 0.1
    8.     }
    9.  
    10.  
    11.     SubShader {
    12.         Tags {"IgnoreProjector"="True"  }
    13.         Pass {
    14.             Name "BASE"
    15.            
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.             #pragma fragmentoption ARB_precision_hint_fastest
    20.             #pragma glsl_no_auto_normalization
    21.             #include "UnityCG.cginc"
    22.  
    23.             sampler2D _MainTex;
    24.             samplerCUBE _Shade;
    25.             fixed4 _MainTex_ST;
    26.             float _AnimationSpeed;
    27.             float _AnimationAmount;
    28.             float _AnimationLength;
    29.  
    30.             struct appdata {
    31.                 fixed4 vertex : POSITION;
    32.                 float2 texcoord : TEXCOORD0;
    33.                 float3 normal : NORMAL;
    34.             };
    35.            
    36.             struct v2f {
    37.                 fixed4 pos : POSITION;
    38.                 float2 texcoord : TEXCOORD0;
    39.                 float3 cubenormal : TEXCOORD1;
    40.             };
    41.  
    42.             v2f vert (appdata v)
    43.             {
    44.                 v2f o;         
    45.                 float s = sin(_Time.y * _AnimationSpeed + (v.vertex.z + v.vertex.x * 0.6f)* _AnimationLength) * _AnimationAmount * v.vertex.z;
    46.                 v.vertex.y += s;
    47.                 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    48.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    49.                 o.cubenormal = mul (UNITY_MATRIX_MV, float4(v.normal,0));
    50.                 return o;
    51.             }
    52.  
    53.             fixed4 frag (v2f i) : COLOR
    54.             {
    55.                 fixed4 col = tex2D(_MainTex, i.texcoord);
    56.                 fixed4 cube = texCUBE(_Shade, i.cubenormal);
    57.                 return fixed4( cube.rgb * col.rgb, col.a);
    58.             }
    59.             ENDCG          
    60.         }
    61.     }
    62.    
    63.     Fallback "VertexLit"
    64. }
     
  2. Loulabelette

    Loulabelette

    Joined:
    Dec 11, 2013
    Posts:
    34
    This is incredible, really I love it.
    Someone knows how to make the animation axis selectable ?
    I'm not used with shader and I don't think I can do it myself.
     
  3. Loulabelette

    Loulabelette

    Joined:
    Dec 11, 2013
    Posts:
    34
    Now I uess, is it possible to make this kind of shader for birds
    I mean if the bird is directed on 1 axis,we can move it wings vertex this way ?
     
  4. reduktion

    reduktion

    Joined:
    Sep 27, 2012
    Posts:
    25
    Just play around with x, y and z. Since a bird flaps its wings simetrically you might also have to use abs(). Something like this:

    Code (csharp):
    1. float s = sin(_Time.y * _AnimationSpeed + abs(v.vertex.z)* _AnimationLength) * _AnimationAmount * v.vertex.z;
    2. v.vertex.y += s;
    (not tested)

    However, a bird's wing flaps are a bit more complicated and not as fluid as a fish's movement. With this animation the bird will look quite boneless.
     
  5. Loulabelette

    Loulabelette

    Joined:
    Dec 11, 2013
    Posts:
    34
    Thanks a lot, I'lll try it. I have to learn how shaders are working because it seems we can do more like I thougth with them.