FXWater4Simple.shader 12 KB

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