forestWindEmitter.cpp 18 KB

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