MADFINGER-Lightmap-Unlit-Wind.shader 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Upgrade NOTE: commented out 'float4 unity_LightmapST', a built-in variable
  2. // Upgrade NOTE: commented out 'sampler2D unity_Lightmap', a built-in variable
  3. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
  4. // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
  5. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  6. // Upgrade NOTE: replaced tex2D unity_Lightmap with UNITY_SAMPLE_TEX2D
  7. // - Unlit
  8. // - Per-vertex (virtual) camera space specular light
  9. // - SUPPORTS lightmap
  10. Shader "MADFINGER/Environment/Lightmap + Wind" {
  11. Properties {
  12. _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
  13. _Wind("Wind params",Vector) = (1,1,1,1)
  14. _WindEdgeFlutter("Wind edge fultter factor", float) = 0.5
  15. _WindEdgeFlutterFreqScale("Wind edge fultter freq scale",float) = 0.5
  16. }
  17. SubShader {
  18. Tags {"Queue"="Transparent" "RenderType"="Transparent" "LightMode"="ForwardBase"}
  19. LOD 100
  20. Blend SrcAlpha OneMinusSrcAlpha
  21. Cull Off ZWrite Off
  22. CGINCLUDE
  23. #include "UnityCG.cginc"
  24. #include "TerrainEngine.cginc"
  25. sampler2D _MainTex;
  26. float4 _MainTex_ST;
  27. samplerCUBE _ReflTex;
  28. #ifndef LIGHTMAP_OFF
  29. // float4 unity_LightmapST;
  30. // sampler2D unity_Lightmap;
  31. #endif
  32. float _WindEdgeFlutter;
  33. float _WindEdgeFlutterFreqScale;
  34. struct v2f {
  35. float4 pos : SV_POSITION;
  36. float2 uv : TEXCOORD0;
  37. #ifndef LIGHTMAP_OFF
  38. float2 lmap : TEXCOORD1;
  39. #endif
  40. fixed3 spec : TEXCOORD2;
  41. };
  42. inline float4 AnimateVertex2(float4 pos, float3 normal, float4 animParams,float4 wind,float2 time)
  43. {
  44. // animParams stored in color
  45. // animParams.x = branch phase
  46. // animParams.y = edge flutter factor
  47. // animParams.z = primary factor
  48. // animParams.w = secondary factor
  49. float fDetailAmp = 0.1f;
  50. float fBranchAmp = 0.3f;
  51. // Phases (object, vertex, branch)
  52. float fObjPhase = dot(unity_ObjectToWorld[3].xyz, 1);
  53. float fBranchPhase = fObjPhase + animParams.x;
  54. float fVtxPhase = dot(pos.xyz, animParams.y + fBranchPhase);
  55. // x is used for edges; y is used for branches
  56. float2 vWavesIn = time + float2(fVtxPhase, fBranchPhase );
  57. // 1.975, 0.793, 0.375, 0.193 are good frequencies
  58. float4 vWaves = (frac( vWavesIn.xxyy * float4(1.975, 0.793, 0.375, 0.193) ) * 2.0 - 1.0);
  59. vWaves = SmoothTriangleWave( vWaves );
  60. float2 vWavesSum = vWaves.xz + vWaves.yw;
  61. // Edge (xz) and branch bending (y)
  62. float3 bend = animParams.y * fDetailAmp * normal.xyz;
  63. bend.y = animParams.w * fBranchAmp;
  64. pos.xyz += ((vWavesSum.xyx * bend) + (wind.xyz * vWavesSum.y * animParams.w)) * wind.w;
  65. // Primary bending
  66. // Displace position
  67. pos.xyz += animParams.z * wind.xyz;
  68. return pos;
  69. }
  70. v2f vert (appdata_full v)
  71. {
  72. v2f o;
  73. float4 wind;
  74. float bendingFact = v.color.a;
  75. wind.xyz = mul((float3x3)unity_WorldToObject,_Wind.xyz);
  76. wind.w = _Wind.w * bendingFact;
  77. float4 windParams = float4(0,_WindEdgeFlutter,bendingFact.xx);
  78. float windTime = _Time.y * float2(_WindEdgeFlutterFreqScale,1);
  79. float4 mdlPos = AnimateVertex2(v.vertex,v.normal,windParams,wind,windTime);
  80. o.pos = UnityObjectToClipPos(mdlPos);
  81. o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  82. o.spec = v.color;
  83. #ifndef LIGHTMAP_OFF
  84. o.lmap = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
  85. #endif
  86. return o;
  87. }
  88. ENDCG
  89. Pass {
  90. CGPROGRAM
  91. #pragma debug
  92. #pragma vertex vert
  93. #pragma fragment frag
  94. #pragma fragmentoption ARB_precision_hint_fastest
  95. fixed4 frag (v2f i) : COLOR
  96. {
  97. fixed4 tex = tex2D (_MainTex, i.uv);
  98. fixed4 c;
  99. c.rgb = tex.rgb;
  100. c.a = tex.a;
  101. #ifndef LIGHTMAP_OFF
  102. fixed3 lm = DecodeLightmap (UNITY_SAMPLE_TEX2D(unity_Lightmap, i.lmap));
  103. c.rgb *= lm;
  104. #endif
  105. return c;
  106. }
  107. ENDCG
  108. }
  109. }
  110. }