1.水面波动UV动画
Shader "Hsj/Whater" {
Properties
{
_MainTex("MainTex",2D) = "white"{}
_MainColor("MainColor",COLOR) = (1,1,1,1)
_AddTex("AddTex",2D) = "white"{}
_Maxset("Max",Range(0.1,1.0)) = 0.1
}
SubShader
{
Tags{"RenderType" = "Transparent" "Queue" = "Transparent" "IgnoreProjector" = "True"}
LOD 200
pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 _MainColor;
sampler2D _MainTex;
sampler2D _AddTex;
float4 _MainTex_ST;
float _Maxset;
struct appdata
{
float4 vertex:POSITION;
float2 texcoord:TEXCOORD0;
};
struct v2f
{
float4 pos:POSITION;
float2 uv:TEXCOORD0;
};
v2f vert(appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}
float4 frag(v2f o) :SV_Target
{
float2 c = (tex2D(_AddTex,o.uv.xy + float2(0,_Time.x)).gb + tex2D(_AddTex,o.uv.xy + float2(_Time.x,0)).gb) - 1;
float2 ruv = o.uv.xy + c.xy*_Maxset;
half4 h = tex2D(_MainTex,ruv)*_MainColor;
return h;
}
ENDCG
}
}
FallBack "Diffuse"
}
2.简单的扰动
Shader "Custom/Water"
{
Properties
{
_MainTint("Tint",Color) = (1,1,1,1)
_MainTex("水纹贴图", 2D) = "white" {}
_WaterAlpha("水不透明度",Range(0,1)) = 0.5
_ScrollSpeed("贴图滚动速度", Range(0,10)) = 2
_NoiseTex("水波噪声贴图",2D) = "white"{}
_Amount("噪声影响因子", Range(0,2)) = 1
_Frequency("水波频率",Range(0,1.4)) = 0.8
_WaveSpeed("水波速度",Range(0.1, 80)) = 10
_Amplitude("水波振幅", Range(-1, 1)) = 1
_CWaveVal("水环振幅", Range(.01, 20)) = 1
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"RenderType" = "Transparent"
}
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard alpha:fade
#pragma vertex vert
#pragma target 4.0
struct Input
{
float2 uv_MainTex;
float2 uv_StoneTex;
float2 uv_NoiseTex;
};
fixed4 _MainTint;
fixed _ScrollSpeed;
sampler2D _MainTex;
sampler2D _NoiseTex;
fixed _WaterAlpha;
fixed _Amount;
fixed _Frequency;
fixed _WaveSpeed;
fixed _Amplitude;
float _CWaveVal;
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_INSTANCING_BUFFER_END(Props)
void surf(Input IN, inout SurfaceOutputStandard o)
{
//引入贴图
fixed2 scrolledUV = IN.uv_MainTex;
fixed2 noiseTex = IN.uv_NoiseTex;
//流动
fixed ScrollValue = _ScrollSpeed * _Time;//非交替定向流动
half4 n = tex2D(_NoiseTex,noiseTex) * _Amount;//噪声因子
scrolledUV += fixed2(ScrollValue + n.x, ScrollValue + n.y);//x,y方向各自偏移量
//圆环旋转
float2 center = (0.5, 0.5);//若要中点,注意与平铺值的一半对应;此处平铺为1,1
float2 dt = IN.uv_MainTex - center;
float len = length(dt);//偏移量
float intensity = sin(_SinTime.x * 3) * _CWaveVal;//旋转强度因子
float off = len * intensity;//对应uv旋转量
float2x2 rot = float2x2(cos(off), -sin(off), sin(off), cos(off));//二维空间旋转矩阵
dt = mul(rot, dt) + center;//乘旋转矩阵,加上需要偏移中心的量
half4 c = tex2D(_MainTex, dt + scrolledUV);//uv偏移(圆环与流动)
//赋值输出
o.Albedo = c.rgb * _MainTint;
o.Alpha = _WaterAlpha;
}
void vert(inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input,o);
float time = _WaveSpeed * _Time;
float waveValue = sin(time + v.vertex.x * _Frequency) * _Amplitude;//偏移量
v.vertex.xyz = float3(v.vertex.x, v.vertex.y + waveValue, v.vertex.z);//在y轴附加偏移量
}
ENDCG
}
FallBack "Diffuse"
}