WavingGrassInput.hlsl 3.6 KB

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