ForestVision.shader 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
  2. Shader "ForestVision/ForestVision" {
  3. Properties {
  4. [Header(Global Control)] _Intensity ("Overall Intensity", Range(10, 1)) = 5
  5. [Space(10)]
  6. [Header(Main Texture Controls)] _Color("Main Tint Color", Color) = (0.5,0.5,0.5,1)//grey
  7. _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
  8. _Bump ("Normal", 2D) = "bump" {}
  9. _BumpPower ("Normal Power", Range (0.1, 2)) = 1 //added slider control for Normal strength
  10. [Header(Detail Texture Controls)]_Detail ("Detail", 2D) = "grey" {}
  11. _DetailPower ("Detail Power", Range (0, 1)) = 0.2 //added slider control for Normal strength
  12. [Space(10)]
  13. [Header(Side Texture Controls)] _SideDirection ("Side Direction", Vector) = (-1,0,1)
  14. _SideLevel ("Side Level", Range(0,1) ) = 0.3 //0.3 default
  15. _SideDepth ("Side Depth", Range(0,1)) = 0.7 //0.7 def
  16. _SideColor ("Side Color", Color) = (0.0,0.392,0.0,1.0) //dark green
  17. _SideTex ("Side Texture", 2D) = "white" {}
  18. //_SideNorm ("Side Normal", 2D) = "white" {}
  19. //_SideNMPower ("Side Normal Power", Range (0, 2)) = 0.25 //added slider control for Normal strength
  20. [Space(10)]
  21. [Header(Top Texture Controls)] _TopDirection ("Top Direction", Vector) = (0,1.5,0)
  22. _TopLevel ("Top Level", Range(0,1) ) = 0.2 //0.2 def
  23. _TopDepth ("Top Depth", Range(0,1)) = 0.7 //0.7 def
  24. _TopColor ("Top Color", Color) = (1,0.894,0.710,1.0) //orange
  25. _TopTex ("Top Texture", 2D) = "white" {}
  26. //_TopNorm ("Top Normal", 2D) = "white" {}
  27. //_TopNMPower ("Top Normal Power", Range (0, 2)) = 0.25 //added slider control for Normal strength
  28. [Space(10)]
  29. [Header(Bottom Texture Controls)] _BottomDirection ("Bottom Direction", Vector) = (0,-1.5,0)
  30. _BottomLevel ("Bottom Level", Range(0,1) ) = 0.3 //0.3 def
  31. _BottomDepth ("Bottom Depth", Range(0,1)) = 0.7 //0.7 def
  32. _BottomColor ("Bottom Color", Color) = (0.502,0.502,0.0,1.0)//olive
  33. _BottomTex ("Bottom Texture", 2D) = "white" {}
  34. //_BottomNorm ("Bottom Normal", 2D) = "white" {}
  35. //_BottomNMPower ("Bottom Normal Power", Range (0, 2)) = 0.25 //added slider control for Normal strength
  36. [Header(Foliage Controls)] _VertexWeight ("Vertex Weight", 2D) = "black" {} //added new weight map slot to drive vertex animation
  37. _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
  38. _ShakeTime ("Animation Time", Range (0, 1.0)) = .25
  39. _ShakeWindspeed ("Wind Speed", Range (0, 1.0)) = .3
  40. _ShakeBending ("Displacement", Range (0, 1.0)) = 0.2
  41. [Space(10)]
  42. [Header(Snow Controls)] _SnowDirection ("Snow Direction", Vector) = (0,1,0)
  43. _SnowLevel ("Snow Level", Range(-0.1,1) ) = 0
  44. _SnowDepth ("Snow Depth", Range(0,1)) = 1
  45. //_SnowTex ("Snow Texture", 2D) = "white" {}
  46. //_SnowNorm ("Snow Normal", 2D) = "bump" {}
  47. //_SnowNMPower ("Snow Normal Power", Range (0.1, 2)) = 1 //added slider control for Normal strength
  48. [HideInInspector] _Depth ("Depth", Range(0,0.2)) = 0.1
  49. [HideInInspector]_SnowColor ("Snow Color", Color) = (1.0,1.0,1.0,1.0)//white
  50. }
  51. SubShader {
  52. Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
  53. LOD 800
  54. Cull [_Cull]
  55. CGPROGRAM
  56. #pragma target 3.0
  57. #pragma surface surf Lambert alphatest:_Cutoff vertex:vert addshadow
  58. #pragma fragmentoption ARB_precision_hint_fastest
  59. #include "UnityCG.cginc"
  60. fixed4 _Color;
  61. sampler2D _MainTex;
  62. sampler2D _Bump;
  63. fixed _BumpPower;
  64. sampler2D _Detail;
  65. fixed _DetailPower;
  66. float _SideLevel;
  67. float4 _SideColor;
  68. sampler2D _SideTex;
  69. float4 _SideDirection;
  70. sampler2D _SideNorm;
  71. fixed _SideNMPower;
  72. float _TopLevel;
  73. float4 _TopColor;
  74. sampler2D _TopTex;
  75. float4 _TopDirection;
  76. sampler2D _TopNorm;
  77. fixed _TopNMPower;
  78. float _BottomLevel;
  79. float4 _BottomColor;
  80. sampler2D _BottomTex;
  81. float4 _BottomDirection;
  82. sampler2D _BottomNorm;
  83. fixed _BottomNMPower;
  84. float _SnowLevel;
  85. float4 _SnowColor;
  86. sampler2D _SnowTex;
  87. float4 _SnowDirection;
  88. sampler2D _SnowNorm;
  89. fixed _SnowNMPower;
  90. float _Depth;
  91. float _SideDepth;
  92. float _TopDepth;
  93. float _BottomDepth;
  94. float _SnowDepth;
  95. float _Intensity;
  96. sampler2D _VertexWeight;
  97. float _ShakeDisplacement;
  98. float _ShakeTime = 0.1;
  99. float _ShakeWindspeed;
  100. float _ShakeBending;
  101. struct Input {
  102. float2 uv_MainTex;
  103. float2 uv_SideTex;
  104. float2 uv_TopTex;
  105. float2 uv_BottomTex;
  106. float2 uv_SnowTex;
  107. float2 uv_Bump;
  108. float2 uv_Detail;
  109. float3 worldNormal;
  110. float2 uv_Normal;
  111. float2 uv_Weight;
  112. INTERNAL_DATA
  113. };
  114. //----------------------------------------------------------------Foliage functions
  115. void FastSinCos (float4 val, out float4 s, out float4 c) {
  116. val = val * 6.408849 - 3.1415927;
  117. float4 r5 = val * val;
  118. float4 r6 = r5 * r5;
  119. float4 r7 = r6 * r5;
  120. float4 r8 = r6 * r5;
  121. float4 r1 = r5 * val;
  122. float4 r2 = r1 * r5;
  123. float4 r3 = r2 * r5;
  124. float4 sin7 = {1, -0.16161616, 0.0083333, -0.00019841} ;
  125. float4 cos8 = {-0.5, 0.041666666, -0.0013888889, 0.000024801587} ;
  126. s = val + r1 * sin7.y + r2 * sin7.z + r3 * sin7.w;
  127. c = 1 + r5 * cos8.x + r6 * cos8.y + r7 * cos8.z + r8 * cos8.w;
  128. }
  129. void vert (inout appdata_full v) {
  130. //Convert the normal to world coortinates
  131. float4 side = mul(UNITY_MATRIX_IT_MV, _SideDirection);
  132. float4 top = mul(UNITY_MATRIX_IT_MV, _TopDirection);
  133. float4 bottom = mul(UNITY_MATRIX_IT_MV, _BottomDirection);
  134. float4 snow = mul(UNITY_MATRIX_IT_MV, _SnowDirection);
  135. //Convert the normal to world coortinates for snow
  136. float3 snormal = normalize(_SnowDirection.xyz);
  137. float3 sn = mul((float3x3)unity_WorldToObject, snormal).xyz;
  138. if(dot(v.normal, sn) >= lerp(1,-1, (_SnowLevel*2)/3))
  139. {
  140. v.vertex.xyz += normalize(sn + v.normal) * _SnowDepth * _SnowLevel/4;
  141. }
  142. //--------------------------------------------------------------------
  143. float4 WeightMap = tex2Dlod (_VertexWeight, float4(v.texcoord.xy,0,0));
  144. float factor = (1 - _ShakeDisplacement - v.color.r) * 0.5;
  145. const float _WindSpeed = (_ShakeWindspeed + v.color.g );
  146. const float _WaveScale = _ShakeDisplacement;
  147. const float4 _waveXSize = float4(0.048, 0.06, 0.24, 0.096);
  148. const float4 _waveZSize = float4 (0.024, .08, 0.08, 0.2);
  149. const float4 waveSpeed = float4 (1.2, 2, 1.6, 4.8);
  150. float4 _waveXmove = float4(0.024, 0.04, -0.12, 0.096);
  151. float4 _waveZmove = float4 (0.006, .02, -0.02, 0.1);
  152. float4 waves;
  153. waves = v.vertex.x * _waveXSize;
  154. waves += v.vertex.z * _waveZSize;
  155. waves += _Time.x * (1 - _ShakeTime * 2 - v.color.b ) * waveSpeed *_WindSpeed;
  156. float4 s, c;
  157. waves = frac (waves);
  158. FastSinCos (waves, s,c);
  159. float waveAmount = WeightMap.r * (v.color.a + _ShakeBending);
  160. s *= waveAmount;
  161. s *= normalize (waveSpeed);
  162. s = s * s;
  163. float fade = dot (s, 1.3);
  164. s = s * s;
  165. float3 waveMove = float3 (0,0,0);
  166. waveMove.x = dot (s, _waveXmove);
  167. waveMove.z = dot (s, _waveZmove);
  168. v.vertex.xz -= mul ((float3x3)unity_WorldToObject, waveMove).xz;
  169. }
  170. //-----------------------------------------------------------------------------------------------
  171. void surf (Input IN, inout SurfaceOutput o) {
  172. // get main textures
  173. fixed4 baseColor = tex2D (_MainTex, IN.uv_MainTex) * _Color;
  174. fixed3 sideColor = tex2D (_SideTex, IN.uv_SideTex);
  175. fixed3 topColor = tex2D (_TopTex, IN.uv_TopTex);
  176. fixed3 bottomColor = tex2D (_BottomTex, IN.uv_BottomTex);
  177. //fixed3 snowColor = tex2D (_SnowTex, IN.uv_SnowTex);
  178. //calculate world normals from inspector directions for color tints
  179. half difference = dot(WorldNormalVector(IN, o.Normal), _SideDirection.xyz) - lerp(1,-1,_SideLevel);
  180. half difference2 = dot(WorldNormalVector(IN, o.Normal), _TopDirection.xyz) - lerp(1,-1,_TopLevel);
  181. half difference3 = dot(WorldNormalVector(IN, o.Normal), _BottomDirection.xyz) - lerp(1,-1,_BottomLevel);
  182. //half difference4 = dot(WorldNormalVector(IN, o.Normal), _SnowDirection.xyz) - lerp(1,-1,_SnowLevel);
  183. // handle intensity levels
  184. difference = saturate(difference / _SideDepth) / _Intensity;
  185. difference2 = saturate(difference2 / _TopDepth)/ _Intensity;
  186. difference3 = saturate(difference3 / _BottomDepth)/ _Intensity;
  187. //difference4 = saturate(difference4 / _SnowDepth);
  188. //perform final coloring
  189. //if detailPower is greater than 0, then begin adding it into the formula, otherwise don't
  190. if(_DetailPower > 0){
  191. baseColor.rgb *= tex2D(_Detail,IN.uv_Detail).rgb * unity_ColorSpaceDouble.r + _DetailPower;
  192. }
  193. sideColor = (difference * (sideColor *2)) * _SideColor.rgb + (1-difference);
  194. topColor = (difference2 * topColor) * _TopColor.rgb + (1-difference2);
  195. bottomColor = (difference3 * (bottomColor*2)) * _BottomColor.rgb + (1-difference3);
  196. //snowColor = difference4 * _SnowColor.rgb + (1-difference4);
  197. //calculate normals
  198. fixed3 normalMain = UnpackNormal(tex2D(_Bump, IN.uv_Bump)); //get main bump
  199. o.Normal = lerp(o.Normal, normalMain, _BumpPower);//so far just main normal map
  200. if(dot(WorldNormalVector(IN, o.Normal), _SnowDirection.xyz)>=lerp(1,-1,_SnowLevel ))
  201. {
  202. o.Albedo = _SnowColor.rgb;
  203. //o.Normal = UnpackNormal (tex2D (_SnowNorm, IN.uv_Bump));
  204. }
  205. else {
  206. o.Albedo = (sideColor * topColor * bottomColor) * baseColor * _Color.rgb;
  207. }
  208. o.Alpha = baseColor.a;
  209. }
  210. ENDCG
  211. }
  212. FallBack "Diffuse"
  213. }