这次要只用顶点着色器和片元着色器实现水面效果,思路很简单,就是先把顶点坐标从模型空间转变到齐次裁剪空间,再左乘unity_ObjectToWorld矩阵转变到世界坐标,将世界坐标的y按照正弦规律变化即可得到水面的波涛汹涌的效果,然后我们设置两个颜色,波峰我们设置为深色,波谷我们设置为浅色,只需要根据我们得到的y坐标偏移值lerp插值一下即可得到一个简单的水面效果,没有用到任何贴图
Shader "Custom/MyWater3" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_Speed ("Speed", Range(0, 10)) = 1
_Height ("Height", Range(0, 100)) = 0.1
_W("W",Range(0,1)) = 0.5
_LowColor("LowColor",Color) = (1,1,1,1)
_HighColor("HighColor",Color) = (0,0,1,1)
}
SubShader {
Tags {"Queue"="Transparent" "RenderType"="Opaque"}
LOD 100
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;
float4 worldPos : TEXCOORD1;
float offset:TEXCOORD2;
};
sampler2D _MainTex;
float _Speed;
float _Height;
float _W;
fixed4 _LowColor;
fixed4 _HighColor;
v2f vert (appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
o.offset = _Height * sin(_Speed * _Time.y + _W * (abs(o.worldPos.x) + abs(o.worldPos.z)));
o.vertex.y += o.offset;
return o;
}
fixed4 frag (v2f i) : SV_Target {
fixed4 waterColor = lerp(_LowColor,_HighColor,1 - i.offset/_Height);
return waterColor;
}
ENDCG
}
}
FallBack "Diffuse"
}
实现的效果图如下:
参数调节如下: