shaderlab波浪扩散效果
记录一下波浪扩散效果
//波浪扩散
Shader "Custom/PointShader"
{
Properties{
_Color("MainColor", Color) = (1,1,1,1)
_MainTex("MainTexture", 2D) = "white" {}
_RotateSpeed("RotateSpeed", Range(-20, 20)) = 20
_WaveSpeed("WaveSpeed",Range(-20, 20)) = 20
}
SubShader{
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
fixed4 _Color;
sampler2D _MainTex;
float _RotateSpeed;
float _WaveSpeed;
struct appdate{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float dis : TEXCOORD1;
};
v2f vert(appdate v){
v2f o;
o.dis = length(v.vertex.xz);
//顶点旋转
// v.vertex.xz = float2(v.vertex.x * cos(_RotateSpeed * _Time.x) - v.vertex.z * sin(_RotateSpeed * _Time.x),
//v.vertex.x * sin(_RotateSpeed * _Time.x) + v.vertex.z * cos(_RotateSpeed * _Time.x));
//顶点中心扩散波浪
v.vertex.y = sin(_Time.y*_WaveSpeed + v.vertex.y + o.dis);
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag(v2f i) : SV_Target{
float2 uv = i.uv.xy - float2(0.5, 0.5);
//旋转矩阵公式
uv = float2(uv.x * cos(_RotateSpeed * _Time.x) - uv.y * sin(_RotateSpeed * _Time.x),
uv.x * sin(_RotateSpeed * _Time.x) + uv.y * cos(_RotateSpeed * _Time.x));
//恢复纹理位置
uv += float2(0.5, 0.5);
//uv += float2(0.5, 0.5)*sin(_Time.x*i.dis);
//像素查询旋转
//fixed4 c = tex2D(_MainTex , uv)*_Color*(0.5,0,0.5,1);
fixed4 c = tex2D(_MainTex , uv)*_Color*i.dis*(_Time.x+0.5);
//fixed4 c = tex2D(_MainTex , uv)*_Color*0.5;
return c;
}
ENDCG
}
}
}

//圆环扩散
Shader "Custom/planeShader"
{
Properties
{
// _Color ("Color", Color) = (1,1,1,1)
//_MainTex ("Albedo (RGB)", 2D) = "white" {}
// _Glossiness ("Smoothness", Range(0,1)) = 0.5
// _Metallic ("Metallic", Range(0,1)) = 0.0
_MainColor ("MainColor", Color) = (1,1,1,1)
_Speed("Speed",Range(0,1)) = 0.5
_Concentration("Concentration",Range(0,5)) = 3
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent" }
// No culling or depth
Cull Off ZWrite On ZTest Always
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : Normal;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
float4 _MainColor;
float _Speed;
float _Concentration;
fixed4 frag (v2f i) : SV_Target
{
//dis的值应该在0-0.5
float dis = length(i.uv - fixed2(0.5,0.5));
//sin(_Time.y * _Speed)
//加0.001防止fade为0 frac函数返回小数部分 fade的值为0-1
fixed fade = frac(_Time.y * _Speed) * 10 + 0.001;
//划分颜色层ladder的值为0-0.5
//float ladder = dis * 10 / fade;
float ladder = ceil(dis * 10) / fade;
//半径为0.5,去除不必要的计算
//fixed3 col = _MainColor.rgb * (1 - dis);
fixed3 col = _MainColor.rgb * (1 - ladder) * sin(_Time.y * _Speed);
//裁剪圆,alpha浓度
fixed a = step(ladder,0.5) * pow(log(2),_Concentration);
//fixed a = step(dis,0.5) * pow(log(2),_Concentration);
return fixed4(col.r,col.g,col.b,a);
}
ENDCG
}
}
}

浙公网安备 33010602011771号