FXWater4Advanced.shader 13 KB

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