Guest User

Untitled

a guest
Feb 4th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
  2. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  3.  
  4. Shader "FX/Water" {
  5. Properties {
  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. [NoScaleOffset] _Foam ("Foam texture", 2D) = "white" {}
  13. [NoScaleOffset] _FoamGradient ("Foam gradient ", 2D) = "white" {}
  14. _FoamStrength ("Foam strength", Range (0, 10.0)) = 1.0
  15. WaveSpeed ("Wave speed (map1 x,y; map2 x,y)", Vector) = (19,9,-16,-7)
  16. [NoScaleOffset] _ReflectiveColor ("Reflective color (RGB) fresnel (A) ", 2D) = "" {}
  17. _HorizonColor ("Simple water horizon color", COLOR) = ( .172, .463, .435, 1)
  18. [HideInInspector] _ReflectionTex ("Internal Reflection", 2D) = "" {}
  19. [HideInInspector] _RefractionTex ("Internal Refraction", 2D) = "" {}
  20. }
  21.  
  22.  
  23. // -----------------------------------------------------------
  24. // Fragment program cards
  25.  
  26.  
  27. Subshader {
  28. Tags { "WaterMode"="Refractive" "RenderType"="Opaque" }
  29. Pass {
  30. CGPROGRAM
  31. #pragma vertex vert
  32. #pragma fragment frag
  33. #pragma multi_compile_fog
  34. #pragma multi_compile WATER_REFRACTIVE WATER_REFLECTIVE WATER_SIMPLE
  35.  
  36. #if defined (WATER_REFLECTIVE) || defined (WATER_REFRACTIVE)
  37. #define HAS_REFLECTION 1
  38. #endif
  39. #if defined (WATER_REFRACTIVE)
  40. #define HAS_REFRACTION 1
  41. #endif
  42.  
  43. #if defined(HAS_REFLECTION) || defined(HAS_REFRACTION)
  44. #define HAS_FOAM
  45. #endif
  46.  
  47. #include "UnityCG.cginc"
  48.  
  49. uniform float4 _WaveScale4;
  50. uniform float4 _WaveOffset;
  51.  
  52. #if defined(HAS_FOAM)
  53. uniform float _FoamStrength;
  54. uniform sampler2D _CameraDepthTexture; //Depth Texture
  55. #endif
  56.  
  57. #if HAS_REFLECTION
  58. uniform float _ReflDistort;
  59. #endif
  60. #if HAS_REFRACTION
  61. uniform float _RefrDistort;
  62. #endif
  63.  
  64. struct appdata {
  65. float4 vertex : POSITION;
  66. float3 normal : NORMAL;
  67. };
  68.  
  69. struct v2f {
  70. float4 pos : SV_POSITION;
  71. #if defined(HAS_REFLECTION) || defined(HAS_REFRACTION)
  72. float4 ref : TEXCOORD0;
  73. float2 bumpuv0 : TEXCOORD1;
  74. float2 bumpuv1 : TEXCOORD2;
  75. float3 viewDir : TEXCOORD3;
  76. #if defined(HAS_FOAM)
  77. float2 foamuv : TEXCOORD4;
  78. #endif
  79. #else
  80. float2 bumpuv0 : TEXCOORD0;
  81. float2 bumpuv1 : TEXCOORD1;
  82. float3 viewDir : TEXCOORD2;
  83. #endif
  84.  
  85. UNITY_FOG_COORDS(4)
  86. };
  87.  
  88. v2f vert(appdata v)
  89. {
  90. v2f o;
  91. o.pos = UnityObjectToClipPos (v.vertex);
  92.  
  93.  
  94. // scroll bump waves
  95. float4 temp;
  96. float4 wpos = mul (unity_ObjectToWorld, v.vertex);
  97. temp.xyzw = wpos.xzxz * _WaveScale4 + _WaveOffset;
  98. o.bumpuv0 = temp.xy;
  99. o.bumpuv1 = temp.wz;
  100.  
  101. #if defined(HAS_FOAM)
  102. o.foamuv = 7.0f * wpos.xz + 0.05 * float2(_SinTime.w, _SinTime.w);
  103. #endif
  104.  
  105. // object space view direction (will normalize per pixel)
  106. o.viewDir.xzy = WorldSpaceViewDir(v.vertex);
  107.  
  108. #if defined(HAS_REFLECTION) || defined(HAS_REFRACTION)
  109. o.ref = ComputeScreenPos(o.pos);
  110. #endif
  111.  
  112. UNITY_TRANSFER_FOG(o,o.pos);
  113. return o;
  114. }
  115.  
  116. #if defined (WATER_REFLECTIVE) || defined (WATER_REFRACTIVE)
  117. sampler2D _ReflectionTex;
  118. #endif
  119. #if defined (WATER_REFLECTIVE) || defined (WATER_SIMPLE)
  120. sampler2D _ReflectiveColor;
  121. #endif
  122. #if defined (WATER_REFRACTIVE)
  123. sampler2D _Fresnel;
  124. sampler2D _RefractionTex;
  125. uniform float4 _RefrColor;
  126. #endif
  127. #if defined (WATER_SIMPLE)
  128. uniform float4 _HorizonColor;
  129. #endif
  130. sampler2D _BumpMap;
  131.  
  132. #if defined(HAS_FOAM)
  133. sampler2D _Foam;
  134. sampler2D _FoamGradient;
  135. #endif
  136.  
  137. half4 frag( v2f i ) : SV_Target
  138. {
  139. i.viewDir = normalize(i.viewDir);
  140.  
  141. // combine two scrolling bumpmaps into one
  142. half3 bump1 = UnpackNormal(tex2D( _BumpMap, i.bumpuv0 )).rgb;
  143. half3 bump2 = UnpackNormal(tex2D( _BumpMap, i.bumpuv1 )).rgb;
  144. half3 bump = (bump1 + bump2) * 0.5;
  145.  
  146. // fresnel factor
  147. half fresnelFac = dot( i.viewDir, bump );
  148.  
  149. // perturb reflection/refraction UVs by bumpmap, and lookup colors
  150. #if HAS_REFLECTION
  151. float4 uv1 = i.ref; uv1.xy += bump * _ReflDistort;
  152. half4 refl = tex2Dproj( _ReflectionTex, UNITY_PROJ_COORD(uv1) );
  153. #endif
  154. #if HAS_REFRACTION
  155. float4 uv2 = i.ref; uv2.xy -= bump * _RefrDistort;
  156. half4 refr = tex2Dproj( _RefractionTex, UNITY_PROJ_COORD(uv2) ) * _RefrColor;
  157. #endif
  158.  
  159. // final color is between refracted and reflected based on fresnel
  160. half4 color;
  161.  
  162. #if defined(WATER_REFRACTIVE)
  163. half fresnel = UNITY_SAMPLE_1CHANNEL( _Fresnel, float2(fresnelFac,fresnelFac) );
  164. color = lerp( refr, refl, fresnel);
  165. #endif
  166.  
  167. #if defined(WATER_REFLECTIVE)
  168. half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) );
  169. color.rgb = lerp( water.rgb, refl.rgb, water.a );
  170. color.a = refl.a * water.a;
  171. #endif
  172.  
  173. #if defined(WATER_SIMPLE)
  174. half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) );
  175. color.rgb = lerp( water.rgb, _HorizonColor.rgb, water.a );
  176. color.a = _HorizonColor.a;
  177. #endif
  178.  
  179. #if defined(HAS_FOAM)
  180. float sceneZ = LinearEyeDepth (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.ref)).r);
  181. float objectZ = i.ref.z;
  182. float intensityFactor = 1 - saturate((sceneZ - objectZ) / _FoamStrength);
  183. half3 foamGradient = 1 - tex2D(_FoamGradient, float2(intensityFactor - _Time.y*0.2, 0) + bump.xy * 0.15);
  184. float2 foamDistortUV = bump.xy * 0.2;
  185. half3 foamColor = tex2D(_Foam, i.foamuv + foamDistortUV).rgb;
  186. color.rgb += foamGradient * intensityFactor * foamColor;
  187. #endif
  188.  
  189. UNITY_APPLY_FOG(i.fogCoord, color);
  190. return color;
  191. }
  192. ENDCG
  193.  
  194. }
  195. }
  196.  
  197. }
Add Comment
Please, Sign In to add comment