2
0

forestWindEmitter.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 windNorm( mDirection.x, mDirection.y );
  128. windNorm.normalizeSafe();
  129. mCurrentTarget = finalVec + windNorm;
  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. mCurrentTarget.zero();
  152. mIsDirty = true;
  153. }
  154. }
  155. void ForestWind::setStrength( F32 strength )
  156. {
  157. if ( mStrength != strength )
  158. {
  159. mStrength = strength;
  160. mIsDirty = true;
  161. }
  162. }
  163. void ForestWind::setDirection( const VectorF &direction )
  164. {
  165. if ( mDirection != direction )
  166. {
  167. mDirection = direction;
  168. mIsDirty = true;
  169. }
  170. }
  171. IMPLEMENT_CO_NETOBJECT_V1(ForestWindEmitter);
  172. ForestWindEmitter::ForestWindEmitter( bool makeClientObject )
  173. :
  174. mAddedToScene( false ),
  175. mEnabled( true ),
  176. mWind( NULL ),
  177. mWindStrength( 1 ),
  178. mWindDirection( 1, 0, 0 ),
  179. mWindGustFrequency( 3.0f ),
  180. mWindGustStrength( 0.25f ),
  181. mWindGustYawAngle( 10.0f ),
  182. mWindGustYawFrequency( 4.0f ),
  183. mWindGustWobbleStrength( 2.0f ),
  184. mWindTurbulenceFrequency( 2.0f ),
  185. mWindTurbulenceStrength( 0.25f ),
  186. mWindRadius( 0 ),
  187. mRadialEmitter( false ),
  188. mHasMount( false ),
  189. mIsMounted( false ),
  190. mMountObject( NULL )
  191. {
  192. mTypeMask |= StaticObjectType | EnvironmentObjectType;
  193. if ( makeClientObject )
  194. mNetFlags.set( IsGhost );
  195. else
  196. mNetFlags.set( Ghostable | ScopeAlways );
  197. }
  198. ForestWindEmitter::~ForestWindEmitter()
  199. {
  200. }
  201. void ForestWindEmitter::initPersistFields()
  202. {
  203. // Initialise parents' persistent fields.
  204. Parent::initPersistFields();
  205. addGroup( "ForestWind" );
  206. addField( "windEnabled", TypeBool, Offset( mEnabled, ForestWindEmitter ), "Determines if the emitter will be counted in wind calculations." );
  207. addField( "radialEmitter", TypeBool, Offset( mRadialEmitter, ForestWindEmitter ), "Determines if the emitter is a global direction or local radial emitter." );
  208. addField( "strength", TypeF32, Offset( mWindStrength, ForestWindEmitter ), "The strength of the wind force." );
  209. addField( "radius", TypeF32, Offset( mWindRadius, ForestWindEmitter ), "The radius of the emitter for local radial emitters." );
  210. addField( "gustStrength", TypeF32, Offset( mWindGustStrength, ForestWindEmitter ), "The maximum strength of a gust." );
  211. addField( "gustFrequency", TypeF32, Offset( mWindGustFrequency, ForestWindEmitter ), "The frequency of gusting in seconds." );
  212. addField( "gustYawAngle", TypeF32, Offset( mWindGustYawAngle, ForestWindEmitter ), "The amount of degrees the wind direction can drift (both positive and negative)." );
  213. addField( "gustYawFrequency", TypeF32, Offset( mWindGustYawFrequency, ForestWindEmitter ), "The frequency of wind yaw drift, in seconds." );
  214. addField( "gustWobbleStrength", TypeF32, Offset( mWindGustWobbleStrength, ForestWindEmitter ), "The amount of random wobble added to gust and turbulence vectors." );
  215. addField( "turbulenceStrength", TypeF32, Offset( mWindTurbulenceStrength, ForestWindEmitter ), "The strength of gust turbulence." );
  216. addField( "turbulenceFrequency", TypeF32, Offset( mWindTurbulenceFrequency, ForestWindEmitter ), "The frequency of gust turbulence, in seconds." );
  217. addField( "hasMount", TypeBool, Offset( mHasMount, ForestWindEmitter ), "Determines if the emitter is mounted to another object." );
  218. endGroup( "ForestWind" );
  219. }
  220. U32 ForestWindEmitter::packUpdate(NetConnection * con, U32 mask, BitStream * stream)
  221. {
  222. U32 retMask = Parent::packUpdate( con, mask, stream );
  223. mathWrite( *stream, mObjToWorld );
  224. if ( stream->writeFlag( mask & EnabledMask ) )
  225. stream->writeFlag( mEnabled );
  226. if ( stream->writeFlag( mask & WindMask ) )
  227. {
  228. stream->write( mWindStrength );
  229. stream->write( mWindRadius );
  230. stream->writeFlag( mRadialEmitter );
  231. stream->write( mWindGustStrength );
  232. stream->write( mWindGustFrequency );
  233. stream->write( mWindGustYawAngle );
  234. stream->write( mWindGustYawFrequency );
  235. stream->write( mWindGustWobbleStrength );
  236. stream->write( mWindTurbulenceStrength );
  237. stream->write( mWindTurbulenceFrequency );
  238. // The wind direction should be normalized!
  239. if ( mWindDirection.isZero() )
  240. {
  241. VectorF forwardVec( 0, 0, 0 );
  242. mWorldToObj.getColumn( 1, &mWindDirection );
  243. }
  244. else
  245. mWindDirection.normalize();
  246. stream->writeNormalVector( mWindDirection, 8 );
  247. stream->writeFlag( mHasMount );
  248. }
  249. return retMask;
  250. }
  251. void ForestWindEmitter::unpackUpdate(NetConnection * con, BitStream * stream)
  252. {
  253. // Unpack Parent.
  254. Parent::unpackUpdate( con, stream );
  255. MatrixF xfm;
  256. mathRead( *stream, &xfm );
  257. Parent::setTransform( xfm );
  258. U32 windMask = 0;
  259. if ( stream->readFlag() ) // EnabledMask
  260. mEnabled = stream->readFlag();
  261. if ( stream->readFlag() ) // WindMask
  262. {
  263. stream->read( &mWindStrength );
  264. stream->read( &mWindRadius );
  265. mRadialEmitter = stream->readFlag();
  266. stream->read( &mWindGustStrength );
  267. stream->read( &mWindGustFrequency );
  268. stream->read( &mWindGustYawAngle );
  269. stream->read( &mWindGustYawFrequency );
  270. stream->read( &mWindGustWobbleStrength );
  271. stream->read( &mWindTurbulenceStrength );
  272. stream->read( &mWindTurbulenceFrequency );
  273. stream->readNormalVector( &mWindDirection, 8 );
  274. windMask |= WindMask;
  275. mHasMount = stream->readFlag();
  276. }
  277. // This does nothing if the masks are not set!
  278. if ( windMask != 0 && isProperlyAdded() )
  279. {
  280. Point3F boxRad( 0, 0, 0 );
  281. if ( !isRadialEmitter() )
  282. boxRad.set( 10000.0f, 10000.0f, 10000.0f );
  283. else
  284. boxRad.set( mWindRadius, mWindRadius, mWindRadius );
  285. mObjBox.set( -boxRad, boxRad );
  286. resetWorldBox();
  287. _initWind( windMask );
  288. }
  289. }
  290. bool ForestWindEmitter::onAdd()
  291. {
  292. if ( !Parent::onAdd() )
  293. return false;
  294. // Only the client side actually does wind.
  295. if ( isClientObject() )
  296. {
  297. // TODO: wasn't this a big hack we already fixed better?
  298. //Projectile::getGhostReceivedSignal().notify( this, &ForestWindEmitter::_onMountObjectGhostReceived );
  299. _initWind();
  300. WINDMGR->addEmitter( this );
  301. }
  302. Point3F boxRad( 0, 0, 0 );
  303. if ( !isRadialEmitter() )
  304. boxRad.set( 10000.0f, 10000.0f, 10000.0f );
  305. else
  306. boxRad.set( mWindRadius, mWindRadius, mWindRadius );
  307. mObjBox.set( -boxRad, boxRad );
  308. resetWorldBox();
  309. enableCollision();
  310. // If we are we editing the mission then
  311. // be sure to add us to the scene.
  312. if ( gEditingMission || mHasMount )
  313. {
  314. addToScene();
  315. mAddedToScene = true;
  316. }
  317. return true;
  318. }
  319. void ForestWindEmitter::onRemove()
  320. {
  321. // Only the client side actually does wind.
  322. if ( isClientObject() )
  323. {
  324. //Projectile::getGhostReceivedSignal().remove( this, &ForestWindEmitter::_onMountObjectGhostReceived );
  325. WINDMGR->removeEmitter( this );
  326. SAFE_DELETE( mWind );
  327. }
  328. // If we are editing the mission then remove
  329. // us from the scene graph.
  330. if ( gEditingMission || mHasMount )
  331. {
  332. removeFromScene();
  333. mAddedToScene = false;
  334. }
  335. // Do Parent.
  336. Parent::onRemove();
  337. }
  338. void ForestWindEmitter::_onMountObjectGhostReceived( SceneObject *object )
  339. {
  340. if ( !object )
  341. return;
  342. attachToObject( object );
  343. }
  344. void ForestWindEmitter::inspectPostApply()
  345. {
  346. // Force the client update!
  347. setMaskBits(0xffffffff);
  348. }
  349. void ForestWindEmitter::onEditorEnable()
  350. {
  351. if ( !mAddedToScene )
  352. {
  353. addToScene();
  354. mAddedToScene = true;
  355. }
  356. }
  357. void ForestWindEmitter::onEditorDisable()
  358. {
  359. // Remove us from the scene.
  360. if ( mAddedToScene )
  361. {
  362. removeFromScene();
  363. mAddedToScene = false;
  364. }
  365. }
  366. void ForestWindEmitter::_initWind( U32 mask )
  367. {
  368. AssertFatal( !isServerObject(), "SpeedWind is never updated on the server!" );
  369. // If we don't have a wind
  370. // object create one now.
  371. if ( !mWind )
  372. mWind = new ForestWind( this );
  373. // Do we need to apply a new direction and strength?
  374. if ( mask & WindMask )
  375. {
  376. mWorldToObj.getColumn( 1, &mWindDirection );
  377. mWind->setStrengthAndDirection( mWindStrength, mWindDirection );
  378. }
  379. }
  380. void ForestWindEmitter::setTransform( const MatrixF &mat )
  381. {
  382. Parent::setTransform( mat );
  383. // Force the client update!
  384. setMaskBits(0xffffffff);
  385. if ( isClientObject() )
  386. _initWind();
  387. }
  388. void ForestWindEmitter::prepRenderImage( SceneRenderState* state )
  389. {
  390. PROFILE_SCOPE( ForestWindEmitter_PrepRenderImage );
  391. // Only render the radius and
  392. // direction if we're in the editor.
  393. // Don't render them if this is a reflect pass.
  394. if ( !state->isDiffusePass() || !gEditingMission )
  395. return;
  396. // This should be sufficient for most objects that don't manage zones, and
  397. // don't need to return a specialized RenderImage...
  398. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  399. ri->renderDelegate.bind( this, &ForestWindEmitter::_renderEmitterInfo );
  400. ri->type = RenderPassManager::RIT_Editor;
  401. state->getRenderPass()->addInst( ri );
  402. }
  403. void ForestWindEmitter::_renderEmitterInfo( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat )
  404. {
  405. if ( overrideMat )
  406. return;
  407. GFXTransformSaver saver;
  408. GFXDrawUtil *drawer = GFX->getDrawUtil();
  409. AssertFatal( drawer, "Got NULL GFXDrawUtil!" );
  410. const Point3F &pos = getPosition();
  411. const VectorF &windVec = mWind->getDirection();
  412. GFXStateBlockDesc desc;
  413. desc.setBlend( true );
  414. desc.setZReadWrite( true, false );
  415. // Draw an arrow pointing
  416. // in the wind direction.
  417. 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 ) );//
  418. drawer->drawArrow( desc, pos, pos + (mWind->getTarget() * mWindStrength ), ColorI( 255, 0, 0, 85 ) );
  419. S32 useRadius = mWindRadius;
  420. // Draw a 2D circle for the wind radius.
  421. if ( isRadialEmitter() )
  422. {
  423. // If the camera is close to the sphere, shrink the sphere so it remains visible.
  424. GameConnection* gc = GameConnection::getConnectionToServer();
  425. GameBase *gb = gc ? gc->getCameraObject() : NULL;
  426. if (gb)
  427. {
  428. F32 camDist = (gb->getPosition() - getPosition()).len();
  429. if ( camDist < mWindRadius )
  430. useRadius = camDist;
  431. }
  432. drawer->drawSphere( desc, useRadius, pos, ColorI( 255, 0, 0, 80 ) );
  433. }
  434. }
  435. F32 ForestWindEmitter::getStrength() const
  436. {
  437. return mWind->getStrength();
  438. }
  439. void ForestWindEmitter::setStrength( F32 strength )
  440. {
  441. mWindStrength = strength;
  442. mWind->setStrength( mWindStrength );
  443. }
  444. void ForestWindEmitter::attachToObject( SceneObject *obj )
  445. {
  446. if ( !obj )
  447. return;
  448. mMountObject = obj;
  449. mIsMounted = true;
  450. if ( isServerObject() )
  451. deleteNotify( mMountObject );
  452. }
  453. void ForestWindEmitter::updateMountPosition()
  454. {
  455. AssertFatal( isClientObject(), "ForestWindEmitter::updateMountPosition - This should only happen on the client!" );
  456. if ( !mHasMount || !mMountObject )
  457. return;
  458. MatrixF mat( true );
  459. mat.setPosition( mMountObject->getPosition() );
  460. Parent::setTransform( mat );
  461. }
  462. void ForestWindEmitter::onDeleteNotify(SimObject *object)
  463. {
  464. AssertFatal( isServerObject(), "ForestWindEmitter::onDeleteNotify - This should never happen on the client!" );
  465. safeDeleteObject();
  466. }
  467. DefineEngineMethod( ForestWindEmitter, attachToObject, void, ( U32 objectID ),,
  468. "@brief Mounts the wind emitter to another scene object\n\n"
  469. "@param objectID Unique ID of the object wind emitter should attach to"
  470. "@tsexample\n"
  471. "// Wind emitter previously created and named %windEmitter\n"
  472. "// Going to attach it to the player, making him a walking wind storm\n"
  473. "%windEmitter.attachToObject(%player);\n"
  474. "@endtsexample\n\n")
  475. {
  476. SceneObject *obj = dynamic_cast<SceneObject*>( Sim::findObject( objectID ) );
  477. if ( !obj )
  478. Con::warnf( "ForestWindEmitter::attachToObject - failed to find object with ID: %d", objectID );
  479. object->attachToObject( obj );
  480. }