2014年2月12日 星期三

Shokwave Shader

參考 http://forums.tigsource.com/index.php?topic=34921.msg928012#msg928012
Shader "Custom/Distort" {
 Properties {
  _MainTex ("Base (RGB)", 2D) = "white" {}
  //_strength("Strength", float) = 1.5
  _strength("Strength", Range(-1.1,1.1)) = 0
  _radius("Radius", float) = 5
  _width("Width", float) = 2.5
  _center("Center", Vector) =(0,0,0,0)
 }
 CGINCLUDE

  #include "UnityCG.cginc"
  
  sampler2D _MainTex;
  
  float _strength;
  float _radius;
  float _width;
  float4 _center;
  struct v2f {
   half4 pos : SV_POSITION;
   half4 uv : TEXCOORD0;
   half4 center : TEXCOORD1;
  };
  float2 offsetFnc(float2 _pos, float2 _center)
  {
   //length from the center of the rendered quad;
   float x = distance(_pos , _center);
   //calculate offset stength
   float val = cos((x-_radius)/(_width/3.1415)) * _strength/2 + _strength/2;
   if(abs(x-_radius)>_width)
      val = 0;
   return normalize(_pos- _center)*val;
   
  }
  v2f vert(appdata_full v) {
   v2f o;
   
   o.pos = mul (UNITY_MATRIX_MVP, v.vertex); 
   o.uv = ComputeScreenPos (o.pos);
   //_center = float4(_Object2World[3][0], _Object2World[3][1], _Object2World[3][2], 1);
   _center = float4(_Object2World[0][3], _Object2World[1][3], _Object2World[2][3], 1);
   o.center = ComputeScreenPos(mul(UNITY_MATRIX_VP, _center));
   //o.center = ComputeScreenPos(mul(UNITY_MATRIX_MVP, _center));
   
   return o; 
  }
  
  fixed4 frag( v2f i ) : COLOR { 
   i.uv.xy /= i.uv.w;
   i.center.xy /= i.center.w;
   fixed4 result = tex2D (_MainTex, i.uv.xy + offsetFnc(i.uv.xy, i.center.xy));
   result.a = 1;
   //result = float4(i.uv.x, i.uv.x, i.uv.x,1);
   return result;
  }    
 ENDCG
 
 SubShader {
  Tags { "RenderType" = "Transparent" "Queue" = "Transparent-2" }
  Cull Off
  Lighting Off
  ZWrite Off
  Fog { Mode Off }
  Blend SrcAlpha OneMinusSrcAlpha
  
 Pass {
 
  CGPROGRAM
  
  #pragma vertex vert
  #pragma fragment frag
  #pragma fragmentoption ARB_precision_hint_fastest 
  #pragma target 3.0
  
  ENDCG
   
  }
    
 } 

 FallBack "Diffuse"
}