Advertisement
josemorval

(Little dirty) Wave effect

Feb 29th, 2016
4,501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "morvaly/WaveEffect01" {
  2.     Properties {
  3.         _MainTex ("Texture", 2D) = "white" { }
  4.         _SecondTex ("Texture", 2D) = "white" { }
  5.         _Dim ("Float", Float) = 0
  6.         _Mouse ("Float", Vector) = (0,0,0,0)
  7.     }
  8.  
  9.         CGINCLUDE
  10.  
  11.         #include "UnityCG.cginc"
  12.         #pragma target 3.0
  13.        
  14.         sampler2D _MainTex;
  15.         sampler2D _SecondTex;
  16.        
  17.         float4 _MainTex_ST;
  18.         float _Dim;
  19.         float4 _Mouse;
  20.  
  21.  
  22.         float4 frag_initialcondition(v2f_img i) : SV_TARGET {
  23.  
  24.            
  25.             float fx = floor(i.uv.x*_Dim)/(_Dim-1.0);
  26.             float fy = floor(i.uv.y*_Dim)/(_Dim-1.0);
  27.  
  28.             return float4(fx,fy,0.0,0.0);
  29.         }
  30.  
  31.         float4 frag_initialconditionvel(v2f_img i) : SV_TARGET {
  32.  
  33.             float fx = floor(i.uv.x*_Dim)/(_Dim-1.0);
  34.             float fy = floor(i.uv.y*_Dim)/(_Dim-1.0);
  35.  
  36.             return .1 * float4(fx-0.5,fy-0.5,0.0,0.0);
  37.  
  38.         }
  39.  
  40.  
  41.         //Aqui MainTex es para la textura de posiciones
  42.         float4 frag_updatevel(v2f_img i) : SV_TARGET {
  43.  
  44.                                                                                                
  45.             float2 xy = tex2D(_MainTex,i.uv).rg;
  46.             float2 xa = tex2D(_MainTex,i.uv-float2(1.0/(_Dim-1.0),0.0)).rg;
  47.             float2 xb = tex2D(_MainTex,i.uv+float2(1.0/(_Dim-1.0),0.0)).rg;
  48.             float2 ya = tex2D(_MainTex,i.uv-float2(0.0,1.0/(_Dim-1.0))).rg;
  49.             float2 yb = tex2D(_MainTex,i.uv+float2(0.0,1.0/(_Dim-1.0))).rg;
  50.  
  51.  
  52.             float2 curvel = tex2D(_SecondTex,i.uv).rg;
  53.             float2 v =  (xy-_Mouse.xy);
  54.             float l = length(v);
  55.  
  56.             if(_Mouse.x>0.0f){
  57.             curvel += 0.1*v*exp(-10.0*l*l);
  58.             }
  59.  
  60.             float k =10.0;
  61.             float2 vel = curvel + 0.2*k*(-(xy-xa)+(xb-xy)-(xy-ya)+(yb-xy)) - 0.01*length(curvel)*normalize(curvel);
  62.             return float4(vel.x,vel.y,0.0,0.0);
  63.         }
  64.  
  65.        float4 frag_updatepos(v2f_img i) : SV_TARGET {
  66.  
  67.             if(i.uv.x<1.0/_Dim || i.uv.x>(_Dim-1.0)/_Dim || i.uv.y<1.0/_Dim || i.uv.y>(_Dim-1.0)/_Dim){
  68.                 return tex2D(_MainTex,i.uv).rgba;  
  69.             }
  70.  
  71.             float2 xy = tex2D(_MainTex,i.uv).rg + 0.2*tex2D(_SecondTex,i.uv).rg;
  72.             return float4(xy.x,xy.y,0.0,0.0);
  73.         }
  74.  
  75.  
  76.         //Aqui MainTex es para la textura de posiciones
  77.         float4 frag_draw(v2f_img i) : COLOR {
  78.  
  79.             float width = 0.95;
  80.             float freq = 20;
  81.  
  82.             float2 uv = tex2D(_MainTex,i.uv).rg;
  83.             float f1 = sin(freq*2.0*3.14159*uv.x);
  84.             f1 = smoothstep(width,1.0,f1);
  85.  
  86.             float f2 = sin(freq*2.0*3.14159*uv.y);
  87.             f2 = smoothstep(width,1.0,f2);
  88.  
  89.             f1 = max(f1,f2);
  90.  
  91.  
  92.  
  93.             return lerp(tex2D(_SecondTex,uv).rgba,float4(0.3,0.3,0.3,1.0),smoothstep(0.3,0.8,i.uv.x))
  94.             +lerp(float4(0.0,0.0,0.0,0.0),f1*float4(0.0,191.0/255.0,1.0,0.0),smoothstep(0.3,0.7,i.uv.x));
  95.  
  96.         }
  97.  
  98.  
  99.  
  100.  
  101.  
  102.         ENDCG
  103.  
  104.  
  105.     SubShader {
  106.             Pass {             
  107.                
  108.                 CGPROGRAM
  109.                 #pragma vertex vert_img
  110.                 #pragma fragment frag_initialcondition
  111.                 ENDCG
  112.             }
  113.  
  114.             Pass {             
  115.                
  116.                 CGPROGRAM
  117.                 #pragma vertex vert_img
  118.                 #pragma fragment frag_initialconditionvel
  119.                 ENDCG
  120.             }
  121.  
  122.             Pass {             
  123.                
  124.                 CGPROGRAM
  125.                 #pragma vertex vert_img
  126.                 #pragma fragment frag_updatevel
  127.                 ENDCG
  128.             }
  129.  
  130.             Pass {             
  131.                
  132.                 CGPROGRAM
  133.                 #pragma vertex vert_img
  134.                 #pragma fragment frag_updatepos
  135.                 ENDCG
  136.             }
  137.  
  138.             Pass {             
  139.                
  140.                 CGPROGRAM
  141.                 #pragma vertex vert_img
  142.                 #pragma fragment frag_draw
  143.                 ENDCG
  144.             }
  145.  
  146.         }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement