通常情况下,水都是透明的,在上次基础上加上透明效果。
核心原理是对对背景采样。
Shader "Customer/Water01"
{
Properties
{
_Color("Color水颜色", color) = (0,0,0.8,0.5)
_WaveNormalMap("Wave Normal Map", 2D) = "bump"{}
_WaveHeight("Wave Height",Range(0,1)) = 0.1
_WaveNormalScale("Wave Scale", float) = 10.0
_WaveNormalSpeed("Wave Speed", float) = 1.0
_waveNumber("Wave Number",float) = 4
_BigWaveSpeed("Big Wave Speed",float) = 10
_BigWaveHeight("Big Wave Height",float) = 0.5
_BigWaveNumber("Big Wave Number",float) = 10
_Opacity("不透明度", range(0, 1)) = 0.5
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"RenderType" = "Transparent"
}
GrabPass{
"_GrabPassTex"
}
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
#pragma multi_compile_fog
float4 _Color;
sampler2D _WaveNormalMap;
fixed _WaveHeight;
float _WaveNormalScale;
fixed _WaveNormalSpeed;
fixed _waveNumber;
fixed _dividHeight;
fixed _BigWaveSpeed;
fixed _BigWaveHeight;
fixed _BigWaveNumber;
// 输入参数
float4 _MainTex_ST;
uniform half _Opacity;
sampler2D _GrabPassTex;//用于渲染透明物体
uniform sampler2D_float _CameraDepthTexture;
// 输入结构
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal:NORMAL;//法线通常都是必须的
float4 tangent : TANGENT;
float3 color:COLOR0;
};
// 输出结构
struct v2f
{
float4 uv : TEXCOORD0;
float4 vertex : SV_POSITION; // 由模型顶点信息换算而来的顶点屏幕位置
float depth : TEXCOORD2;
float3 normal:NORMAL;//法线通常都是必须的
float4 lightDir_Tangent:TEXCOORD5;
float4 screenuv:TEXCOORD1;
float3 viewDir:TEXCOORD3;
float4 vertex_Local : TEXCOORD4;
float3 color:COLOR0;
};
// 输入结构>>>顶点Shader>>>输出结构
v2f vert (appdata v)
{
v2f o;
UNITY_INITIALIZE_OUTPUT(v2f, o);
//获取自身深度值
COMPUTE_EYEDEPTH(o.depth);
float2 flow = float2(_Time.x*_WaveNormalSpeed, _Time.z*1.215*_WaveNormalSpeed);
fixed3 n = UnpackNormal(tex2Dlod(_WaveNormalMap, float4(v.uv.xy*_WaveNormalScale + flow, 0, 0))).xyz;
//float tempN = tex2Dlod(_WaveNormalMap, float4(v.uv.xy*_WaveNormalScale + flow, 0, 0)).g;这种写法也可以,当然下面的高度也要用下面这句
//v.vertex.y = v.vertex.y + tempN * _WaveHeight + height;
float height = 0;
fixed wavebevel = 5;
fixed attenuation = (1.0 - v.uv.y);
fixed b = 0.8;
fixed x = v.uv.y + b * sin(v.uv.y);
if (v.vertex.y > _dividHeight)
{
height = (cos(x*_BigWaveNumber - _Time.z*_BigWaveSpeed + v.uv.x*wavebevel) + 1)*0.5*_BigWaveHeight*attenuation;
height += (cos(x*_BigWaveNumber - _Time.z*_BigWaveSpeed + (1.0 - v.uv.x)*wavebevel) + 1)*0.5*_BigWaveHeight*attenuation;
}
v.vertex.y = v.vertex.y + n * _WaveHeight + height;
if (v.vertex.y > _dividHeight)
v.vertex.z -= height * 0.1;
o.vertex = UnityObjectToClipPos(v.vertex);
//屏幕UV
o.screenuv = ComputeScreenPos(o.vertex);// 背景纹理采样坐标
UNITY_TRANSFER_FOG(o, o.vertex);
return o;
}
fixed4 texCross(sampler2D tex, float2 uv, float speed)
{
float4 col1 = tex2D(tex, uv + float2(-_Time.x * speed, -_Time.x * speed));
float4 col2 = tex2D(tex, uv + float2(_Time.x * speed * 0.5, _Time.x * 0.15 * speed));
float4 col3 = tex2D(tex, uv + float2(_Time.x * speed * 0.84, -_Time.x * 0.11 * speed));
float4 col4 = tex2D(tex, uv + float2(_Time.x * speed * 0.105, -_Time.x * 0.3515 * speed));
return (col1 + col2 + col3 + col4) / 4;
}
// 输出结构>>>像素
fixed4 frag (v2f i) : COLOR
{
//屏幕深度Z
float ScreenZ = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.screenuv)));
float4 finalCol = _Color;
//Grab Tex
fixed4 grabCol;
grabCol = tex2D(_GrabPassTex, i.screenuv.xy / i.screenuv.w);
//深色
grabCol = lerp(grabCol, lerp(grabCol, _Color, _Color.a), _Opacity);
fixed2 waveUV = i.uv.xy;
return grabCol;
}
ENDCG
}
}
}
网友评论