particle.cpp 23 KB

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