particleEmitterNode.cpp 13 KB

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