WaterFlow.shader 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // a simple water shader used for demos
  2. // Animated Water Shader
  3. // Copyright (c) 2012, Stanislaw Adaszewski (http://algoholic.eu)
  4. // All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are met:
  8. //
  9. // 1. Redistributions of source code must retain the above copyright notice, this
  10. // list of conditions and the following disclaimer.
  11. // 2. Redistributions in binary form must reproduce the above copyright notice,
  12. // this list of conditions and the following disclaimer in the documentation
  13. // and/or other materials provided with the distribution.
  14. //
  15. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19. // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. Shader "MTE/Surface/WaterFlow"
  26. {
  27. Properties
  28. {
  29. _Color ("Water Color (RGB) Transparency (A)", COLOR) = (1, 1, 1, 0.5)
  30. _BumpMap ("Normal Map 1", 2D) = "white" {}
  31. _BumpMap2 ("Normal Map 2", 2D) = "white" {}
  32. _FlowMap ("Flow Map", 2D) = "white" {}
  33. _NoiseMap ("Noise Map", 2D) = "black" {}
  34. _Cube ("Reflection Cubemap", Cube) = "white" { }
  35. _Cycle ("Cycle", float) = 1.0
  36. _Speed ("Speed", float) = 0.05
  37. _SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
  38. _Shininess ("Shininess", Range (0.01, 2)) = 0.078125
  39. _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
  40. }
  41. SubShader
  42. {
  43. Tags { "RenderType"="Transparent" "Queue"="Transparent" }
  44. Blend SrcAlpha OneMinusSrcAlpha
  45. LOD 200
  46. CGPROGRAM
  47. #pragma surface surf BlinnPhong alpha
  48. #pragma target 2.0
  49. float4 _Color;
  50. sampler2D _BumpMap;
  51. sampler2D _BumpMap2;
  52. samplerCUBE _Cube;
  53. sampler2D _FlowMap;
  54. sampler2D _NoiseMap;
  55. float _Cycle;
  56. float _Speed;
  57. float _Shininess;
  58. float4 _ReflectColor;
  59. struct Input
  60. {
  61. float2 uv_BumpMap;
  62. float2 uv_FlowMap;
  63. float3 worldRefl;
  64. float3 worldNormal;
  65. float3 viewDir;
  66. INTERNAL_DATA
  67. };
  68. void surf (Input IN, inout SurfaceOutput o)
  69. {
  70. float3 flowDir = tex2D(_FlowMap, IN.uv_FlowMap) * 2 - 1;
  71. flowDir *= _Speed;
  72. flowDir.y *= -1; // just a hack to fit mte created/coverted mesh
  73. float3 noise = tex2D(_NoiseMap, IN.uv_FlowMap);
  74. float phase = _Time[1] / _Cycle + noise.r * 0.5f;
  75. float phase0 = frac(phase + 0.5f);
  76. float phase1 = frac(phase + 1.0f);
  77. half3 n1 = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap + flowDir.xy * phase0));
  78. half3 n2 = UnpackNormal(tex2D(_BumpMap2, IN.uv_BumpMap + flowDir.xy * phase1));
  79. float flowLerp = abs((0.5f - phase0) / 0.5f);
  80. o.Normal = lerp(n1, n2, flowLerp);
  81. o.Alpha = _Color.a;
  82. o.Gloss = 1;
  83. o.Specular = _Shininess;
  84. fixed4 reflcol = texCUBE (_Cube, WorldReflectionVector(IN, o.Normal));
  85. o.Albedo = _Color.rgb;
  86. o.Emission = reflcol.rgb * _ReflectColor.rgb;
  87. }
  88. ENDCG
  89. }
  90. //FallBack "Reflective/Bumped Specular"
  91. }