FV_Foliage.shader 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
  2. // Thanks to Gambinolnd for the base of this shader which I modified for ForestVision
  3. // http://forum.unity3d.com/threads/shader-moving-trees-grass-in-wind-outside-of-terrain.230911/
  4. Shader "ForestVision/FV_Foliage" {
  5. Properties {
  6. _Color ("Main Color", Color) = (1,1,1,1)
  7. _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
  8. _Normal ("Normal", 2D) = "bump" {}
  9. _BumpPower ("Normal Power", Range (0.01, 2)) = 1 //added slider control for Normal strength
  10. _VertexWeight ("Vertex Weight", 2D) = "weight" {} //added new weight map slot to drive vertex animation
  11. _OcclusionMap("Occlusion", 2D) = "white" {}
  12. _OcclusionStrength ("Occlusion Strength", Range(0.01,1)) = 1
  13. _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
  14. //_ShakeDisplacement ("Displacement", Range (0, 1.0)) = 1.0
  15. _ShakeTime ("Animation Time", Range (0, 1.0)) = .25
  16. _ShakeWindspeed ("Wind Speed", Range (0, 1.0)) = .3
  17. _ShakeBending ("Displacement", Range (0, 1.0)) = 0.2
  18. }
  19. SubShader {
  20. Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
  21. LOD 400
  22. Cull [_Cull]
  23. CGPROGRAM
  24. #pragma target 3.0
  25. #pragma surface surf Lambert alphatest:_Cutoff vertex:vert addshadow
  26. sampler2D _MainTex;
  27. sampler2D _Normal;
  28. sampler2D _VertexWeight;
  29. sampler2D _OcclusionMap;
  30. fixed4 _Color;
  31. fixed _BumpPower;
  32. fixed _OcclusionStrength;
  33. float _ShakeDisplacement;
  34. float _ShakeTime = 0.1;
  35. float _ShakeWindspeed;
  36. float _ShakeBending;
  37. struct Input {
  38. float2 uv_MainTex;
  39. float2 uv_Normal;
  40. float2 uv_Weight;
  41. float2 uv_OcculsionMap;
  42. };
  43. void FastSinCos (float4 val, out float4 s, out float4 c) {
  44. val = val * 6.408849 - 3.1415927;
  45. float4 r5 = val * val;
  46. float4 r6 = r5 * r5;
  47. float4 r7 = r6 * r5;
  48. float4 r8 = r6 * r5;
  49. float4 r1 = r5 * val;
  50. float4 r2 = r1 * r5;
  51. float4 r3 = r2 * r5;
  52. float4 sin7 = {1, -0.16161616, 0.0083333, -0.00019841} ;
  53. float4 cos8 = {-0.5, 0.041666666, -0.0013888889, 0.000024801587} ;
  54. s = val + r1 * sin7.y + r2 * sin7.z + r3 * sin7.w;
  55. c = 1 + r5 * cos8.x + r6 * cos8.y + r7 * cos8.z + r8 * cos8.w;
  56. }
  57. void vert (inout appdata_full v) {
  58. float4 WeightMap = tex2Dlod (_VertexWeight, float4(v.texcoord.xy,0,0));
  59. float factor = (1 - _ShakeDisplacement - v.color.r) * 0.5;
  60. const float _WindSpeed = (_ShakeWindspeed + v.color.g );
  61. const float _WaveScale = _ShakeDisplacement;
  62. const float4 _waveXSize = float4(0.048, 0.06, 0.24, 0.096);
  63. const float4 _waveZSize = float4 (0.024, .08, 0.08, 0.2);
  64. const float4 waveSpeed = float4 (1.2, 2, 1.6, 4.8);
  65. float4 _waveXmove = float4(0.024, 0.04, -0.12, 0.096);
  66. float4 _waveZmove = float4 (0.006, .02, -0.02, 0.1);
  67. float4 waves;
  68. waves = v.vertex.x * _waveXSize;
  69. waves += v.vertex.z * _waveZSize;
  70. waves += _Time.x * (1 - _ShakeTime * 2 - v.color.b ) * waveSpeed *_WindSpeed;
  71. float4 s, c;
  72. waves = frac (waves);
  73. FastSinCos (waves, s,c);
  74. float waveAmount = WeightMap.r * (v.color.a + _ShakeBending);
  75. s *= waveAmount;
  76. s *= normalize (waveSpeed);
  77. s = s * s;
  78. float fade = dot (s, 1.3);
  79. s = s * s;
  80. float3 waveMove = float3 (0,0,0);
  81. waveMove.x = dot (s, _waveXmove);
  82. waveMove.z = dot (s, _waveZmove);
  83. v.vertex.xz -= mul ((float3x3)unity_WorldToObject, waveMove).xz;
  84. }
  85. void surf (Input IN, inout SurfaceOutput o) {
  86. fixed4 baseColor = tex2D(_MainTex, IN.uv_MainTex) * _Color;
  87. fixed3 occlusion = tex2D(_OcclusionMap, IN.uv_OcculsionMap);
  88. //c.rgb =
  89. o.Albedo = baseColor.rgb;
  90. o.Alpha = baseColor.a;
  91. //c.rgb = c.rgb * occlusion.rgb;
  92. fixed3 normal = UnpackNormal(tex2D(_Normal, IN.uv_Normal));
  93. normal.z = normal.z / _BumpPower;
  94. o.Normal = normalize(normal);
  95. //o.Albedo = c.rgb * occlusion.rgb / normalize(_OcclusionStrength);
  96. }
  97. ENDCG
  98. } //end subshader
  99. Fallback "Transparent/Cutout/VertexLit"
  100. }