CalmWater_Helper.cginc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
  2. #include "CalmWater_Variables.cginc"
  3. #ifndef CALMWATER_HELPER_INCLUDED
  4. #define CALMWATER_HELPER_INCLUDED
  5. // =====================================
  6. // Lighting
  7. // =====================================
  8. // NOTE: some intricacy in shader compiler on some GLES2.0 platforms (iOS) needs 'viewDir' & 'h'
  9. // to be mediump instead of lowp, otherwise specular highlight becomes too bright.
  10. // Lighting Terms ===============================================================================
  11. half DiffuseTerm (half3 normalDir,half3 lightDir){
  12. return max (0, dot(normalDir,lightDir));
  13. }
  14. half NdotVTerm(half3 normalDir,half3 viewDir){
  15. return dot(normalDir,viewDir);
  16. }
  17. float3 SpecularColor (half gloss, half3 lightDir,half3 viewDir,half3 normalDir)
  18. {
  19. half reflectiveFactor = max(0.0, dot(-viewDir, reflect(lightDir, normalDir)));
  20. //half diffuseFactor = max(0.0, dot(normalDir, lightDir));
  21. half3 spec = pow(reflectiveFactor, gloss * 128);
  22. return _LightColor0.rgb * _SpecColor.rgb * spec;
  23. }
  24. float FresnelSpecular(float NdotV, float power)
  25. {
  26. float fresnel = pow(max(0.0, 1.0 - NdotV), power);
  27. float result = fresnel * fresnel;
  28. return saturate(result);
  29. }
  30. // Helpers ======================================================================================
  31. float smootherstep(float x) {
  32. x = saturate(x);
  33. return saturate(x * x * x * (x * (6 * x - 15) + 10));
  34. }
  35. inline float4 AnimateBump(float2 uv){
  36. // #if _WORLDSPACE_ON
  37. // uv = -uv;
  38. // #endif
  39. //
  40. float4 coords;
  41. coords.xy = TRANSFORM_TEX(uv,_BumpMap);
  42. coords.zw = TRANSFORM_TEX(uv,_BumpMap) * 0.5;
  43. coords += frac(_Speeds * _Time.x);
  44. return coords;
  45. }
  46. inline float2 AnimateLargeBump(half4 ST, float2 uv, float2 speed)
  47. {
  48. float2 coords;
  49. coords = uv * ST.xy + ST.zw;
  50. coords += frac(speed * _Time.x);
  51. return coords;
  52. }
  53. inline half3 SafeNormalize(half3 inVec)
  54. {
  55. half dp3 = max(0.001f, dot(inVec, inVec));
  56. return inVec * rsqrt(dp3);
  57. }
  58. inline fixed4 SampleFlowMap(
  59. sampler2D tex,
  60. float2 texUV,
  61. sampler2D flowMap,
  62. float2 uv,
  63. float speed,
  64. float intensity)
  65. {
  66. half4 flowVal = (tex2D(flowMap, uv) * 2 - 1) * intensity;
  67. float dif1 = frac(_Time.x * speed + 0.5);
  68. float dif2 = frac(_Time.x * speed);
  69. half lerpVal = abs((0.5 - dif1) / 0.5);
  70. half4 col1 = tex2D(tex, texUV - flowVal.xy * dif1);
  71. half4 col2 = tex2D(tex, texUV - flowVal.xy * dif2);
  72. return lerp(col1, col2, lerpVal);
  73. }
  74. inline float4 OffsetUV(float4 uv, float2 offset)
  75. {
  76. #ifdef UNITY_Z_0_FAR_FROM_CLIPSPACE
  77. uv.xy = offset * UNITY_Z_0_FAR_FROM_CLIPSPACE(uv.z) + uv.xy;
  78. #else
  79. uv.xy = offset * uv.z + uv.xy;
  80. #endif
  81. return uv;
  82. }
  83. inline float4 OffsetDepth(float4 uv, float2 offset)
  84. {
  85. uv.xy = offset * uv.z + uv.xy;
  86. return uv;
  87. }
  88. inline float texDepth (float4 uv)
  89. {
  90. return LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(uv)));
  91. }
  92. inline half3 WorldNormal(half3 t0,half3 t1, half3 t2, half3 bump)
  93. {
  94. return normalize( half3( dot(t0, bump) , dot(t1, bump) , dot(t2, bump) ) );
  95. }
  96. inline float3 ProjectedWorldPos(float3 worldPos, float sceneDepth, float pixelDepth)
  97. {
  98. float3 pos = worldPos - _WorldSpaceCameraPos;
  99. float depthDiff = sceneDepth / pixelDepth;
  100. pos.xyz *= depthDiff;
  101. pos.xyz += _WorldSpaceCameraPos;
  102. return pos;
  103. }
  104. inline float DistanceFade(float depth, float pixelDepth, float start, float end)
  105. {
  106. float dist = (abs(depth - pixelDepth) - end) / (start - end);
  107. return saturate(dist);
  108. }
  109. //==========================================================================================================
  110. // Rim
  111. //==========================================================================================================
  112. inline fixed RimLight (half3 vDir,fixed3 n,fixed rimPower)
  113. {
  114. return pow(1.0 - saturate(dot(SafeNormalize(vDir),n)),rimPower);
  115. }
  116. //==========================================================================================================
  117. // UnpackNormals blend and scale
  118. //==========================================================================================================
  119. half3 UnpackNormalScale(half4 n1, half scale)
  120. {
  121. #if defined(UNITY_NO_DXT5nm)
  122. half3 normal = normalize((n1.xyz * 2 - 1));
  123. #if (SHADER_TARGET >= 30)
  124. normal.xy *= scale;
  125. #endif
  126. return normal;
  127. #else
  128. half3 normal;
  129. normal.xy = (n1.wy * 2 - 1);
  130. #if (SHADER_TARGET >= 30)
  131. normal.xy *= scale;
  132. #endif
  133. normal.z = sqrt(1.0 - saturate(dot(normal.xy, normal.xy)));
  134. return normalize(normal);
  135. #endif
  136. }
  137. // =======================================================
  138. // Displacement
  139. // =======================================================
  140. void Wave (out half3 offs, out half3 nrml, half3 vtx, half4 tileableVtx,half amplitude ,half frequency,half s)
  141. {
  142. float4 v0 = tileableVtx;
  143. float4 v1 = v0 + float4(0.05,0,0,0);
  144. float4 v2 = v0 + float4(0,0,0.05,0);
  145. float speed = s * _Time.y;
  146. amplitude *= 0.01;
  147. v0.y += sin ( speed + (v0.x * frequency )) * amplitude;
  148. v1.y += sin ( speed + (v1.x * frequency )) * amplitude;
  149. v2.y += sin ( speed + (v2.x * frequency )) * amplitude;
  150. v0.y -= cos ( speed + (v0.z * frequency )) * amplitude;
  151. v1.y -= cos ( speed + (v1.z * frequency )) * amplitude;
  152. v2.y -= cos ( speed + (v2.z * frequency )) * amplitude;
  153. v1.y -= (v1.y - v0.y) * (1 - _Smoothing);
  154. v2.y -= (v2.y - v0.y) * (1 - _Smoothing);
  155. float3 vna = cross(v2-v0,v1-v0);
  156. float4 vn = mul(float4x4(unity_WorldToObject), float4(vna,0) );
  157. nrml = normalize (vn).xyz;
  158. offs = mul(float4x4(unity_WorldToObject),v0).xyz;
  159. }
  160. half3 GerstnerNormal (half2 xzVtx, half4 amp, half4 freq, half4 speed, half4 dirAB, half4 dirCD)
  161. {
  162. half3 nrml = half3(0,2.0,0);
  163. half4 AB = freq.xxyy * amp.xxyy * dirAB.xyzw;
  164. half4 CD = freq.zzww * amp.zzww * dirCD.xyzw;
  165. half4 dotABCD = freq.xyzw * half4(dot(dirAB.xy, xzVtx), dot(dirAB.zw, xzVtx), dot(dirCD.xy, xzVtx), dot(dirCD.zw, xzVtx));
  166. half4 TIME = _Time.yyyy * speed;
  167. half4 COS = cos (dotABCD + TIME);
  168. nrml.x -= dot(COS, half4(AB.xz, CD.xz));
  169. nrml.z -= dot(COS, half4(AB.yw, CD.yw));
  170. nrml.xz *= _Smoothing;
  171. nrml = normalize (nrml);
  172. return nrml;
  173. }
  174. half3 GerstnerOffset (half2 xzVtx, half steepness, half4 amp, half4 freq, half4 speed, half4 dirAB, half4 dirCD)
  175. {
  176. half3 offsets;
  177. half4 AB = steepness * amp.xxyy * dirAB.xyzw;
  178. half4 CD = steepness * amp.zzww * dirCD.xyzw;
  179. half4 dotABCD = freq.xyzw * half4(dot(dirAB.xy, xzVtx), dot(dirAB.zw, xzVtx), dot(dirCD.xy, xzVtx), dot(dirCD.zw, xzVtx));
  180. half4 TIME = _Time.yyyy * speed;
  181. half4 COS = cos (dotABCD + TIME);
  182. half4 SIN = sin (dotABCD + TIME);
  183. offsets.x = dot(COS, half4(AB.xz, CD.xz));
  184. offsets.z = dot(COS, half4(AB.yw, CD.yw));
  185. offsets.y = dot(SIN, amp);
  186. return offsets;
  187. }
  188. void Gerstner ( out half3 offs, out half3 nrml,
  189. half3 vtx, half3 tileableVtx,
  190. half4 amplitude, half4 frequency, half4 steepness,
  191. half4 speed, half4 directionAB, half4 directionCD)
  192. {
  193. offs = GerstnerOffset(tileableVtx.xz, steepness, amplitude, frequency, speed, directionAB, directionCD);
  194. nrml = GerstnerNormal(tileableVtx.xz + offs.xz, amplitude, frequency, speed, directionAB, directionCD);
  195. }
  196. // Texture Displacement
  197. float sampleDisplacementTexture(float2 uv)
  198. {
  199. uv *= _DisplacementTex_ST.xy * 0.1;
  200. uv += _DisplacementTex_ST.zw;
  201. float4 uv1 = float4(uv + frac(_DisplacementSpeed.xy * _Time.x), 0, 0);
  202. float4 uv2 = float4(uv * float2(0.5, 0.5) - frac(_DisplacementSpeed.zw * _Time.x * 0.5) , 0, 0);
  203. float wave1 = tex2Dlod(_DisplacementTex, uv1);
  204. float wave2 = tex2Dlod(_DisplacementTex, uv2);
  205. float waveMix = wave1 + wave2;
  206. return waveMix * 2.0 - 1.0;
  207. }
  208. void TextureDisplacement( out half3 offs, out half3 nrml, float4 vtx, float intensity, float vectorLength)
  209. {
  210. float4 v0 = vtx;
  211. float4 v1 = v0 + float4(vectorLength, 0.0, 0.0, 0.0);
  212. float4 v2 = v0 + float4(0.0, 0.0, vectorLength, 0.0);
  213. float2 v0UV = mul(unity_ObjectToWorld, v0).xz;
  214. float2 v1UV = mul(unity_ObjectToWorld, v1).xz;
  215. float2 v2UV = mul(unity_ObjectToWorld, v2).xz;
  216. v0.y += sampleDisplacementTexture(v0UV) * intensity;
  217. v1.y += sampleDisplacementTexture(v1UV) * intensity;
  218. v2.y += sampleDisplacementTexture(v2UV) * intensity;
  219. offs = v0;
  220. float3 vn = cross(v2.xyz - v0.xyz, v1.xyz - v0.xyz);
  221. vn.xz *= _Smoothing.xx;
  222. nrml = normalize(vn);
  223. }
  224. #endif