WavingGrassInput.hlsl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #ifndef UNIVERSAL_WAVING_GRASS_INPUT_INCLUDED
  2. #define UNIVERSAL_WAVING_GRASS_INPUT_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  4. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
  5. // Terrain engine shader helpers
  6. CBUFFER_START(TerrainGrass)
  7. half4 _WavingTint;
  8. float4 _WaveAndDistance; // wind speed, wave size, wind amount, max sqr distance
  9. float4 _CameraPosition; // .xyz = camera position, .w = 1 / (max sqr distance)
  10. float3 _CameraRight, _CameraUp;
  11. CBUFFER_END
  12. CBUFFER_START(UnityPerMaterial)
  13. float4 _MainTex_ST;
  14. half4 _BaseColor;
  15. half4 _SpecColor;
  16. half4 _EmissionColor;
  17. half _Cutoff;
  18. half _Shininess;
  19. CBUFFER_END
  20. TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex);
  21. // ---- Grass helpers
  22. // Calculate a 4 fast sine-cosine pairs
  23. // val: the 4 input values - each must be in the range (0 to 1)
  24. // s: The sine of each of the 4 values
  25. // c: The cosine of each of the 4 values
  26. void FastSinCos (float4 val, out float4 s, out float4 c) {
  27. val = val * 6.408849 - 3.1415927;
  28. // powers for taylor series
  29. float4 r5 = val * val; // wavevec ^ 2
  30. float4 r6 = r5 * r5; // wavevec ^ 4;
  31. float4 r7 = r6 * r5; // wavevec ^ 6;
  32. float4 r8 = r6 * r5; // wavevec ^ 8;
  33. float4 r1 = r5 * val; // wavevec ^ 3
  34. float4 r2 = r1 * r5; // wavevec ^ 5;
  35. float4 r3 = r2 * r5; // wavevec ^ 7;
  36. //Vectors for taylor's series expansion of sin and cos
  37. float4 sin7 = {1, -0.16161616, 0.0083333, -0.00019841};
  38. float4 cos8 = {-0.5, 0.041666666, -0.0013888889, 0.000024801587};
  39. // sin
  40. s = val + r1 * sin7.y + r2 * sin7.z + r3 * sin7.w;
  41. // cos
  42. c = 1 + r5 * cos8.x + r6 * cos8.y + r7 * cos8.z + r8 * cos8.w;
  43. }
  44. half4 TerrainWaveGrass (inout float4 vertex, float waveAmount, half4 color)
  45. {
  46. half4 _waveXSize = half4(0.012, 0.02, 0.06, 0.024) * _WaveAndDistance.y;
  47. half4 _waveZSize = half4 (0.006, .02, 0.02, 0.05) * _WaveAndDistance.y;
  48. half4 waveSpeed = half4 (1.2, 2, 1.6, 4.8);
  49. half4 _waveXmove = half4(0.024, 0.04, -0.12, 0.096);
  50. half4 _waveZmove = half4 (0.006, .02, -0.02, 0.1);
  51. float4 waves;
  52. waves = vertex.x * _waveXSize;
  53. waves += vertex.z * _waveZSize;
  54. // Add in time to model them over time
  55. waves += _WaveAndDistance.x * waveSpeed;
  56. float4 s, c;
  57. waves = frac (waves);
  58. FastSinCos (waves, s,c);
  59. s = s * s;
  60. s = s * s;
  61. half lighting = dot (s, normalize (half4 (1,1,.4,.2))) * 0.7;
  62. s = s * waveAmount;
  63. half3 waveMove = 0;
  64. waveMove.x = dot (s, _waveXmove);
  65. waveMove.z = dot (s, _waveZmove);
  66. vertex.xz -= waveMove.xz * _WaveAndDistance.z;
  67. // apply color animation
  68. half3 waveColor = lerp (real3(0.5, 0.5, 0.5), _WavingTint.rgb, lighting);
  69. // Fade the grass out before detail distance.
  70. // Saturate because Radeon HD drivers on OS X 10.4.10 don't saturate vertex colors properly.
  71. half3 offset = vertex.xyz - _CameraPosition.xyz;
  72. color.a = saturate (2 * (_WaveAndDistance.w - dot (offset, offset)) * _CameraPosition.w);
  73. return half4(2 * waveColor * color.rgb, color.a);
  74. }
  75. void TerrainBillboardGrass( inout float4 pos, float2 offset )
  76. {
  77. float3 grasspos = pos.xyz - _CameraPosition.xyz;
  78. if (dot(grasspos, grasspos) > _WaveAndDistance.w)
  79. offset = 0.0;
  80. pos.xyz += offset.x * _CameraRight.xyz;
  81. pos.y += offset.y;
  82. }
  83. #endif