root / Assets / Standard Assets / Environment / Water / Water / Shaders / FXWaterPro.shader @ 175:f9f5640c2a3a
History | View | Annotate | Download (4.1 kB)
1 | // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' |
---|---|
2 | |
3 | Shader "FX/Water" { |
4 | Properties { |
5 | _WaveScale ("Wave scale", Range (0.02,0.15)) = 0.063 |
6 | _ReflDistort ("Reflection distort", Range (0,1.5)) = 0.44 |
7 | _RefrDistort ("Refraction distort", Range (0,1.5)) = 0.40 |
8 | _RefrColor ("Refraction color", COLOR) = ( .34, .85, .92, 1) |
9 | [NoScaleOffset] _Fresnel ("Fresnel (A) ", 2D) = "gray" {} |
10 | [NoScaleOffset] _BumpMap ("Normalmap ", 2D) = "bump" {} |
11 | WaveSpeed ("Wave speed (map1 x,y; map2 x,y)", Vector) = (19,9,-16,-7) |
12 | [NoScaleOffset] _ReflectiveColor ("Reflective color (RGB) fresnel (A) ", 2D) = "" {} |
13 | _HorizonColor ("Simple water horizon color", COLOR) = ( .172, .463, .435, 1) |
14 | [HideInInspector] _ReflectionTex ("Internal Reflection", 2D) = "" {} |
15 | [HideInInspector] _RefractionTex ("Internal Refraction", 2D) = "" {} |
16 | } |
17 | |
18 | |
19 | // ----------------------------------------------------------- |
20 | // Fragment program cards |
21 | |
22 | |
23 | Subshader { |
24 | Tags { "WaterMode"="Refractive" "RenderType"="Opaque" } |
25 | Pass { |
26 | CGPROGRAM |
27 | #pragma vertex vert |
28 | #pragma fragment frag |
29 | #pragma multi_compile_fog |
30 | #pragma multi_compile WATER_REFRACTIVE WATER_REFLECTIVE WATER_SIMPLE |
31 | |
32 | #if defined (WATER_REFLECTIVE) || defined (WATER_REFRACTIVE) |
33 | #define HAS_REFLECTION 1 |
34 | #endif |
35 | #if defined (WATER_REFRACTIVE) |
36 | #define HAS_REFRACTION 1 |
37 | #endif |
38 | |
39 | |
40 | #include "UnityCG.cginc" |
41 | |
42 | uniform float4 _WaveScale4; |
43 | uniform float4 _WaveOffset; |
44 | |
45 | #if HAS_REFLECTION |
46 | uniform float _ReflDistort; |
47 | #endif |
48 | #if HAS_REFRACTION |
49 | uniform float _RefrDistort; |
50 | #endif |
51 | |
52 | struct appdata { |
53 | float4 vertex : POSITION; |
54 | float3 normal : NORMAL; |
55 | }; |
56 | |
57 | struct v2f { |
58 | float4 pos : SV_POSITION; |
59 | #if defined(HAS_REFLECTION) || defined(HAS_REFRACTION) |
60 | float4 ref : TEXCOORD0; |
61 | float2 bumpuv0 : TEXCOORD1; |
62 | float2 bumpuv1 : TEXCOORD2; |
63 | float3 viewDir : TEXCOORD3; |
64 | #else |
65 | float2 bumpuv0 : TEXCOORD0; |
66 | float2 bumpuv1 : TEXCOORD1; |
67 | float3 viewDir : TEXCOORD2; |
68 | #endif |
69 | UNITY_FOG_COORDS(4) |
70 | }; |
71 | |
72 | v2f vert(appdata v) |
73 | { |
74 | v2f o; |
75 | o.pos = mul (UNITY_MATRIX_MVP, v.vertex); |
76 | |
77 | |
78 | // scroll bump waves |
79 | float4 temp; |
80 | float4 wpos = mul (unity_ObjectToWorld, v.vertex); |
81 | temp.xyzw = wpos.xzxz * _WaveScale4 + _WaveOffset; |
82 | o.bumpuv0 = temp.xy; |
83 | o.bumpuv1 = temp.wz; |
84 | |
85 | // object space view direction (will normalize per pixel) |
86 | o.viewDir.xzy = WorldSpaceViewDir(v.vertex); |
87 | |
88 | #if defined(HAS_REFLECTION) || defined(HAS_REFRACTION) |
89 | o.ref = ComputeScreenPos(o.pos); |
90 | #endif |
91 | |
92 | UNITY_TRANSFER_FOG(o,o.pos); |
93 | return o; |
94 | } |
95 | |
96 | #if defined (WATER_REFLECTIVE) || defined (WATER_REFRACTIVE) |
97 | sampler2D _ReflectionTex; |
98 | #endif |
99 | #if defined (WATER_REFLECTIVE) || defined (WATER_SIMPLE) |
100 | sampler2D _ReflectiveColor; |
101 | #endif |
102 | #if defined (WATER_REFRACTIVE) |
103 | sampler2D _Fresnel; |
104 | sampler2D _RefractionTex; |
105 | uniform float4 _RefrColor; |
106 | #endif |
107 | #if defined (WATER_SIMPLE) |
108 | uniform float4 _HorizonColor; |
109 | #endif |
110 | sampler2D _BumpMap; |
111 | |
112 | half4 frag( v2f i ) : SV_Target |
113 | { |
114 | i.viewDir = normalize(i.viewDir); |
115 | |
116 | // combine two scrolling bumpmaps into one |
117 | half3 bump1 = UnpackNormal(tex2D( _BumpMap, i.bumpuv0 )).rgb; |
118 | half3 bump2 = UnpackNormal(tex2D( _BumpMap, i.bumpuv1 )).rgb; |
119 | half3 bump = (bump1 + bump2) * 0.5; |
120 | |
121 | // fresnel factor |
122 | half fresnelFac = dot( i.viewDir, bump ); |
123 | |
124 | // perturb reflection/refraction UVs by bumpmap, and lookup colors |
125 | |
126 | #if HAS_REFLECTION |
127 | float4 uv1 = i.ref; uv1.xy += bump * _ReflDistort; |
128 | half4 refl = tex2Dproj( _ReflectionTex, UNITY_PROJ_COORD(uv1) ); |
129 | #endif |
130 | #if HAS_REFRACTION |
131 | float4 uv2 = i.ref; uv2.xy -= bump * _RefrDistort; |
132 | half4 refr = tex2Dproj( _RefractionTex, UNITY_PROJ_COORD(uv2) ) * _RefrColor; |
133 | #endif |
134 | |
135 | // final color is between refracted and reflected based on fresnel |
136 | half4 color; |
137 | |
138 | #if defined(WATER_REFRACTIVE) |
139 | half fresnel = UNITY_SAMPLE_1CHANNEL( _Fresnel, float2(fresnelFac,fresnelFac) ); |
140 | color = lerp( refr, refl, fresnel ); |
141 | #endif |
142 | |
143 | #if defined(WATER_REFLECTIVE) |
144 | half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) ); |
145 | color.rgb = lerp( water.rgb, refl.rgb, water.a ); |
146 | color.a = refl.a * water.a; |
147 | #endif |
148 | |
149 | #if defined(WATER_SIMPLE) |
150 | half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) ); |
151 | color.rgb = lerp( water.rgb, _HorizonColor.rgb, water.a ); |
152 | color.a = _HorizonColor.a; |
153 | #endif |
154 | |
155 | UNITY_APPLY_FOG(i.fogCoord, color); |
156 | return color; |
157 | } |
158 | ENDCG |
159 | |
160 | } |
161 | } |
162 | |
163 | } |