Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.32 KB | None | 0 0
  1. Shader "GPU Gems/Wave"
  2. {
  3.   Properties
  4.   {
  5.     _Wavelength ("Wavelength", Float) = 1.0
  6.     _Amplitude ("Amplitude", Float) = 1.0                   // The height from the water plane, to the wave crest.
  7.     _Speed("Speed", Float) = 0.5
  8.     _Direction("Direction", Vector) = (1.0, 0.0, 0.0, 1.0)  // Only using the first two variables here, one should be able to pack all values into one vector, then use a custom material editor to allow user interface.
  9.   }
  10.   SubShader
  11.   {
  12.     Tags { "RenderType"="Opaque" }
  13.     LOD 100
  14.  
  15.     Pass
  16.     {
  17.       // Keyword to start the CG Program
  18.       CGPROGRAM
  19.  
  20.       // Preprocessors to define vertex and fragment shaders as vert and frag respectively
  21.       #pragma vertex vert
  22.       #pragma fragment frag
  23.      
  24.       // Include the cginc file from your Unity installation (../Unity/2018.1.0f2/Editor/Data/CGIncludes)
  25.       // defining a lot of helper functions and utilities. We're not using any in this version however.
  26.       // #include "UnityCG.cginc"
  27.  
  28.       // Define a structure with information we want from each vertex.
  29.       struct appdata
  30.       {
  31.         float4 vertex : POSITION;
  32.       };
  33.  
  34.       struct v2f
  35.       {
  36.         float4 vertex : SV_POSITION;
  37.         float4 color: COLOR;
  38.       };
  39.  
  40.       // Our properties exposed in Unity must be made into variables readable by the CG compiler,
  41.       // to do this we simply define variables of the same type (or one that can be converted) and
  42.       // same name.
  43.       float _Wavelength;
  44.       float _Amplitude;
  45.       float _Speed;
  46.       float4 _Direction;
  47.  
  48.       /*
  49.        *  Equation 1.
  50.        *    Wi(x,y,t) = Ai * sin(dot(Di, float2(x, y)) * wi + t * PHASE)
  51.        *
  52.        *  Wave is a function of the vertex position on the horizontal plane, and time.
  53.        *
  54.        */
  55.       float Equation_1(float x, float y, float t)
  56.       {
  57.         float w = 2 / _Wavelength;
  58.         float phase = _Speed * w;
  59.  
  60.         float wave = _Amplitude * sin(dot(_Direction.xz, float2(x, y)) * w + t * phase);
  61.  
  62.         return wave;
  63.       }
  64.  
  65.       /*
  66.        *  Normals and Tangents,
  67.        *  I think the document is using a Z-up world where as Unity used Y-up, so will likely have to move
  68.        *  the partial differential equation to fit accordingly.
  69.        */
  70.  
  71.       /* Calculate the Binormal */
  72.       float3 Equation_4(float x, float y)
  73.       {
  74.         return float3(1, 2 / _Wavelength * dot(_Direction.xz, float2(x, 0)) * _Amplitude * cos(dot(_Direction.xz, float2(x, y)) * 2 / _Wavelength + _Time.y * _Speed * 2 / _Wavelength) , 0);
  75.       }
  76.  
  77.       /* Tangent */
  78.       float3 Equation_5(float x, float y)
  79.       {
  80.         return float3(0, 2 / _Wavelength * dot(_Direction.xz, float2(0, y)) * _Amplitude * cos(dot(_Direction.xz, float2(x, y)) * 2 / _Wavelength + _Time.y * _Speed * 2 / _Wavelength) , 1);
  81.       }
  82.      
  83.       /* Normal */
  84.       float3 Equation_6(float x, float y)
  85.       {
  86.         return cross(Equation_4(x, y), Equation_5(x, y));
  87.       }
  88.  
  89.       // v2f, seeing as this is the structure we will return. And we call this function vert
  90.       // as we defined with the pragma earlier.
  91.       v2f vert (appdata v)
  92.       {
  93.         // Create an empty v2f structure to load all the information unto.
  94.         v2f o;  
  95.  
  96.         // Get the vertex position in Clip space from previously object space.
  97.         o.vertex = UnityObjectToClipPos(v.vertex);
  98.  
  99.         /*
  100.          *  _Time is already defined float4 by Unity, holding different values of time from which
  101.          *  we will use _Time.y which should be the curent time.
  102.          *
  103.          *  Explained at Unity Answers: http://answers.unity.com/answers/1292847/view.html
  104.          */
  105.  
  106.         // Equation 2 is the sum of all waves, and for now we only have one.
  107.         // Equation 3 is the final position P for each vertex.xy in time.
  108.         o.vertex.y += Equation_1(v.vertex.x, v.vertex.z, _Time.y);
  109.  
  110.         o.color.xyz = normalize(Equation_6(v.vertex.x, v.vertex.z));
  111.         o.color.w = 1.0;
  112.  
  113.         return o;
  114.       }
  115.      
  116.      
  117.       // Pixel shader, now that we know the position for each vertex. Draw a flat gray color for all surfaces.
  118.       fixed4 frag (v2f i) : SV_Target
  119.       {
  120.         fixed4 col = i.color;
  121.         return col;
  122.       }
  123.  
  124.       // And finally, end the CG program to return to Unity shaderlab code.
  125.       ENDCG
  126.     }
  127.   }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement