Statistics
| Branch: | Tag: | Revision:

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

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