1 // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
2
3 Shader "FX/SimpleWater4" {
4 Properties {
5 _ReflectionTex ("Internal reflection", 2D) = "white" {}
6
7 _MainTex ("Fallback texture", 2D) = "black" {}
8 _BumpMap ("Normals ", 2D) = "bump" {}
9
10 _DistortParams ("Distortions (Bump waves, Reflection, Fresnel power, Fresnel bias)", Vector) = (1.0 ,1.0, 2.0, 1.15)
11 _InvFadeParemeter ("Auto blend parameter (Edge, Shore, Distance scale)", Vector) = (0.15 ,0.15, 0.5, 1.0)
12
13 _AnimationTiling ("Animation Tiling (Displacement)", Vector) = (2.2 ,2.2, -1.1, -1.1)
14 _AnimationDirection ("Animation Direction (displacement)", Vector) = (1.0 ,1.0, 1.0, 1.0)
15
16 _BumpTiling ("Bump Tiling", Vector) = (1.0 ,1.0, -2.0, 3.0)
17 _BumpDirection ("Bump Direction & Speed", Vector) = (1.0 ,1.0, -1.0, 1.0)
18
19 _FresnelScale ("FresnelScale", Range (0.15, 4.0)) = 0.75
20
21 _BaseColor ("Base color", COLOR) = ( .54, .95, .99, 0.5)
22 _ReflectionColor ("Reflection color", COLOR) = ( .54, .95, .99, 0.5)
23 _SpecularColor ("Specular color", COLOR) = ( .72, .72, .72, 1)
24
25 _WorldLightDir ("Specular light direction", Vector) = (0.0, 0.1, -0.5, 0.0)
26 _Shininess ("Shininess", Range (2.0, 500.0)) = 200.0
27
28 _GerstnerIntensity("Per vertex displacement", Float) = 1.0
29 _GAmplitude ("Wave Amplitude", Vector) = (0.3 ,0.35, 0.25, 0.25)
30 _GFrequency ("Wave Frequency", Vector) = (1.3, 1.35, 1.25, 1.25)
31 _GSteepness ("Wave Steepness", Vector) = (1.0, 1.0, 1.0, 1.0)
32 _GSpeed ("Wave Speed", Vector) = (1.2, 1.375, 1.1, 1.5)
33 _GDirectionAB ("Wave Direction", Vector) = (0.3 ,0.85, 0.85, 0.25)
34 _GDirectionCD ("Wave Direction", Vector) = (0.1 ,0.9, 0.5, 0.5)
35 }
36
37
38 CGINCLUDE
39
40 #include "UnityCG.cginc"
41 #include "WaterInclude.cginc"
42
43 struct appdata
44 {
45 float4 vertex : POSITION;
46 float3 normal : NORMAL;
47 };
48
49 // interpolator structs
50
51 struct v2f
52 {
53 float4 pos : SV_POSITION;
54 float4 normalInterpolator : TEXCOORD0;
55 float3 viewInterpolator : TEXCOORD1;
56 float4 bumpCoords : TEXCOORD2;
57 float4 screenPos : TEXCOORD3;
58 float4 grabPassPos : TEXCOORD4;
59 UNITY_FOG_COORDS(5)
60 };
61
62 struct v2f_noGrab
63 {
64 float4 pos : SV_POSITION;
65 float4 normalInterpolator : TEXCOORD0;
66 float3 viewInterpolator : TEXCOORD1;
67 float4 bumpCoords : TEXCOORD2;
68 float4 screenPos : TEXCOORD3;
69 UNITY_FOG_COORDS(4)
70 };
71
72 struct v2f_simple
73 {
74 float4 pos : SV_POSITION;
75 float3 viewInterpolator : TEXCOORD0;
76 float4 bumpCoords : TEXCOORD1;
77 UNITY_FOG_COORDS(2)
78 };
79
80 // textures
81 sampler2D _BumpMap;
82 sampler2D _ReflectionTex;
83 sampler2D _RefractionTex;
84 sampler2D _ShoreTex;
85 sampler2D_float _CameraDepthTexture;
86
87 // colors in use
88 uniform float4 _RefrColorDepth;
89 uniform float4 _SpecularColor;
90 uniform float4 _BaseColor;
91 uniform float4 _ReflectionColor;
92
93 // edge & shore fading
94 uniform float4 _InvFadeParemeter;
95
96 // specularity
97 uniform float _Shininess;
98 uniform float4 _WorldLightDir;
99
100 // fresnel, vertex & bump displacements & strength
101 uniform float4 _DistortParams;
102 uniform float _FresnelScale;
103 uniform float4 _BumpTiling;
104 uniform float4 _BumpDirection;
105
106 uniform float4 _GAmplitude;
107 uniform float4 _GFrequency;
108 uniform float4 _GSteepness;
109 uniform float4 _GSpeed;
110 uniform float4 _GDirectionAB;
111 uniform float4 _GDirectionCD;
112
113 // shortcuts
114 #define PER_PIXEL_DISPLACE _DistortParams.x
115 #define REALTIME_DISTORTION _DistortParams.y
116 #define FRESNEL_POWER _DistortParams.z
117 #define VERTEX_WORLD_NORMAL i.normalInterpolator.xyz
118 #define DISTANCE_SCALE _InvFadeParemeter.z
119 #define FRESNEL_BIAS _DistortParams.w
120
121 //
122 // HQ VERSION
123 //
124
125 v2f vert(appdata_full v)
126 {
127 v2f o;
128
129 half3 worldSpaceVertex = mul(unity_ObjectToWorld,(v.vertex)).xyz;
130 half3 vtxForAni = (worldSpaceVertex).xzz;
131
132 half3 nrml;
133 half3 offsets;
134
135 Gerstner (
136 offsets, nrml, v.vertex.xyz, vtxForAni, // offsets, nrml will be written
137 _GAmplitude, // amplitude
138 _GFrequency, // frequency
139 _GSteepness, // steepness
140 _GSpeed, // speed
141 _GDirectionAB, // direction # 1, 2
142 _GDirectionCD // direction # 3, 4
143 );
144
145 v.vertex.xyz += offsets;
146
147 half2 tileableUv = worldSpaceVertex.xz;
148
149 o.bumpCoords.xyzw = (tileableUv.xyxy + _Time.xxxx * _BumpDirection.xyzw) * _BumpTiling.xyzw;
150
151 o.viewInterpolator.xyz = worldSpaceVertex - _WorldSpaceCameraPos;
152
153 o.pos = UnityObjectToClipPos(v.vertex);
154
155 ComputeScreenAndGrabPassPos(o.pos, o.screenPos, o.grabPassPos);
156
157 o.normalInterpolator.xyz = nrml;
158
159 o.normalInterpolator.w = 1;//GetDistanceFadeout(o.screenPos.w, DISTANCE_SCALE);
160
161 UNITY_TRANSFER_FOG(o,o.pos);
162 return o;
163 }
164
165 half4 frag( v2f i ) : SV_Target
166 {
167 half3 worldNormal = PerPixelNormal(_BumpMap, i.bumpCoords, VERTEX_WORLD_NORMAL, PER_PIXEL_DISPLACE);
168 half3 viewVector = normalize(i.viewInterpolator.xyz);
169
170 half4 distortOffset = half4(worldNormal.xz * REALTIME_DISTORTION * 10.0, 0, 0);
171 half4 screenWithOffset = i.screenPos + distortOffset;
172 half4 grabWithOffset = i.grabPassPos + distortOffset;
173
174 half4 rtRefractionsNoDistort = tex2Dproj(_RefractionTex, UNITY_PROJ_COORD(i.grabPassPos));
175 half refrFix = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(grabWithOffset));
176 half4 rtRefractions = tex2Dproj(_RefractionTex, UNITY_PROJ_COORD(grabWithOffset));
177
178 #ifdef WATER_REFLECTIVE
179 half4 rtReflections = tex2Dproj(_ReflectionTex, UNITY_PROJ_COORD(screenWithOffset));
180 #endif
181
182 #ifdef WATER_EDGEBLEND_ON
183 if (LinearEyeDepth(refrFix) < i.screenPos.z)
184 rtRefractions = rtRefractionsNoDistort;
185 #endif
186
187 half3 reflectVector = normalize(reflect(viewVector, worldNormal));
188 half3 h = normalize ((_WorldLightDir.xyz) + viewVector.xyz);
189 float nh = max (0, dot (worldNormal, -h));
190 float spec = max(0.0,pow (nh, _Shininess));
191
192 half4 edgeBlendFactors = half4(1.0, 0.0, 0.0, 0.0);
193
194 #ifdef WATER_EDGEBLEND_ON
195 half depth = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.screenPos));
196 depth = LinearEyeDepth(depth);
197 edgeBlendFactors = saturate(_InvFadeParemeter * (depth-i.screenPos.w));
198 #endif
199
200 // shading for fresnel term
201 worldNormal.xz *= _FresnelScale;
202 half refl2Refr = Fresnel(viewVector, worldNormal, FRESNEL_BIAS, FRESNEL_POWER);
203
204 // base, depth & reflection colors
205 half4 baseColor = _BaseColor;
206 #ifdef WATER_REFLECTIVE
207 half4 reflectionColor = lerp (rtReflections,_ReflectionColor,_ReflectionColor.a);
208 #else
209 half4 reflectionColor = _ReflectionColor;
210 #endif
211
212 baseColor = lerp (lerp (rtRefractions, baseColor, baseColor.a), reflectionColor, refl2Refr);
213 baseColor = baseColor + spec * _SpecularColor;
214
215 baseColor.a = edgeBlendFactors.x;
216 UNITY_APPLY_FOG(i.fogCoord, baseColor);
217 return baseColor;
218 }
219
220 //
221 // MQ VERSION
222 //
223
224 v2f_noGrab vert300(appdata_full v)
225 {
226 v2f_noGrab o;
227
228 half3 worldSpaceVertex = mul(unity_ObjectToWorld,(v.vertex)).xyz;
229 half3 vtxForAni = (worldSpaceVertex).xzz;
230
231 half3 nrml;
232 half3 offsets;
233 Gerstner (
234 offsets, nrml, v.vertex.xyz, vtxForAni, // offsets, nrml will be written
235 _GAmplitude, // amplitude
236 _GFrequency, // frequency
237 _GSteepness, // steepness
238 _GSpeed, // speed
239 _GDirectionAB, // direction # 1, 2
240 _GDirectionCD // direction # 3, 4
241 );
242
243 v.vertex.xyz += offsets;
244
245 half2 tileableUv = worldSpaceVertex.xz;
246
247 o.bumpCoords.xyzw = (tileableUv.xyxy + _Time.xxxx * _BumpDirection.xyzw) * _BumpTiling.xyzw;
248
249 o.viewInterpolator.xyz = worldSpaceVertex - _WorldSpaceCameraPos;
250
251 o.pos = UnityObjectToClipPos(v.vertex);
252
253 o.screenPos = ComputeNonStereoScreenPos(o.pos);
254
255 o.normalInterpolator.xyz = nrml;
256
257 o.normalInterpolator.w = 1;//GetDistanceFadeout(o.screenPos.w, DISTANCE_SCALE);
258
259 UNITY_TRANSFER_FOG(o,o.pos);
260 return o;
261 }
262
263 half4 frag300( v2f_noGrab i ) : SV_Target
264 {
265 half3 worldNormal = PerPixelNormal(_BumpMap, i.bumpCoords, VERTEX_WORLD_NORMAL, PER_PIXEL_DISPLACE);
266 half3 viewVector = normalize(i.viewInterpolator.xyz);
267
268 half4 distortOffset = half4(worldNormal.xz * REALTIME_DISTORTION * 10.0, 0, 0);
269 half4 screenWithOffset = i.screenPos + distortOffset;
270
271 #ifdef WATER_REFLECTIVE
272 half4 rtReflections = tex2Dproj(_ReflectionTex, UNITY_PROJ_COORD(screenWithOffset));
273 #endif
274
275 half3 reflectVector = normalize(reflect(viewVector, worldNormal));
276 half3 h = normalize (_WorldLightDir.xyz + viewVector.xyz);
277 float nh = max (0, dot (worldNormal, -h));
278 float spec = max(0.0,pow (nh, _Shininess));
279
280 half4 edgeBlendFactors = half4(1.0, 0.0, 0.0, 0.0);
281
282 #ifdef WATER_EDGEBLEND_ON
283 half depth = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.screenPos));
284 depth = LinearEyeDepth(depth);
285 edgeBlendFactors = saturate(_InvFadeParemeter * (depth-i.screenPos.z));
286 #endif
287
288 worldNormal.xz *= _FresnelScale;
289 half refl2Refr = Fresnel(viewVector, worldNormal, FRESNEL_BIAS, FRESNEL_POWER);
290
291 half4 baseColor = _BaseColor;
292 #ifdef WATER_REFLECTIVE
293 baseColor = lerp (baseColor, lerp (rtReflections,_ReflectionColor,_ReflectionColor.a), saturate(refl2Refr * 1.0));
294 #else
295 baseColor = _ReflectionColor;//lerp (baseColor, _ReflectionColor, saturate(refl2Refr * 2.0));
296 #endif
297
298 baseColor = baseColor + spec * _SpecularColor;
299
300 baseColor.a = edgeBlendFactors.x * saturate(0.5 + refl2Refr * 1.0);
301 UNITY_APPLY_FOG(i.fogCoord, baseColor);
302 return baseColor;
303 }
304
305 //
306 // LQ VERSION
307 //
308
309 v2f_simple vert200(appdata_full v)
310 {
311 v2f_simple o;
312
313 half3 worldSpaceVertex = mul(unity_ObjectToWorld, v.vertex).xyz;
314 half2 tileableUv = worldSpaceVertex.xz;
315
316 o.bumpCoords.xyzw = (tileableUv.xyxy + _Time.xxxx * _BumpDirection.xyzw) * _BumpTiling.xyzw;
317
318 o.viewInterpolator.xyz = worldSpaceVertex-_WorldSpaceCameraPos;
319
320 o.pos = UnityObjectToClipPos( v.vertex);
321
322 UNITY_TRANSFER_FOG(o,o.pos);
323 return o;
324
325 }
326
327 half4 frag200( v2f_simple i ) : SV_Target
328 {
329 half3 worldNormal = PerPixelNormal(_BumpMap, i.bumpCoords, half3(0,1,0), PER_PIXEL_DISPLACE);
330 half3 viewVector = normalize(i.viewInterpolator.xyz);
331
332 half3 reflectVector = normalize(reflect(viewVector, worldNormal));
333 half3 h = normalize ((_WorldLightDir.xyz) + viewVector.xyz);
334 float nh = max (0, dot (worldNormal, -h));
335 float spec = max(0.0,pow (nh, _Shininess));
336
337 worldNormal.xz *= _FresnelScale;
338 half refl2Refr = Fresnel(viewVector, worldNormal, FRESNEL_BIAS, FRESNEL_POWER);
339
340 half4 baseColor = _BaseColor;
341 baseColor = lerp(baseColor, _ReflectionColor, saturate(refl2Refr * 2.0));
342 baseColor.a = saturate(2.0 * refl2Refr + 0.5);
343
344 baseColor.rgb += spec * _SpecularColor.rgb;
345 UNITY_APPLY_FOG(i.fogCoord, baseColor);
346 return baseColor;
347 }
348
349 ENDCG
350
351 Subshader
352 {
353 Tags {"RenderType"="Transparent" "Queue"="Transparent"}
354
355 Lod 500
356 ColorMask RGB
357
358 GrabPass { "_RefractionTex" }
359
360 Pass {
361 Blend SrcAlpha OneMinusSrcAlpha
362 ZTest LEqual
363 ZWrite Off
364 Cull Off
365
366 CGPROGRAM
367
368 #pragma target 3.0
369
370 #pragma vertex vert
371 #pragma fragment frag
372 #pragma multi_compile_fog
373
374 #pragma multi_compile WATER_VERTEX_DISPLACEMENT_ON WATER_VERTEX_DISPLACEMENT_OFF
375 #pragma multi_compile WATER_EDGEBLEND_ON WATER_EDGEBLEND_OFF
376 #pragma multi_compile WATER_REFLECTIVE WATER_SIMPLE
377
378 ENDCG
379 }
380 }
381
382 Subshader
383 {
384 Tags {"RenderType"="Transparent" "Queue"="Transparent"}
385
386 Lod 300
387 ColorMask RGB
388
389 Pass {
390 Blend SrcAlpha OneMinusSrcAlpha
391 ZTest LEqual
392 ZWrite Off
393 Cull Off
394
395 CGPROGRAM
396
397 #pragma target 3.0
398
399 #pragma vertex vert300
400 #pragma fragment frag300
401 #pragma multi_compile_fog
402
403 #pragma multi_compile WATER_VERTEX_DISPLACEMENT_ON WATER_VERTEX_DISPLACEMENT_OFF
404 #pragma multi_compile WATER_EDGEBLEND_ON WATER_EDGEBLEND_OFF
405 #pragma multi_compile WATER_REFLECTIVE WATER_SIMPLE
406
407 ENDCG
408 }
409 }
410
411 Subshader
412 {
413 Tags {"RenderType"="Transparent" "Queue"="Transparent"}
414
415 Lod 200
416 ColorMask RGB
417
418 Pass {
419 Blend SrcAlpha OneMinusSrcAlpha
420 ZTest LEqual
421 ZWrite Off
422 Cull Off
423
424 CGPROGRAM
425
426 #pragma vertex vert200
427 #pragma fragment frag200
428 #pragma multi_compile_fog
429
430 ENDCG
431 }
432 }
433
434 Fallback "Transparent/Diffuse"
435 }