Statistics
| Branch: | Tag: | Revision:

root / Assets / Standard Assets / Water (Pro Only) / Water4 / Sources / Shaders / FX-Water4.shader @ 175:f9f5640c2a3a

History | View | Annotate | Download (12.8 kB)

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