forestWindEmitter.cpp 18 KB

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