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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
| Shader "HHF/Example/Water" { Properties { [Header(Common)] _Water01("Direction(XY) Speed(Z) Distort(W)",vector) = (1,1,0.1,0.05) _Water02("Atten(X) Lightness(Y) Caustic(Z)",vector) = (1,1,3,0) _Water03("Specular: Distort(X) Intensity(Y) Smoothness(Z)",vector) = (0.8,5,8,0) _Water04("FoamRange(X) FoamNoise(Y)",vector) = (5,2.8,0,0) _WaterRampTex("WaterRampTex",2D) = "white"{} [Header(Normal)] _NormalTex("NormalTex",2D) = "white"{}
[Header(Specular)] _SpecularColor("Specular Color",color) = (1,1,1,1)
[Header(Reflection)] _ReflectionTex("ReflectionTex",Cube) = "white"{}
[Header(Caustic)] _CausticTex("CausticTex",2D) = "white"{}
[Header(Foam)] _FoamTex("FoamTex", 2D) = "white" {} _FoamColor("FoamColor",color) = (1,1,1,1) }
SubShader { Tags { "Queue"="Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True" "RenderPipeline" = "UniversalPipeline" }
Pass { HLSLPROGRAM #pragma prefer_hlslcc gles #pragma exclude_renderers d3d11_9x #pragma vertex vert #pragma fragment frag #pragma multi_compile_fog #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; float3 normal : NORMAL; };
struct v2f { float4 positionCS : SV_POSITION; float4 uv : TEXCOORD0; float4 normalUV : TEXCOORD1; float3 positionVS : TEXCOORD2; float3 positionWS : TEXCOORD3; float3 normalWS : TEXCOORD4; float2 timeOffset : TEXCOORD5; };
CBUFFER_START(UnityPerMaterial) float4 _Water01,_Water02,_Water03,_Water04; float4 _WaterRampTex_ST; float4 _NormalTex_ST; half4 _SpecularColor; float4 _FoamTex_ST; half4 _FoamColor; float4 _CausticTex_ST; CBUFFER_END TEXTURE2D (_NormalTex);SAMPLER(sampler_NormalTex); TEXTURECUBE (_ReflectionTex);SAMPLER(sampler_ReflectionTex); TEXTURE2D (_CausticTex);SAMPLER(sampler_CausticTex); TEXTURE2D (_FoamTex);SAMPLER(sampler_FoamTex);
TEXTURE2D (_WaterRampTex);SAMPLER(sampler_WaterRampTex); TEXTURE2D (_CameraDepthTexture);SAMPLER(sampler_CameraDepthTexture); TEXTURE2D (_CameraOpaqueTexture);SAMPLER(sampler_CameraOpaqueTexture);
v2f vert(appdata v) { v2f o = (v2f)0;
o.positionWS = TransformObjectToWorld(v.vertex.xyz); o.positionVS = TransformWorldToView(o.positionWS); o.positionCS = TransformObjectToHClip(v.vertex.xyz);
o.timeOffset = _Time.y * _Water01.z * _Water01.xy; o.uv.xy = o.positionWS.xz * _FoamTex_ST.xy + o.timeOffset; o.normalUV.xy = TRANSFORM_TEX(v.uv,_NormalTex) + o.timeOffset ; o.normalUV.zw = TRANSFORM_TEX(v.uv,_NormalTex) + o.timeOffset * float2(-1.07, 1.13);
o.normalWS = TransformObjectToWorldNormal(v.normal); return o; }
half4 frag(v2f i) : SV_Target { float distort = _Water01.w; float atten = _Water02.x; float lightness = _Water02.y; float causticIntensity = _Water02.z; float specularDistort = _Water03.x; float specularIntensity = _Water03.y; float specularSmoothness = _Water03.z; float foamRange = _Water04.x; float foamNoise = _Water04.y;
float2 screenUV = i.positionCS.xy/_ScreenParams.xy;
half depthTex = SAMPLE_TEXTURE2D(_CameraDepthTexture,sampler_CameraDepthTexture,screenUV).r; half depthScene = LinearEyeDepth(depthTex,_ZBufferParams); half depthWater = depthScene + i.positionVS.z; depthWater *= atten;
half4 normalTex01 = SAMPLE_TEXTURE2D(_NormalTex,sampler_NormalTex,i.normalUV.xy); half4 normalTex02 = SAMPLE_TEXTURE2D(_NormalTex,sampler_NormalTex,i.normalUV.zw); half4 normalTex = normalTex01*normalTex02; half3 normal = normalTex.xyz * distort;
float2 distortUV = screenUV + normal.xy; half depthDistortTex = SAMPLE_TEXTURE2D(_CameraDepthTexture,sampler_CameraDepthTexture,distortUV).r; half depthDistortScene = LinearEyeDepth(depthDistortTex,_ZBufferParams); half depthDistortWater = depthDistortScene + i.positionVS.z; half opaqueUVMask = step(0, depthDistortWater); float2 opaqueUV = distortUV * opaqueUVMask + screenUV * (1 - opaqueUVMask); half4 opaqueTex = SAMPLE_TEXTURE2D(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, opaqueUV);
float4 depthVS = 1; depthVS.xy = i.positionVS.xy * depthDistortScene / -i.positionVS.z; depthVS.z = depthDistortScene; float3 depthWS = mul(unity_CameraToWorld,depthVS).xyz; float2 causticUV01 = depthWS.xz * _CausticTex_ST.xy + depthWS.y * 0.2 + i.timeOffset; half4 causticTex01 = SAMPLE_TEXTURE2D(_CausticTex,sampler_CausticTex,causticUV01); float2 causticUV02 = depthWS.xz * _CausticTex_ST.xy + depthWS.y * 0.1 + i.timeOffset * float2(-1.07,1.43); half4 causticTex02 = SAMPLE_TEXTURE2D(_CausticTex,sampler_CausticTex,causticUV02); half4 caustic = min(causticTex01,causticTex02); caustic *= causticIntensity;
half3 N = lerp(normalize(i.normalWS),normalTex.xyz,specularDistort); Light light = GetMainLight(); half3 L = light.direction; half3 V = normalize(_WorldSpaceCameraPos.xyz - i.positionWS.xyz); half3 H = normalize(L+V); half4 specular = _SpecularColor * specularIntensity * pow(saturate(dot(N,H)),specularSmoothness);
half3 reflectionUV = reflect(-V,normal); half4 reflectionTex = SAMPLE_TEXTURECUBE(_ReflectionTex,sampler_ReflectionTex,reflectionUV); half fresnel = pow(1-saturate(dot(i.normalWS,V)),3); half4 reflection = reflectionTex * fresnel;
half foamWidth = depthWater * foamRange; half foamTex = SAMPLE_TEXTURE2D(_FoamTex,sampler_FoamTex,i.uv.xy).r; foamTex = pow(abs(foamTex),foamNoise); half foamMask = step(foamWidth,foamTex); half4 foam = foamMask * _FoamColor;
half4 c = half4(0,0,0,1);
half4 waterColor = SAMPLE_TEXTURE2D(_WaterRampTex, sampler_WaterRampTex, float2(depthWater, 0)); c += waterColor * lightness; c += specular * reflection; c += foam; half4 waterDepthColor = SAMPLE_TEXTURE2D(_WaterRampTex, sampler_WaterRampTex, float2(depthWater, 1)); c += (opaqueTex + caustic * lightness) * waterDepthColor; return c; } ENDHLSL } }
SubShader { Tags { "Queue"="Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True" }
GrabPass { "_CameraOpaqueTexture" } Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_fog #include "UnityCG.cginc"
struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; float3 normal : NORMAL; };
struct v2f { float4 positionCS : SV_POSITION; float4 uv : TEXCOORD0; float4 normalUV : TEXCOORD1; float3 positionVS : TEXCOORD2; float3 positionWS : TEXCOORD3; float3 normalWS : TEXCOORD4; float2 timeOffset : TEXCOORD5; };
float4 _Water01,_Water02, _Water03,_Water04; float4 _WaterRampTex_ST; float4 _NormalTex_ST; half4 _SpecularColor; float4 _FoamTex_ST; half4 _FoamColor; float4 _CausticTex_ST; sampler2D _NormalTex; sampler2D _CausticTex; sampler2D _FoamTex; sampler2D _WaterRampTex; sampler2D _CameraDepthTexture; sampler2D _CameraOpaqueTexture; samplerCUBE _ReflectionTex; v2f vert(appdata v) { v2f o = (v2f)0;
o.positionWS = mul(UNITY_MATRIX_M, v.vertex); o.positionVS = mul(UNITY_MATRIX_V, float4(o.positionWS.xyz, 1)); o.positionCS = UnityObjectToClipPos(v.vertex);
o.timeOffset = _Time.y * _Water01.z * _Water01.xy; o.uv.xy = o.positionWS.xz * _FoamTex_ST.xy + o.timeOffset; o.normalUV.xy = TRANSFORM_TEX(v.uv,_NormalTex) + o.timeOffset ; o.normalUV.zw = TRANSFORM_TEX(v.uv,_NormalTex) + o.timeOffset * float2(-1.07, 1.13);
o.normalWS = UnityObjectToWorldNormal(v.normal); return o; }
half4 frag(v2f i) : SV_Target { float distort = _Water01.w; float atten = _Water02.x; float lightness = _Water02.y; float causticIntensity = _Water02.z; float specularDistort = _Water03.x; float specularIntensity = _Water03.y; float specularSmoothness = _Water03.z; float foamRange = _Water04.x; float foamNoise = _Water04.y;
float2 screenUV = i.positionCS.xy/_ScreenParams.xy;
half4 depthTex = tex2D(_CameraDepthTexture, screenUV).r; half depthScene = LinearEyeDepth(depthTex); half depthWater = depthScene + i.positionVS.z; depthWater *= atten;
half4 normalTex01 = tex2D(_NormalTex, i.normalUV.xy); half4 normalTex02 = tex2D(_NormalTex, i.normalUV.zw); half4 normalTex = normalTex01*normalTex02; half3 normal = normalTex.xyz * distort;
float2 distortUV = screenUV + normal.xy; half depthDistortTex = tex2D(_CameraDepthTexture, distortUV).r; half depthDistortScene = LinearEyeDepth(depthDistortTex); half depthDistortWater = depthDistortScene + i.positionVS.z; half opaqueUVMask = step(0, depthDistortWater); float2 opaqueUV = distortUV * opaqueUVMask + screenUV * (1 - opaqueUVMask); half4 opaqueTex = tex2D(_CameraOpaqueTexture, opaqueUV);
float4 depthVS = 1; depthVS.xy = i.positionVS.xy * depthDistortScene / -i.positionVS.z; depthVS.z = depthDistortScene; float3 depthWS = mul(unity_CameraToWorld,depthVS).xyz; float2 causticUV01 = depthWS.xz * _CausticTex_ST.xy + depthWS.y * 0.2 + i.timeOffset; half4 causticTex01 = tex2D(_CausticTex, causticUV01); float2 causticUV02 = depthWS.xz * _CausticTex_ST.xy + depthWS.y * 0.1 + i.timeOffset * float2(-1.07,1.43); half4 causticTex02 = tex2D(_CausticTex, causticUV02); half4 caustic = min(causticTex01,causticTex02); caustic *= causticIntensity;
half3 N = lerp(normalize(i.normalWS),normalTex.xyz,specularDistort); half3 L = _WorldSpaceLightPos0; half3 V = normalize(_WorldSpaceCameraPos.xyz - i.positionWS.xyz); half3 H = normalize(L+V); half4 specular = _SpecularColor * specularIntensity * pow(saturate(dot(N,H)),specularSmoothness);
half3 reflectionUV = reflect(-V,normal); half4 reflectionTex = texCUBE(_ReflectionTex, reflectionUV); half fresnel = pow(1-saturate(dot(i.normalWS,V)),3); half4 reflection = reflectionTex * fresnel;
half foamWidth = depthWater * foamRange; half foamTex = tex2D(_FoamTex, i.uv.xy).r; foamTex = pow(abs(foamTex),foamNoise); half foamMask = step(foamWidth,foamTex); half4 foam = foamMask * _FoamColor;
half4 c = half4(0,0,0,1);
half4 waterColor = tex2D(_WaterRampTex, float2(depthWater, 0)); c += waterColor * lightness; c += specular * reflection; c += foam; half4 waterDepthColor = tex2D(_WaterRampTex, float2(depthWater, 1)); c += (opaqueTex + caustic * lightness) * waterDepthColor; return c; } ENDCG } } }
|