11.3.1波浪效果
Shader "Custom/chap11.3.1/vertex Animation"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {} // 河流纹理
_Color("Main Color", Color) = (1,1,1,1) // 颜色
_Magnitude("Distortion Magnitude", Float) = 1 // 控制水流波动的幅度
_Frequency("Distortion Frequency", Float) = 1 // 控制水流波动的频率
_InvWaveLength("Distortion Inverse Wave Length", Float) = 10 // 控制波长的倒数(越小波长越大。 反比)
_Speed("animation Speed1", Range(0,100)) = 0.5 // 动画速度
}
SubShader
{
// 序列帧动画通常带有透明度, 因此这里的 renderType 和 Queue 均设置为 Transparent
// DisableBatching = true, 批处理会合并相关模型,而顶点动画会修改模型的顶点偏移。所以要取消批处理
Tags { "RenderPipeLine"="UniversalRenderPipeline" "RenderType"="Transparent" "Queue"="Transparent" "IgnoreProjector"="True" "DisableBatching"="True" }
// 透明模式下通常关闭深度写入
ZWrite Off
// 指定混合因子
Blend SrcAlpha OneMinusSrcAlpha
HLSLINCLUDE
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl"
CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST;
half4 _Color;
float _Speed;
float _Magnitude;
float _Frequency;
float _InvWaveLength;
CBUFFER_END
ENDHLSL
Pass
{
Tags { "LightMode"="UniversalForward" }
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
TEXTURE2D (_MainTex);
SAMPLER(sampler_MainTex);
struct appdata
{
float4 vertex : POSITION;
float4 uv : TEXCOORD0;
float3 normal: NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
// UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
float3 worldNormal: TEXCOORD1;
float3 worldPos: TEXCOORD2;
};
v2f vert (appdata v)
{
float4 offset;
offset = float4(0.0, 0.0, 0.0, 0.0);
// 原理: sin (aX + b) * c 中
// c 就是这里的 _Magnitude 控制振幅
// b 就是代指 v.vertex.z * _InvWaveLength 控制初始偏移量 -> 出现波浪的条件
// a 就是代指 _Frequency -> 控制频率
// x 就是代指 _Time.y 出现波浪流动的条件。(变量)
offset.x = sin(_Frequency * _Time.y + v.vertex.z * _InvWaveLength) * _Magnitude;
// offset.x = sin(v.vertex.z * _InvWaveLength) * _Magnitude;
// offset.x = sin(_Frequency * _Time.y + v.vertex.x * _InvWaveLength + v.vertex.z * _InvWaveLength) * _Magnitude;
// offset.z = sin(_Frequency * _Time.y + v.vertex.x * _InvWaveLength + v.vertex.z * _InvWaveLength) * _Magnitude;
// offset.y = sin(_Frequency * _Time.y + v.vertex.x * _InvWaveLength + v.vertex.z * _InvWaveLength) * _Magnitude;
// offset.z = sin(_Frequency * _Time.y + v.vertex.x * _InvWaveLength + v.vertex.z * _InvWaveLength) * _Magnitude;
// offset.x = sin(_Frequency * _Time.y + v.vertex.x * _InvWaveLength + v.vertex.z * _InvWaveLength) * _Magnitude;
// offset.x = sin(_Frequency * _Time.y + v.vertex.x * _InvWaveLength + v.vertex.y * _InvWaveLength + v.vertex.z * _InvWaveLength) * _Magnitude;
v2f o;
// 裁剪空间坐标
o.vertex = TransformObjectToHClip(v.vertex + offset);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
// 修改纹理的 y 坐标,可以让纹理中的白线流动使得水流更加真实
o.uv += float2(0.0, _Time.y * _Speed);
return o;
}
float4 frag (v2f i) : SV_Target
{
float4 c = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
c.rgb *= _Color.rgb;
return c;
}
ENDHLSL
}
}
}