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.

429 lines
12 KiB

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