1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
| Shader "Custom/URP/WaterSurface" { Properties { // 水面颜色 _ShallowColor ("Shallow Color", Color) = (0.1, 0.5, 0.6, 0.7) _DeepColor ("Deep Color", Color) = (0.02, 0.1, 0.3, 0.9) // 法线贴图(两层,不同方向流动) _NormalMap ("Normal Map (Layer 1)", 2D) = "bump" {} _NormalMap2 ("Normal Map (Layer 2)", 2D) = "bump" {} _NormalStrength ("Normal Strength", Range(0.0, 2.0)) = 0.8 // UV 流动速度(Layer1 XY,Layer2 XY) _FlowSpeed1 ("Flow Speed Layer 1", Vector) = (0.05, 0.02, 0.0, 0.0) _FlowSpeed2 ("Flow Speed Layer 2", Vector) = (-0.02, 0.04, 0.0, 0.0) // UV 缩放 _NormalTiling ("Normal Map Tiling", Float) = 4.0 // 反射 _ReflectionCube ("Reflection Cubemap", CUBE) = "" {} _ReflectionStrength ("Reflection Strength", Range(0.0, 1.0)) = 0.6 _Roughness ("Water Roughness", Range(0.0, 1.0)) = 0.05 // 折射/水深 _RefractionStrength ("Refraction Distortion", Range(0.0, 0.1)) = 0.03 _DepthFade ("Depth Fade Distance", Range(0.1, 10.0)) = 3.0 // 泡沫(接岸边缘) _FoamColor ("Foam Color", Color) = (1.0, 1.0, 1.0, 1.0) _FoamRange ("Foam Range", Range(0.0, 2.0)) = 0.5 // 高光 _SpecularColor ("Specular Color", Color) = (1.0, 1.0, 1.0, 1.0) _SpecularPower ("Specular Power", Range(8.0, 256.0)) = 64.0 }
SubShader { Tags { "RenderType" = "Transparent" "Queue" = "Transparent" "RenderPipeline" = "UniversalPipeline" }
// 半透明水面:标准 Alpha 混合 Blend SrcAlpha OneMinusSrcAlpha ZWrite Off
Pass { Name "WaterForward" Tags { "LightMode" = "UniversalForward" }
HLSLPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE #pragma multi_compile_fog
#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.universal/ShaderLibrary/DeclareDepthTexture.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareOpaqueTexture.hlsl"
// 纹理声明 TEXTURE2D(_NormalMap); SAMPLER(sampler_NormalMap); TEXTURE2D(_NormalMap2); SAMPLER(sampler_NormalMap2); TEXTURECUBE(_ReflectionCube); SAMPLER(sampler_ReflectionCube);
CBUFFER_START(UnityPerMaterial) float4 _NormalMap_ST; float4 _NormalMap2_ST; float4 _ShallowColor; float4 _DeepColor; float4 _FlowSpeed1; float4 _FlowSpeed2; float4 _FoamColor; float4 _SpecularColor; float _NormalStrength; float _NormalTiling; float _ReflectionStrength; float _Roughness; float _RefractionStrength; float _DepthFade; float _FoamRange; float _SpecularPower; CBUFFER_END
struct Attributes { float4 positionOS : POSITION; float3 normalOS : NORMAL; float4 tangentOS : TANGENT; float2 uv : TEXCOORD0; UNITY_VERTEX_INPUT_INSTANCE_ID };
struct Varyings { float4 positionHCS : SV_POSITION; float2 uv : TEXCOORD0; float3 positionWS : TEXCOORD1; float3 normalWS : TEXCOORD2; float3 tangentWS : TEXCOORD3; float3 bitangentWS : TEXCOORD4; float4 screenPos : TEXCOORD5; // 屏幕坐标(折射、深度采样) float fogFactor : TEXCOORD6; UNITY_VERTEX_OUTPUT_STEREO };
// ======== 法线贴图解码 ========
// UnpackNormal: DXT5nm 格式(URP 标准,重建 Z 分量) // UnpackNormalScale: 带强度缩放 float3 decodeNormal(TEXTURE2D_PARAM(normalMap, sampler_n), float2 uv, float strength) { // SAMPLE_TEXTURE2D 采样后用 URP 内置函数解码 float4 packed = SAMPLE_TEXTURE2D(normalMap, sampler_n, uv); // UnpackNormalScale 内部处理了 DXT5nm(BC5)和 iOS/Android 格式差异 return UnpackNormalScale(packed, strength); }
// 法线混合:Reoriented Normal Mapping(RNM) // 比简单的 normalize(n1 + n2) 更物理正确 float3 blendNormalsRNM(float3 n1, float3 n2) { float3 t = n1.xyz + float3(0, 0, 1); float3 u = n2.xyz * float3(-1, -1, 1); return normalize(t * dot(t, u) / t.z - u); }
Varyings vert(Attributes IN) { Varyings OUT; UNITY_SETUP_INSTANCE_ID(IN); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
VertexPositionInputs posInputs = GetVertexPositionInputs(IN.positionOS.xyz); VertexNormalInputs normalInputs = GetVertexNormalInputs(IN.normalOS, IN.tangentOS);
OUT.positionHCS = posInputs.positionCS; OUT.positionWS = posInputs.positionWS; OUT.normalWS = normalInputs.normalWS; OUT.tangentWS = normalInputs.tangentWS; OUT.bitangentWS = normalInputs.bitangentWS; OUT.uv = IN.uv;
// 屏幕坐标(用于深度采样和折射 UV 偏移) OUT.screenPos = ComputeScreenPos(posInputs.positionCS); OUT.fogFactor = ComputeFogFactor(posInputs.positionCS.z);
return OUT; }
half4 frag(Varyings IN) : SV_Target { float time = _Time.y;
// ===== 1. 双层流动 UV ===== float2 uv1 = IN.uv * _NormalTiling + _FlowSpeed1.xy * time; float2 uv2 = IN.uv * _NormalTiling * 0.7 + _FlowSpeed2.xy * time;
// ===== 2. 双层法线叠加 ===== float3 normalTangent1 = decodeNormal(TEXTURE2D_ARGS(_NormalMap, sampler_NormalMap), uv1, _NormalStrength); float3 normalTangent2 = decodeNormal(TEXTURE2D_ARGS(_NormalMap2, sampler_NormalMap2), uv2, _NormalStrength * 0.7); // RNM 混合(比简单相加更准确) float3 blendedNormalTangent = blendNormalsRNM(normalTangent1, normalTangent2);
// 切线空间 → 世界空间 float3x3 TBN = float3x3( normalize(IN.tangentWS), normalize(IN.bitangentWS), normalize(IN.normalWS) ); // HLSL 的 mul(v, TBN) 等价于 transpose(TBN) * v(转置后右乘) float3 normalWS = normalize(mul(blendedNormalTangent, TBN));
// ===== 3. 折射 UV(扰动背景色纹理) ===== float2 screenUV = IN.screenPos.xy / IN.screenPos.w; float2 refractionOffset = blendedNormalTangent.xy * _RefractionStrength; float2 refractionUV = screenUV + refractionOffset; half3 refractionColor = SampleSceneColor(refractionUV).rgb;
// ===== 4. 深度采样(水深/软边缘/泡沫) ===== float sceneDepth = LinearEyeDepth(SampleSceneDepth(screenUV), _ZBufferParams); float waterDepth = IN.screenPos.w; // 水面片元的视空间深度 float depthDifference = sceneDepth - waterDepth;
// 水深颜色混合(越深越暗越蓝) float depthT = saturate(depthDifference / _DepthFade); half4 waterColor = lerp(_ShallowColor, _DeepColor, depthT);
// 接岸泡沫(浅水区域) float foamFactor = 1.0 - saturate(depthDifference / _FoamRange); foamFactor = pow(foamFactor, 2.0); // 非线性,让泡沫更集中在边缘
// ===== 5. 反射(Cubemap) ===== float3 viewDirWS = normalize(GetCameraPositionWS() - IN.positionWS); float3 reflectDir = reflect(-viewDirWS, normalWS); // 用粗糙度选择 mip(roughness 0 = 镜面反射,1 = 漫反射) float reflectMip = _Roughness * 6.0; half3 reflectionColor = SAMPLE_TEXTURECUBE_LOD( _ReflectionCube, sampler_ReflectionCube, reflectDir, reflectMip ).rgb;
// Fresnel(掠射角时反射更强) float NdotV = saturate(dot(normalWS, viewDirWS)); float fresnel = pow(1.0 - NdotV, 4.0); float3 reflectFinal = reflectionColor * (_ReflectionStrength + fresnel * (1.0 - _ReflectionStrength));
// ===== 6. 镜面高光(Blinn-Phong,主光源) ===== Light mainLight = GetMainLight(); float3 halfDir = normalize(viewDirWS + mainLight.direction); float NdotH = saturate(dot(normalWS, halfDir)); float spec = pow(NdotH, _SpecularPower); half3 specular = _SpecularColor.rgb * spec * mainLight.color;
// ===== 7. 合并所有层 ===== // 基础颜色 = 折射(水下场景)+ 水色调 half3 baseColor = lerp(refractionColor, waterColor.rgb, waterColor.a * 0.6); // 加反射 baseColor = lerp(baseColor, reflectFinal, fresnel * _ReflectionStrength); // 加高光 baseColor += specular; // 加泡沫 baseColor = lerp(baseColor, _FoamColor.rgb, foamFactor * _FoamColor.a);
// 透明度:浅水更透明,泡沫不透明 float finalAlpha = lerp(waterColor.a, 1.0, foamFactor);
// 应用雾效 half3 finalColor = MixFog(baseColor, IN.fogFactor);
return half4(finalColor, finalAlpha); } ENDHLSL } } }
|