根据《GPU Gems》的Gerstner 公式,实现水波的效果。
参考文档地址 https://developer.nvidia.com/gpugems/gpugems/part-i-natural-effects/chapter-1-effective-water-simulation-physical-models
这个水面网格我只是用了Unity Create -> 3D Object ->Plane的内置Plane网格来模拟,顶点数量较少,边缘看着有些不够"柔软"。
GersterWaves公式
顶点偏移:
法线计算:
波长和速度的关系中,w的计算公式为
要注意的是Qi是一个控制水波陡度的系数,取值范围应该为0-1,QI为0时为通常滚动的正弦波,要使得水波顶部更为尖锐时,应该避免Qi的值过大,不然会形成顶部的水波穿插,形成错误的网格顶点位置,甚至露出到水面背面的网格面。
由此得出Shader:
Water.shader
Shader "Custom/Water"
{
Properties
{
_Color("Color", Color) = (1,1,1,1)
_WaveLength("WaveLength", float) = 4//水波长度,世界空间中波之间的波峰到波峰的距离
_WaveAmplitude("WaveAmplitude", float) = 0.4//振幅,从水平面到波峰的高度
_WindDirection("WindDirection", Range(0, 360)) = 70//风方向
_WindSpeed("WindSpeed", float) = 0.4//风速系数
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent" }
LOD 100
ZWrite On
ZTest On
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _Color;
float _WaveLength;
float _WaveAmplitude;
float _WindDirection;
float _WindSpeed;
struct Wave {
float3 vertex;
float3 normal;
};
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 vertex : SV_POSITION;
float3 normal : NORMAL;
};
Wave GerstnerWave(half2 pos, float waveCount, half waveLen, half amplitude, half direction, half windSpeed) {
//方向(D):垂直于波峰传播的波阵面的水平矢量。
//波长(L):世界空间中波之间的波峰到波峰的距离。
//振幅(A):从水平面到波峰的高度。
//速度(S):波峰每秒向前移动的距离。
//陡度 (Q) : 控制水波的陡度。
Wave waveOut;
float time = _Time.y;
direction = radians(direction);
half2 D = normalize(half2(sin(direction), cos(direction)));
half w = 6.28318 / waveLen;
half L = waveLen;
half A = amplitude;
half S = windSpeed * sqrt(9.8 * w);
half Q = 1 / (A * w * waveCount);
half commonCalc = w * dot(D, pos) + time * S;
half cosC = cos(commonCalc);
half sinC = sin(commonCalc);
waveOut.vertex.xz = Q * A * D.xy * cosC;
waveOut.vertex.y = (A * sinC) / waveCount;
half WA = w * A;
waveOut.normal = half3(-(D.xy * WA * cosC), 1 - (Q * WA * sinC));
waveOut.normal = waveOut.normal/waveCount;
return waveOut;
}
Wave GenWave(float3 vertex) {
half2 pos = vertex.xz;
Wave waveOut;
uint count = 4;
for (uint i = 0; i < count; i++) {
Wave wave = GerstnerWave(pos, count, _WaveLength, _WaveAmplitude, _WindDirection, _WindSpeed);
waveOut.vertex += wave.vertex;
waveOut.normal += wave.normal;
}
return waveOut;
}
v2f vert (appdata v)
{
v2f o;
Wave wave = GenWave(v.vertex.xyz);
v.vertex.xyz += wave.vertex;//叠加偏移
o.vertex = UnityObjectToClipPos(v.vertex);
o.normal = mul(wave.normal, (float3x3)unity_WorldToObject);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//漫反射公式
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb;
fixed3 worldNormal = normalize(i.normal);
fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
fixed3 diffuse = _LightColor0.rgb * _Color.rgb * saturate(dot(worldNormal,worldLightDir));
fixed3 color = ambient + diffuse;
return fixed4(color, _Color.a);
}
ENDCG
}
}
}