No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WavingGrassInput.hlsl 3.7KB

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