FXWater4Advanced.shader 13 KB

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