wind.hlsl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //
  23. // A tip of the hat....
  24. //
  25. // The following wind effects were derived from the GPU Gems
  26. // 3 chapter "Vegetation Procedural Animation and Shading in Crysis"
  27. // by Tiago Sousa of Crytek.
  28. //
  29. float4 smoothCurve( float4 x )
  30. {
  31. return x * x * ( 3.0 - 2.0 * x );
  32. }
  33. float4 triangleWave( float4 x )
  34. {
  35. return abs( frac( x + 0.5 ) * 2.0 - 1.0 );
  36. }
  37. float4 smoothTriangleWave( float4 x )
  38. {
  39. return smoothCurve( triangleWave( x ) );
  40. }
  41. float3 windTrunkBending( float3 vPos, float2 vWind, float fBendFactor )
  42. {
  43. // Smooth the bending factor and increase
  44. // the near by height limit.
  45. fBendFactor += 1.0;
  46. fBendFactor *= fBendFactor;
  47. fBendFactor = fBendFactor * fBendFactor - fBendFactor;
  48. // Displace the vert.
  49. float3 vNewPos = vPos;
  50. vNewPos.xy += vWind * fBendFactor;
  51. // Limit the length which makes the bend more
  52. // spherical and prevents stretching.
  53. float fLength = length( vPos );
  54. vPos = normalize( vNewPos ) * fLength;
  55. return vPos;
  56. }
  57. float3 windBranchBending( float3 vPos,
  58. float3 vNormal,
  59. float fTime,
  60. float fWindSpeed,
  61. float fBranchPhase,
  62. float fBranchAmp,
  63. float fBranchAtten,
  64. float fDetailPhase,
  65. float fDetailAmp,
  66. float fDetailFreq,
  67. float fEdgeAtten )
  68. {
  69. float fVertPhase = dot( vPos, fDetailPhase + fBranchPhase );
  70. float2 vWavesIn = fTime + float2( fVertPhase, fBranchPhase );
  71. float4 vWaves = ( frac( vWavesIn.xxyy *
  72. float4( 1.975, 0.793, 0.375, 0.193 ) ) *
  73. 2.0 - 1.0 ) * fWindSpeed * fDetailFreq;
  74. vWaves = smoothTriangleWave( vWaves );
  75. float2 vWavesSum = vWaves.xz + vWaves.yw;
  76. // We want the branches to bend both up and down.
  77. vWavesSum.y = 1 - ( vWavesSum.y * 2 );
  78. vPos += vWavesSum.xxy * float3( fEdgeAtten * fDetailAmp * vNormal.xy,
  79. fBranchAtten * fBranchAmp );
  80. return vPos;
  81. }