Home>

### Unity shader realizes the effect of vertex animation wave

Need to know the background knowledge:

Example of fluctuation:y=asin (ωx + φ)

φ:Determine the positional relationship or lateral movement distance between the waveform and the x-axis (left plus right minus)

ω:Determine the period (minimum positive period t=2Π/| ω |)

a:Determine the peak value (multiple of longitudinal stretching and compression)

The main calculation of the vertex shader:

1. Vertex position

2. Matrix conversion

2. Light effect

_time represents the time period float4 (t/20, t, t * 2, t * 3)

``````shader "custom/wave" {
properties {
_maintex ("texture", 2d)="white" {} //Texture
_arange ("amplitute", float)=1
_frequency ("frequency", float)=2 //fluctuation frequency
_speed ("speed", float)=0.5 //Control the speed of texture movement
}
{
pass
{
cgprogram
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"
struct appdata
{
float4 vertex:position;
float2 uv:texcoord0;
};
struct v2f
{
float2 uv:texcoord0;
float4 vertex:sv_position;
};
float _frequency;
float _arange;
float _speed;
v2f vert (appdata v)
{
v2f o;
float timer=_time.y * _speed;
//Make a fluctuation before the change y=asin (ωx + φ)
float waver=_arange * sin (timer + v.vertex.x * _frequency);
v.vertex.y=v.vertex.y + waver;
o.vertex=mul (unity_matrix_mvp, v.vertex);
o.uv=v.uv ;;
return o;
}
sampler2d _maintex;
fixed4 frag (v2f i):sv_target
{
fixed4 col=tex2d (_maintex, i.uv);
return col;
}
endcg
}
}
fallback "diffuse"
}
``````

### Related articles

• Previous Spring + Quartz configuration timing task implementation code