particleEmitterNode.cpp 13 KB

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