Advertisement
SebastianLague

Water pro with edge blend

Dec 16th, 2016
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
  2.  
  3. Shader "FX/Water" {
  4. Properties {
  5. blendFactor ("Edge blend", Range(0.01,5)) = .15
  6. _WaveScale ("Wave scale", Range (0.02,0.15)) = 0.063
  7. _ReflDistort ("Reflection distort", Range (0,1.5)) = 0.44
  8. _RefrDistort ("Refraction distort", Range (0,1.5)) = 0.40
  9. _RefrColor ("Refraction color", COLOR) = ( .34, .85, .92, 1)
  10. [NoScaleOffset] _Fresnel ("Fresnel (A) ", 2D) = "gray" {}
  11. [NoScaleOffset] _BumpMap ("Normalmap ", 2D) = "bump" {}
  12. WaveSpeed ("Wave speed (map1 x,y; map2 x,y)", Vector) = (19,9,-16,-7)
  13. [NoScaleOffset] _ReflectiveColor ("Reflective color (RGB) fresnel (A) ", 2D) = "" {}
  14. _HorizonColor ("Simple water horizon color", COLOR) = ( .172, .463, .435, 1)
  15. [HideInInspector] _ReflectionTex ("Internal Reflection", 2D) = "" {}
  16. [HideInInspector] _RefractionTex ("Internal Refraction", 2D) = "" {}
  17. }
  18.  
  19.  
  20. // -----------------------------------------------------------
  21. // Fragment program cards
  22.  
  23.  
  24. Subshader {
  25. Tags { "WaterMode"="Refractive" "RenderType"="Transparent" "Queue"="Transparent"}
  26. Pass {
  27.  
  28. Blend SrcAlpha OneMinusSrcAlpha
  29. ZTest LEqual
  30. ZWrite Off
  31. Cull Off
  32. CGPROGRAM
  33. #pragma vertex vert
  34. #pragma fragment frag
  35. #pragma multi_compile_fog
  36. #pragma multi_compile WATER_REFRACTIVE WATER_REFLECTIVE WATER_SIMPLE
  37.  
  38. #if defined (WATER_REFLECTIVE) || defined (WATER_REFRACTIVE)
  39. #define HAS_REFLECTION 1
  40. #endif
  41. #if defined (WATER_REFRACTIVE)
  42. #define HAS_REFRACTION 1
  43. #endif
  44.  
  45.  
  46. #include "UnityCG.cginc"
  47.  
  48. uniform float4 _WaveScale4;
  49. uniform float4 _WaveOffset;
  50.  
  51. #if HAS_REFLECTION
  52. uniform float _ReflDistort;
  53. #endif
  54. #if HAS_REFRACTION
  55. uniform float _RefrDistort;
  56. #endif
  57.  
  58. sampler2D_float _CameraDepthTexture;
  59. float blendFactor;
  60.  
  61. struct appdata {
  62. float4 vertex : POSITION;
  63. float3 normal : NORMAL;
  64. };
  65.  
  66. struct v2f {
  67. float4 pos : SV_POSITION;
  68. float4 screenPos : TEXCOORD4;
  69. #if defined(HAS_REFLECTION) || defined(HAS_REFRACTION)
  70. float4 ref : TEXCOORD0;
  71. float2 bumpuv0 : TEXCOORD1;
  72. float2 bumpuv1 : TEXCOORD2;
  73. float3 viewDir : TEXCOORD3;
  74. #else
  75. float2 bumpuv0 : TEXCOORD0;
  76. float2 bumpuv1 : TEXCOORD1;
  77. float3 viewDir : TEXCOORD2;
  78. #endif
  79.  
  80. UNITY_FOG_COORDS(4)
  81. };
  82.  
  83. v2f vert(appdata v)
  84. {
  85. v2f o;
  86. o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
  87. o.screenPos = ComputeNonStereoScreenPos(o.pos);
  88.  
  89. // scroll bump waves
  90. float4 temp;
  91. float4 wpos = mul (unity_ObjectToWorld, v.vertex);
  92. temp.xyzw = wpos.xzxz * _WaveScale4 + _WaveOffset;
  93. o.bumpuv0 = temp.xy;
  94. o.bumpuv1 = temp.wz;
  95.  
  96. // object space view direction (will normalize per pixel)
  97. o.viewDir.xzy = WorldSpaceViewDir(v.vertex);
  98.  
  99. #if defined(HAS_REFLECTION) || defined(HAS_REFRACTION)
  100. o.ref = ComputeNonStereoScreenPos(o.pos);
  101. #endif
  102.  
  103. UNITY_TRANSFER_FOG(o,o.pos);
  104. return o;
  105. }
  106.  
  107. #if defined (WATER_REFLECTIVE) || defined (WATER_REFRACTIVE)
  108. sampler2D _ReflectionTex;
  109. #endif
  110. #if defined (WATER_REFLECTIVE) || defined (WATER_SIMPLE)
  111. sampler2D _ReflectiveColor;
  112. #endif
  113. #if defined (WATER_REFRACTIVE)
  114. sampler2D _Fresnel;
  115. sampler2D _RefractionTex;
  116. uniform float4 _RefrColor;
  117. #endif
  118. #if defined (WATER_SIMPLE)
  119. uniform float4 _HorizonColor;
  120. #endif
  121. sampler2D _BumpMap;
  122.  
  123. half4 frag( v2f i ) : SV_Target
  124. {
  125. i.viewDir = normalize(i.viewDir);
  126.  
  127.  
  128. half depth = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.screenPos));
  129. depth = LinearEyeDepth(depth);
  130. half fadeAlpha = saturate(blendFactor * (depth-i.screenPos.w)).x;
  131. // combine two scrolling bumpmaps into one
  132. half3 bump1 = UnpackNormal(tex2D( _BumpMap, i.bumpuv0 )).rgb;
  133. half3 bump2 = UnpackNormal(tex2D( _BumpMap, i.bumpuv1 )).rgb;
  134. half3 bump = (bump1 + bump2) * 0.5;
  135.  
  136. // fresnel factor
  137. half fresnelFac = dot( i.viewDir, bump );
  138.  
  139. // perturb reflection/refraction UVs by bumpmap, and lookup colors
  140.  
  141. #if HAS_REFLECTION
  142. float4 uv1 = i.ref; uv1.xy += bump * _ReflDistort;
  143. half4 refl = tex2Dproj( _ReflectionTex, UNITY_PROJ_COORD(uv1) );
  144. #endif
  145. #if HAS_REFRACTION
  146. float4 uv2 = i.ref; uv2.xy -= bump * _RefrDistort;
  147. half4 refr = tex2Dproj( _RefractionTex, UNITY_PROJ_COORD(uv2) ) * _RefrColor;
  148. #endif
  149.  
  150. // final color is between refracted and reflected based on fresnel
  151. half4 color;
  152.  
  153. #if defined(WATER_REFRACTIVE)
  154. half fresnel = UNITY_SAMPLE_1CHANNEL( _Fresnel, float2(fresnelFac,fresnelFac) );
  155. color = lerp( refr, refl, fresnel);
  156. color.a = fadeAlpha;
  157. #endif
  158.  
  159. #if defined(WATER_REFLECTIVE)
  160. half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) );
  161. color.rgb = lerp( water.rgb, refl.rgb, water.a );
  162. color.a = refl.a * water.a * fadeAlpha;
  163. #endif
  164.  
  165. #if defined(WATER_SIMPLE)
  166. half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) );
  167. color.rgb = lerp( water.rgb, _HorizonColor.rgb, water.a );
  168. color.a = fadeAlpha;
  169. #endif
  170.  
  171. UNITY_APPLY_FOG(i.fogCoord, color);
  172. return color;
  173. }
  174. ENDCG
  175.  
  176. }
  177. }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement