You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
5.0 KiB

  1. Shader "FX/Water" {
  2. Properties {
  3. _WaveScale ("Wave scale", Range (0.02,0.15)) = 0.063
  4. _ReflDistort ("Reflection distort", Range (0,1.5)) = 0.44
  5. _RefrDistort ("Refraction distort", Range (0,1.5)) = 0.40
  6. _RefrColor ("Refraction color", COLOR) = ( .34, .85, .92, 1)
  7. _Fresnel ("Fresnel (A) ", 2D) = "gray" {}
  8. _BumpMap ("Normalmap ", 2D) = "bump" {}
  9. WaveSpeed ("Wave speed (map1 x,y; map2 x,y)", Vector) = (19,9,-16,-7)
  10. _ReflectiveColor ("Reflective color (RGB) fresnel (A) ", 2D) = "" {}
  11. _ReflectiveColorCube ("Reflective color cube (RGB) fresnel (A)", Cube) = "" { TexGen CubeReflect }
  12. _HorizonColor ("Simple water horizon color", COLOR) = ( .172, .463, .435, 1)
  13. _MainTex ("Fallback texture", 2D) = "" {}
  14. _ReflectionTex ("Internal Reflection", 2D) = "" {}
  15. _RefractionTex ("Internal Refraction", 2D) = "" {}
  16. }
  17. // -----------------------------------------------------------
  18. // Fragment program cards
  19. Subshader {
  20. Tags { "WaterMode"="Refractive" "RenderType"="Opaque" }
  21. Pass {
  22. CGPROGRAM
  23. #pragma vertex vert
  24. #pragma fragment frag
  25. #pragma fragmentoption ARB_precision_hint_fastest
  26. #pragma multi_compile WATER_REFRACTIVE WATER_REFLECTIVE WATER_SIMPLE
  27. #if defined (WATER_REFLECTIVE) || defined (WATER_REFRACTIVE)
  28. #define HAS_REFLECTION 1
  29. #endif
  30. #if defined (WATER_REFRACTIVE)
  31. #define HAS_REFRACTION 1
  32. #endif
  33. #include "UnityCG.cginc"
  34. uniform float4 _WaveScale4;
  35. uniform float4 _WaveOffset;
  36. #if HAS_REFLECTION
  37. uniform float _ReflDistort;
  38. #endif
  39. #if HAS_REFRACTION
  40. uniform float _RefrDistort;
  41. #endif
  42. struct appdata {
  43. float4 vertex : POSITION;
  44. float3 normal : NORMAL;
  45. };
  46. struct v2f {
  47. float4 pos : SV_POSITION;
  48. #if defined(HAS_REFLECTION) || defined(HAS_REFRACTION)
  49. float4 ref : TEXCOORD0;
  50. float2 bumpuv0 : TEXCOORD1;
  51. float2 bumpuv1 : TEXCOORD2;
  52. float3 viewDir : TEXCOORD3;
  53. #else
  54. float2 bumpuv0 : TEXCOORD0;
  55. float2 bumpuv1 : TEXCOORD1;
  56. float3 viewDir : TEXCOORD2;
  57. #endif
  58. };
  59. v2f vert(appdata v)
  60. {
  61. v2f o;
  62. o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
  63. // scroll bump waves
  64. float4 temp;
  65. temp.xyzw = v.vertex.xzxz * _WaveScale4 / unity_Scale.w + _WaveOffset;
  66. o.bumpuv0 = temp.xy;
  67. o.bumpuv1 = temp.wz;
  68. // object space view direction (will normalize per pixel)
  69. o.viewDir.xzy = ObjSpaceViewDir(v.vertex);
  70. #if defined(HAS_REFLECTION) || defined(HAS_REFRACTION)
  71. o.ref = ComputeScreenPos(o.pos);
  72. #endif
  73. return o;
  74. }
  75. #if defined (WATER_REFLECTIVE) || defined (WATER_REFRACTIVE)
  76. sampler2D _ReflectionTex;
  77. #endif
  78. #if defined (WATER_REFLECTIVE) || defined (WATER_SIMPLE)
  79. sampler2D _ReflectiveColor;
  80. #endif
  81. #if defined (WATER_REFRACTIVE)
  82. sampler2D _Fresnel;
  83. sampler2D _RefractionTex;
  84. uniform float4 _RefrColor;
  85. #endif
  86. #if defined (WATER_SIMPLE)
  87. uniform float4 _HorizonColor;
  88. #endif
  89. sampler2D _BumpMap;
  90. half4 frag( v2f i ) : SV_Target
  91. {
  92. i.viewDir = normalize(i.viewDir);
  93. // combine two scrolling bumpmaps into one
  94. half3 bump1 = UnpackNormal(tex2D( _BumpMap, i.bumpuv0 )).rgb;
  95. half3 bump2 = UnpackNormal(tex2D( _BumpMap, i.bumpuv1 )).rgb;
  96. half3 bump = (bump1 + bump2) * 0.5;
  97. // fresnel factor
  98. half fresnelFac = dot( i.viewDir, bump );
  99. // perturb reflection/refraction UVs by bumpmap, and lookup colors
  100. #if HAS_REFLECTION
  101. float4 uv1 = i.ref; uv1.xy += bump * _ReflDistort;
  102. half4 refl = tex2Dproj( _ReflectionTex, UNITY_PROJ_COORD(uv1) );
  103. #endif
  104. #if HAS_REFRACTION
  105. float4 uv2 = i.ref; uv2.xy -= bump * _RefrDistort;
  106. half4 refr = tex2Dproj( _RefractionTex, UNITY_PROJ_COORD(uv2) ) * _RefrColor;
  107. #endif
  108. // final color is between refracted and reflected based on fresnel
  109. half4 color;
  110. #if defined(WATER_REFRACTIVE)
  111. half fresnel = UNITY_SAMPLE_1CHANNEL( _Fresnel, float2(fresnelFac,fresnelFac) );
  112. color = lerp( refr, refl, fresnel );
  113. #endif
  114. #if defined(WATER_REFLECTIVE)
  115. half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) );
  116. color.rgb = lerp( water.rgb, refl.rgb, water.a );
  117. color.a = refl.a * water.a;
  118. #endif
  119. #if defined(WATER_SIMPLE)
  120. half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) );
  121. color.rgb = lerp( water.rgb, _HorizonColor.rgb, water.a );
  122. color.a = _HorizonColor.a;
  123. #endif
  124. return color;
  125. }
  126. ENDCG
  127. }
  128. }
  129. // -----------------------------------------------------------
  130. // Old cards
  131. // three texture, cubemaps
  132. Subshader {
  133. Tags { "WaterMode"="Simple" "RenderType"="Opaque" }
  134. Pass {
  135. Color (0.5,0.5,0.5,0.5)
  136. SetTexture [_MainTex] {
  137. Matrix [_WaveMatrix]
  138. combine texture * primary
  139. }
  140. SetTexture [_MainTex] {
  141. Matrix [_WaveMatrix2]
  142. combine texture * primary + previous
  143. }
  144. SetTexture [_ReflectiveColorCube] {
  145. combine texture +- previous, primary
  146. Matrix [_Reflection]
  147. }
  148. }
  149. }
  150. // dual texture, cubemaps
  151. Subshader {
  152. Tags { "WaterMode"="Simple" "RenderType"="Opaque" }
  153. Pass {
  154. Color (0.5,0.5,0.5,0.5)
  155. SetTexture [_MainTex] {
  156. Matrix [_WaveMatrix]
  157. combine texture
  158. }
  159. SetTexture [_ReflectiveColorCube] {
  160. combine texture +- previous, primary
  161. Matrix [_Reflection]
  162. }
  163. }
  164. }
  165. // single texture
  166. Subshader {
  167. Tags { "WaterMode"="Simple" "RenderType"="Opaque" }
  168. Pass {
  169. Color (0.5,0.5,0.5,0)
  170. SetTexture [_MainTex] {
  171. Matrix [_WaveMatrix]
  172. combine texture, primary
  173. }
  174. }
  175. }
  176. }