Advertisement
Guest User

FX-Water4.shader

a guest
Aug 5th, 2014
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.04 KB | None | 0 0
  1. Shader "FX/Water4" {
  2. Properties {
  3. _ReflectionTex ("Internal reflection", 2D) = "white" {}
  4.  
  5. _MainTex ("Fallback texture", 2D) = "black" {}
  6. _ShoreTex ("Shore & Foam texture ", 2D) = "black" {}
  7. _BumpMap ("Normals ", 2D) = "bump" {}
  8.  
  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.  
  12. _AnimationTiling ("Animation Tiling (Displacement)", Vector) = (2.2 ,2.2, -1.1, -1.1)
  13. _AnimationDirection ("Animation Direction (displacement)", Vector) = (1.0 ,1.0, 1.0, 1.0)
  14.  
  15. _BumpTiling ("Bump Tiling", Vector) = (1.0 ,1.0, -2.0, 3.0)
  16. _BumpDirection ("Bump Direction & Speed", Vector) = (1.0 ,1.0, -1.0, 1.0)
  17.  
  18. _FresnelScale ("FresnelScale", Range (0.15, 4.0)) = 0.75
  19.  
  20. _BaseColor ("Base color", COLOR) = ( .54, .95, .99, 0.5)
  21. _ReflectionColor ("Reflection color", COLOR) = ( .54, .95, .99, 0.5)
  22. _SpecularColor ("Specular color", COLOR) = ( .72, .72, .72, 1)
  23.  
  24. _WorldLightDir ("Specular light direction", Vector) = (0.0, 0.1, -0.5, 0.0)
  25. _Shininess ("Shininess", Range (2.0, 500.0)) = 200.0
  26.  
  27. _Foam ("Foam (intensity, cutoff)", Vector) = (0.1, 0.375, 0.0, 0.0)
  28.  
  29. _GerstnerIntensity("Per vertex displacement", Float) = 1.0
  30. _GAmplitude ("Wave Amplitude", Vector) = (0.3 ,0.35, 0.25, 0.25)
  31. _GFrequency ("Wave Frequency", Vector) = (1.3, 1.35, 1.25, 1.25)
  32. _GSteepness ("Wave Steepness", Vector) = (1.0, 1.0, 1.0, 1.0)
  33. _GSpeed ("Wave Speed", Vector) = (1.2, 1.375, 1.1, 1.5)
  34. _GDirectionAB ("Wave Direction", Vector) = (0.3 ,0.85, 0.85, 0.25)
  35. _GDirectionCD ("Wave Direction", Vector) = (0.1 ,0.9, 0.5, 0.5)
  36. }
  37.  
  38.  
  39. CGINCLUDE
  40.  
  41. #include "UnityCG.cginc"
  42. #include "WaterInclude.cginc"
  43.  
  44. struct appdata
  45. {
  46. float4 vertex : POSITION;
  47. float3 normal : NORMAL;
  48. };
  49.  
  50. // interpolator structs
  51.  
  52. struct v2f
  53. {
  54. float4 pos : SV_POSITION;
  55. float4 normalInterpolator : TEXCOORD0;
  56. float4 viewInterpolator : TEXCOORD1;
  57. float4 bumpCoords : TEXCOORD2;
  58. float4 screenPos : TEXCOORD3;
  59. float4 grabPassPos : TEXCOORD4;
  60. };
  61.  
  62. struct v2f_noGrab
  63. {
  64. float4 pos : SV_POSITION;
  65. float4 normalInterpolator : TEXCOORD0;
  66. float3 viewInterpolator : TEXCOORD1;
  67. float4 bumpCoords : TEXCOORD2;
  68. float4 screenPos : TEXCOORD3;
  69. };
  70.  
  71. struct v2f_simple
  72. {
  73. float4 pos : SV_POSITION;
  74. float4 viewInterpolator : TEXCOORD0;
  75. float4 bumpCoords : TEXCOORD1;
  76. };
  77.  
  78. // textures
  79. sampler2D _BumpMap;
  80. sampler2D _ReflectionTex;
  81. sampler2D _RefractionTex;
  82. sampler2D _ShoreTex;
  83. sampler2D_float _CameraDepthTexture;
  84.  
  85. // colors in use
  86. uniform float4 _RefrColorDepth;
  87. uniform float4 _SpecularColor;
  88. uniform float4 _BaseColor;
  89. uniform float4 _ReflectionColor;
  90.  
  91. // edge & shore fading
  92. uniform float4 _InvFadeParemeter;
  93.  
  94. // specularity
  95. uniform float _Shininess;
  96. uniform float4 _WorldLightDir;
  97.  
  98. // fresnel, vertex & bump displacements & strength
  99. uniform float4 _DistortParams;
  100. uniform float _FresnelScale;
  101. uniform float4 _BumpTiling;
  102. uniform float4 _BumpDirection;
  103.  
  104. uniform float4 _GAmplitude;
  105. uniform float4 _GFrequency;
  106. uniform float4 _GSteepness;
  107. uniform float4 _GSpeed;
  108. uniform float4 _GDirectionAB;
  109. uniform float4 _GDirectionCD;
  110.  
  111. // foam
  112. uniform float4 _Foam;
  113.  
  114. // shortcuts
  115. #define PER_PIXEL_DISPLACE _DistortParams.x
  116. #define REALTIME_DISTORTION _DistortParams.y
  117. #define FRESNEL_POWER _DistortParams.z
  118. #define VERTEX_WORLD_NORMAL i.normalInterpolator.xyz
  119. #define FRESNEL_BIAS _DistortParams.w
  120. #define NORMAL_DISPLACEMENT_PER_VERTEX _InvFadeParemeter.z
  121.  
  122. //
  123. // HQ VERSION
  124. //
  125.  
  126. v2f vert(appdata_full v)
  127. {
  128. v2f o;
  129.  
  130. half3 worldSpaceVertex = mul(_Object2World,(v.vertex)).xyz;
  131. half3 vtxForAni = (worldSpaceVertex).xzz * unity_Scale.w;
  132.  
  133. half3 nrml;
  134. half3 offsets;
  135. Gerstner (
  136. offsets, nrml, v.vertex.xyz, vtxForAni, // offsets, nrml will be written
  137. _GAmplitude, // amplitude
  138. _GFrequency, // frequency
  139. _GSteepness, // steepness
  140. _GSpeed, // speed
  141. _GDirectionAB, // direction # 1, 2
  142. _GDirectionCD // direction # 3, 4
  143. );
  144.  
  145. v.vertex.xyz += offsets;
  146.  
  147. // one can also use worldSpaceVertex.xz here (speed!), albeit it'll end up a little skewed
  148. half2 tileableUv = mul(_Object2World,(v.vertex)).xz;
  149.  
  150. o.bumpCoords.xyzw = (tileableUv.xyxy + _Time.xxxx * _BumpDirection.xyzw) * _BumpTiling.xyzw;
  151.  
  152. o.viewInterpolator.xyz = worldSpaceVertex - _WorldSpaceCameraPos;
  153.  
  154. o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  155.  
  156. ComputeScreenAndGrabPassPos(o.pos, o.screenPos, o.grabPassPos);
  157.  
  158. o.normalInterpolator.xyz = nrml;
  159.  
  160. o.viewInterpolator.w = saturate(offsets.y);
  161. o.normalInterpolator.w = 1;//GetDistanceFadeout(o.screenPos.w, DISTANCE_SCALE);
  162.  
  163. return o;
  164. }
  165.  
  166. half4 frag( v2f i ) : SV_Target
  167. {
  168. half3 worldNormal = PerPixelNormal(_BumpMap, i.bumpCoords, VERTEX_WORLD_NORMAL, PER_PIXEL_DISPLACE);
  169. half3 viewVector = normalize(i.viewInterpolator.xyz);
  170.  
  171. half4 distortOffset = half4(worldNormal.xz * REALTIME_DISTORTION * 10.0, 0, 0);
  172. half4 screenWithOffset = i.screenPos + distortOffset;
  173. half4 grabWithOffset = i.grabPassPos + distortOffset;
  174.  
  175. half4 rtRefractionsNoDistort = tex2Dproj(_RefractionTex, UNITY_PROJ_COORD(i.grabPassPos));
  176. half refrFix = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(grabWithOffset));
  177. half4 rtRefractions = tex2Dproj(_RefractionTex, UNITY_PROJ_COORD(grabWithOffset));
  178.  
  179. #ifdef WATER_REFLECTIVE
  180. half4 rtReflections = tex2Dproj(_ReflectionTex, UNITY_PROJ_COORD(screenWithOffset));
  181. #endif
  182.  
  183. #ifdef WATER_EDGEBLEND_ON
  184. if (LinearEyeDepth(refrFix) < i.screenPos.z)
  185. rtRefractions = rtRefractionsNoDistort;
  186. #endif
  187.  
  188. half3 reflectVector = normalize(reflect(viewVector, worldNormal));
  189. half3 h = normalize ((_WorldLightDir.xyz) + viewVector.xyz);
  190. float nh = max (0, dot (worldNormal, -h));
  191. float spec = max(0.0,pow (nh, _Shininess));
  192.  
  193. half4 edgeBlendFactors = half4(1.0, 0.0, 0.0, 0.0);
  194.  
  195. #ifdef WATER_EDGEBLEND_ON
  196. half depth = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.screenPos));
  197. depth = LinearEyeDepth(depth);
  198. edgeBlendFactors = saturate(_InvFadeParemeter * (depth-i.screenPos.w));
  199. edgeBlendFactors.y = 1.0-edgeBlendFactors.y;
  200. #endif
  201.  
  202. // shading for fresnel term
  203. worldNormal.xz *= _FresnelScale;
  204. half refl2Refr = Fresnel(viewVector, worldNormal, FRESNEL_BIAS, FRESNEL_POWER);
  205.  
  206. // base, depth & reflection colors
  207. half4 baseColor = ExtinctColor (_BaseColor, i.viewInterpolator.w * _InvFadeParemeter.w);
  208. #ifdef WATER_REFLECTIVE
  209. half4 reflectionColor = lerp (rtReflections,_ReflectionColor,_ReflectionColor.a);
  210. #else
  211. half4 reflectionColor = _ReflectionColor;
  212. #endif
  213.  
  214. baseColor = lerp (lerp (rtRefractions, baseColor, baseColor.a), reflectionColor, refl2Refr);
  215. baseColor = baseColor + spec * _SpecularColor;
  216.  
  217. // handle foam
  218. half4 foam = Foam(_ShoreTex, i.bumpCoords * 2.0);
  219. baseColor.rgb += foam.rgb * _Foam.x * (edgeBlendFactors.y + saturate(i.viewInterpolator.w - _Foam.y));
  220.  
  221. baseColor.a = edgeBlendFactors.x;
  222. return baseColor;
  223. }
  224.  
  225. //
  226. // MQ VERSION
  227. //
  228.  
  229. v2f_noGrab vert300(appdata_full v)
  230. {
  231. v2f_noGrab o;
  232.  
  233. half3 worldSpaceVertex = mul(_Object2World,(v.vertex)).xyz;
  234. half3 vtxForAni = (worldSpaceVertex).xzz * unity_Scale.w;
  235.  
  236. half3 nrml;
  237. half3 offsets;
  238. Gerstner (
  239. offsets, nrml, v.vertex.xyz, vtxForAni, // offsets, nrml will be written
  240. _GAmplitude, // amplitude
  241. _GFrequency, // frequency
  242. _GSteepness, // steepness
  243. _GSpeed, // speed
  244. _GDirectionAB, // direction # 1, 2
  245. _GDirectionCD // direction # 3, 4
  246. );
  247.  
  248. v.vertex.xyz += offsets;
  249.  
  250. // one can also use worldSpaceVertex.xz here (speed!), albeit it'll end up a little skewed
  251. half2 tileableUv = mul(_Object2World,v.vertex).xz;
  252. o.bumpCoords.xyzw = (tileableUv.xyxy + _Time.xxxx * _BumpDirection.xyzw) * _BumpTiling.xyzw;
  253.  
  254. o.viewInterpolator.xyz = worldSpaceVertex - _WorldSpaceCameraPos;
  255.  
  256. o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  257.  
  258. o.screenPos = ComputeScreenPos(o.pos);
  259.  
  260. o.normalInterpolator.xyz = nrml;
  261. o.normalInterpolator.w = 1;//GetDistanceFadeout(o.screenPos.w, DISTANCE_SCALE);
  262.  
  263. return o;
  264. }
  265.  
  266. half4 frag300( v2f_noGrab i ) : SV_Target
  267. {
  268. half3 worldNormal = PerPixelNormal(_BumpMap, i.bumpCoords, normalize(VERTEX_WORLD_NORMAL), PER_PIXEL_DISPLACE);
  269.  
  270. half3 viewVector = normalize(i.viewInterpolator.xyz);
  271.  
  272. half4 distortOffset = half4(worldNormal.xz * REALTIME_DISTORTION * 10.0, 0, 0);
  273. half4 screenWithOffset = i.screenPos + distortOffset;
  274.  
  275. #ifdef WATER_REFLECTIVE
  276. half4 rtReflections = tex2Dproj(_ReflectionTex, UNITY_PROJ_COORD(screenWithOffset));
  277. #endif
  278.  
  279. half3 reflectVector = normalize(reflect(viewVector, worldNormal));
  280. half3 h = normalize (_WorldLightDir.xyz + viewVector.xyz);
  281. float nh = max (0, dot (worldNormal, -h));
  282. float spec = max(0.0,pow (nh, _Shininess));
  283.  
  284. half4 edgeBlendFactors = half4(1.0, 0.0, 0.0, 0.0);
  285.  
  286. #ifdef WATER_EDGEBLEND_ON
  287. half depth = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.screenPos));
  288. depth = LinearEyeDepth(depth);
  289. edgeBlendFactors = saturate(_InvFadeParemeter * (depth-i.screenPos.z));
  290. edgeBlendFactors.y = 1.0-edgeBlendFactors.y;
  291. #endif
  292.  
  293. worldNormal.xz *= _FresnelScale;
  294. half refl2Refr = Fresnel(viewVector, worldNormal, FRESNEL_BIAS, FRESNEL_POWER);
  295.  
  296. half4 baseColor = _BaseColor;
  297. #ifdef WATER_REFLECTIVE
  298. baseColor = lerp (baseColor, lerp (rtReflections,_ReflectionColor,_ReflectionColor.a), saturate(refl2Refr * 2.0));
  299. #else
  300. baseColor = lerp (baseColor, _ReflectionColor, saturate(refl2Refr * 2.0));
  301. #endif
  302.  
  303. baseColor = baseColor + spec * _SpecularColor;
  304.  
  305. baseColor.a = edgeBlendFactors.x * saturate(0.5 + refl2Refr * 1.0);
  306. return baseColor;
  307. }
  308.  
  309. //
  310. // LQ VERSION
  311. //
  312.  
  313. v2f_simple vert200(appdata_full v)
  314. {
  315. v2f_simple o;
  316.  
  317. half3 worldSpaceVertex = mul(_Object2World, v.vertex).xyz;
  318. half2 tileableUv = worldSpaceVertex.xz;
  319.  
  320. o.bumpCoords.xyzw = (tileableUv.xyxy + _Time.xxxx * _BumpDirection.xyzw) * _BumpTiling.xyzw;
  321.  
  322. o.viewInterpolator.xyz = worldSpaceVertex-_WorldSpaceCameraPos;
  323.  
  324. o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  325.  
  326. o.viewInterpolator.w = 1;//GetDistanceFadeout(ComputeScreenPos(o.pos).w, DISTANCE_SCALE);
  327.  
  328. return o;
  329.  
  330. }
  331.  
  332. half4 frag200( v2f_simple i ) : SV_Target
  333. {
  334. half3 worldNormal = PerPixelNormal(_BumpMap, i.bumpCoords, half3(0,1,0), PER_PIXEL_DISPLACE);
  335. half3 viewVector = normalize(i.viewInterpolator.xyz);
  336.  
  337. half3 reflectVector = normalize(reflect(viewVector, worldNormal));
  338. half3 h = normalize ((_WorldLightDir.xyz) + viewVector.xyz);
  339. float nh = max (0, dot (worldNormal, -h));
  340. float spec = max(0.0,pow (nh, _Shininess));
  341.  
  342. worldNormal.xz *= _FresnelScale;
  343. half refl2Refr = Fresnel(viewVector, worldNormal, FRESNEL_BIAS, FRESNEL_POWER);
  344.  
  345. half4 baseColor = _BaseColor;
  346. baseColor = lerp(baseColor, _ReflectionColor, saturate(refl2Refr * 2.0));
  347. baseColor.a = saturate(2.0 * refl2Refr + 0.5);
  348.  
  349. baseColor.rgb += spec * _SpecularColor.rgb;
  350. return baseColor;
  351. }
  352.  
  353. ENDCG
  354.  
  355. Subshader
  356. {
  357. Tags {"RenderType"="Transparent" "Queue"="Transparent"}
  358.  
  359. Lod 500
  360. ColorMask RGB
  361.  
  362. GrabPass { "_RefractionTex" }
  363.  
  364. Pass {
  365. Blend SrcAlpha OneMinusSrcAlpha
  366. ZTest LEqual
  367. ZWrite Off
  368. Cull Off
  369.  
  370. CGPROGRAM
  371.  
  372. #pragma target 3.0
  373.  
  374. #pragma vertex vert
  375. #pragma fragment frag
  376.  
  377. #pragma glsl
  378.  
  379. #pragma fragmentoption ARB_precision_hint_fastest
  380.  
  381. #pragma multi_compile WATER_VERTEX_DISPLACEMENT_ON WATER_VERTEX_DISPLACEMENT_OFF
  382. #pragma multi_compile WATER_EDGEBLEND_ON WATER_EDGEBLEND_OFF
  383. #pragma multi_compile WATER_REFLECTIVE WATER_SIMPLE
  384.  
  385. ENDCG
  386. }
  387. }
  388.  
  389. Subshader
  390. {
  391. Tags {"RenderType"="Transparent" "Queue"="Transparent"}
  392.  
  393. Lod 300
  394. ColorMask RGB
  395.  
  396. Pass {
  397. Blend SrcAlpha OneMinusSrcAlpha
  398. ZTest LEqual
  399. ZWrite Off
  400. Cull Off
  401.  
  402. CGPROGRAM
  403.  
  404. #pragma target 3.0
  405.  
  406. #pragma vertex vert300
  407. #pragma fragment frag300
  408.  
  409. #pragma glsl
  410.  
  411. #pragma fragmentoption ARB_precision_hint_fastest
  412. #pragma multi_compile WATER_VERTEX_DISPLACEMENT_ON WATER_VERTEX_DISPLACEMENT_OFF
  413. #pragma multi_compile WATER_EDGEBLEND_ON WATER_EDGEBLEND_OFF
  414. #pragma multi_compile WATER_REFLECTIVE WATER_SIMPLE
  415.  
  416. ENDCG
  417. }
  418. }
  419.  
  420. Subshader
  421. {
  422. Tags {"RenderType"="Transparent" "Queue"="Transparent"}
  423.  
  424. Lod 200
  425. ColorMask RGB
  426.  
  427. Pass {
  428. Blend SrcAlpha OneMinusSrcAlpha
  429. ZTest LEqual
  430. ZWrite Off
  431. Cull Off
  432.  
  433. CGPROGRAM
  434.  
  435. #pragma vertex vert200
  436. #pragma fragment frag200
  437. #pragma fragmentoption ARB_precision_hint_fastest
  438.  
  439. ENDCG
  440. }
  441. }
  442.  
  443. Fallback "Transparent/Diffuse"
  444. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement