windDeformation.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include "platform/platform.h"
  23. #include "forest/windDeformation.h"
  24. #include "forest/forestItem.h"
  25. #include "forest/forestWindMgr.h"
  26. #include "forest/forestWindAccumulator.h"
  27. #include "materials/sceneData.h"
  28. #include "scene/sceneRenderState.h"
  29. #include "gfx/gfxShader.h"
  30. #include "gfx/gfxStructs.h"
  31. ImplementFeatureType( MFT_WindEffect, MFG_PreTransform, 0, false );
  32. void WindDeformationConstHandles::init( GFXShader *shader )
  33. {
  34. mWindDirAndSpeed = shader->getShaderConstHandle( "$windDirAndSpeed" );
  35. mWindParams = shader->getShaderConstHandle( "$windParams" );
  36. }
  37. void WindDeformationConstHandles::setConsts( SceneRenderState *state,
  38. const SceneData &sgData,
  39. GFXShaderConstBuffer *buffer )
  40. {
  41. PROFILE_SCOPE( WindDeformationConstHandles_setConsts );
  42. Point3F windDir( 0, 0, 0 );
  43. F32 windSpeed = 0;
  44. F32 bendScale = 0;
  45. F32 branchAmp = 0;
  46. F32 detailAmp = 0;
  47. F32 detailFreq = 0;
  48. const ForestItem *item = (const ForestItem*)sgData.materialHint;
  49. ForestWindAccumulator *wind = NULL;
  50. if ( item )
  51. {
  52. // First setup the per-datablock shader constants.
  53. //
  54. // These cannot be skipped as they modifiy the rest
  55. // state of a tree even without wind.
  56. //
  57. const ForestItemData *itemData = item->getData();
  58. const F32 windScale = itemData->mWindScale;
  59. bendScale = itemData->mTrunkBendScale * windScale;
  60. branchAmp = itemData->mWindBranchAmp * windScale;
  61. detailAmp = itemData->mWindDetailAmp * windScale;
  62. detailFreq = itemData->mWindDetailFreq * windScale;
  63. // Look for wind state.
  64. wind = WINDMGR->getLocalWind( item->getKey() );
  65. }
  66. if ( wind )
  67. {
  68. // Calculate distance to camera fade.
  69. F32 toCamLen = ( state->getDiffuseCameraPosition() - wind->getPosition()).len();
  70. F32 toCamInterp = 1.0f - (toCamLen / ForestWindMgr::smWindEffectRadius);
  71. toCamInterp = mClampF( toCamInterp, 0.0f, 1.0f );
  72. windDir.set( wind->getDirection().x, wind->getDirection().y, 0.0f );
  73. windSpeed = wind->getStrength();
  74. // Since the effect is just a displacement
  75. // of geometry i need to scale it up for smaller
  76. // trees so it looks like its affecting it more.
  77. //
  78. // TODO: This is possibly a side effect of not
  79. // properly doing the displacement physics in the
  80. // first place.
  81. //
  82. const F32 strengthScale = 1.0f / item->getScale();
  83. windDir *= strengthScale;
  84. windSpeed *= strengthScale;
  85. // Add in fade.
  86. windDir *= toCamInterp;
  87. windSpeed *= toCamInterp;
  88. MatrixF invXfm( item->getTransform() );
  89. invXfm.inverse();
  90. invXfm.mulV( windDir );
  91. }
  92. else
  93. {
  94. /*
  95. // HACK: This should go away... we need to work out a strategy
  96. // for allocating accumulators that accounts for random objects
  97. // and not just forest elements.
  98. //
  99. // Maybe add a generic 'id' value to SceneData that is set
  100. // by the RenderInst that can be any unique identifier for that
  101. // object that we can used down here to fetch an item id.
  102. // TODO: This is horrible... we should avoid access to the script
  103. // system while rendering as its really slow!
  104. if ( Con::getBoolVariable( "$tsStaticWindHack", false ) )
  105. {
  106. VectorF windVec( mSin( Sim::getCurrentTime() / 1000.0f ) * 0.25f, 0.0f, 0.0f );
  107. //sgData.objTrans.mulV( windVec );
  108. windDir.x = windVec.x;
  109. windDir.y = windVec.y;
  110. windSpeed = 0.75f;
  111. }
  112. */
  113. }
  114. buffer->setSafe( mWindDirAndSpeed, Point3F( windDir.x, windDir.y, windSpeed ) );
  115. buffer->setSafe( mWindParams, Point4F( bendScale, branchAmp, detailAmp, detailFreq ) );
  116. }