一、工程截图
二、控制水底波的Shader
//水底光影效果 Shader "Custom/UnderwaterLightShadow" { Properties { //水底纹理 _MainTex ("Texture", 2D) = "white" {} //光影灰度图 _LightTex("Light Shadow", 2D) = "white" {} //光影颜色 _LightColor("Light Color", Color) = (1,1,1,1) //频率(X轴方向) _FrequencyX("FrequencyX", float) = 2 //频率(Y轴方向) _FrequencyY("FrequencyY", float) = 2 //波动周期(X轴方向) _CycleX("CycleX", float) = 10 //波动周期(Y轴方向) _CycleY("CycleY", float) = 10 //波动幅度(X轴方向) _AmplitudeX("AmplitudeX", float) = 0.003 //波动幅度(Y轴方向) _AmplitudeY("AmplitudeY", float) = 0.003 } SubShader { Tags { "RenderType"="Opaque" } LOD 100 Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag // make fog work #pragma multi_compile_fog #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float2 uv : TEXCOORD0; UNITY_FOG_COORDS(1) float4 vertex : SV_POSITION; }; sampler2D _MainTex; float4 _MainTex_ST; sampler2D _LightTex; float4 _LightTex_ST; float4 _LightColor; float _FrequencyX; float _FrequencyY; float _CycleX; float _CycleY; float _AmplitudeX; float _AmplitudeY; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); UNITY_TRANSFER_FOG(o,o.vertex); return o; } fixed4 frag(v2f i) : SV_Target { //控制光影图像素偏移 float2 uv_offset = float2(0,0); //水平震动 uv_offset.x = sin(_FrequencyX * _Time.y + _CycleY * i.uv.y * UNITY_TWO_PI) * _AmplitudeX; //垂直震动 uv_offset.y = sin(_FrequencyY * _Time.y + _CycleX * i.uv.x * UNITY_TWO_PI) * _AmplitudeY; //处理Tiling和Offset float2 uv = i.uv * _LightTex_ST.xy + _LightTex_ST.zw; //纹理采样 fixed4 light_color = tex2D(_LightTex, uv + uv_offset) * _LightColor; // sample the texture fixed4 col = tex2D(_MainTex, i.uv) + light_color; // apply fog UNITY_APPLY_FOG(i.fogCoord, col); return col; } ENDCG } } }
运行效果