particleEmitterNode.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 "particleEmitterNode.h"
  23. #include "console/consoleTypes.h"
  24. #include "core/stream/bitStream.h"
  25. #include "T3D/fx/particleEmitter.h"
  26. #include "math/mathIO.h"
  27. #include "sim/netConnection.h"
  28. #include "console/engineAPI.h"
  29. IMPLEMENT_CO_DATABLOCK_V1(ParticleEmitterNodeData);
  30. IMPLEMENT_CO_NETOBJECT_V1(ParticleEmitterNode);
  31. ConsoleDocClass( ParticleEmitterNodeData,
  32. "@brief Contains additional data to be associated with a ParticleEmitterNode."
  33. "@ingroup FX\n"
  34. );
  35. ConsoleDocClass( ParticleEmitterNode,
  36. "@brief A particle emitter object that can be positioned in the world and "
  37. "dynamically enabled or disabled.\n\n"
  38. "@tsexample\n"
  39. "datablock ParticleEmitterNodeData( SimpleEmitterNodeData )\n"
  40. "{\n"
  41. " timeMultiple = 1.0;\n"
  42. "};\n\n"
  43. "%emitter = new ParticleEmitterNode()\n"
  44. "{\n"
  45. " datablock = SimpleEmitterNodeData;\n"
  46. " active = true;\n"
  47. " emitter = FireEmitterData;\n"
  48. " velocity = 3.5;\n"
  49. "};\n\n"
  50. "// Dynamically change emitter datablock\n"
  51. "%emitter.setEmitterDataBlock( DustEmitterData );\n"
  52. "@endtsexample\n"
  53. "@note To change the emitter field dynamically (after the ParticleEmitterNode "
  54. "object has been created) you must use the setEmitterDataBlock() method or the "
  55. "change will not be replicated to other clients in the game.\n"
  56. "Similarly, use the setActive() method instead of changing the active field "
  57. "directly. When changing velocity, you need to toggle setActive() on and off "
  58. "to force the state change to be transmitted to other clients.\n\n"
  59. "@ingroup FX\n"
  60. "@see ParticleEmitterNodeData\n"
  61. "@see ParticleEmitterData\n"
  62. );
  63. //-----------------------------------------------------------------------------
  64. // ParticleEmitterNodeData
  65. //-----------------------------------------------------------------------------
  66. ParticleEmitterNodeData::ParticleEmitterNodeData()
  67. {
  68. timeMultiple = 1.0;
  69. }
  70. ParticleEmitterNodeData::~ParticleEmitterNodeData()
  71. {
  72. }
  73. //-----------------------------------------------------------------------------
  74. // initPersistFields
  75. //-----------------------------------------------------------------------------
  76. void ParticleEmitterNodeData::initPersistFields()
  77. {
  78. addField( "timeMultiple", TYPEID< F32 >(), Offset(timeMultiple, ParticleEmitterNodeData),
  79. "@brief Time multiplier for particle emitter nodes.\n\n"
  80. "Increasing timeMultiple is like running the emitter at a faster rate - single-shot "
  81. "emitters will complete in a shorter time, and continuous emitters will generate "
  82. "particles more quickly.\n\n"
  83. "Valid range is 0.01 - 100." );
  84. Parent::initPersistFields();
  85. }
  86. //-----------------------------------------------------------------------------
  87. // onAdd
  88. //-----------------------------------------------------------------------------
  89. bool ParticleEmitterNodeData::onAdd()
  90. {
  91. if( !Parent::onAdd() )
  92. return false;
  93. if( timeMultiple < 0.01 || timeMultiple > 100 )
  94. {
  95. Con::warnf("ParticleEmitterNodeData::onAdd(%s): timeMultiple must be between 0.01 and 100", getName());
  96. timeMultiple = timeMultiple < 0.01 ? 0.01 : 100;
  97. }
  98. return true;
  99. }
  100. //-----------------------------------------------------------------------------
  101. // preload
  102. //-----------------------------------------------------------------------------
  103. bool ParticleEmitterNodeData::preload(bool server, String &errorStr)
  104. {
  105. if( Parent::preload(server, errorStr) == false )
  106. return false;
  107. return true;
  108. }
  109. //-----------------------------------------------------------------------------
  110. // packData
  111. //-----------------------------------------------------------------------------
  112. void ParticleEmitterNodeData::packData(BitStream* stream)
  113. {
  114. Parent::packData(stream);
  115. stream->write(timeMultiple);
  116. }
  117. //-----------------------------------------------------------------------------
  118. // unpackData
  119. //-----------------------------------------------------------------------------
  120. void ParticleEmitterNodeData::unpackData(BitStream* stream)
  121. {
  122. Parent::unpackData(stream);
  123. stream->read(&timeMultiple);
  124. }
  125. //-----------------------------------------------------------------------------
  126. // ParticleEmitterNode
  127. //-----------------------------------------------------------------------------
  128. ParticleEmitterNode::ParticleEmitterNode()
  129. {
  130. // Todo: ScopeAlways?
  131. mNetFlags.set(Ghostable);
  132. mTypeMask |= EnvironmentObjectType;
  133. mActive = true;
  134. mDataBlock = NULL;
  135. mEmitterDatablock = NULL;
  136. mEmitterDatablockId = 0;
  137. mEmitter = NULL;
  138. mVelocity = 1.0;
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Destructor
  142. //-----------------------------------------------------------------------------
  143. ParticleEmitterNode::~ParticleEmitterNode()
  144. {
  145. //
  146. }
  147. //-----------------------------------------------------------------------------
  148. // initPersistFields
  149. //-----------------------------------------------------------------------------
  150. void ParticleEmitterNode::initPersistFields()
  151. {
  152. addField( "active", TYPEID< bool >(), Offset(mActive,ParticleEmitterNode),
  153. "Controls whether particles are emitted from this node." );
  154. addField( "emitter", TYPEID< ParticleEmitterData >(), Offset(mEmitterDatablock, ParticleEmitterNode),
  155. "Datablock to use when emitting particles." );
  156. addField( "velocity", TYPEID< F32 >(), Offset(mVelocity, ParticleEmitterNode),
  157. "Velocity to use when emitting particles (in the direction of the "
  158. "ParticleEmitterNode object's up (Z) axis)." );
  159. Parent::initPersistFields();
  160. }
  161. //-----------------------------------------------------------------------------
  162. // onAdd
  163. //-----------------------------------------------------------------------------
  164. bool ParticleEmitterNode::onAdd()
  165. {
  166. if( !Parent::onAdd() )
  167. return false;
  168. if( !mEmitterDatablock && mEmitterDatablockId != 0 )
  169. {
  170. if( Sim::findObject(mEmitterDatablockId, mEmitterDatablock) == false )
  171. Con::errorf(ConsoleLogEntry::General, "ParticleEmitterNode::onAdd: Invalid packet, bad datablockId(mEmitterDatablock): %d", mEmitterDatablockId);
  172. }
  173. if( isClientObject() )
  174. {
  175. setEmitterDataBlock( mEmitterDatablock );
  176. }
  177. else
  178. {
  179. setMaskBits( StateMask | EmitterDBMask );
  180. }
  181. mObjBox.minExtents.set(-0.5, -0.5, -0.5);
  182. mObjBox.maxExtents.set( 0.5, 0.5, 0.5);
  183. resetWorldBox();
  184. addToScene();
  185. return true;
  186. }
  187. //-----------------------------------------------------------------------------
  188. // onRemove
  189. //-----------------------------------------------------------------------------
  190. void ParticleEmitterNode::onRemove()
  191. {
  192. removeFromScene();
  193. if( isClientObject() )
  194. {
  195. if( mEmitter )
  196. {
  197. mEmitter->deleteWhenEmpty();
  198. mEmitter = NULL;
  199. }
  200. }
  201. Parent::onRemove();
  202. }
  203. //-----------------------------------------------------------------------------
  204. // onNewDataBlock
  205. //-----------------------------------------------------------------------------
  206. bool ParticleEmitterNode::onNewDataBlock( GameBaseData *dptr, bool reload )
  207. {
  208. mDataBlock = dynamic_cast<ParticleEmitterNodeData*>( dptr );
  209. if ( !mDataBlock || !Parent::onNewDataBlock( dptr, reload ) )
  210. return false;
  211. // Todo: Uncomment if this is a "leaf" class
  212. scriptOnNewDataBlock();
  213. return true;
  214. }
  215. //-----------------------------------------------------------------------------
  216. void ParticleEmitterNode::inspectPostApply()
  217. {
  218. Parent::inspectPostApply();
  219. setMaskBits(StateMask | EmitterDBMask);
  220. }
  221. //-----------------------------------------------------------------------------
  222. // advanceTime
  223. //-----------------------------------------------------------------------------
  224. void ParticleEmitterNode::processTick(const Move* move)
  225. {
  226. Parent::processTick(move);
  227. if ( isMounted() )
  228. {
  229. MatrixF mat;
  230. mMount.object->getMountTransform( mMount.node, mMount.xfm, &mat );
  231. setTransform( mat );
  232. }
  233. }
  234. void ParticleEmitterNode::advanceTime(F32 dt)
  235. {
  236. Parent::advanceTime(dt);
  237. if(!mActive || mEmitter.isNull() || !mDataBlock)
  238. return;
  239. Point3F emitPoint, emitVelocity;
  240. Point3F emitAxis(0, 0, 1);
  241. getTransform().mulV(emitAxis);
  242. getTransform().getColumn(3, &emitPoint);
  243. emitVelocity = emitAxis * mVelocity;
  244. mEmitter->emitParticles(emitPoint, emitPoint,
  245. emitAxis,
  246. emitVelocity, (U32)(dt * mDataBlock->timeMultiple * 1000.0f));
  247. }
  248. //-----------------------------------------------------------------------------
  249. // packUpdate
  250. //-----------------------------------------------------------------------------
  251. U32 ParticleEmitterNode::packUpdate(NetConnection* con, U32 mask, BitStream* stream)
  252. {
  253. U32 retMask = Parent::packUpdate(con, mask, stream);
  254. if ( stream->writeFlag( mask & InitialUpdateMask ) )
  255. {
  256. mathWrite(*stream, getTransform());
  257. mathWrite(*stream, getScale());
  258. }
  259. if ( stream->writeFlag( mask & EmitterDBMask ) )
  260. {
  261. if( stream->writeFlag(mEmitterDatablock != NULL) )
  262. {
  263. stream->writeRangedU32(mEmitterDatablock->getId(), DataBlockObjectIdFirst,
  264. DataBlockObjectIdLast);
  265. }
  266. }
  267. if ( stream->writeFlag( mask & StateMask ) )
  268. {
  269. stream->writeFlag( mActive );
  270. stream->write( mVelocity );
  271. }
  272. return retMask;
  273. }
  274. //-----------------------------------------------------------------------------
  275. // unpackUpdate
  276. //-----------------------------------------------------------------------------
  277. void ParticleEmitterNode::unpackUpdate(NetConnection* con, BitStream* stream)
  278. {
  279. Parent::unpackUpdate(con, stream);
  280. if ( stream->readFlag() )
  281. {
  282. MatrixF temp;
  283. Point3F tempScale;
  284. mathRead(*stream, &temp);
  285. mathRead(*stream, &tempScale);
  286. setScale(tempScale);
  287. setTransform(temp);
  288. }
  289. if ( stream->readFlag() )
  290. {
  291. mEmitterDatablockId = stream->readFlag() ?
  292. stream->readRangedU32(DataBlockObjectIdFirst, DataBlockObjectIdLast) : 0;
  293. ParticleEmitterData *emitterDB = NULL;
  294. Sim::findObject( mEmitterDatablockId, emitterDB );
  295. if ( isProperlyAdded() )
  296. setEmitterDataBlock( emitterDB );
  297. }
  298. if ( stream->readFlag() )
  299. {
  300. mActive = stream->readFlag();
  301. stream->read( &mVelocity );
  302. }
  303. }
  304. void ParticleEmitterNode::setEmitterDataBlock(ParticleEmitterData* data)
  305. {
  306. if ( isServerObject() )
  307. {
  308. setMaskBits( EmitterDBMask );
  309. }
  310. else
  311. {
  312. ParticleEmitter* pEmitter = NULL;
  313. if ( data )
  314. {
  315. // Create emitter with new datablock
  316. pEmitter = new ParticleEmitter;
  317. pEmitter->onNewDataBlock( data, false );
  318. if( pEmitter->registerObject() == false )
  319. {
  320. Con::warnf(ConsoleLogEntry::General, "Could not register base emitter for particle of class: %s", data->getName() ? data->getName() : data->getIdString() );
  321. delete pEmitter;
  322. return;
  323. }
  324. }
  325. // Replace emitter
  326. if ( mEmitter )
  327. mEmitter->deleteWhenEmpty();
  328. mEmitter = pEmitter;
  329. }
  330. mEmitterDatablock = data;
  331. }
  332. DefineEngineMethod(ParticleEmitterNode, setEmitterDataBlock, void, (ParticleEmitterData* emitterDatablock), (nullAsType<ParticleEmitterData*>()),
  333. "Assigns the datablock for this emitter node.\n"
  334. "@param emitterDatablock ParticleEmitterData datablock to assign\n"
  335. "@tsexample\n"
  336. "// Assign a new emitter datablock\n"
  337. "%emitter.setEmitterDatablock( %emitterDatablock );\n"
  338. "@endtsexample\n" )
  339. {
  340. if ( !emitterDatablock )
  341. {
  342. Con::errorf("ParticleEmitterData datablock could not be found when calling setEmitterDataBlock in particleEmitterNode.");
  343. return;
  344. }
  345. object->setEmitterDataBlock(emitterDatablock);
  346. }
  347. DefineEngineMethod(ParticleEmitterNode, setActive, void, (bool active),,
  348. "Turns the emitter on or off.\n"
  349. "@param active New emitter state\n" )
  350. {
  351. object->setActive( active );
  352. }