FXWaterPro.shader 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
  2. Shader "FX/Water" {
  3. Properties {
  4. _WaveScale ("Wave scale", Range (0.02,0.15)) = 0.063
  5. _ReflDistort ("Reflection distort", Range (0,1.5)) = 0.44
  6. _RefrDistort ("Refraction distort", Range (0,1.5)) = 0.40
  7. _RefrColor ("Refraction color", COLOR) = ( .34, .85, .92, 1)
  8. [NoScaleOffset] _Fresnel ("Fresnel (A) ", 2D) = "gray" {}
  9. [NoScaleOffset] _BumpMap ("Normalmap ", 2D) = "bump" {}
  10. WaveSpeed ("Wave speed (map1 x,y; map2 x,y)", Vector) = (19,9,-16,-7)
  11. [NoScaleOffset] _ReflectiveColor ("Reflective color (RGB) fresnel (A) ", 2D) = "" {}
  12. _HorizonColor ("Simple water horizon color", COLOR) = ( .172, .463, .435, 1)
  13. [HideInInspector] _ReflectionTex ("Internal Reflection", 2D) = "" {}
  14. [HideInInspector] _RefractionTex ("Internal Refraction", 2D) = "" {}
  15. }
  16. // -----------------------------------------------------------
  17. // Fragment program cards
  18. Subshader {
  19. Tags { "WaterMode"="Refractive" "RenderType"="Opaque" }
  20. Pass {
  21. CGPROGRAM
  22. #pragma vertex vert
  23. #pragma fragment frag
  24. #pragma multi_compile_fog
  25. #pragma multi_compile WATER_REFRACTIVE WATER_REFLECTIVE WATER_SIMPLE
  26. #if defined (WATER_REFLECTIVE) || defined (WATER_REFRACTIVE)
  27. #define HAS_REFLECTION 1
  28. #endif
  29. #if defined (WATER_REFRACTIVE)
  30. #define HAS_REFRACTION 1
  31. #endif
  32. #include "UnityCG.cginc"
  33. uniform float4 _WaveScale4;
  34. uniform float4 _WaveOffset;
  35. #if HAS_REFLECTION
  36. uniform float _ReflDistort;
  37. #endif
  38. #if HAS_REFRACTION
  39. uniform float _RefrDistort;
  40. #endif
  41. struct appdata {
  42. float4 vertex : POSITION;
  43. float3 normal : NORMAL;
  44. };
  45. struct v2f {
  46. float4 pos : SV_POSITION;
  47. #if defined(HAS_REFLECTION) || defined(HAS_REFRACTION)
  48. float4 ref : TEXCOORD0;
  49. float2 bumpuv0 : TEXCOORD1;
  50. float2 bumpuv1 : TEXCOORD2;
  51. float3 viewDir : TEXCOORD3;
  52. #else
  53. float2 bumpuv0 : TEXCOORD0;
  54. float2 bumpuv1 : TEXCOORD1;
  55. float3 viewDir : TEXCOORD2;
  56. #endif
  57. UNITY_FOG_COORDS(4)
  58. };
  59. v2f vert(appdata v)
  60. {
  61. v2f o;
  62. o.pos = UnityObjectToClipPos(v.vertex);
  63. // scroll bump waves
  64. float4 temp;
  65. float4 wpos = mul (unity_ObjectToWorld, v.vertex);
  66. temp.xyzw = wpos.xzxz * _WaveScale4 + _WaveOffset;
  67. o.bumpuv0 = temp.xy;
  68. o.bumpuv1 = temp.wz;
  69. // object space view direction (will normalize per pixel)
  70. o.viewDir.xzy = WorldSpaceViewDir(v.vertex);
  71. #if defined(HAS_REFLECTION) || defined(HAS_REFRACTION)
  72. o.ref = ComputeNonStereoScreenPos(o.pos);
  73. #endif
  74. UNITY_TRANSFER_FOG(o,o.pos);
  75. return o;
  76. }
  77. #if defined (WATER_REFLECTIVE) || defined (WATER_REFRACTIVE)
  78. sampler2D _ReflectionTex;
  79. #endif
  80. #if defined (WATER_REFLECTIVE) || defined (WATER_SIMPLE)
  81. sampler2D _ReflectiveColor;
  82. #endif
  83. #if defined (WATER_REFRACTIVE)
  84. sampler2D _Fresnel;
  85. sampler2D _RefractionTex;
  86. uniform float4 _RefrColor;
  87. #endif
  88. #if defined (WATER_SIMPLE)
  89. uniform float4 _HorizonColor;
  90. #endif
  91. sampler2D _BumpMap;
  92. half4 frag( v2f i ) : SV_Target
  93. {
  94. i.viewDir = normalize(i.viewDir);
  95. // combine two scrolling bumpmaps into one
  96. half3 bump1 = UnpackNormal(tex2D( _BumpMap, i.bumpuv0 )).rgb;
  97. half3 bump2 = UnpackNormal(tex2D( _BumpMap, i.bumpuv1 )).rgb;
  98. half3 bump = (bump1 + bump2) * 0.5;
  99. // fresnel factor
  100. half fresnelFac = dot( i.viewDir, bump );
  101. // perturb reflection/refraction UVs by bumpmap, and lookup colors
  102. #if HAS_REFLECTION
  103. float4 uv1 = i.ref; uv1.xy += bump * _ReflDistort;
  104. half4 refl = tex2Dproj( _ReflectionTex, UNITY_PROJ_COORD(uv1) );
  105. #endif
  106. #if HAS_REFRACTION
  107. float4 uv2 = i.ref; uv2.xy -= bump * _RefrDistort;
  108. half4 refr = tex2Dproj( _RefractionTex, UNITY_PROJ_COORD(uv2) ) * _RefrColor;
  109. #endif
  110. // final color is between refracted and reflected based on fresnel
  111. half4 color;
  112. #if defined(WATER_REFRACTIVE)
  113. half fresnel = UNITY_SAMPLE_1CHANNEL( _Fresnel, float2(fresnelFac,fresnelFac) );
  114. color = lerp( refr, refl, fresnel );
  115. #endif
  116. #if defined(WATER_REFLECTIVE)
  117. half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) );
  118. color.rgb = lerp( water.rgb, refl.rgb, water.a );
  119. color.a = refl.a * water.a;
  120. #endif
  121. #if defined(WATER_SIMPLE)
  122. half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) );
  123. color.rgb = lerp( water.rgb, _HorizonColor.rgb, water.a );
  124. color.a = _HorizonColor.a;
  125. #endif
  126. UNITY_APPLY_FOG(i.fogCoord, color);
  127. return color;
  128. }
  129. ENDCG
  130. }
  131. }
  132. }