forestWindEmitter.cpp 18 KB

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