forestWindEmitter.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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/forestWindEmitter.h"
  24. #include "forest/forestWindMgr.h"
  25. #include "console/consoleInternal.h"
  26. #include "core/stream/bitStream.h"
  27. #include "core/util/safeDelete.h"
  28. #include "platform/profiler.h"
  29. #include "math/mathIO.h"
  30. #include "math/mRandom.h"
  31. #include "scene/sceneManager.h"
  32. #include "scene/sceneRenderState.h"
  33. #include "renderInstance/renderPassManager.h"
  34. #include "gfx/gfxDrawUtil.h"
  35. #include "gfx/gfxTransformSaver.h"
  36. #include "sim/netConnection.h"
  37. #include "T3D/gameBase/processList.h"
  38. #include "console/engineAPI.h"
  39. ConsoleDocClass( ForestWindEmitter,
  40. "@brief Object responsible for simulating wind in a level.\n\n"
  41. "When placed in the level, a ForestWindEmitter will cause tree branches to bend and sway, leaves "
  42. "to flutter, and create vertical bending on the tree's trunk.\n\n"
  43. "@tsexample\n"
  44. "// The following is a full declaration of a wind emitter\n"
  45. "new ForestWindEmitter()\n"
  46. "{\n"
  47. " position = \"497.739 765.821 102.395\";\n"
  48. " windEnabled = \"1\";\n"
  49. " radialEmitter = \"1\";\n"
  50. " strength = \"1\";\n"
  51. " radius = \"3\";\n"
  52. " gustStrength = \"0.5\";\n"
  53. " gustFrequency = \"1\";\n"
  54. " gustYawAngle = \"10\";\n"
  55. " gustYawFrequency = \"4\";\n"
  56. " gustWobbleStrength = \"2\";\n"
  57. " turbulenceStrength = \"1\";\n"
  58. " turbulenceFrequency = \"2\";\n"
  59. " hasMount = \"0\";\n"
  60. " scale = \"3 3 3\";\n"
  61. " canSave = \"1\";\n"
  62. " canSaveDynamicFields = \"1\";\n"
  63. " rotation = \"1 0 0 0\";\n"
  64. "};\n"
  65. "@endtsexample\n\n"
  66. "@ingroup FX\n"
  67. "@ingroup Forest\n"
  68. "@ingroup Atmosphere\n"
  69. );
  70. // We need to know when the mission editor is enabled.
  71. extern bool gEditingMission;
  72. ForestWind::ForestWind( ForestWindEmitter *emitter )
  73. : mStrength( 0.0f ),
  74. mDirection( 1.0f, 0, 0 ),
  75. mLastGustTime( 0 ),
  76. mLastYawTime( 0 ),
  77. mCurrentTarget( 0, 0 ),
  78. mCurrentInterp( 0 ),
  79. mTargetYawAngle( 0 ),
  80. mParent( emitter ),
  81. mIsDirty( false ),
  82. mRandom( Platform::getRealMilliseconds() + 1 )
  83. {
  84. }
  85. ForestWind::~ForestWind()
  86. {
  87. }
  88. void ForestWind::processTick()
  89. {
  90. PROFILE_SCOPE( ForestWind_ProcessTick );
  91. const F32 deltaTime = 0.032f;
  92. const U32 simTime = Sim::getCurrentTime();
  93. Point2F finalVec( 0, 0 );
  94. Point2F windDir( mParent->mWindDirection.x, mParent->mWindDirection.y );
  95. if ( mLastGustTime < simTime )
  96. {
  97. Point2F turbVec( 0, 0 );
  98. if ( mLastGustTime < simTime + (mParent->mWindTurbulenceFrequency * 1000.0f) )
  99. turbVec = (mRandom.randF() * mParent->mWindTurbulenceStrength) * windDir;
  100. mLastGustTime = simTime + (mParent->mWindGustFrequency * 1000.0f);
  101. Point2F gustVec = (mRandom.randF() * mParent->mWindGustStrength + mRandom.randF() * mParent->mWindGustWobbleStrength) * windDir;
  102. finalVec += gustVec + turbVec;
  103. //finalVec.normalizeSafe();
  104. }
  105. //bool rotationChange = false;
  106. if ( mLastYawTime < simTime )
  107. {
  108. mLastYawTime = simTime + (mParent->mWindGustYawFrequency * 1000.0f);
  109. F32 rotateAmt = mRandom.randF() * mParent->mWindGustYawAngle + mRandom.randF() * mParent->mWindGustWobbleStrength;
  110. if ( mRandom.randF() <= 0.5f )
  111. rotateAmt = -rotateAmt;
  112. rotateAmt = mDegToRad( rotateAmt );
  113. if ( rotateAmt > M_2PI_F )
  114. rotateAmt -= M_2PI_F;
  115. else if ( rotateAmt < -M_2PI_F )
  116. rotateAmt += M_2PI_F;
  117. mTargetYawAngle = rotateAmt;
  118. //finalVec.rotate( rotateAmt );
  119. mCurrentTarget.rotate( rotateAmt );
  120. }
  121. //mCurrentTarget.normalizeSafe();
  122. if ( mCurrentTarget.isZero() || mCurrentInterp >= 1.0f )
  123. {
  124. mCurrentInterp = 0;
  125. mCurrentTarget.set( 0, 0 );
  126. Point2F windDir( mDirection.x, mDirection.y );
  127. windDir.normalizeSafe();
  128. mCurrentTarget = finalVec + windDir;
  129. }
  130. else
  131. {
  132. mCurrentInterp += deltaTime;
  133. mCurrentInterp = mClampF( mCurrentInterp, 0.0f, 1.0f );
  134. mDirection.interpolate( mDirection, Point3F( mCurrentTarget.x, mCurrentTarget.y, 0 ), mCurrentInterp );
  135. //F32 rotateAmt = mLerp( 0, mTargetYawAngle, mCurrentInterp );
  136. //mTargetYawAngle -= rotateAmt;
  137. //Point2F dir( mDirection.x, mDirection.y );
  138. //if ( mTargetYawAngle > 0.0f )
  139. // dir.rotate( rotateAmt );
  140. //mDirection.set( dir.x, dir.y, 0 );
  141. }
  142. }
  143. void ForestWind::setStrengthAndDirection( F32 strength, const VectorF &direction )
  144. {
  145. if ( mStrength != strength ||
  146. mDirection != direction )
  147. {
  148. mStrength = strength;
  149. mDirection = direction;
  150. mIsDirty = true;
  151. }
  152. }
  153. void ForestWind::setStrength( F32 strength )
  154. {
  155. if ( mStrength != strength )
  156. {
  157. mStrength = strength;
  158. mIsDirty = true;
  159. }
  160. }
  161. void ForestWind::setDirection( const VectorF &direction )
  162. {
  163. if ( mDirection != direction )
  164. {
  165. mDirection = direction;
  166. mIsDirty = true;
  167. }
  168. }
  169. IMPLEMENT_CO_NETOBJECT_V1(ForestWindEmitter);
  170. ForestWindEmitter::ForestWindEmitter( bool makeClientObject )
  171. : mEnabled( true ),
  172. mAddedToScene( false ),
  173. mWind( NULL ),
  174. mWindStrength( 1 ),
  175. mWindDirection( 1, 0, 0 ),
  176. mWindGustFrequency( 3.0f ),
  177. mWindGustStrength( 0.25f ),
  178. mWindGustYawAngle( 10.0f ),
  179. mWindGustYawFrequency( 4.0f ),
  180. mWindGustWobbleStrength( 2.0f ),
  181. mWindTurbulenceFrequency( 2.0f ),
  182. mWindTurbulenceStrength( 0.25f ),
  183. mWindRadius( 0 ),
  184. mRadialEmitter( false ),
  185. mHasMount( false ),
  186. mIsMounted( false ),
  187. mMountObject( NULL )
  188. {
  189. mTypeMask |= StaticObjectType | EnvironmentObjectType;
  190. if ( makeClientObject )
  191. mNetFlags.set( IsGhost );
  192. else
  193. mNetFlags.set( Ghostable | ScopeAlways );
  194. }
  195. ForestWindEmitter::~ForestWindEmitter()
  196. {
  197. }
  198. void ForestWindEmitter::initPersistFields()
  199. {
  200. // Initialise parents' persistent fields.
  201. Parent::initPersistFields();
  202. addGroup( "ForestWind" );
  203. addField( "windEnabled", TypeBool, Offset( mEnabled, ForestWindEmitter ), "Determines if the emitter will be counted in wind calculations." );
  204. addField( "radialEmitter", TypeBool, Offset( mRadialEmitter, ForestWindEmitter ), "Determines if the emitter is a global direction or local radial emitter." );
  205. addField( "strength", TypeF32, Offset( mWindStrength, ForestWindEmitter ), "The strength of the wind force." );
  206. addField( "radius", TypeF32, Offset( mWindRadius, ForestWindEmitter ), "The radius of the emitter for local radial emitters." );
  207. addField( "gustStrength", TypeF32, Offset( mWindGustStrength, ForestWindEmitter ), "The maximum strength of a gust." );
  208. addField( "gustFrequency", TypeF32, Offset( mWindGustFrequency, ForestWindEmitter ), "The frequency of gusting in seconds." );
  209. addField( "gustYawAngle", TypeF32, Offset( mWindGustYawAngle, ForestWindEmitter ), "The amount of degrees the wind direction can drift (both positive and negative)." );
  210. addField( "gustYawFrequency", TypeF32, Offset( mWindGustYawFrequency, ForestWindEmitter ), "The frequency of wind yaw drift, in seconds." );
  211. addField( "gustWobbleStrength", TypeF32, Offset( mWindGustWobbleStrength, ForestWindEmitter ), "The amount of random wobble added to gust and turbulence vectors." );
  212. addField( "turbulenceStrength", TypeF32, Offset( mWindTurbulenceStrength, ForestWindEmitter ), "The strength of gust turbulence." );
  213. addField( "turbulenceFrequency", TypeF32, Offset( mWindTurbulenceFrequency, ForestWindEmitter ), "The frequency of gust turbulence, in seconds." );
  214. addField( "hasMount", TypeBool, Offset( mHasMount, ForestWindEmitter ), "Determines if the emitter is mounted to another object." );
  215. endGroup( "ForestWind" );
  216. }
  217. U32 ForestWindEmitter::packUpdate(NetConnection * con, U32 mask, BitStream * stream)
  218. {
  219. U32 retMask = Parent::packUpdate( con, mask, stream );
  220. mathWrite( *stream, mObjToWorld );
  221. if ( stream->writeFlag( mask & EnabledMask ) )
  222. stream->writeFlag( mEnabled );
  223. if ( stream->writeFlag( mask & WindMask ) )
  224. {
  225. stream->write( mWindStrength );
  226. stream->write( mWindRadius );
  227. stream->writeFlag( mRadialEmitter );
  228. stream->write( mWindGustStrength );
  229. stream->write( mWindGustFrequency );
  230. stream->write( mWindGustYawAngle );
  231. stream->write( mWindGustYawFrequency );
  232. stream->write( mWindGustWobbleStrength );
  233. stream->write( mWindTurbulenceStrength );
  234. stream->write( mWindTurbulenceFrequency );
  235. // The wind direction should be normalized!
  236. if ( mWindDirection.isZero() )
  237. {
  238. VectorF forwardVec( 0, 0, 0 );
  239. mWorldToObj.getColumn( 1, &mWindDirection );
  240. }
  241. else
  242. mWindDirection.normalize();
  243. stream->writeNormalVector( mWindDirection, 8 );
  244. stream->writeFlag( mHasMount );
  245. }
  246. return retMask;
  247. }
  248. void ForestWindEmitter::unpackUpdate(NetConnection * con, BitStream * stream)
  249. {
  250. // Unpack Parent.
  251. Parent::unpackUpdate( con, stream );
  252. MatrixF xfm;
  253. mathRead( *stream, &xfm );
  254. Parent::setTransform( xfm );
  255. U32 windMask = 0;
  256. if ( stream->readFlag() ) // EnabledMask
  257. mEnabled = stream->readFlag();
  258. if ( stream->readFlag() ) // WindMask
  259. {
  260. stream->read( &mWindStrength );
  261. stream->read( &mWindRadius );
  262. mRadialEmitter = stream->readFlag();
  263. stream->read( &mWindGustStrength );
  264. stream->read( &mWindGustFrequency );
  265. stream->read( &mWindGustYawAngle );
  266. stream->read( &mWindGustYawFrequency );
  267. stream->read( &mWindGustWobbleStrength );
  268. stream->read( &mWindTurbulenceStrength );
  269. stream->read( &mWindTurbulenceFrequency );
  270. stream->readNormalVector( &mWindDirection, 8 );
  271. windMask |= WindMask;
  272. mHasMount = stream->readFlag();
  273. }
  274. // This does nothing if the masks are not set!
  275. if ( windMask != 0 && isProperlyAdded() )
  276. {
  277. Point3F boxRad( 0, 0, 0 );
  278. if ( !isRadialEmitter() )
  279. boxRad.set( 10000.0f, 10000.0f, 10000.0f );
  280. else
  281. boxRad.set( mWindRadius, mWindRadius, mWindRadius );
  282. mObjBox.set( -boxRad, boxRad );
  283. resetWorldBox();
  284. _initWind( windMask );
  285. }
  286. }
  287. bool ForestWindEmitter::onAdd()
  288. {
  289. if ( !Parent::onAdd() )
  290. return false;
  291. // Only the client side actually does wind.
  292. if ( isClientObject() )
  293. {
  294. // TODO: wasn't this a big hack we already fixed better?
  295. //Projectile::getGhostReceivedSignal().notify( this, &ForestWindEmitter::_onMountObjectGhostReceived );
  296. _initWind();
  297. WINDMGR->addEmitter( this );
  298. }
  299. Point3F boxRad( 0, 0, 0 );
  300. if ( !isRadialEmitter() )
  301. boxRad.set( 10000.0f, 10000.0f, 10000.0f );
  302. else
  303. boxRad.set( mWindRadius, mWindRadius, mWindRadius );
  304. mObjBox.set( -boxRad, boxRad );
  305. resetWorldBox();
  306. enableCollision();
  307. // If we are we editing the mission then
  308. // be sure to add us to the scene.
  309. if ( gEditingMission || mHasMount )
  310. {
  311. addToScene();
  312. mAddedToScene = true;
  313. }
  314. return true;
  315. }
  316. void ForestWindEmitter::onRemove()
  317. {
  318. // Only the client side actually does wind.
  319. if ( isClientObject() )
  320. {
  321. //Projectile::getGhostReceivedSignal().remove( this, &ForestWindEmitter::_onMountObjectGhostReceived );
  322. WINDMGR->removeEmitter( this );
  323. SAFE_DELETE( mWind );
  324. }
  325. // If we are editing the mission then remove
  326. // us from the scene graph.
  327. if ( gEditingMission || mHasMount )
  328. {
  329. removeFromScene();
  330. mAddedToScene = false;
  331. }
  332. // Do Parent.
  333. Parent::onRemove();
  334. }
  335. void ForestWindEmitter::_onMountObjectGhostReceived( SceneObject *object )
  336. {
  337. if ( !object )
  338. return;
  339. attachToObject( object );
  340. }
  341. void ForestWindEmitter::inspectPostApply()
  342. {
  343. // Force the client update!
  344. setMaskBits(0xffffffff);
  345. }
  346. void ForestWindEmitter::onEditorEnable()
  347. {
  348. if ( !mAddedToScene )
  349. {
  350. addToScene();
  351. mAddedToScene = true;
  352. }
  353. }
  354. void ForestWindEmitter::onEditorDisable()
  355. {
  356. // Remove us from the scene.
  357. if ( mAddedToScene )
  358. {
  359. removeFromScene();
  360. mAddedToScene = false;
  361. }
  362. }
  363. void ForestWindEmitter::_initWind( U32 mask )
  364. {
  365. AssertFatal( !isServerObject(), "SpeedWind is never updated on the server!" );
  366. // If we don't have a wind
  367. // object create one now.
  368. if ( !mWind )
  369. mWind = new ForestWind( this );
  370. // Do we need to apply a new direction and strength?
  371. if ( mask & WindMask )
  372. {
  373. mWorldToObj.getColumn( 1, &mWindDirection );
  374. mWind->setStrengthAndDirection( mWindStrength, mWindDirection );
  375. }
  376. }
  377. void ForestWindEmitter::setTransform( const MatrixF &mat )
  378. {
  379. Parent::setTransform( mat );
  380. // Force the client update!
  381. setMaskBits(0xffffffff);
  382. if ( isClientObject() )
  383. _initWind();
  384. }
  385. void ForestWindEmitter::prepRenderImage( SceneRenderState* state )
  386. {
  387. PROFILE_SCOPE( ForestWindEmitter_PrepRenderImage );
  388. // Only render the radius and
  389. // direction if we're in the editor.
  390. // Don't render them if this is a reflect pass.
  391. if ( !state->isDiffusePass() || !gEditingMission )
  392. return;
  393. // This should be sufficient for most objects that don't manage zones, and
  394. // don't need to return a specialized RenderImage...
  395. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  396. ri->renderDelegate.bind( this, &ForestWindEmitter::_renderEmitterInfo );
  397. ri->type = RenderPassManager::RIT_Editor;
  398. state->getRenderPass()->addInst( ri );
  399. }
  400. void ForestWindEmitter::_renderEmitterInfo( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat )
  401. {
  402. if ( overrideMat )
  403. return;
  404. GFXTransformSaver saver;
  405. GFXDrawUtil *drawer = GFX->getDrawUtil();
  406. AssertFatal( drawer, "Got NULL GFXDrawUtil!" );
  407. const Point3F &pos = getPosition();
  408. const VectorF &windVec = mWind->getDirection();
  409. GFXStateBlockDesc desc;
  410. desc.setBlend( true );
  411. desc.setZReadWrite( true, false );
  412. // Draw an arrow pointing
  413. // in the wind direction.
  414. drawer->drawArrow( desc, pos, pos + (windVec * mWindStrength), ColorI( 0, 0, 255, 255 ) );//Point3F( -235.214, 219.589, 34.0991 ), Point3F( -218.814, 244.731, 37.5587 ), ColorI( 255, 255, 0, 255 ) );//
  415. drawer->drawArrow( desc, pos, pos + (mWind->getTarget() * mWindStrength ), ColorI( 255, 0, 0, 85 ) );
  416. // Draw a 2D circle for the wind radius.
  417. if ( isRadialEmitter() )
  418. drawer->drawSphere( desc, mWindRadius, pos, ColorI( 255, 0, 0, 80 ) );
  419. }
  420. F32 ForestWindEmitter::getStrength() const
  421. {
  422. return mWind->getStrength();
  423. }
  424. void ForestWindEmitter::setStrength( F32 strength )
  425. {
  426. mWindStrength = strength;
  427. mWind->setStrength( mWindStrength );
  428. }
  429. void ForestWindEmitter::attachToObject( SceneObject *obj )
  430. {
  431. if ( !obj )
  432. return;
  433. mMountObject = obj;
  434. mIsMounted = true;
  435. if ( isServerObject() )
  436. deleteNotify( mMountObject );
  437. }
  438. void ForestWindEmitter::updateMountPosition()
  439. {
  440. AssertFatal( isClientObject(), "ForestWindEmitter::updateMountPosition - This should only happen on the client!" );
  441. if ( !mHasMount || !mMountObject )
  442. return;
  443. MatrixF mat( true );
  444. mat.setPosition( mMountObject->getPosition() );
  445. Parent::setTransform( mat );
  446. }
  447. void ForestWindEmitter::onDeleteNotify(SimObject *object)
  448. {
  449. AssertFatal( isServerObject(), "ForestWindEmitter::onDeleteNotify - This should never happen on the client!" );
  450. safeDeleteObject();
  451. }
  452. DefineEngineMethod( ForestWindEmitter, attachToObject, void, ( U32 objectID ),,
  453. "@brief Mounts the wind emitter to another scene object\n\n"
  454. "@param objectID Unique ID of the object wind emitter should attach to"
  455. "@tsexample\n"
  456. "// Wind emitter previously created and named %windEmitter\n"
  457. "// Going to attach it to the player, making him a walking wind storm\n"
  458. "%windEmitter.attachToObject(%player);\n"
  459. "@endtsexample\n\n")
  460. {
  461. SceneObject *obj = dynamic_cast<SceneObject*>( Sim::findObject( objectID ) );
  462. if ( !obj )
  463. Con::warnf( "ForestWindEmitter::attachToObject - failed to find object with ID: %d", objectID );
  464. object->attachToObject( obj );
  465. }