You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

444 lines
13 KiB

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