FXWater4Simple.shader 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
  3. Shader "FX/SimpleWater4" {
  4. Properties {
  5. _ReflectionTex ("Internal reflection", 2D) = "white" {}
  6. _MainTex ("Fallback 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. _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. float3 viewInterpolator : TEXCOORD1;
  42. float4 bumpCoords : TEXCOORD2;
  43. float4 screenPos : TEXCOORD3;
  44. float4 grabPassPos : TEXCOORD4;
  45. UNITY_FOG_COORDS(5)
  46. };
  47. struct v2f_noGrab
  48. {
  49. float4 pos : SV_POSITION;
  50. float4 normalInterpolator : TEXCOORD0;
  51. float3 viewInterpolator : TEXCOORD1;
  52. float4 bumpCoords : TEXCOORD2;
  53. float4 screenPos : TEXCOORD3;
  54. UNITY_FOG_COORDS(4)
  55. };
  56. struct v2f_simple
  57. {
  58. float4 pos : SV_POSITION;
  59. float3 viewInterpolator : TEXCOORD0;
  60. float4 bumpCoords : TEXCOORD1;
  61. UNITY_FOG_COORDS(2)
  62. };
  63. // textures
  64. sampler2D _BumpMap;
  65. sampler2D _ReflectionTex;
  66. sampler2D _RefractionTex;
  67. sampler2D _ShoreTex;
  68. sampler2D_float _CameraDepthTexture;
  69. // colors in use
  70. uniform float4 _RefrColorDepth;
  71. uniform float4 _SpecularColor;
  72. uniform float4 _BaseColor;
  73. uniform float4 _ReflectionColor;
  74. // edge & shore fading
  75. uniform float4 _InvFadeParemeter;
  76. // specularity
  77. uniform float _Shininess;
  78. uniform float4 _WorldLightDir;
  79. // fresnel, vertex & bump displacements & strength
  80. uniform float4 _DistortParams;
  81. uniform float _FresnelScale;
  82. uniform float4 _BumpTiling;
  83. uniform float4 _BumpDirection;
  84. uniform float4 _GAmplitude;
  85. uniform float4 _GFrequency;
  86. uniform float4 _GSteepness;
  87. uniform float4 _GSpeed;
  88. uniform float4 _GDirectionAB;
  89. uniform float4 _GDirectionCD;
  90. // shortcuts
  91. #define PER_PIXEL_DISPLACE _DistortParams.x
  92. #define REALTIME_DISTORTION _DistortParams.y
  93. #define FRESNEL_POWER _DistortParams.z
  94. #define VERTEX_WORLD_NORMAL i.normalInterpolator.xyz
  95. #define DISTANCE_SCALE _InvFadeParemeter.z
  96. #define FRESNEL_BIAS _DistortParams.w
  97. //
  98. // HQ VERSION
  99. //
  100. v2f vert(appdata_full v)
  101. {
  102. v2f o;
  103. half3 worldSpaceVertex = mul(unity_ObjectToWorld,(v.vertex)).xyz;
  104. half3 vtxForAni = (worldSpaceVertex).xzz;
  105. half3 nrml;
  106. half3 offsets;
  107. Gerstner (
  108. offsets, nrml, v.vertex.xyz, vtxForAni, // offsets, nrml will be written
  109. _GAmplitude, // amplitude
  110. _GFrequency, // frequency
  111. _GSteepness, // steepness
  112. _GSpeed, // speed
  113. _GDirectionAB, // direction # 1, 2
  114. _GDirectionCD // direction # 3, 4
  115. );
  116. v.vertex.xyz += offsets;
  117. half2 tileableUv = worldSpaceVertex.xz;
  118. o.bumpCoords.xyzw = (tileableUv.xyxy + _Time.xxxx * _BumpDirection.xyzw) * _BumpTiling.xyzw;
  119. o.viewInterpolator.xyz = worldSpaceVertex - _WorldSpaceCameraPos;
  120. o.pos = UnityObjectToClipPos(v.vertex);
  121. ComputeScreenAndGrabPassPos(o.pos, o.screenPos, o.grabPassPos);
  122. o.normalInterpolator.xyz = nrml;
  123. o.normalInterpolator.w = 1;//GetDistanceFadeout(o.screenPos.w, DISTANCE_SCALE);
  124. UNITY_TRANSFER_FOG(o,o.pos);
  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. #endif
  154. // shading for fresnel term
  155. worldNormal.xz *= _FresnelScale;
  156. half refl2Refr = Fresnel(viewVector, worldNormal, FRESNEL_BIAS, FRESNEL_POWER);
  157. // base, depth & reflection colors
  158. half4 baseColor = _BaseColor;
  159. #ifdef WATER_REFLECTIVE
  160. half4 reflectionColor = lerp (rtReflections,_ReflectionColor,_ReflectionColor.a);
  161. #else
  162. half4 reflectionColor = _ReflectionColor;
  163. #endif
  164. baseColor = lerp (lerp (rtRefractions, baseColor, baseColor.a), reflectionColor, refl2Refr);
  165. baseColor = baseColor + spec * _SpecularColor;
  166. baseColor.a = edgeBlendFactors.x;
  167. UNITY_APPLY_FOG(i.fogCoord, baseColor);
  168. return baseColor;
  169. }
  170. //
  171. // MQ VERSION
  172. //
  173. v2f_noGrab vert300(appdata_full v)
  174. {
  175. v2f_noGrab o;
  176. half3 worldSpaceVertex = mul(unity_ObjectToWorld,(v.vertex)).xyz;
  177. half3 vtxForAni = (worldSpaceVertex).xzz;
  178. half3 nrml;
  179. half3 offsets;
  180. Gerstner (
  181. offsets, nrml, v.vertex.xyz, vtxForAni, // offsets, nrml will be written
  182. _GAmplitude, // amplitude
  183. _GFrequency, // frequency
  184. _GSteepness, // steepness
  185. _GSpeed, // speed
  186. _GDirectionAB, // direction # 1, 2
  187. _GDirectionCD // direction # 3, 4
  188. );
  189. v.vertex.xyz += offsets;
  190. half2 tileableUv = worldSpaceVertex.xz;
  191. o.bumpCoords.xyzw = (tileableUv.xyxy + _Time.xxxx * _BumpDirection.xyzw) * _BumpTiling.xyzw;
  192. o.viewInterpolator.xyz = worldSpaceVertex - _WorldSpaceCameraPos;
  193. o.pos = UnityObjectToClipPos(v.vertex);
  194. o.screenPos = ComputeScreenPos(o.pos);
  195. o.normalInterpolator.xyz = nrml;
  196. o.normalInterpolator.w = 1;//GetDistanceFadeout(o.screenPos.w, DISTANCE_SCALE);
  197. UNITY_TRANSFER_FOG(o,o.pos);
  198. return o;
  199. }
  200. half4 frag300( v2f_noGrab i ) : SV_Target
  201. {
  202. half3 worldNormal = PerPixelNormal(_BumpMap, i.bumpCoords, VERTEX_WORLD_NORMAL, PER_PIXEL_DISPLACE);
  203. half3 viewVector = normalize(i.viewInterpolator.xyz);
  204. half4 distortOffset = half4(worldNormal.xz * REALTIME_DISTORTION * 10.0, 0, 0);
  205. half4 screenWithOffset = i.screenPos + distortOffset;
  206. #ifdef WATER_REFLECTIVE
  207. half4 rtReflections = tex2Dproj(_ReflectionTex, UNITY_PROJ_COORD(screenWithOffset));
  208. #endif
  209. half3 reflectVector = normalize(reflect(viewVector, worldNormal));
  210. half3 h = normalize (_WorldLightDir.xyz + viewVector.xyz);
  211. float nh = max (0, dot (worldNormal, -h));
  212. float spec = max(0.0,pow (nh, _Shininess));
  213. half4 edgeBlendFactors = half4(1.0, 0.0, 0.0, 0.0);
  214. #ifdef WATER_EDGEBLEND_ON
  215. half depth = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.screenPos));
  216. depth = LinearEyeDepth(depth);
  217. edgeBlendFactors = saturate(_InvFadeParemeter * (depth-i.screenPos.z));
  218. #endif
  219. worldNormal.xz *= _FresnelScale;
  220. half refl2Refr = Fresnel(viewVector, worldNormal, FRESNEL_BIAS, FRESNEL_POWER);
  221. half4 baseColor = _BaseColor;
  222. #ifdef WATER_REFLECTIVE
  223. baseColor = lerp (baseColor, lerp (rtReflections,_ReflectionColor,_ReflectionColor.a), saturate(refl2Refr * 1.0));
  224. #else
  225. baseColor = _ReflectionColor;//lerp (baseColor, _ReflectionColor, saturate(refl2Refr * 2.0));
  226. #endif
  227. baseColor = baseColor + spec * _SpecularColor;
  228. baseColor.a = edgeBlendFactors.x * saturate(0.5 + refl2Refr * 1.0);
  229. UNITY_APPLY_FOG(i.fogCoord, baseColor);
  230. return baseColor;
  231. }
  232. //
  233. // LQ VERSION
  234. //
  235. v2f_simple vert200(appdata_full v)
  236. {
  237. v2f_simple o;
  238. half3 worldSpaceVertex = mul(unity_ObjectToWorld, v.vertex).xyz;
  239. half2 tileableUv = worldSpaceVertex.xz;
  240. o.bumpCoords.xyzw = (tileableUv.xyxy + _Time.xxxx * _BumpDirection.xyzw) * _BumpTiling.xyzw;
  241. o.viewInterpolator.xyz = worldSpaceVertex-_WorldSpaceCameraPos;
  242. o.pos = UnityObjectToClipPos(v.vertex);
  243. UNITY_TRANSFER_FOG(o,o.pos);
  244. return o;
  245. }
  246. half4 frag200( v2f_simple i ) : SV_Target
  247. {
  248. half3 worldNormal = PerPixelNormal(_BumpMap, i.bumpCoords, half3(0,1,0), PER_PIXEL_DISPLACE);
  249. half3 viewVector = normalize(i.viewInterpolator.xyz);
  250. half3 reflectVector = normalize(reflect(viewVector, worldNormal));
  251. half3 h = normalize ((_WorldLightDir.xyz) + viewVector.xyz);
  252. float nh = max (0, dot (worldNormal, -h));
  253. float spec = max(0.0,pow (nh, _Shininess));
  254. worldNormal.xz *= _FresnelScale;
  255. half refl2Refr = Fresnel(viewVector, worldNormal, FRESNEL_BIAS, FRESNEL_POWER);
  256. half4 baseColor = _BaseColor;
  257. baseColor = lerp(baseColor, _ReflectionColor, saturate(refl2Refr * 2.0));
  258. baseColor.a = saturate(2.0 * refl2Refr + 0.5);
  259. baseColor.rgb += spec * _SpecularColor.rgb;
  260. UNITY_APPLY_FOG(i.fogCoord, baseColor);
  261. return baseColor;
  262. }
  263. ENDCG
  264. Subshader
  265. {
  266. Tags {"RenderType"="Transparent" "Queue"="Transparent"}
  267. Lod 500
  268. ColorMask RGB
  269. GrabPass { "_RefractionTex" }
  270. Pass {
  271. Blend SrcAlpha OneMinusSrcAlpha
  272. ZTest LEqual
  273. ZWrite Off
  274. Cull Off
  275. CGPROGRAM
  276. #pragma target 3.0
  277. #pragma vertex vert
  278. #pragma fragment frag
  279. #pragma multi_compile_fog
  280. #pragma multi_compile WATER_VERTEX_DISPLACEMENT_ON WATER_VERTEX_DISPLACEMENT_OFF
  281. #pragma multi_compile WATER_EDGEBLEND_ON WATER_EDGEBLEND_OFF
  282. #pragma multi_compile WATER_REFLECTIVE WATER_SIMPLE
  283. ENDCG
  284. }
  285. }
  286. Subshader
  287. {
  288. Tags {"RenderType"="Transparent" "Queue"="Transparent"}
  289. Lod 300
  290. ColorMask RGB
  291. Pass {
  292. Blend SrcAlpha OneMinusSrcAlpha
  293. ZTest LEqual
  294. ZWrite Off
  295. Cull Off
  296. CGPROGRAM
  297. #pragma target 3.0
  298. #pragma vertex vert300
  299. #pragma fragment frag300
  300. #pragma multi_compile_fog
  301. #pragma multi_compile WATER_VERTEX_DISPLACEMENT_ON WATER_VERTEX_DISPLACEMENT_OFF
  302. #pragma multi_compile WATER_EDGEBLEND_ON WATER_EDGEBLEND_OFF
  303. #pragma multi_compile WATER_REFLECTIVE WATER_SIMPLE
  304. ENDCG
  305. }
  306. }
  307. Subshader
  308. {
  309. Tags {"RenderType"="Transparent" "Queue"="Transparent"}
  310. Lod 200
  311. ColorMask RGB
  312. Pass {
  313. Blend SrcAlpha OneMinusSrcAlpha
  314. ZTest LEqual
  315. ZWrite Off
  316. Cull Off
  317. CGPROGRAM
  318. #pragma vertex vert200
  319. #pragma fragment frag200
  320. #pragma multi_compile_fog
  321. ENDCG
  322. }
  323. }
  324. Fallback "Transparent/Diffuse"
  325. }