particle.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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 "particle.h"
  23. #include "console/consoleTypes.h"
  24. #include "console/typeValidators.h"
  25. #include "core/stream/bitStream.h"
  26. #include "math/mRandom.h"
  27. #include "math/mathIO.h"
  28. #include "console/engineAPI.h"
  29. IMPLEMENT_CO_DATABLOCK_V1( ParticleData );
  30. ConsoleDocClass( ParticleData,
  31. "@brief Contains information for how specific particles should look and react "
  32. "including particle colors, particle imagemap, acceleration value for individual "
  33. "particles and spin information.\n"
  34. "@tsexample\n"
  35. "datablock ParticleData( GLWaterExpSmoke )\n"
  36. "{\n"
  37. " textureName = \"art/shapes/particles/smoke\";\n"
  38. " dragCoefficient = 0.4;\n"
  39. " gravityCoefficient = -0.25;\n"
  40. " inheritedVelFactor = 0.025;\n"
  41. " constantAcceleration = -1.1;\n"
  42. " lifetimeMS = 1250;\n"
  43. " lifetimeVarianceMS = 0;\n"
  44. " useInvAlpha = false;\n"
  45. " spinSpeed = 1;\n"
  46. " spinRandomMin = -200.0;\n"
  47. " spinRandomMax = 200.0;\n\n"
  48. " colors[0] = \"0.1 0.1 1.0 1.0\";\n"
  49. " colors[1] = \"0.4 0.4 1.0 1.0\";\n"
  50. " colors[2] = \"0.4 0.4 1.0 0.0\";\n\n"
  51. " sizes[0] = 2.0;\n"
  52. " sizes[1] = 6.0;\n"
  53. " sizes[2] = 2.0;\n\n"
  54. " times[0] = 0.0;\n"
  55. " times[1] = 0.5;\n"
  56. " times[2] = 1.0;\n"
  57. "};\n"
  58. "@endtsexample\n"
  59. "@ingroup FX\n"
  60. "@see ParticleEmitter\n"
  61. "@see ParticleEmitterData\n"
  62. "@see ParticleEmitterNode\n"
  63. );
  64. static const float sgDefaultWindCoefficient = 0.0f;
  65. static const float sgDefaultConstantAcceleration = 0.f;
  66. static const float sgDefaultSpinSpeed = 1.f;
  67. static const float sgDefaultSpinRandomMin = 0.f;
  68. static const float sgDefaultSpinRandomMax = 0.f;
  69. //-----------------------------------------------------------------------------
  70. // Constructor
  71. //-----------------------------------------------------------------------------
  72. ParticleData::ParticleData()
  73. {
  74. dragCoefficient = 0.0f;
  75. windCoefficient = sgDefaultWindCoefficient;
  76. gravityCoefficient = 0.0f;
  77. inheritedVelFactor = 0.0f;
  78. constantAcceleration = sgDefaultConstantAcceleration;
  79. lifetimeMS = 1000;
  80. lifetimeVarianceMS = 0;
  81. spinSpeed = sgDefaultSpinSpeed;
  82. spinRandomMin = sgDefaultSpinRandomMin;
  83. spinRandomMax = sgDefaultSpinRandomMax;
  84. useInvAlpha = false;
  85. animateTexture = false;
  86. numFrames = 1;
  87. framesPerSec = numFrames;
  88. S32 i;
  89. for( i=0; i<PDC_NUM_KEYS; i++ )
  90. {
  91. colors[i].set( 1.0, 1.0, 1.0, 1.0 );
  92. sizes[i] = 1.0;
  93. }
  94. times[0] = 0.0f;
  95. times[1] = 0.33f;
  96. times[2] = 0.66f;
  97. times[3] = 1.0f;
  98. texCoords[0].set(0.0,0.0); // texture coords at 4 corners
  99. texCoords[1].set(0.0,1.0); // of particle quad
  100. texCoords[2].set(1.0,1.0); // (defaults to entire particle)
  101. texCoords[3].set(1.0,0.0);
  102. animTexTiling.set(0,0); // tiling dimensions
  103. animTexFramesString = NULL; // string of animation frame indices
  104. animTexUVs = NULL; // array of tile vertex UVs
  105. textureName = NULL; // texture filename
  106. textureHandle = NULL; // loaded texture handle
  107. }
  108. //-----------------------------------------------------------------------------
  109. // Destructor
  110. //-----------------------------------------------------------------------------
  111. ParticleData::~ParticleData()
  112. {
  113. if (animTexUVs)
  114. {
  115. delete [] animTexUVs;
  116. }
  117. }
  118. //-----------------------------------------------------------------------------
  119. // initPersistFields
  120. //-----------------------------------------------------------------------------
  121. void ParticleData::initPersistFields()
  122. {
  123. addFieldV( "dragCoefficient", TYPEID< F32 >(), Offset(dragCoefficient, ParticleData), new FRangeValidator(0, 5),
  124. "Particle physics drag amount." );
  125. addField( "windCoefficient", TYPEID< F32 >(), Offset(windCoefficient, ParticleData),
  126. "Strength of wind on the particles." );
  127. addFieldV( "gravityCoefficient", TYPEID< F32 >(), Offset(gravityCoefficient, ParticleData), new FRangeValidator(-10, 10),
  128. "Strength of gravity on the particles." );
  129. addFieldV( "inheritedVelFactor", TYPEID< F32 >(), Offset(inheritedVelFactor, ParticleData), &CommonValidators::NormalizedFloat,
  130. "Amount of emitter velocity to add to particle initial velocity." );
  131. addField( "constantAcceleration", TYPEID< F32 >(), Offset(constantAcceleration, ParticleData),
  132. "Constant acceleration to apply to this particle." );
  133. addField( "lifetimeMS", TYPEID< S32 >(), Offset(lifetimeMS, ParticleData),
  134. "Time in milliseconds before this particle is destroyed." );
  135. addField( "lifetimeVarianceMS", TYPEID< S32 >(), Offset(lifetimeVarianceMS, ParticleData),
  136. "Variance in lifetime of particle, from 0 - lifetimeMS." );
  137. addField( "spinSpeed", TYPEID< F32 >(), Offset(spinSpeed, ParticleData),
  138. "Speed at which to spin the particle." );
  139. addField( "spinRandomMin", TYPEID< F32 >(), Offset(spinRandomMin, ParticleData),
  140. "Minimum allowed spin speed of this particle, between -10000 and spinRandomMax." );
  141. addField( "spinRandomMax", TYPEID< F32 >(), Offset(spinRandomMax, ParticleData),
  142. "Maximum allowed spin speed of this particle, between spinRandomMin and 10000." );
  143. addField( "useInvAlpha", TYPEID< bool >(), Offset(useInvAlpha, ParticleData),
  144. "@brief Controls how particles blend with the scene.\n\n"
  145. "If true, particles blend like ParticleBlendStyle NORMAL, if false, "
  146. "blend like ParticleBlendStyle ADDITIVE.\n"
  147. "@note If ParticleEmitterData::blendStyle is set, it will override this value." );
  148. addField( "animateTexture", TYPEID< bool >(), Offset(animateTexture, ParticleData),
  149. "If true, allow the particle texture to be an animated sprite." );
  150. addField( "framesPerSec", TYPEID< S32 >(), Offset(framesPerSec, ParticleData),
  151. "If animateTexture is true, this defines the frames per second of the "
  152. "sprite animation." );
  153. addField( "textureCoords", TYPEID< Point2F >(), Offset(texCoords, ParticleData), 4,
  154. "@brief 4 element array defining the UV coords into textureName to use "
  155. "for this particle.\n\n"
  156. "Coords should be set for the first tile only when using animTexTiling; "
  157. "coordinates for other tiles will be calculated automatically. \"0 0\" is "
  158. "top left and \"1 1\" is bottom right." );
  159. addField( "animTexTiling", TYPEID< Point2I >(), Offset(animTexTiling, ParticleData),
  160. "@brief The number of frames, in rows and columns stored in textureName "
  161. "(when animateTexture is true).\n\n"
  162. "A maximum of 256 frames can be stored in a single texture when using "
  163. "animTexTiling. Value should be \"NumColumns NumRows\", for example \"4 4\"." );
  164. addField( "animTexFrames", TYPEID< StringTableEntry >(), Offset(animTexFramesString,ParticleData),
  165. "@brief A list of frames and/or frame ranges to use for particle "
  166. "animation if animateTexture is true.\n\n"
  167. "Each frame token must be separated by whitespace. A frame token must be "
  168. "a positive integer frame number or a range of frame numbers separated "
  169. "with a '-'. The range separator, '-', cannot have any whitspace around "
  170. "it.\n\n"
  171. "Ranges can be specified to move through the frames in reverse as well "
  172. "as forward (eg. 19-14). Frame numbers exceeding the number of tiles will "
  173. "wrap.\n"
  174. "@tsexample\n"
  175. "animTexFrames = \"0-16 20 19 18 17 31-21\";\n"
  176. "@endtsexample\n" );
  177. addField( "textureName", TYPEID< StringTableEntry >(), Offset(textureName, ParticleData),
  178. "Texture file to use for this particle." );
  179. addField( "animTexName", TYPEID< StringTableEntry >(), Offset(textureName, ParticleData),
  180. "@brief Texture file to use for this particle if animateTexture is true.\n\n"
  181. "Deprecated. Use textureName instead." );
  182. // Interpolation variables
  183. addField( "colors", TYPEID< ColorF >(), Offset(colors, ParticleData), PDC_NUM_KEYS,
  184. "@brief Particle RGBA color keyframe values.\n\n"
  185. "The particle color will linearly interpolate between the color/time keys "
  186. "over the lifetime of the particle." );
  187. addField( "sizes", TYPEID< F32 >(), Offset(sizes, ParticleData), PDC_NUM_KEYS,
  188. "@brief Particle size keyframe values.\n\n"
  189. "The particle size will linearly interpolate between the size/time keys "
  190. "over the lifetime of the particle." );
  191. addProtectedField( "times", TYPEID< F32 >(), Offset(times, ParticleData), &protectedSetTimes,
  192. &defaultProtectedGetFn, PDC_NUM_KEYS,
  193. "@brief Time keys used with the colors and sizes keyframes.\n\n"
  194. "Values are from 0.0 (particle creation) to 1.0 (end of lifespace)." );
  195. Parent::initPersistFields();
  196. }
  197. //-----------------------------------------------------------------------------
  198. // Pack data
  199. //-----------------------------------------------------------------------------
  200. void ParticleData::packData(BitStream* stream)
  201. {
  202. Parent::packData(stream);
  203. stream->writeFloat(dragCoefficient / 5, 10);
  204. if( stream->writeFlag(windCoefficient != sgDefaultWindCoefficient ) )
  205. stream->write(windCoefficient);
  206. if (stream->writeFlag(gravityCoefficient != 0.0f))
  207. stream->writeSignedFloat(gravityCoefficient / 10, 12);
  208. stream->writeFloat(inheritedVelFactor, 9);
  209. if( stream->writeFlag( constantAcceleration != sgDefaultConstantAcceleration ) )
  210. stream->write(constantAcceleration);
  211. stream->write( lifetimeMS );
  212. stream->write( lifetimeVarianceMS );
  213. if( stream->writeFlag( spinSpeed != sgDefaultSpinSpeed ) )
  214. stream->write(spinSpeed);
  215. if(stream->writeFlag(spinRandomMin != sgDefaultSpinRandomMin || spinRandomMax != sgDefaultSpinRandomMax))
  216. {
  217. stream->writeInt((S32)(spinRandomMin + 1000), 11);
  218. stream->writeInt((S32)(spinRandomMax + 1000), 11);
  219. }
  220. stream->writeFlag(useInvAlpha);
  221. S32 i, count;
  222. // see how many frames there are:
  223. for(count = 0; count < 3; count++)
  224. if(times[count] >= 1)
  225. break;
  226. count++;
  227. stream->writeInt(count-1, 2);
  228. for( i=0; i<count; i++ )
  229. {
  230. stream->writeFloat( colors[i].red, 7);
  231. stream->writeFloat( colors[i].green, 7);
  232. stream->writeFloat( colors[i].blue, 7);
  233. stream->writeFloat( colors[i].alpha, 7);
  234. stream->writeFloat( sizes[i]/MaxParticleSize, 14);
  235. stream->writeFloat( times[i], 8);
  236. }
  237. if (stream->writeFlag(textureName && textureName[0]))
  238. stream->writeString(textureName);
  239. for (i = 0; i < 4; i++)
  240. mathWrite(*stream, texCoords[i]);
  241. if (stream->writeFlag(animateTexture))
  242. {
  243. if (stream->writeFlag(animTexFramesString && animTexFramesString[0]))
  244. {
  245. stream->writeString(animTexFramesString);
  246. }
  247. mathWrite(*stream, animTexTiling);
  248. stream->writeInt(framesPerSec, 8);
  249. }
  250. }
  251. //-----------------------------------------------------------------------------
  252. // Unpack data
  253. //-----------------------------------------------------------------------------
  254. void ParticleData::unpackData(BitStream* stream)
  255. {
  256. Parent::unpackData(stream);
  257. dragCoefficient = stream->readFloat(10) * 5;
  258. if(stream->readFlag())
  259. stream->read(&windCoefficient);
  260. else
  261. windCoefficient = sgDefaultWindCoefficient;
  262. if (stream->readFlag())
  263. gravityCoefficient = stream->readSignedFloat(12)*10;
  264. else
  265. gravityCoefficient = 0.0f;
  266. inheritedVelFactor = stream->readFloat(9);
  267. if(stream->readFlag())
  268. stream->read(&constantAcceleration);
  269. else
  270. constantAcceleration = sgDefaultConstantAcceleration;
  271. stream->read( &lifetimeMS );
  272. stream->read( &lifetimeVarianceMS );
  273. if(stream->readFlag())
  274. stream->read(&spinSpeed);
  275. else
  276. spinSpeed = sgDefaultSpinSpeed;
  277. if(stream->readFlag())
  278. {
  279. spinRandomMin = (F32)(stream->readInt(11) - 1000);
  280. spinRandomMax = (F32)(stream->readInt(11) - 1000);
  281. }
  282. else
  283. {
  284. spinRandomMin = sgDefaultSpinRandomMin;
  285. spinRandomMax = sgDefaultSpinRandomMax;
  286. }
  287. useInvAlpha = stream->readFlag();
  288. S32 i;
  289. S32 count = stream->readInt(2) + 1;
  290. for(i = 0;i < count; i++)
  291. {
  292. colors[i].red = stream->readFloat(7);
  293. colors[i].green = stream->readFloat(7);
  294. colors[i].blue = stream->readFloat(7);
  295. colors[i].alpha = stream->readFloat(7);
  296. sizes[i] = stream->readFloat(14) * MaxParticleSize;
  297. times[i] = stream->readFloat(8);
  298. }
  299. textureName = (stream->readFlag()) ? stream->readSTString() : 0;
  300. for (i = 0; i < 4; i++)
  301. mathRead(*stream, &texCoords[i]);
  302. animateTexture = stream->readFlag();
  303. if (animateTexture)
  304. {
  305. animTexFramesString = (stream->readFlag()) ? stream->readSTString() : 0;
  306. mathRead(*stream, &animTexTiling);
  307. framesPerSec = stream->readInt(8);
  308. }
  309. }
  310. bool ParticleData::protectedSetTimes( void *object, const char *index, const char *data)
  311. {
  312. ParticleData *pData = static_cast<ParticleData*>( object );
  313. F32 val = dAtof(data);
  314. U32 i;
  315. if (!index)
  316. i = 0;
  317. else
  318. i = dAtoui(index);
  319. pData->times[i] = mClampF( val, 0.f, 1.f );
  320. return true;
  321. }
  322. //-----------------------------------------------------------------------------
  323. // onAdd
  324. //-----------------------------------------------------------------------------
  325. bool ParticleData::onAdd()
  326. {
  327. if (Parent::onAdd() == false)
  328. return false;
  329. if (dragCoefficient < 0.0) {
  330. Con::warnf(ConsoleLogEntry::General, "ParticleData(%s) drag coeff less than 0", getName());
  331. dragCoefficient = 0.0f;
  332. }
  333. if (lifetimeMS < 1) {
  334. Con::warnf(ConsoleLogEntry::General, "ParticleData(%s) lifetime < 1 ms", getName());
  335. lifetimeMS = 1;
  336. }
  337. if (lifetimeVarianceMS >= lifetimeMS) {
  338. Con::warnf(ConsoleLogEntry::General, "ParticleData(%s) lifetimeVariance >= lifetime", getName());
  339. lifetimeVarianceMS = lifetimeMS - 1;
  340. }
  341. if (spinSpeed > 10000.0 || spinSpeed < -10000.0) {
  342. Con::warnf(ConsoleLogEntry::General, "ParticleData(%s) spinSpeed invalid", getName());
  343. return false;
  344. }
  345. if (spinRandomMin > 10000.0 || spinRandomMin < -10000.0) {
  346. Con::warnf(ConsoleLogEntry::General, "ParticleData(%s) spinRandomMin invalid", getName());
  347. spinRandomMin = -360.0;
  348. return false;
  349. }
  350. if (spinRandomMin > spinRandomMax) {
  351. Con::warnf(ConsoleLogEntry::General, "ParticleData(%s) spinRandomMin greater than spinRandomMax", getName());
  352. spinRandomMin = spinRandomMax - (spinRandomMin - spinRandomMax );
  353. return false;
  354. }
  355. if (spinRandomMax > 10000.0 || spinRandomMax < -10000.0) {
  356. Con::warnf(ConsoleLogEntry::General, "ParticleData(%s) spinRandomMax invalid", getName());
  357. spinRandomMax = 360.0;
  358. return false;
  359. }
  360. if (framesPerSec > 255)
  361. {
  362. Con::warnf(ConsoleLogEntry::General, "ParticleData(%s) framesPerSec > 255, too high", getName());
  363. framesPerSec = 255;
  364. return false;
  365. }
  366. times[0] = 0.0f;
  367. for (U32 i = 1; i < 4; i++) {
  368. if (times[i] < times[i-1]) {
  369. Con::warnf(ConsoleLogEntry::General, "ParticleData(%s) times[%d] < times[%d]", getName(), i, i-1);
  370. times[i] = times[i-1];
  371. }
  372. }
  373. // Here we validate parameters
  374. if (animateTexture)
  375. {
  376. // Tiling dimensions must be positive and non-zero
  377. if (animTexTiling.x <= 0 || animTexTiling.y <= 0)
  378. {
  379. Con::warnf(ConsoleLogEntry::General,
  380. "ParticleData(%s) bad value(s) for animTexTiling [%d or %d <= 0], invalid datablock",
  381. animTexTiling.x, animTexTiling.y, getName());
  382. return false;
  383. }
  384. // Indices must fit into a byte so these are also bad
  385. if (animTexTiling.x * animTexTiling.y > 256)
  386. {
  387. Con::warnf(ConsoleLogEntry::General,
  388. "ParticleData(%s) bad values for animTexTiling [%d*%d > %d], invalid datablock",
  389. animTexTiling.x, animTexTiling.y, 256, getName());
  390. return false;
  391. }
  392. // A list of frames is required
  393. if (!animTexFramesString || !animTexFramesString[0])
  394. {
  395. Con::warnf(ConsoleLogEntry::General, "ParticleData(%s) no animTexFrames, invalid datablock", getName());
  396. return false;
  397. }
  398. // The frame list cannot be too long.
  399. if (animTexFramesString && dStrlen(animTexFramesString) > 255)
  400. {
  401. Con::errorf(ConsoleLogEntry::General, "ParticleData(%s) animTexFrames string too long [> 255 chars]", getName());
  402. return false;
  403. }
  404. }
  405. return true;
  406. }
  407. //-----------------------------------------------------------------------------
  408. // preload
  409. //-----------------------------------------------------------------------------
  410. bool ParticleData::preload(bool server, String &errorStr)
  411. {
  412. if (Parent::preload(server, errorStr) == false)
  413. return false;
  414. bool error = false;
  415. if(!server)
  416. {
  417. // Here we attempt to load the particle's texture if specified. An undefined
  418. // texture is *not* an error since the emitter may provide one.
  419. if (textureName && textureName[0])
  420. {
  421. textureHandle = GFXTexHandle(textureName, &GFXDefaultStaticDiffuseProfile, avar("%s() - textureHandle (line %d)", __FUNCTION__, __LINE__));
  422. if (!textureHandle)
  423. {
  424. errorStr = String::ToString("Missing particle texture: %s", textureName);
  425. error = true;
  426. }
  427. }
  428. if (animateTexture)
  429. {
  430. // Here we parse animTexFramesString into byte-size frame numbers in animTexFrames.
  431. // Each frame token must be separated by whitespace.
  432. // A frame token must be a positive integer frame number or a range of frame numbers
  433. // separated with a '-'.
  434. // The range separator, '-', cannot have any whitspace around it.
  435. // Ranges can be specified to move through the frames in reverse as well as forward.
  436. // Frame numbers exceeding the number of tiles will wrap.
  437. // example:
  438. // "0-16 20 19 18 17 31-21"
  439. S32 n_tiles = animTexTiling.x * animTexTiling.y;
  440. AssertFatal(n_tiles > 0 && n_tiles <= 256, "Error, bad animTexTiling setting." );
  441. animTexFrames.clear();
  442. char* tokCopy = new char[dStrlen(animTexFramesString) + 1];
  443. dStrcpy(tokCopy, animTexFramesString);
  444. char* currTok = dStrtok(tokCopy, " \t");
  445. while (currTok != NULL)
  446. {
  447. char* minus = dStrchr(currTok, '-');
  448. if (minus)
  449. {
  450. // add a range of frames
  451. *minus = '\0';
  452. S32 range_a = dAtoi(currTok);
  453. S32 range_b = dAtoi(minus+1);
  454. if (range_b < range_a)
  455. {
  456. // reverse frame range
  457. for (S32 i = range_a; i >= range_b; i--)
  458. animTexFrames.push_back((U8)(i % n_tiles));
  459. }
  460. else
  461. {
  462. // forward frame range
  463. for (S32 i = range_a; i <= range_b; i++)
  464. animTexFrames.push_back((U8)(i % n_tiles));
  465. }
  466. }
  467. else
  468. {
  469. // add one frame
  470. animTexFrames.push_back((U8)(dAtoi(currTok) % n_tiles));
  471. }
  472. currTok = dStrtok(NULL, " \t");
  473. }
  474. // Here we pre-calculate the UVs for each frame tile, which are
  475. // tiled inside the UV region specified by texCoords. Since the
  476. // UVs are calculated using bilinear interpolation, the texCoords
  477. // region does *not* have to be an axis-aligned rectangle.
  478. if (animTexUVs)
  479. delete [] animTexUVs;
  480. animTexUVs = new Point2F[(animTexTiling.x+1)*(animTexTiling.y+1)];
  481. // interpolate points on the left and right edge of the uv quadrangle
  482. Point2F lf_pt = texCoords[0];
  483. Point2F rt_pt = texCoords[3];
  484. // per-row delta for left and right interpolated points
  485. Point2F lf_d = (texCoords[1] - texCoords[0])/(F32)animTexTiling.y;
  486. Point2F rt_d = (texCoords[2] - texCoords[3])/(F32)animTexTiling.y;
  487. S32 idx = 0;
  488. for (S32 yy = 0; yy <= animTexTiling.y; yy++)
  489. {
  490. Point2F p = lf_pt;
  491. Point2F dp = (rt_pt - lf_pt)/(F32)animTexTiling.x;
  492. for (S32 xx = 0; xx <= animTexTiling.x; xx++)
  493. {
  494. animTexUVs[idx++] = p;
  495. p += dp;
  496. }
  497. lf_pt += lf_d;
  498. rt_pt += rt_d;
  499. }
  500. // cleanup
  501. delete [] tokCopy;
  502. numFrames = animTexFrames.size();
  503. }
  504. }
  505. return !error;
  506. }
  507. //-----------------------------------------------------------------------------
  508. // Initialize particle
  509. //-----------------------------------------------------------------------------
  510. void ParticleData::initializeParticle(Particle* init, const Point3F& inheritVelocity)
  511. {
  512. init->dataBlock = this;
  513. // Calculate the constant accleration...
  514. init->vel += inheritVelocity * inheritedVelFactor;
  515. init->acc = init->vel * constantAcceleration;
  516. // Calculate this instance's lifetime...
  517. init->totalLifetime = lifetimeMS;
  518. if (lifetimeVarianceMS != 0)
  519. init->totalLifetime += S32(gRandGen.randI() % (2 * lifetimeVarianceMS + 1)) - S32(lifetimeVarianceMS);
  520. // assign spin amount
  521. init->spinSpeed = spinSpeed * gRandGen.randF( spinRandomMin, spinRandomMax );
  522. }
  523. bool ParticleData::reload(char errorBuffer[256])
  524. {
  525. bool error = false;
  526. if (textureName && textureName[0])
  527. {
  528. textureHandle = GFXTexHandle(textureName, &GFXDefaultStaticDiffuseProfile, avar("%s() - textureHandle (line %d)", __FUNCTION__, __LINE__));
  529. if (!textureHandle)
  530. {
  531. dSprintf(errorBuffer, 256, "Missing particle texture: %s", textureName);
  532. error = true;
  533. }
  534. }
  535. /*
  536. numFrames = 0;
  537. for( int i=0; i<PDC_MAX_TEX; i++ )
  538. {
  539. if( textureNameList[i] && textureNameList[i][0] )
  540. {
  541. textureList[i] = TextureHandle( textureNameList[i], MeshTexture );
  542. if (!textureList[i].getName())
  543. {
  544. dSprintf(errorBuffer, 256, "Missing particle texture: %s", textureNameList[i]);
  545. error = true;
  546. }
  547. numFrames++;
  548. }
  549. }
  550. */
  551. return !error;
  552. }
  553. DefineEngineMethod(ParticleData, reload, void, (),,
  554. "Reloads this particle.\n"
  555. "@tsexample\n"
  556. "// Get the editor's current particle\n"
  557. "%particle = PE_ParticleEditor.currParticle\n\n"
  558. "// Change a particle value\n"
  559. "%particle.setFieldValue( %propertyField, %value );\n\n"
  560. "// Reload it\n"
  561. "%particle.reload();\n"
  562. "@endtsexample\n" )
  563. {
  564. char errorBuffer[256];
  565. object->reload(errorBuffer);
  566. }