afxParticleEmitter.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //
  23. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  24. #include "afx/arcaneFX.h"
  25. #include "math/mathIO.h"
  26. #include "scene/sceneManager.h"
  27. #include "T3D/gameBase/gameProcess.h"
  28. #include "afx/util/afxPath.h"
  29. #include "afx/util/afxPath3D.h"
  30. #include "afx/ce/afxParticleEmitter.h"
  31. IMPLEMENT_CO_DATABLOCK_V1(afxParticleEmitterData);
  32. ConsoleDocClass( afxParticleEmitterData,
  33. "@brief A base datablock inherited by AFX Particle Emitter effects.\n\n"
  34. "A base datablock inherited by AFX Particle Emitter effects."
  35. "\n\n"
  36. "@ingroup afxEffects\n"
  37. "@ingroup AFX\n"
  38. "@ingroup Datablocks\n"
  39. );
  40. afxParticleEmitterData::afxParticleEmitterData()
  41. {
  42. fade_velocity = false; // coordinate velocity amount with fade amout
  43. fade_offset = false; // coordinate ejection-offset amount with fade amount
  44. pe_vector.set(0.0,0.0,0.0);
  45. pe_vector_is_world = false;
  46. tpaths_string = ST_NULLSTRING;
  47. tPathDataBlocks.clear();
  48. tPathDataBlockIds.clear();
  49. }
  50. afxParticleEmitterData::afxParticleEmitterData(const afxParticleEmitterData& other, bool temp_clone) : ParticleEmitterData(other, temp_clone)
  51. {
  52. fade_velocity = other.fade_velocity;
  53. fade_offset = other.fade_offset;
  54. pe_vector = other.pe_vector;
  55. pe_vector_is_world = other.pe_vector_is_world;
  56. tpaths_string = other.tpaths_string;
  57. tPathDataBlocks = other.tPathDataBlocks;
  58. //tPathDataBlockIds = other.tPathDataBlockIds;
  59. }
  60. void afxParticleEmitterData::initPersistFields()
  61. {
  62. docsURL;
  63. addField("fadeVelocity", TypeBool, Offset(fade_velocity, afxParticleEmitterData),
  64. "If true, the initial velocity of emitted particles is multiplied by the fade amount "
  65. "of the containing effect wrapper. As the effect fades-in and out, so does the "
  66. "initial velocity of new particles.");
  67. addField("fadeOffset", TypeBool, Offset(fade_offset, afxParticleEmitterData),
  68. "If true, the ejection offset of emitted particles is multiplied by the fade amount "
  69. "of the containing effect wrapper. As the effect fades-in and out, so does the "
  70. "ejection offset of new particles.");
  71. addField("vector", TypePoint3F, Offset(pe_vector, afxParticleEmitterData),
  72. "General direction vector used for emitting particles. Its exact interpretation is "
  73. "determined by the particle emitter subclass.");
  74. addField("vectorIsWorld", TypeBool, Offset(pe_vector_is_world, afxParticleEmitterData),
  75. "Sets whether the vector field should be interpreted as a vector in the world "
  76. "coordinate system.");
  77. addField("pathsTransform", TypeString, Offset(tpaths_string, afxParticleEmitterData),
  78. "A string of paths to be used as transform paths. Each path name must reference an "
  79. "afxPathData datablock. Transform paths are used to translate particles along a given "
  80. "path or series of paths.");
  81. Parent::initPersistFields();
  82. }
  83. void afxParticleEmitterData::packData(BitStream* stream)
  84. {
  85. Parent::packData(stream);
  86. stream->writeFlag(fade_velocity);
  87. stream->writeFlag(fade_offset);
  88. mathWrite(*stream, pe_vector);
  89. stream->writeFlag(pe_vector_is_world);
  90. stream->write(tPathDataBlockIds.size());
  91. for (int i = 0; i < tPathDataBlockIds.size(); i++)
  92. stream->write(tPathDataBlockIds[i]);
  93. }
  94. void afxParticleEmitterData::unpackData(BitStream* stream)
  95. {
  96. Parent::unpackData(stream);
  97. fade_velocity = stream->readFlag();
  98. fade_offset = stream->readFlag();
  99. mathRead(*stream, &pe_vector);
  100. pe_vector_is_world = stream->readFlag();
  101. U32 n_db; stream->read(&n_db);
  102. tPathDataBlockIds.setSize(n_db);
  103. for (U32 i = 0; i < n_db; i++)
  104. stream->read(&tPathDataBlockIds[i]);
  105. }
  106. bool afxParticleEmitterData::onAdd()
  107. {
  108. if( Parent::onAdd() == false )
  109. return false;
  110. if (tpaths_string != ST_NULLSTRING && tpaths_string[0] == '\0')
  111. {
  112. Con::warnf(ConsoleLogEntry::General, "ParticleEmitterData(%s) empty transform paths string.", getName());
  113. return false;
  114. }
  115. if (tpaths_string != ST_NULLSTRING && dStrlen(tpaths_string) > 255)
  116. {
  117. Con::errorf(ConsoleLogEntry::General, "ParticleEmitterData(%s) transform paths string too long [> 255 chars].", getName());
  118. return false;
  119. }
  120. if (tpaths_string != ST_NULLSTRING)
  121. {
  122. Vector<char*> dataBlocks(__FILE__, __LINE__);
  123. dsize_t tokCopyLen = dStrlen(tpaths_string) + 1;
  124. char* tokCopy = new char[tokCopyLen];
  125. dStrcpy(tokCopy, tpaths_string, tokCopyLen);
  126. char* currTok = dStrtok(tokCopy, " \t");
  127. while (currTok != NULL)
  128. {
  129. dataBlocks.push_back(currTok);
  130. currTok = dStrtok(NULL, " \t");
  131. }
  132. if (dataBlocks.size() == 0)
  133. {
  134. Con::warnf(ConsoleLogEntry::General, "ParticleEmitterData(%s) invalid transform paths string. No datablocks found", getName());
  135. delete [] tokCopy;
  136. return false;
  137. }
  138. tPathDataBlocks.clear();
  139. tPathDataBlockIds.clear();
  140. for (U32 i = 0; i < dataBlocks.size(); i++)
  141. {
  142. afxPathData* pData = NULL;
  143. if (Sim::findObject(dataBlocks[i], pData) == false)
  144. {
  145. Con::warnf(ConsoleLogEntry::General, "ParticleEmitterData(%s) unable to find transform path datablock: %s", getName(), dataBlocks[i]);
  146. }
  147. else
  148. {
  149. tPathDataBlocks.push_back(pData);
  150. tPathDataBlockIds.push_back(pData->getId());
  151. }
  152. }
  153. delete [] tokCopy;
  154. if (tPathDataBlocks.size() == 0)
  155. {
  156. Con::warnf(ConsoleLogEntry::General, "ParticleEmitterData(%s) unable to find any transform path datablocks", getName());
  157. return false;
  158. }
  159. }
  160. return true;
  161. }
  162. bool afxParticleEmitterData::preload(bool server, String &errorStr)
  163. {
  164. if (Parent::preload(server, errorStr) == false)
  165. return false;
  166. tPathDataBlocks.clear();
  167. for (U32 i = 0; i < tPathDataBlockIds.size(); i++)
  168. {
  169. afxPathData* pData = NULL;
  170. if (Sim::findObject(tPathDataBlockIds[i], pData) == false)
  171. {
  172. Con::warnf(ConsoleLogEntry::General,
  173. "ParticleEmitterData(%s) unable to find transform path datablock: %d",
  174. getName(), tPathDataBlockIds[i]);
  175. }
  176. else
  177. tPathDataBlocks.push_back(pData);
  178. }
  179. return true;
  180. }
  181. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  182. // VECTOR
  183. IMPLEMENT_CO_DATABLOCK_V1(afxParticleEmitterVectorData);
  184. ConsoleDocClass( afxParticleEmitterVectorData,
  185. "@brief An AFX customized particle emitter that emits particles along a 3D vector.\n\n"
  186. "An AFX customized particle emitter that emits particles along a 3D vector."
  187. "\n\n"
  188. "@ingroup afxEffects\n"
  189. "@ingroup AFX\n"
  190. "@ingroup Datablocks\n"
  191. );
  192. afxParticleEmitterVectorData::afxParticleEmitterVectorData()
  193. {
  194. }
  195. afxParticleEmitterVectorData::afxParticleEmitterVectorData(const afxParticleEmitterVectorData& other, bool temp_clone) : afxParticleEmitterData(other, temp_clone)
  196. {
  197. }
  198. void afxParticleEmitterVectorData::initPersistFields()
  199. {
  200. docsURL;
  201. Parent::initPersistFields();
  202. }
  203. void afxParticleEmitterVectorData::packData(BitStream* stream)
  204. {
  205. Parent::packData(stream);
  206. }
  207. void afxParticleEmitterVectorData::unpackData(BitStream* stream)
  208. {
  209. Parent::unpackData(stream);
  210. }
  211. bool afxParticleEmitterVectorData::onAdd()
  212. {
  213. if (Parent::onAdd() == false)
  214. return false;
  215. return true;
  216. }
  217. bool afxParticleEmitterVectorData::preload(bool server, String &errorStr)
  218. {
  219. if (Parent::preload(server, errorStr) == false)
  220. return false;
  221. return true;
  222. }
  223. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  224. // CONE
  225. IMPLEMENT_CO_DATABLOCK_V1(afxParticleEmitterConeData);
  226. ConsoleDocClass( afxParticleEmitterConeData,
  227. "@brief An AFX customized particle emitter that emits particles within a cone shape.\n\n"
  228. "An AFX customized particle emitter that emits particles within a cone shape."
  229. "\n\n"
  230. "@ingroup afxEffects\n"
  231. "@ingroup AFX\n"
  232. "@ingroup Datablocks\n"
  233. );
  234. afxParticleEmitterConeData::afxParticleEmitterConeData()
  235. {
  236. spread_min = 0.0f;
  237. spread_max = 90.0f;
  238. }
  239. afxParticleEmitterConeData::afxParticleEmitterConeData(const afxParticleEmitterConeData& other, bool temp_clone) : afxParticleEmitterData(other, temp_clone)
  240. {
  241. spread_min = other.spread_min;
  242. spread_max = other.spread_max;
  243. }
  244. void afxParticleEmitterConeData::initPersistFields()
  245. {
  246. docsURL;
  247. addField("spreadMin", TypeF32, Offset(spread_min, afxParticleEmitterConeData),
  248. "...");
  249. addField("spreadMax", TypeF32, Offset(spread_max, afxParticleEmitterConeData),
  250. "...");
  251. Parent::initPersistFields();
  252. }
  253. void afxParticleEmitterConeData::packData(BitStream* stream)
  254. {
  255. Parent::packData(stream);
  256. stream->writeRangedU32((U32)spread_min, 0, 180);
  257. stream->writeRangedU32((U32)spread_max, 0, 180);
  258. }
  259. void afxParticleEmitterConeData::unpackData(BitStream* stream)
  260. {
  261. Parent::unpackData(stream);
  262. spread_min = stream->readRangedU32(0, 180);
  263. spread_max = stream->readRangedU32(0, 180);
  264. }
  265. bool afxParticleEmitterConeData::onAdd()
  266. {
  267. if( Parent::onAdd() == false )
  268. return false;
  269. if (spread_min < 0.0f)
  270. {
  271. Con::warnf(ConsoleLogEntry::General, "ParticleEmitterData(%s) spreadMin < 0.0", getName());
  272. spread_min = 0.0f;
  273. }
  274. if (spread_max > 180.0f)
  275. {
  276. Con::warnf(ConsoleLogEntry::General, "ParticleEmitterData(%s) spreadMax > 180.0f", getName());
  277. spread_max = 180.0f;
  278. }
  279. if (spread_max > 179.5f)
  280. spread_max = 179.5f;
  281. if (spread_min > 179.5f)
  282. spread_min = 179.5f;
  283. if (spread_min > spread_max)
  284. {
  285. Con::warnf(ConsoleLogEntry::General, "ParticleEmitterData(%s) spreadMin > spreadMax", getName());
  286. spread_min = spread_max;
  287. }
  288. return true;
  289. }
  290. bool afxParticleEmitterConeData::preload(bool server, String &errorStr)
  291. {
  292. if (Parent::preload(server, errorStr) == false)
  293. return false;
  294. return true;
  295. }
  296. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  297. // PATH
  298. IMPLEMENT_CO_DATABLOCK_V1(afxParticleEmitterPathData);
  299. ConsoleDocClass( afxParticleEmitterPathData,
  300. "@brief An AFX customized particle emitter that emits particles along a path.\n\n"
  301. "An AFX customized particle emitter that emits particles along a path."
  302. "\n\n"
  303. "@ingroup afxEffects\n"
  304. "@ingroup AFX\n"
  305. "@ingroup Datablocks\n"
  306. );
  307. afxParticleEmitterPathData::afxParticleEmitterPathData()
  308. {
  309. epaths_string = ST_NULLSTRING;
  310. epathDataBlocks.clear();
  311. epathDataBlockIds.clear();
  312. path_origin_type = PATHEMIT_ORIGIN;
  313. ground_conform = false;
  314. ground_conform_terrain = true;
  315. ground_conform_interiors = true;
  316. ground_conform_height = 0.0f;
  317. }
  318. afxParticleEmitterPathData::afxParticleEmitterPathData(const afxParticleEmitterPathData& other, bool temp_clone) : afxParticleEmitterData(other, temp_clone)
  319. {
  320. epaths_string = other.epaths_string;
  321. epathDataBlocks = other.epathDataBlocks;
  322. //epathDataBlockIds = other.epathDataBlockIds;
  323. path_origin_type = other.path_origin_type;
  324. ground_conform = other.ground_conform;
  325. ground_conform_terrain = other.ground_conform_terrain;
  326. ground_conform_interiors = other.ground_conform_interiors;
  327. ground_conform_height = other.ground_conform_height;
  328. }
  329. ImplementEnumType( afxParticleEmitterPath_OriginType, "Possible particle emitter path origin types.\n" "@ingroup afxParticleEmitterPath\n\n" )
  330. { afxParticleEmitterPathData::PATHEMIT_ORIGIN, "origin", "..." },
  331. { afxParticleEmitterPathData::PATHEMIT_POINT, "point", "..." },
  332. { afxParticleEmitterPathData::PATHEMIT_VECTOR, "vector", "..." },
  333. { afxParticleEmitterPathData::PATHEMIT_TANGENT, "tangent", "..." },
  334. EndImplementEnumType;
  335. void afxParticleEmitterPathData::initPersistFields()
  336. {
  337. docsURL;
  338. addField("paths", TypeString, Offset(epaths_string, afxParticleEmitterPathData),
  339. "...");
  340. addField("pathOrigin", TYPEID<afxParticleEmitterPathData::PathOriginType>(), Offset(path_origin_type, afxParticleEmitterPathData),
  341. "...");
  342. // JTF Note: take a look at these and make sure they are ok.
  343. addField("groundConform", TypeBool, Offset(ground_conform, afxParticleEmitterPathData),
  344. "...");
  345. addField("groundConformTerrain", TypeBool, Offset(ground_conform_terrain, afxParticleEmitterPathData),
  346. "...");
  347. addField("groundConformInteriors", TypeBool, Offset(ground_conform_interiors, afxParticleEmitterPathData),
  348. "...");
  349. addField("groundConformHeight", TypeF32, Offset(ground_conform_height, afxParticleEmitterPathData),
  350. "...");
  351. Parent::initPersistFields();
  352. }
  353. void afxParticleEmitterPathData::packData(BitStream* stream)
  354. {
  355. Parent::packData(stream);
  356. stream->write(epathDataBlockIds.size());
  357. for (int i = 0; i < epathDataBlockIds.size(); i++)
  358. stream->write(epathDataBlockIds[i]);
  359. stream->write(path_origin_type);
  360. stream->writeFlag(ground_conform);
  361. stream->writeFlag(ground_conform_terrain);
  362. stream->writeFlag(ground_conform_interiors);
  363. stream->write(ground_conform_height);
  364. }
  365. void afxParticleEmitterPathData::unpackData(BitStream* stream)
  366. {
  367. Parent::unpackData(stream);
  368. U32 n_db; stream->read(&n_db);
  369. epathDataBlockIds.setSize(n_db);
  370. for (U32 i = 0; i < n_db; i++)
  371. stream->read(&epathDataBlockIds[i]);
  372. stream->read(&path_origin_type);
  373. ground_conform = stream->readFlag();
  374. ground_conform_terrain = stream->readFlag();
  375. ground_conform_interiors = stream->readFlag();
  376. stream->read(&ground_conform_height);
  377. }
  378. bool afxParticleEmitterPathData::onAdd()
  379. {
  380. if( Parent::onAdd() == false )
  381. return false;
  382. // path
  383. if (epaths_string != ST_NULLSTRING && epaths_string[0] == '\0')
  384. {
  385. Con::warnf(ConsoleLogEntry::General, "afxParticleEmitterPathData(%s) empty paths string.", getName());
  386. return false;
  387. }
  388. if (epaths_string != ST_NULLSTRING && dStrlen(epaths_string) > 255)
  389. {
  390. Con::errorf(ConsoleLogEntry::General, "afxParticleEmitterPathData(%s) paths string too long [> 255 chars].", getName());
  391. return false;
  392. }
  393. if (epaths_string != ST_NULLSTRING)
  394. {
  395. Vector<char*> dataBlocks(__FILE__, __LINE__);
  396. dsize_t tokCopyLen = dStrlen(epaths_string) + 1;
  397. char* tokCopy = new char[tokCopyLen];
  398. dStrcpy(tokCopy, epaths_string, tokCopyLen);
  399. char* currTok = dStrtok(tokCopy, " \t");
  400. while (currTok != NULL)
  401. {
  402. dataBlocks.push_back(currTok);
  403. currTok = dStrtok(NULL, " \t");
  404. }
  405. if (dataBlocks.size() == 0)
  406. {
  407. Con::warnf(ConsoleLogEntry::General, "afxParticleEmitterPathData(%s) invalid paths string. No datablocks found", getName());
  408. delete [] tokCopy;
  409. return false;
  410. }
  411. epathDataBlocks.clear();
  412. epathDataBlockIds.clear();
  413. for (U32 i = 0; i < dataBlocks.size(); i++)
  414. {
  415. afxPathData* pData = NULL;
  416. if (Sim::findObject(dataBlocks[i], pData) == false)
  417. {
  418. Con::warnf(ConsoleLogEntry::General, "afxParticleEmitterPathData(%s) unable to find path datablock: %s", getName(), dataBlocks[i]);
  419. }
  420. else
  421. {
  422. epathDataBlocks.push_back(pData);
  423. epathDataBlockIds.push_back(pData->getId());
  424. }
  425. }
  426. delete [] tokCopy;
  427. if (epathDataBlocks.size() == 0)
  428. {
  429. Con::warnf(ConsoleLogEntry::General, "afxParticleEmitterPathData(%s) unable to find any path datablocks", getName());
  430. return false;
  431. }
  432. }
  433. return true;
  434. }
  435. bool afxParticleEmitterPathData::preload(bool server, String &errorStr)
  436. {
  437. if (Parent::preload(server, errorStr) == false)
  438. return false;
  439. epathDataBlocks.clear();
  440. for (U32 i = 0; i < epathDataBlockIds.size(); i++)
  441. {
  442. afxPathData* pData = NULL;
  443. if (Sim::findObject(epathDataBlockIds[i], pData) == false)
  444. {
  445. Con::warnf(ConsoleLogEntry::General,
  446. "afxParticleEmitterPathData(%s) unable to find path datablock: %d",
  447. getName(), epathDataBlockIds[i]);
  448. }
  449. else
  450. epathDataBlocks.push_back(pData);
  451. }
  452. parts_per_eject = epathDataBlocks.size();
  453. return true;
  454. }
  455. void afxParticleEmitterPathData::onPerformSubstitutions()
  456. {
  457. Parent::onPerformSubstitutions();
  458. if (epaths_string != ST_NULLSTRING && epaths_string[0] == '\0')
  459. {
  460. Con::warnf(ConsoleLogEntry::General, "afxParticleEmitterPathData(%s) empty paths string.", getName());
  461. return;// false;
  462. }
  463. if (epaths_string != ST_NULLSTRING && dStrlen(epaths_string) > 255)
  464. {
  465. Con::errorf(ConsoleLogEntry::General, "afxParticleEmitterPathData(%s) paths string too long [> 255 chars].", getName());
  466. return;// false;
  467. }
  468. if (epaths_string != ST_NULLSTRING)
  469. {
  470. Vector<char*> dataBlocks(__FILE__, __LINE__);
  471. dsize_t tokCopyLen = dStrlen(epaths_string) + 1;
  472. char* tokCopy = new char[tokCopyLen];
  473. dStrcpy(tokCopy, epaths_string, tokCopyLen);
  474. char* currTok = dStrtok(tokCopy, " \t");
  475. while (currTok != NULL)
  476. {
  477. dataBlocks.push_back(currTok);
  478. currTok = dStrtok(NULL, " \t");
  479. }
  480. if (dataBlocks.size() == 0)
  481. {
  482. Con::warnf(ConsoleLogEntry::General, "afxParticleEmitterPathData(%s) invalid paths string. No datablocks found", getName());
  483. delete [] tokCopy;
  484. return;// false;
  485. }
  486. epathDataBlocks.clear();
  487. epathDataBlockIds.clear();
  488. for (U32 i = 0; i < dataBlocks.size(); i++)
  489. {
  490. afxPathData* pData = NULL;
  491. if (Sim::findObject(dataBlocks[i], pData) == false)
  492. {
  493. Con::warnf(ConsoleLogEntry::General, "afxParticleEmitterPathData(%s) unable to find path datablock: %s", getName(), dataBlocks[i]);
  494. }
  495. else
  496. {
  497. epathDataBlocks.push_back(pData);
  498. epathDataBlockIds.push_back(pData->getId());
  499. }
  500. }
  501. delete [] tokCopy;
  502. if (epathDataBlocks.size() == 0)
  503. {
  504. Con::warnf(ConsoleLogEntry::General, "afxParticleEmitterPathData(%s) unable to find any path datablocks", getName());
  505. return;// false;
  506. }
  507. }
  508. /*epathDataBlocks.clear();
  509. for (U32 i = 0; i < epathDataBlockIds.size(); i++)
  510. {
  511. afxPathData* pData = NULL;
  512. if (Sim::findObject(epathDataBlockIds[i], pData) == false)
  513. {
  514. Con::warnf(ConsoleLogEntry::General,
  515. "afxParticleEmitterPathData(%s) unable to find path datablock: %d",
  516. getName(), epathDataBlockIds[i]);
  517. }
  518. else
  519. epathDataBlocks.push_back(pData);
  520. }
  521. */
  522. parts_per_eject = epathDataBlocks.size();
  523. }
  524. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  525. // DISC
  526. IMPLEMENT_CO_DATABLOCK_V1(afxParticleEmitterDiscData);
  527. ConsoleDocClass( afxParticleEmitterDiscData,
  528. "@brief An AFX customized particle emitter that emits particles within a disc shape.\n\n"
  529. "An AFX customized particle emitter that emits particles within a disc shape."
  530. "\n\n"
  531. "@ingroup afxEffects\n"
  532. "@ingroup AFX\n"
  533. "@ingroup Datablocks\n"
  534. );
  535. afxParticleEmitterDiscData::afxParticleEmitterDiscData()
  536. {
  537. pe_radius_min = 0.0f;
  538. pe_radius_max = 1.0f;
  539. }
  540. afxParticleEmitterDiscData::afxParticleEmitterDiscData(const afxParticleEmitterDiscData& other, bool temp_clone) : afxParticleEmitterData(other, temp_clone)
  541. {
  542. pe_radius_min = other.pe_radius_min;
  543. pe_radius_max = other.pe_radius_max;
  544. }
  545. void afxParticleEmitterDiscData::initPersistFields()
  546. {
  547. docsURL;
  548. addField("radiusMin", TypeF32, Offset(pe_radius_min, afxParticleEmitterDiscData),
  549. "...");
  550. addField("radiusMax", TypeF32, Offset(pe_radius_max, afxParticleEmitterDiscData),
  551. "...");
  552. Parent::initPersistFields();
  553. }
  554. void afxParticleEmitterDiscData::packData(BitStream* stream)
  555. {
  556. Parent::packData(stream);
  557. stream->writeInt((S32)(pe_radius_min * 100), 16);
  558. stream->writeInt((S32)(pe_radius_max * 100), 16);
  559. }
  560. void afxParticleEmitterDiscData::unpackData(BitStream* stream)
  561. {
  562. Parent::unpackData(stream);
  563. pe_radius_min = stream->readInt(16) / 100.0f;
  564. pe_radius_max = stream->readInt(16) / 100.0f;
  565. }
  566. bool afxParticleEmitterDiscData::onAdd()
  567. {
  568. if( Parent::onAdd() == false )
  569. return false;
  570. return true;
  571. }
  572. bool afxParticleEmitterDiscData::preload(bool server, String &errorStr)
  573. {
  574. if (Parent::preload(server, errorStr) == false)
  575. return false;
  576. return true;
  577. }
  578. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  579. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  580. afxParticleEmitter::afxParticleEmitter()
  581. {
  582. mDataBlock = NULL;
  583. pe_vector.set(0,0,1);
  584. pe_vector_norm.set(0,0,1);
  585. tpaths.clear();
  586. tpath_mults.clear();
  587. n_tpath_points = 0;
  588. tpath_points = NULL;
  589. afx_owner = 0;
  590. }
  591. afxParticleEmitter::~afxParticleEmitter()
  592. {
  593. }
  594. bool afxParticleEmitter::onAdd()
  595. {
  596. if( !Parent::onAdd() )
  597. return false;
  598. if (dynamic_cast<afxParticleEmitterData*>(mDataBlock))
  599. init_paths();
  600. return true;
  601. }
  602. void afxParticleEmitter::onRemove()
  603. {
  604. if (dynamic_cast<afxParticleEmitterData*>(mDataBlock))
  605. cleanup_paths();
  606. Parent::onRemove();
  607. }
  608. void afxParticleEmitter::init_paths()
  609. {
  610. if (!mDataBlock)
  611. {
  612. n_tpath_points = 0;
  613. tpath_points = NULL;
  614. return;
  615. }
  616. if (mDataBlock->tPathDataBlocks.size() < 1)
  617. {
  618. n_tpath_points = 0;
  619. tpath_points = NULL;
  620. }
  621. else
  622. {
  623. n_tpath_points = mDataBlock->tPathDataBlocks.size();
  624. tpath_points = new Point3F*[n_tpath_points];
  625. for (U32 i=0; i < n_tpath_points; i++)
  626. {
  627. afxPathData* pd = mDataBlock->tPathDataBlocks[i];
  628. if (!pd)
  629. continue;
  630. if (pd->getSubstitutionCount() > 0 && afx_owner)
  631. {
  632. afxPathData* orig_db = pd;
  633. pd = new afxPathData(*orig_db, true);
  634. orig_db->performSubstitutions(pd, afx_owner);
  635. }
  636. if (pd->num_points > 0)
  637. {
  638. afxPath3D* path = new afxPath3D();
  639. if (pd->times)
  640. path->buildPath( pd->num_points, pd->points, pd->times, pd->delay, 1.0f );
  641. else if (pd->lifetime == 0)
  642. path->buildPath( pd->num_points, pd->points, pd->delay, 1.0f );
  643. else
  644. path->buildPath( pd->num_points, pd->points, pd->delay, pd->delay+pd->lifetime );
  645. path->setLoopType( pd->loop_type );
  646. tpaths.push_back(path);
  647. tpath_mults.push_back( pd->mult );
  648. tpath_points[i] = new Point3F[pd->num_points];
  649. for (U32 j=0; j<pd->num_points; j++)
  650. tpath_points[i][j] = pd->points[j];
  651. }
  652. else
  653. {
  654. Con::warnf("afxParticleEmitter::init_paths() -- pathsTransform datablock (%d) has no points.", i);
  655. }
  656. if (pd->isTempClone())
  657. delete pd;
  658. }
  659. }
  660. }
  661. void afxParticleEmitter::cleanup_paths()
  662. {
  663. if (n_tpath_points < 1)
  664. return;
  665. for (U32 i=0; i < tpaths.size(); i++)
  666. {
  667. if (tpaths[i])
  668. delete tpaths[i];
  669. }
  670. tpaths.clear();
  671. if (tpath_points)
  672. {
  673. if (mDataBlock)
  674. {
  675. for (U32 i=0; i < n_tpath_points; i++)
  676. {
  677. if (tpath_points[i])
  678. delete [] tpath_points[i];
  679. }
  680. }
  681. delete [] tpath_points;
  682. tpath_points = 0;
  683. }
  684. }
  685. void afxParticleEmitter::sub_particleUpdate(Particle* part)
  686. {
  687. if (tpaths.size() < 1)
  688. return;
  689. F32 t = ((F32)part->currentAge)/((F32)part->totalLifetime);
  690. for (U32 i=0; i < tpaths.size(); i++)
  691. {
  692. F32 t_last = part->t_last;
  693. Point3F path_delta = (t_last <= 0.0f) ? tpaths[i]->evaluateAtTime(t) : tpaths[i]->evaluateAtTime(t_last, t);
  694. if (mDataBlock->tPathDataBlocks[i]->concentric)
  695. {
  696. // scale radial vector by x-component of path
  697. part->pos_local += part->radial_v*path_delta.x;
  698. // scale axis vector by z-component of path
  699. part->pos_local += pe_vector_norm*path_delta.z;
  700. // y-component is ignored
  701. }
  702. else
  703. {
  704. part->pos_local += path_delta;
  705. }
  706. }
  707. part->t_last = t;
  708. }
  709. void afxParticleEmitter::preCompute(const MatrixF& mat)
  710. {
  711. // Put vector into the space of the input matrix
  712. pe_vector = mDataBlock->pe_vector;
  713. if (!mDataBlock->pe_vector_is_world)
  714. mat.mulV(pe_vector);
  715. pe_vector_norm = pe_vector;
  716. pe_vector_norm.normalize();
  717. // Transform Paths: rebuild with current matrix
  718. for( U32 i=0; i < tpaths.size(); i++ )
  719. {
  720. for( U32 j=0; j < tpaths[i]->getNumPoints(); j++ )
  721. {
  722. Point3F p = tpath_points[i][j];
  723. mat.mulV(p);
  724. tpaths[i]->setPointPosition(j, p);
  725. }
  726. tpaths[i]->reBuildPath();
  727. }
  728. sub_preCompute(mat);
  729. }
  730. void afxParticleEmitter::afx_emitParticles(const Point3F& point, const bool useLastPosition, const Point3F& velocity, const U32 numMilliseconds)
  731. {
  732. if (mDead) return;
  733. // lifetime over - no more particles
  734. if (mLifetimeMS > 0 && mElapsedTimeMS > mLifetimeMS)
  735. return;
  736. Point3F realStart;
  737. if (useLastPosition && mHasLastPosition)
  738. realStart = mLastPosition;
  739. else
  740. realStart = point;
  741. afx_emitParticles(realStart, point, velocity, numMilliseconds);
  742. }
  743. void afxParticleEmitter::afx_emitParticles(const Point3F& start, const Point3F& end, const Point3F& velocity, const U32 numMilliseconds)
  744. {
  745. if (mDead) return;
  746. // lifetime over - no more particles
  747. if (mLifetimeMS > 0 && mElapsedTimeMS > mLifetimeMS)
  748. return;
  749. U32 currTime = 0;
  750. bool particlesAdded = false;
  751. if (mNextParticleTime != 0)
  752. {
  753. // Need to handle next particle
  754. //
  755. if (mNextParticleTime > numMilliseconds)
  756. {
  757. // Defer to next update
  758. // (Note that this introduces a potential spatial irregularity if the owning
  759. // object is accelerating, and updating at a low frequency)
  760. //
  761. mNextParticleTime -= numMilliseconds;
  762. mInternalClock += numMilliseconds;
  763. mLastPosition = end;
  764. mHasLastPosition = true;
  765. return;
  766. }
  767. else
  768. {
  769. currTime += mNextParticleTime;
  770. mInternalClock += mNextParticleTime;
  771. // Emit particle at curr time
  772. // Create particle at the correct position
  773. Point3F pos;
  774. pos.interpolate(start, end, F32(currTime) / F32(numMilliseconds));
  775. for (S32 p = 0; p < mDataBlock->parts_per_eject; p++)
  776. {
  777. sub_addParticle(pos, velocity, numMilliseconds-currTime, p);
  778. particlesAdded = true;
  779. }
  780. mNextParticleTime = 0;
  781. }
  782. }
  783. while (currTime < numMilliseconds)
  784. {
  785. S32 nextTime = mDataBlock->ejectionPeriodMS;
  786. if (mDataBlock->periodVarianceMS != 0)
  787. {
  788. nextTime += S32(gRandGen.randI() % (2 * mDataBlock->periodVarianceMS + 1)) -
  789. S32(mDataBlock->periodVarianceMS);
  790. }
  791. AssertFatal(nextTime > 0, "Error, next particle ejection time must always be greater than 0");
  792. if (currTime + nextTime > numMilliseconds)
  793. {
  794. mNextParticleTime = (currTime + nextTime) - numMilliseconds;
  795. mInternalClock += numMilliseconds - currTime;
  796. AssertFatal(mNextParticleTime > 0, "Error, should not have deferred this particle!");
  797. break;
  798. }
  799. currTime += nextTime;
  800. mInternalClock += nextTime;
  801. // Create particle at the correct position
  802. Point3F pos;
  803. pos.interpolate(start, end, F32(currTime) / F32(numMilliseconds));
  804. U32 advanceMS = numMilliseconds - currTime;
  805. if (mDataBlock->overrideAdvance == false && advanceMS != 0)
  806. {
  807. for (S32 p = 0; p < mDataBlock->parts_per_eject; p++)
  808. {
  809. sub_addParticle(pos, velocity, numMilliseconds-currTime, p);
  810. particlesAdded = true;
  811. Particle* last_part = part_list_head.next;
  812. if (last_part)
  813. {
  814. if (advanceMS > last_part->totalLifetime)
  815. {
  816. part_list_head.next = last_part->next;
  817. n_parts--;
  818. last_part->next = part_freelist;
  819. part_freelist = last_part;
  820. }
  821. else
  822. {
  823. F32 t = F32(advanceMS) / 1000.0;
  824. Point3F a = last_part->acc;
  825. a -= last_part->vel*last_part->dataBlock->dragCoefficient;
  826. a -= mWindVelocity*last_part->dataBlock->windCoefficient;
  827. //a += Point3F(0, 0, -9.81) * last_part->dataBlock->gravityCoefficient;
  828. a.z += -9.81f*last_part->dataBlock->gravityCoefficient; // as long as gravity is a constant, this is faster
  829. last_part->vel += a * t;
  830. last_part->pos_local += last_part->vel * t;
  831. // allow subclasses to adjust the particle params here
  832. sub_particleUpdate(last_part);
  833. if (last_part->dataBlock->constrain_pos)
  834. last_part->pos = last_part->pos_local + this->pos_pe;
  835. else
  836. last_part->pos = last_part->pos_local;
  837. updateKeyData(last_part);
  838. }
  839. }
  840. }
  841. }
  842. else
  843. {
  844. for (S32 p = 0; p < mDataBlock->parts_per_eject; p++)
  845. {
  846. sub_addParticle(pos, velocity, numMilliseconds-currTime, p);
  847. particlesAdded = true;
  848. }
  849. }
  850. }
  851. if( particlesAdded == true )
  852. updateBBox();
  853. if( n_parts > 0 && mSceneManager == NULL )
  854. {
  855. gClientSceneGraph->addObjectToScene(this);
  856. ClientProcessList::get()->addObject(this);
  857. }
  858. mLastPosition = end;
  859. mHasLastPosition = true;
  860. }
  861. Particle* afxParticleEmitter::alloc_particle()
  862. {
  863. n_parts++;
  864. // this should happen rarely
  865. if (n_parts > n_part_capacity)
  866. {
  867. Particle* store_block = new Particle[16];
  868. part_store.push_back(store_block);
  869. n_part_capacity += 16;
  870. for (S32 i = 0; i < 16; i++)
  871. {
  872. store_block[i].next = part_freelist;
  873. part_freelist = &store_block[i];
  874. }
  875. mDataBlock->allocPrimBuffer(n_part_capacity);
  876. }
  877. Particle* pNew = part_freelist;
  878. part_freelist = pNew->next;
  879. pNew->next = part_list_head.next;
  880. part_list_head.next = pNew;
  881. return pNew;
  882. }
  883. ParticleData* afxParticleEmitter::pick_particle_type()
  884. {
  885. U32 dBlockIndex = (U32)(mCeil(gRandGen.randF() * F32(mDataBlock->particleDataBlocks.size())) - 1);
  886. return mDataBlock->particleDataBlocks[dBlockIndex];
  887. }
  888. bool afxParticleEmitter::onNewDataBlock(GameBaseData* dptr, bool reload)
  889. {
  890. mDataBlock = dynamic_cast<afxParticleEmitterData*>(dptr);
  891. if( !mDataBlock || !Parent::onNewDataBlock(dptr, reload) )
  892. return false;
  893. if (mDataBlock->isTempClone())
  894. return true;
  895. scriptOnNewDataBlock();
  896. return true;
  897. }
  898. void afxParticleEmitter::emitParticlesExt(const MatrixF& xfm, const Point3F& point, const Point3F& velocity, const U32 numMilliseconds)
  899. {
  900. if (mDataBlock->use_emitter_xfm)
  901. {
  902. Point3F zero_point(0.0f, 0.0f, 0.0f);
  903. pos_pe = zero_point;
  904. setTransform(xfm);
  905. preCompute(xfm);
  906. afx_emitParticles(zero_point, true, velocity, numMilliseconds);
  907. }
  908. else
  909. {
  910. pos_pe = point;
  911. preCompute(xfm);
  912. afx_emitParticles(point, true, velocity, numMilliseconds);
  913. }
  914. }
  915. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  916. // VECTOR
  917. afxParticleEmitterVector::afxParticleEmitterVector()
  918. {
  919. mDataBlock = NULL;
  920. }
  921. afxParticleEmitterVector::~afxParticleEmitterVector()
  922. {
  923. }
  924. bool afxParticleEmitterVector::onNewDataBlock(GameBaseData* dptr, bool reload)
  925. {
  926. mDataBlock = dynamic_cast<afxParticleEmitterVectorData*>(dptr);
  927. if( !mDataBlock || !Parent::onNewDataBlock(dptr, reload) )
  928. return false;
  929. if (mDataBlock->isTempClone())
  930. return true;
  931. scriptOnNewDataBlock();
  932. return true;
  933. }
  934. void afxParticleEmitterVector::sub_addParticle(const Point3F& pos, const Point3F& vel, const U32 age_offset, S32 part_idx)
  935. {
  936. Particle* pNew = alloc_particle();
  937. ParticleData* part_db = pick_particle_type();
  938. Point3F pos_start = pos;
  939. if (part_db->constrain_pos)
  940. pos_start.set(0,0,0);
  941. F32 initialVel = mDataBlock->ejectionVelocity;
  942. initialVel += (mDataBlock->velocityVariance * 2.0f * gRandGen.randF()) - mDataBlock->velocityVariance;
  943. if(mDataBlock->fade_velocity)
  944. initialVel *= fade_amt;
  945. F32 ejection_offset = mDataBlock->ejectionOffset;
  946. if(mDataBlock->fade_offset)
  947. ejection_offset *= fade_amt;
  948. pNew->pos = pos_start + (pe_vector_norm * ejection_offset);
  949. pNew->pos_local = pNew->pos;
  950. pNew->vel = pe_vector_norm * initialVel;
  951. if (mDataBlock->orientParticles)
  952. pNew->orientDir = pe_vector_norm;
  953. else
  954. // note -- for non-oriented particles, we use orientDir.x to store the billboard start angle.
  955. pNew->orientDir.x = mDegToRad(part_db->start_angle + part_db->angle_variance*2.0f*gRandGen.randF() - part_db->angle_variance);
  956. pNew->acc.set(0, 0, 0);
  957. pNew->currentAge = age_offset;
  958. pNew->t_last = 0.0f;
  959. pNew->radial_v.set(0.0f, 0.0f, 0.0f);
  960. part_db->initializeParticle(pNew, vel);
  961. updateKeyData( pNew );
  962. }
  963. void afxParticleEmitterVector::sub_preCompute(const MatrixF& mat)
  964. {
  965. }
  966. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  967. // CONE
  968. afxParticleEmitterCone::afxParticleEmitterCone()
  969. {
  970. mDataBlock = NULL;
  971. cone_v.set(0,0,1);
  972. cone_s0.set(0,0,1);
  973. cone_s1.set(0,0,1);
  974. }
  975. afxParticleEmitterCone::~afxParticleEmitterCone()
  976. {
  977. }
  978. bool afxParticleEmitterCone::onNewDataBlock(GameBaseData* dptr, bool reload)
  979. {
  980. mDataBlock = dynamic_cast<afxParticleEmitterConeData*>(dptr);
  981. if( !mDataBlock || !Parent::onNewDataBlock(dptr, reload) )
  982. return false;
  983. if (mDataBlock->isTempClone())
  984. return true;
  985. scriptOnNewDataBlock();
  986. return true;
  987. }
  988. void afxParticleEmitterCone::sub_addParticle(const Point3F& pos, const Point3F& vel, const U32 age_offset, S32 part_idx)
  989. {
  990. Particle* pNew = alloc_particle();
  991. ParticleData* part_db = pick_particle_type();
  992. Point3F pos_start = pos;
  993. if (part_db->constrain_pos)
  994. pos_start.set(0,0,0);
  995. F32 initialVel = mDataBlock->ejectionVelocity;
  996. initialVel += (mDataBlock->velocityVariance * 2.0f * gRandGen.randF()) - mDataBlock->velocityVariance;
  997. if(mDataBlock->fade_velocity)
  998. initialVel *= fade_amt;
  999. // Randomly choose a vector between cone_s0 and cone_s1 and normalize:
  1000. Point3F vec;
  1001. F32 t = mRandF(0.0f, 1.0f);
  1002. vec.interpolate(cone_s0, cone_s1, t);
  1003. vec.normalize();
  1004. // Randomly rotate about cone_v
  1005. F32 theta = mRandF(0.0f, M_2PI_F);
  1006. AngAxisF thetaRot(cone_v, theta);
  1007. MatrixF temp(true);
  1008. thetaRot.setMatrix(&temp);
  1009. temp.mulP(vec);
  1010. F32 ejection_offset = mDataBlock->ejectionOffset;
  1011. if(mDataBlock->fade_offset)
  1012. ejection_offset *= fade_amt;
  1013. pNew->pos = pos_start + (vec * ejection_offset);
  1014. pNew->pos_local = pNew->pos;
  1015. pNew->vel = mDataBlock->ejectionInvert ? vec * -initialVel : vec * initialVel;
  1016. if (mDataBlock->orientParticles)
  1017. pNew->orientDir = vec;
  1018. else
  1019. // note -- for non-oriented particles, we use orientDir.x to store the billboard start angle.
  1020. pNew->orientDir.x = mDegToRad(part_db->start_angle + part_db->angle_variance*2.0f*gRandGen.randF() - part_db->angle_variance);
  1021. pNew->acc.set(0, 0, 0);
  1022. pNew->currentAge = age_offset;
  1023. pNew->t_last = 0.0f;
  1024. pNew->radial_v.set(0.0f, 0.0f, 0.0f);
  1025. part_db->initializeParticle(pNew, vel);
  1026. updateKeyData( pNew );
  1027. }
  1028. void afxParticleEmitterCone::sub_preCompute(const MatrixF& mat)
  1029. {
  1030. // Find vectors on the XZ plane corresponding to the inner and outer spread angles:
  1031. // (tan is infinite at PI/4 or 90 degrees)
  1032. cone_v.set( 0.0f, 0.0f, 1.0f );
  1033. cone_s0.x = mTan( mDegToRad( ((afxParticleEmitterConeData*)mDataBlock)->spread_min / 2.0f ));
  1034. cone_s0.y = 0.0f;
  1035. cone_s0.z = 1.0f;
  1036. cone_s1.x = mTan( mDegToRad(((afxParticleEmitterConeData*)mDataBlock)->spread_max / 2.0f ));
  1037. cone_s1.y = 0.0f;
  1038. cone_s1.z = 1.0f;
  1039. Point3F axis;
  1040. F32 theta = mAcos( mDot(cone_v, pe_vector_norm) );
  1041. if( M_PI_F-theta < POINT_EPSILON )
  1042. {
  1043. cone_v.neg();
  1044. cone_s0.neg();
  1045. cone_s1.neg();
  1046. }
  1047. else if( theta > POINT_EPSILON )
  1048. {
  1049. mCross(pe_vector_norm, cone_v, &axis);
  1050. axis.normalize();
  1051. AngAxisF thetaRot(axis, theta);
  1052. MatrixF temp(true);
  1053. thetaRot.setMatrix(&temp);
  1054. temp.mulP(cone_v);
  1055. temp.mulP(cone_s0);
  1056. temp.mulP(cone_s1);
  1057. }
  1058. }
  1059. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  1060. // PATH
  1061. afxParticleEmitterPath::afxParticleEmitterPath()
  1062. {
  1063. mDataBlock = NULL;
  1064. epaths.clear();
  1065. epath_mults.clear();
  1066. n_epath_points = 0;
  1067. epath_points = NULL;
  1068. }
  1069. afxParticleEmitterPath::~afxParticleEmitterPath()
  1070. {
  1071. }
  1072. bool afxParticleEmitterPath::onNewDataBlock(GameBaseData* dptr, bool reload)
  1073. {
  1074. mDataBlock = dynamic_cast<afxParticleEmitterPathData*>(dptr);
  1075. if( !mDataBlock || !Parent::onNewDataBlock(dptr, reload) )
  1076. return false;
  1077. if (mDataBlock->isTempClone())
  1078. return true;
  1079. scriptOnNewDataBlock();
  1080. return true;
  1081. }
  1082. bool afxParticleEmitterPath::onAdd()
  1083. {
  1084. if( !Parent::onAdd() )
  1085. return false;
  1086. if (dynamic_cast<afxParticleEmitterPathData*>(mDataBlock))
  1087. init_paths();
  1088. return true;
  1089. }
  1090. void afxParticleEmitterPath::onRemove()
  1091. {
  1092. if (dynamic_cast<afxParticleEmitterPathData*>(mDataBlock))
  1093. cleanup_paths();
  1094. Parent::onRemove();
  1095. }
  1096. void afxParticleEmitterPath::init_paths()
  1097. {
  1098. if (!mDataBlock || ((afxParticleEmitterPathData*)mDataBlock)->epathDataBlocks.size() < 1)
  1099. {
  1100. n_epath_points = 0;
  1101. epath_points = NULL;
  1102. return;
  1103. }
  1104. n_epath_points = ((afxParticleEmitterPathData*)mDataBlock)->epathDataBlocks.size();
  1105. epath_points = new Point3F*[n_epath_points];
  1106. dMemset(epath_points, 0, n_epath_points*sizeof(Point3F*));
  1107. for (U32 i=0; i < n_epath_points; i++)
  1108. {
  1109. afxPathData* pd = ((afxParticleEmitterPathData*)mDataBlock)->epathDataBlocks[i];
  1110. if (!pd)
  1111. continue;
  1112. if (pd->getSubstitutionCount() > 0 && afx_owner)
  1113. {
  1114. afxPathData* orig_db = pd;
  1115. pd = new afxPathData(*orig_db, true);
  1116. orig_db->performSubstitutions(pd, afx_owner);
  1117. }
  1118. if (pd->num_points > 0)
  1119. {
  1120. afxPath3D* path = new afxPath3D();
  1121. if (pd->times)
  1122. path->buildPath( pd->num_points, pd->points, pd->times, 0.0f, 1.0f );
  1123. else
  1124. path->buildPath( pd->num_points, pd->points, 0.0f, 1.0f );
  1125. epaths.push_back(path);
  1126. epath_mults.push_back( pd->mult );
  1127. epath_points[i] = new Point3F[pd->num_points];
  1128. for (U32 j=0; j<pd->num_points; j++)
  1129. epath_points[i][j] = pd->points[j];
  1130. }
  1131. else
  1132. {
  1133. Con::warnf("afxParticleEmitterPath::init_paths() -- paths datablock (%d) has no points.", i);
  1134. }
  1135. if (pd->isTempClone())
  1136. delete pd;
  1137. }
  1138. }
  1139. void afxParticleEmitterPath::cleanup_paths()
  1140. {
  1141. if (n_epath_points < 1)
  1142. return;
  1143. for (U32 i=0; i < epaths.size(); i++)
  1144. {
  1145. if (epaths[i])
  1146. delete epaths[i];
  1147. }
  1148. epaths.clear();
  1149. if (epath_points)
  1150. {
  1151. if (mDataBlock)
  1152. {
  1153. for (U32 i=0; i < n_epath_points; i++)
  1154. {
  1155. if (epath_points[i])
  1156. delete [] epath_points[i];
  1157. }
  1158. }
  1159. delete [] epath_points;
  1160. epath_points = 0;
  1161. }
  1162. }
  1163. void afxParticleEmitterPath::sub_addParticle(const Point3F& pos, const Point3F& vel, const U32 age_offset, S32 part_idx)
  1164. {
  1165. if (part_idx >= epaths.size() || !epaths[part_idx])
  1166. return;
  1167. Particle* pNew = alloc_particle();
  1168. ParticleData* part_db = pick_particle_type();
  1169. Point3F pos_start = pos;
  1170. if (part_db->constrain_pos)
  1171. pos_start.set(0,0,0);
  1172. F32 initialVel = mDataBlock->ejectionVelocity;
  1173. initialVel += (mDataBlock->velocityVariance * 2.0f * gRandGen.randF()) - mDataBlock->velocityVariance;
  1174. if(mDataBlock->fade_velocity)
  1175. initialVel *= fade_amt;
  1176. // Randomly choose a curve parameter between [0.0,1.0] and evaluate the curve there
  1177. F32 param = mRandF(0.0f, 1.0f);
  1178. Point3F curve_pos = epaths[part_idx]->evaluateAtTime(param);
  1179. Point3F vec;
  1180. switch (((afxParticleEmitterPathData*)mDataBlock)->path_origin_type)
  1181. {
  1182. case afxParticleEmitterPathData::PATHEMIT_ORIGIN :
  1183. vec = curve_pos;
  1184. vec.normalize();
  1185. break;
  1186. case afxParticleEmitterPathData::PATHEMIT_POINT :
  1187. vec = curve_pos-pe_vector;
  1188. vec.normalize();
  1189. break;
  1190. case afxParticleEmitterPathData::PATHEMIT_VECTOR :
  1191. vec = pe_vector_norm;
  1192. break;
  1193. case afxParticleEmitterPathData::PATHEMIT_TANGENT :
  1194. vec = epaths[part_idx]->evaluateTangentAtTime(param);
  1195. vec.normalize();
  1196. break;
  1197. }
  1198. F32 ejection_offset = mDataBlock->ejectionOffset;
  1199. if(mDataBlock->fade_offset)
  1200. ejection_offset *= fade_amt;
  1201. pNew->pos = pos_start + curve_pos + (vec * ejection_offset);
  1202. pNew->pos_local = pNew->pos;
  1203. pNew->vel = mDataBlock->ejectionInvert ? vec * -initialVel : vec * initialVel;
  1204. if (mDataBlock->orientParticles)
  1205. pNew->orientDir = vec;
  1206. else
  1207. // note -- for non-oriented particles, we use orientDir.x to store the billboard start angle.
  1208. pNew->orientDir.x = mDegToRad(part_db->start_angle + part_db->angle_variance*2.0f*gRandGen.randF() - part_db->angle_variance);
  1209. pNew->acc.set(0, 0, 0);
  1210. pNew->currentAge = age_offset;
  1211. pNew->t_last = 0.0f;
  1212. pNew->radial_v.set(0.0f, 0.0f, 0.0f);
  1213. part_db->initializeParticle(pNew, vel);
  1214. updateKeyData( pNew );
  1215. }
  1216. void afxParticleEmitterPath::sub_preCompute(const MatrixF& mat)
  1217. {
  1218. for( U32 i=0; i < epaths.size(); i++ )
  1219. {
  1220. for( U32 j=0; j < epaths[i]->getNumPoints(); j++ )
  1221. {
  1222. Point3F p = epath_points[i][j];
  1223. mat.mulV(p);
  1224. p *= epath_mults[i];
  1225. if(mDataBlock->ground_conform) {
  1226. groundConformPoint(p, mat);
  1227. }
  1228. epaths[i]->setPointPosition(j, p);
  1229. }
  1230. epaths[i]->reBuildPath();
  1231. }
  1232. }
  1233. void afxParticleEmitterPath::groundConformPoint(Point3F& point, const MatrixF& mat)
  1234. {
  1235. point += mat.getPosition();
  1236. RayInfo rInfo;
  1237. bool hit = false;
  1238. if (mDataBlock->ground_conform_interiors)
  1239. {
  1240. U32 mask = InteriorLikeObjectType;
  1241. if (mDataBlock->ground_conform_terrain)
  1242. {
  1243. mask |= TerrainObjectType | TerrainLikeObjectType;
  1244. }
  1245. Point3F above_pos(point); above_pos.z += 0.1f;
  1246. Point3F below_pos(point); below_pos.z -= 10000;
  1247. hit = gClientContainer.castRay(above_pos, below_pos, mask, &rInfo);
  1248. if (!hit)
  1249. {
  1250. above_pos.z = point.z + 10000;
  1251. below_pos.z = point.z - 0.1f;
  1252. hit = gClientContainer.castRay(below_pos, above_pos, mask, &rInfo);
  1253. }
  1254. }
  1255. else if (mDataBlock->ground_conform_terrain)
  1256. {
  1257. U32 mask = TerrainObjectType | TerrainLikeObjectType;
  1258. Point3F above_pos(point); above_pos.z += 10000;
  1259. Point3F below_pos(point); below_pos.z -= 10000;
  1260. hit = gClientContainer.castRay(above_pos, below_pos, mask, &rInfo);
  1261. }
  1262. if (hit)
  1263. {
  1264. F32 terrain_z = rInfo.point.z;
  1265. F32 new_z = terrain_z + mDataBlock->ground_conform_height;
  1266. point.z = new_z;
  1267. }
  1268. point -= mat.getPosition();
  1269. }
  1270. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  1271. // DISC
  1272. afxParticleEmitterDisc::afxParticleEmitterDisc()
  1273. {
  1274. mDataBlock = NULL;
  1275. disc_v.set(0,0,1);
  1276. disc_r.set(1,0,0);
  1277. }
  1278. afxParticleEmitterDisc::~afxParticleEmitterDisc()
  1279. {
  1280. }
  1281. bool afxParticleEmitterDisc::onNewDataBlock(GameBaseData* dptr, bool reload)
  1282. {
  1283. mDataBlock = dynamic_cast<afxParticleEmitterDiscData*>(dptr);
  1284. if( !mDataBlock || !Parent::onNewDataBlock(dptr, reload) )
  1285. return false;
  1286. if (mDataBlock->isTempClone())
  1287. return true;
  1288. return true;
  1289. }
  1290. void afxParticleEmitterDisc::sub_addParticle(const Point3F& pos, const Point3F& vel, const U32 age_offset, S32 part_idx)
  1291. {
  1292. Particle* pNew = alloc_particle();
  1293. ParticleData* part_db = pick_particle_type();
  1294. Point3F pos_start = pos;
  1295. if (part_db->constrain_pos)
  1296. pos_start.set(0,0,0);
  1297. F32 initialVel = mDataBlock->ejectionVelocity;
  1298. initialVel += (mDataBlock->velocityVariance * 2.0f * gRandGen.randF()) - mDataBlock->velocityVariance;
  1299. if(mDataBlock->fade_velocity)
  1300. initialVel *= fade_amt;
  1301. // Randomly choose a radius vector
  1302. Point3F r( disc_r );
  1303. F32 radius = mRandF(((afxParticleEmitterDiscData*)mDataBlock)->pe_radius_min, ((afxParticleEmitterDiscData*)mDataBlock)->pe_radius_max);
  1304. r *= radius;
  1305. // Randomly rotate r about disc_v
  1306. F32 theta = mRandF(0.0f, M_2PI_F);
  1307. AngAxisF thetaRot(disc_v, theta);
  1308. MatrixF temp(true);
  1309. thetaRot.setMatrix(&temp);
  1310. temp.mulP(r);
  1311. F32 ejection_offset = mDataBlock->ejectionOffset;
  1312. if(mDataBlock->fade_offset)
  1313. ejection_offset *= fade_amt;
  1314. pNew->pos = pos_start + r + (disc_v * ejection_offset);
  1315. pNew->pos_local = pNew->pos;
  1316. pNew->vel = (mDataBlock->ejectionInvert) ? (disc_v * -initialVel) : (disc_v * initialVel);
  1317. if (mDataBlock->orientParticles)
  1318. pNew->orientDir = disc_v;
  1319. else
  1320. // note -- for non-oriented particles, we use orientDir.x to store the billboard start angle.
  1321. pNew->orientDir.x = mDegToRad(part_db->start_angle + part_db->angle_variance*2.0f*gRandGen.randF() - part_db->angle_variance);
  1322. pNew->acc.set(0, 0, 0);
  1323. pNew->currentAge = age_offset;
  1324. pNew->t_last = 0.0f;
  1325. pNew->radial_v = r;
  1326. part_db->initializeParticle(pNew, vel);
  1327. updateKeyData( pNew );
  1328. }
  1329. void afxParticleEmitterDisc::sub_preCompute(const MatrixF& mat)
  1330. {
  1331. disc_v.set(0.0f, 0.0f, 1.0f);
  1332. disc_r.set(1.0f, 0.0f, 0.0f);
  1333. Point3F axis;
  1334. F32 theta = mAcos( mDot(disc_v, pe_vector_norm) );
  1335. if( M_PI_F-theta < POINT_EPSILON )
  1336. {
  1337. disc_v.neg();
  1338. }
  1339. else if( theta > POINT_EPSILON )
  1340. {
  1341. mCross(pe_vector_norm, disc_v, &axis);
  1342. axis.normalize();
  1343. AngAxisF thetaRot(axis, theta);
  1344. MatrixF temp(true);
  1345. thetaRot.setMatrix(&temp);
  1346. temp.mulP(disc_v);
  1347. temp.mulP(disc_r);
  1348. }
  1349. }
  1350. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//