afxModel.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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 "T3D/objectTypes.h"
  26. #include "T3D/gameBase/gameProcess.h"
  27. #include "core/resourceManager.h"
  28. #include "sim/netConnection.h"
  29. #include "scene/sceneRenderState.h"
  30. #include "scene/sceneManager.h"
  31. #include "ts/tsShapeInstance.h"
  32. #include "ts/tsMaterialList.h"
  33. #include "afx/ce/afxModel.h"
  34. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  35. // afxModelData
  36. IMPLEMENT_CO_DATABLOCK_V1(afxModelData);
  37. ConsoleDocClass( afxModelData,
  38. "@brief A datablock that specifies a Model effect.\n\n"
  39. "A Model effect is a lightweight client-only geometry object useful for effect-driven props."
  40. "\n\n"
  41. "@ingroup afxEffects\n"
  42. "@ingroup AFX\n"
  43. "@ingroup Datablocks\n"
  44. );
  45. afxModelData::afxModelData()
  46. {
  47. shapeName = ST_NULLSTRING;
  48. sequence = ST_NULLSTRING;
  49. seq_rate = 1.0f;
  50. seq_offset = 0.0f;
  51. alpha_mult = 1.0f;
  52. use_vertex_alpha = false;
  53. force_on_material_flags = 0;
  54. force_off_material_flags = 0;
  55. texture_filtering = true;
  56. fog_mult = 1.0f;
  57. remap_txr_tags = ST_NULLSTRING;
  58. remap_buffer = 0;
  59. overrideLightingOptions = false;
  60. receiveSunLight = true;
  61. receiveLMLighting = true;
  62. useAdaptiveSelfIllumination = false;
  63. useCustomAmbientLighting = false;
  64. customAmbientForSelfIllumination = false;
  65. customAmbientLighting = LinearColorF(0.0f, 0.0f, 0.0f);
  66. shadowEnable = false;
  67. shadowSize = 128;
  68. shadowMaxVisibleDistance = 80.0f;
  69. shadowProjectionDistance = 10.0f;
  70. shadowSphereAdjust = 1.0;
  71. }
  72. afxModelData::afxModelData(const afxModelData& other, bool temp_clone) : GameBaseData(other, temp_clone)
  73. {
  74. shapeName = other.shapeName;
  75. shape = other.shape; // --
  76. sequence = other.sequence;
  77. seq_rate = other.seq_rate;
  78. seq_offset = other.seq_offset;
  79. alpha_mult = other.alpha_mult;
  80. use_vertex_alpha = other.use_vertex_alpha;
  81. force_on_material_flags = other.force_on_material_flags;
  82. force_off_material_flags = other.force_off_material_flags;
  83. texture_filtering = other.texture_filtering;
  84. fog_mult = other.fog_mult;
  85. remap_txr_tags = other.remap_txr_tags;
  86. remap_buffer = other.remap_buffer;
  87. overrideLightingOptions = other.overrideLightingOptions;
  88. receiveSunLight = other.receiveSunLight;
  89. receiveLMLighting = other.receiveLMLighting;
  90. useAdaptiveSelfIllumination = other.useAdaptiveSelfIllumination;
  91. useCustomAmbientLighting = other.useCustomAmbientLighting;
  92. customAmbientForSelfIllumination = other.customAmbientForSelfIllumination;
  93. customAmbientLighting = other.customAmbientLighting;
  94. shadowEnable = other.shadowEnable;
  95. }
  96. afxModelData::~afxModelData()
  97. {
  98. if (remap_buffer)
  99. dFree(remap_buffer);
  100. }
  101. bool afxModelData::preload(bool server, String &errorStr)
  102. {
  103. if (Parent::preload(server, errorStr) == false)
  104. return false;
  105. // don't need to do this stuff on the server
  106. if (server)
  107. return true;
  108. if (shapeName != ST_NULLSTRING && !shape)
  109. {
  110. shape = ResourceManager::get().load(shapeName);
  111. if (!shape)
  112. {
  113. errorStr = String::ToString("afxModelData::load: Failed to load shape \"%s\"", shapeName);
  114. return false;
  115. }
  116. // just parse up the string and collect the remappings in txr_tag_remappings.
  117. if (remap_txr_tags != ST_NULLSTRING)
  118. {
  119. txr_tag_remappings.clear();
  120. if (remap_buffer)
  121. dFree(remap_buffer);
  122. remap_buffer = dStrdup(remap_txr_tags);
  123. char* remap_token = dStrtok(remap_buffer, " \t");
  124. while (remap_token != NULL)
  125. {
  126. char* colon = dStrchr(remap_token, ':');
  127. if (colon)
  128. {
  129. *colon = '\0';
  130. txr_tag_remappings.increment();
  131. txr_tag_remappings.last().old_tag = remap_token;
  132. txr_tag_remappings.last().new_tag = colon+1;
  133. }
  134. remap_token = dStrtok(NULL, " \t");
  135. }
  136. }
  137. // this little hack messes things up when remapping texture tags
  138. if (txr_tag_remappings.size() == 0)
  139. {
  140. // this little hack forces the textures to preload
  141. TSShapeInstance* pDummy = new TSShapeInstance(shape);
  142. delete pDummy;
  143. }
  144. }
  145. return true;
  146. }
  147. #define myOffset(field) Offset(field, afxModelData)
  148. void afxModelData::initPersistFields()
  149. {
  150. addField("shapeFile", TypeFilename, myOffset(shapeName),
  151. "The name of a .dts format file to use for the model.");
  152. addField("sequence", TypeFilename, myOffset(sequence),
  153. "The name of an animation sequence to play in the model.");
  154. addField("sequenceRate", TypeF32, myOffset(seq_rate),
  155. "The rate of playback for the sequence.");
  156. addField("sequenceOffset", TypeF32, myOffset(seq_offset),
  157. "An offset in seconds indicating a starting point for the animation sequence "
  158. "specified by the sequence field. A rate of 1.0 (rather than sequenceRate) is used "
  159. "to convert from seconds to the thread offset.");
  160. addField("alphaMult", TypeF32, myOffset(alpha_mult),
  161. "An alpha multiplier used to set maximum opacity of the model.");
  162. addField("fogMult", TypeF32, myOffset(fog_mult),
  163. "");
  164. addField("remapTextureTags", TypeString, myOffset(remap_txr_tags),
  165. "Rename one or more texture tags in the model. Texture tags are what link a "
  166. "model's textures to materials.\n"
  167. "Field should be a string containing space-separated remapping tokens. A remapping "
  168. "token is two names separated by a colon, ':'. The first name should be a texture-tag "
  169. "that exists in the model, while the second is a new name to replace it. The string "
  170. "can have any number of remapping tokens as long as the total string length does not "
  171. "exceed 255.");
  172. addField("shadowEnable", TypeBool, myOffset(shadowEnable),
  173. "Sets whether the model casts a shadow.");
  174. addField("useVertexAlpha", TypeBool, myOffset(use_vertex_alpha),
  175. "deprecated");
  176. addField("forceOnMaterialFlags", TypeS32, myOffset(force_on_material_flags),
  177. "deprecated");
  178. addField("forceOffMaterialFlags", TypeS32, myOffset(force_off_material_flags),
  179. "deprecated");
  180. addField("textureFiltering", TypeBool, myOffset(texture_filtering),
  181. "deprecated");
  182. addField("overrideLightingOptions", TypeBool, myOffset(overrideLightingOptions),
  183. "deprecated");
  184. addField("receiveSunLight", TypeBool, myOffset(receiveSunLight),
  185. "");
  186. addField("receiveLMLighting", TypeBool, myOffset(receiveLMLighting),
  187. "deprecated");
  188. addField("useAdaptiveSelfIllumination", TypeBool, myOffset(useAdaptiveSelfIllumination),
  189. "deprecated");
  190. addField("useCustomAmbientLighting", TypeBool, myOffset(useCustomAmbientLighting),
  191. "deprecated");
  192. addField("customAmbientSelfIllumination", TypeBool, myOffset(customAmbientForSelfIllumination),
  193. "deprecated");
  194. addField("customAmbientLighting", TypeColorF, myOffset(customAmbientLighting),
  195. "deprecated");
  196. addField("shadowSize", TypeS32, myOffset(shadowSize),
  197. "deprecated");
  198. addField("shadowMaxVisibleDistance", TypeF32, myOffset(shadowMaxVisibleDistance),
  199. "deprecated");
  200. addField("shadowProjectionDistance", TypeF32, myOffset(shadowProjectionDistance),
  201. "deprecated");
  202. addField("shadowSphereAdjust", TypeF32, myOffset(shadowSphereAdjust),
  203. "deprecated");
  204. Parent::initPersistFields();
  205. // Material Flags
  206. Con::setIntVariable("$MaterialFlags::S_Wrap", TSMaterialList::S_Wrap);
  207. Con::setIntVariable("$MaterialFlags::T_Wrap", TSMaterialList::T_Wrap);
  208. Con::setIntVariable("$MaterialFlags::Translucent", TSMaterialList::Translucent);
  209. Con::setIntVariable("$MaterialFlags::Additive", TSMaterialList::Additive);
  210. Con::setIntVariable("$MaterialFlags::Subtractive", TSMaterialList::Subtractive);
  211. Con::setIntVariable("$MaterialFlags::SelfIlluminating", TSMaterialList::SelfIlluminating);
  212. Con::setIntVariable("$MaterialFlags::NeverEnvMap", TSMaterialList::NeverEnvMap);
  213. Con::setIntVariable("$MaterialFlags::NoMipMap", TSMaterialList::NoMipMap);
  214. Con::setIntVariable("$MaterialFlags::MipMap_ZeroBorder", TSMaterialList::MipMap_ZeroBorder);
  215. Con::setIntVariable("$MaterialFlags::AuxiliaryMap", TSMaterialList::AuxiliaryMap);
  216. #if defined(AFX_CAP_AFXMODEL_TYPE)
  217. Con::setIntVariable("$TypeMasks::afxModelObjectType", afxModelObjectType);
  218. #endif
  219. }
  220. void afxModelData::packData(BitStream* stream)
  221. {
  222. Parent::packData(stream);
  223. stream->writeString(shapeName);
  224. stream->writeString(sequence);
  225. stream->write(seq_rate);
  226. stream->write(seq_offset);
  227. stream->write(alpha_mult);
  228. stream->write(use_vertex_alpha);
  229. stream->write(force_on_material_flags);
  230. stream->write(force_off_material_flags);
  231. stream->writeFlag(texture_filtering);
  232. stream->write(fog_mult);
  233. stream->writeString(remap_txr_tags);
  234. stream->writeFlag(overrideLightingOptions);
  235. stream->writeFlag(receiveSunLight);
  236. stream->writeFlag(useAdaptiveSelfIllumination);
  237. stream->writeFlag(useCustomAmbientLighting);
  238. stream->writeFlag(customAmbientForSelfIllumination);
  239. stream->write(customAmbientLighting);
  240. stream->writeFlag(receiveLMLighting);
  241. stream->writeFlag(shadowEnable);
  242. stream->write(shadowSize);
  243. stream->write(shadowMaxVisibleDistance);
  244. stream->write(shadowProjectionDistance);
  245. stream->write(shadowSphereAdjust);
  246. }
  247. void afxModelData::unpackData(BitStream* stream)
  248. {
  249. Parent::unpackData(stream);
  250. shapeName = stream->readSTString();
  251. sequence = stream->readSTString();
  252. stream->read(&seq_rate);
  253. stream->read(&seq_offset);
  254. stream->read(&alpha_mult);
  255. stream->read(&use_vertex_alpha);
  256. stream->read(&force_on_material_flags);
  257. stream->read(&force_off_material_flags);
  258. texture_filtering = stream->readFlag();
  259. stream->read(&fog_mult);
  260. remap_txr_tags = stream->readSTString();
  261. overrideLightingOptions = stream->readFlag();
  262. receiveSunLight = stream->readFlag();
  263. useAdaptiveSelfIllumination = stream->readFlag();
  264. useCustomAmbientLighting = stream->readFlag();
  265. customAmbientForSelfIllumination = stream->readFlag();
  266. stream->read(&customAmbientLighting);
  267. receiveLMLighting = stream->readFlag();
  268. shadowEnable = stream->readFlag();
  269. stream->read(&shadowSize);
  270. stream->read(&shadowMaxVisibleDistance);
  271. stream->read(&shadowProjectionDistance);
  272. stream->read(&shadowSphereAdjust);
  273. }
  274. void afxModelData::onPerformSubstitutions()
  275. {
  276. if (shapeName != ST_NULLSTRING)
  277. {
  278. shape = ResourceManager::get().load(shapeName);
  279. if (!shape)
  280. {
  281. Con::errorf("afxModelData::onPerformSubstitutions: Failed to load shape \"%s\"", shapeName);
  282. return;
  283. }
  284. // REMAP-TEXTURE-TAGS ISSUES?
  285. }
  286. }
  287. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  288. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  289. // afxModel
  290. IMPLEMENT_CO_NETOBJECT_V1(afxModel);
  291. ConsoleDocClass( afxModel,
  292. "@brief A Model effect as defined by an afxModelData datablock.\n\n"
  293. "A Model effect is a lightweight client-only geometry object useful for effect-driven "
  294. "props.\n"
  295. "@ingroup afxEffects\n"
  296. "@ingroup AFX\n"
  297. );
  298. afxModel::afxModel()
  299. {
  300. mTypeMask |= DynamicShapeObjectType;
  301. #if defined(AFX_CAP_AFXMODEL_TYPE)
  302. mTypeMask |= afxModelObjectType;
  303. #endif
  304. shape_inst = 0;
  305. main_seq_thread = 0;
  306. main_seq_id = -1;
  307. seq_rate_factor = 1.0f;
  308. last_anim_tag = 0;
  309. seq_animates_vis = false;
  310. fade_amt = 1.0f;
  311. is_visible = true;
  312. sort_priority = 0;
  313. mNetFlags.set( IsGhost );
  314. }
  315. afxModel::~afxModel()
  316. {
  317. delete shape_inst;
  318. }
  319. void afxModel::setSequenceRateFactor(F32 factor)
  320. {
  321. seq_rate_factor = factor;
  322. if (shape_inst != NULL && main_seq_thread != NULL)
  323. shape_inst->setTimeScale(main_seq_thread, seq_rate_factor*mDataBlock->seq_rate);
  324. }
  325. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  326. bool afxModel::onNewDataBlock(GameBaseData* dptr, bool reload)
  327. {
  328. mDataBlock = dynamic_cast<afxModelData*>(dptr);
  329. if (!mDataBlock || !Parent::onNewDataBlock(dptr, reload))
  330. return false;
  331. return true;
  332. }
  333. bool afxModel::onAdd()
  334. {
  335. // first check if we have a server connection, if we don't then this is on the server
  336. // and we should exit, then check if the parent fails to add the object
  337. NetConnection* conn = NetConnection::getConnectionToServer();
  338. if (!conn || !Parent::onAdd())
  339. return false;
  340. // setup our bounding box
  341. if (mDataBlock->shape)
  342. mObjBox = mDataBlock->shape->mBounds;
  343. else
  344. mObjBox = Box3F(Point3F(-1, -1, -1), Point3F(1, 1, 1));
  345. // setup the shape instance and sequence
  346. if (mDataBlock->shape)
  347. {
  348. if (/*isClientObject() && */mDataBlock->txr_tag_remappings.size() > 0)
  349. {
  350. // temporarily substitute material tags with alternates
  351. TSMaterialList* mat_list = mDataBlock->shape->materialList;
  352. if (mat_list)
  353. {
  354. for (S32 i = 0; i < mDataBlock->txr_tag_remappings.size(); i++)
  355. {
  356. afxModelData::TextureTagRemapping* remap = &mDataBlock->txr_tag_remappings[i];
  357. Vector<String> & mat_names = (Vector<String>&) mat_list->getMaterialNameList();
  358. for (S32 j = 0; j < mat_names.size(); j++)
  359. {
  360. if (mat_names[j].compare(remap->old_tag, dStrlen(remap->old_tag), String::NoCase) == 0)
  361. {
  362. //Con::printf("REMAP TEXTURE TAG [%s] TO [%s]", remap->old_tag, remap->new_tag);
  363. mat_names[j] = String(remap->new_tag);
  364. mat_names[j].insert(0,'#');
  365. break;
  366. }
  367. }
  368. }
  369. }
  370. }
  371. shape_inst = new TSShapeInstance(mDataBlock->shape);
  372. if (true) // isClientObject())
  373. {
  374. shape_inst->cloneMaterialList();
  375. // restore the material tags to original form
  376. if (mDataBlock->txr_tag_remappings.size() > 0)
  377. {
  378. TSMaterialList* mat_list = mDataBlock->shape->materialList;
  379. if (mat_list)
  380. {
  381. for (S32 i = 0; i < mDataBlock->txr_tag_remappings.size(); i++)
  382. {
  383. afxModelData::TextureTagRemapping* remap = &mDataBlock->txr_tag_remappings[i];
  384. Vector<String> & mat_names = (Vector<String>&) mat_list->getMaterialNameList();
  385. for (S32 j = 0; j < mat_names.size(); j++)
  386. {
  387. if (mat_names[j].compare(remap->new_tag, dStrlen(remap->new_tag)) == 0)
  388. {
  389. //Con::printf("UNREMAP TEXTURE TAG [%s] TO [%s]", remap->new_tag, remap->old_tag);
  390. mat_names[j] = String(remap->old_tag);
  391. break;
  392. }
  393. }
  394. }
  395. }
  396. }
  397. }
  398. if (mDataBlock->sequence == ST_NULLSTRING)
  399. {
  400. main_seq_thread = 0;
  401. main_seq_id = -1;
  402. }
  403. else
  404. {
  405. // here we start the default animation sequence
  406. TSShape* shape = shape_inst->getShape();
  407. main_seq_id = shape->findSequence(mDataBlock->sequence);
  408. if (main_seq_id != -1)
  409. {
  410. main_seq_thread = shape_inst->addThread();
  411. F32 seq_pos = 0.0f;
  412. if (mDataBlock->seq_offset > 0.0f && mDataBlock->seq_offset < shape_inst->getDuration(main_seq_thread))
  413. seq_pos = mDataBlock->seq_offset / shape_inst->getDuration(main_seq_thread);
  414. shape_inst->setTimeScale(main_seq_thread, seq_rate_factor*mDataBlock->seq_rate);
  415. shape_inst->setSequence(main_seq_thread, main_seq_id, seq_pos);
  416. seq_animates_vis = shape->sequences[main_seq_id].visMatters.testAll();
  417. }
  418. }
  419. // deal with material changes
  420. if (shape_inst && (mDataBlock->force_on_material_flags | mDataBlock->force_off_material_flags))
  421. {
  422. shape_inst->cloneMaterialList();
  423. TSMaterialList* mats = shape_inst->getMaterialList();
  424. if (mDataBlock->force_on_material_flags != 0)
  425. {
  426. for (U32 i = 0; i < mats->size(); i++)
  427. mats->setFlags(i, mats->getFlags(i) | mDataBlock->force_on_material_flags);
  428. }
  429. if (mDataBlock->force_off_material_flags != 0)
  430. {
  431. for (U32 i = 0; i < mats->size(); i++)
  432. mats->setFlags(i, mats->getFlags(i) & ~mDataBlock->force_off_material_flags);
  433. }
  434. }
  435. }
  436. resetWorldBox();
  437. if (mDataBlock->shape)
  438. {
  439. // Scan out the collision hulls...
  440. static const String sCollisionStr( "collision-" );
  441. for (U32 i = 0; i < mDataBlock->shape->details.size(); i++)
  442. {
  443. const String &name = mDataBlock->shape->names[mDataBlock->shape->details[i].nameIndex];
  444. if (name.compare( sCollisionStr, sCollisionStr.length(), String::NoCase ) == 0)
  445. {
  446. mCollisionDetails.push_back(i);
  447. // The way LOS works is that it will check to see if there is a LOS detail that matches
  448. // the the collision detail + 1 + MaxCollisionShapes (this variable name should change in
  449. // the future). If it can't find a matching LOS it will simply use the collision instead.
  450. // We check for any "unmatched" LOS's further down
  451. mLOSDetails.increment();
  452. char buff[128];
  453. dSprintf(buff, sizeof(buff), "LOS-%d", i + 1 + 8/*MaxCollisionShapes*/);
  454. U32 los = mDataBlock->shape->findDetail(buff);
  455. if (los == -1)
  456. mLOSDetails.last() = i;
  457. else
  458. mLOSDetails.last() = los;
  459. }
  460. }
  461. // Snag any "unmatched" LOS details
  462. static const String sLOSStr( "LOS-" );
  463. for (U32 i = 0; i < mDataBlock->shape->details.size(); i++)
  464. {
  465. const String &name = mDataBlock->shape->names[mDataBlock->shape->details[i].nameIndex];
  466. if (name.compare( sLOSStr, sLOSStr.length(), String::NoCase ) == 0)
  467. {
  468. // See if we already have this LOS
  469. bool found = false;
  470. for (U32 j = 0; j < mLOSDetails.size(); j++)
  471. {
  472. if (mLOSDetails[j] == i)
  473. {
  474. found = true;
  475. break;
  476. }
  477. }
  478. if (!found)
  479. mLOSDetails.push_back(i);
  480. }
  481. }
  482. // Compute the hull accelerators (actually, just force the shape to compute them)
  483. for (U32 i = 0; i < mCollisionDetails.size(); i++)
  484. shape_inst->getShape()->getAccelerator(mCollisionDetails[i]);
  485. }
  486. // tell engine the model exists
  487. gClientSceneGraph->addObjectToScene(this);
  488. removeFromProcessList();
  489. ClientProcessList::get()->addObject(this);
  490. conn->addObject(this);
  491. return true;
  492. }
  493. void afxModel::onRemove()
  494. {
  495. mSceneManager->removeObjectFromScene(this);
  496. getContainer()->removeObject(this);
  497. Parent::onRemove();
  498. }
  499. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  500. void afxModel::advanceTime(F32 dt)
  501. {
  502. if (main_seq_thread)
  503. shape_inst->advanceTime(dt, main_seq_thread);
  504. for (S32 i = 0; i < blend_clips.size(); i++)
  505. shape_inst->advanceTime(dt, blend_clips[i].thread);
  506. }
  507. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  508. void afxModel::prepRenderImage(SceneRenderState* state)
  509. {
  510. if (!is_visible || !shape_inst)
  511. return;
  512. // calculate distance to camera
  513. Point3F cameraOffset;
  514. getRenderTransform().getColumn(3, &cameraOffset);
  515. cameraOffset -= state->getCameraPosition();
  516. F32 dist = cameraOffset.len();
  517. if (dist < 0.01f)
  518. dist = 0.01f;
  519. F32 invScale = (1.0f/getMax(getMax(mObjScale.x,mObjScale.y),mObjScale.z));
  520. shape_inst->setDetailFromDistance(state, dist*invScale);
  521. if ( shape_inst->getCurrentDetail() < 0 )
  522. return;
  523. renderObject(state);
  524. }
  525. bool afxModel::castRay(const Point3F &start, const Point3F &end, RayInfo* info)
  526. {
  527. if (shape_inst)
  528. {
  529. RayInfo shortest;
  530. shortest.t = 1e8;
  531. info->object = NULL;
  532. if (mLOSDetails.size() > 0)
  533. {
  534. for (U32 i = 0; i < mLOSDetails.size(); i++)
  535. {
  536. shape_inst->animate(mLOSDetails[i]);
  537. if (shape_inst->castRay(start, end, info, mLOSDetails[i]))
  538. {
  539. info->object = this;
  540. if (info->t < shortest.t)
  541. shortest = *info;
  542. }
  543. }
  544. }
  545. else
  546. {
  547. if (mCollisionDetails.size() > 0)
  548. {
  549. for (U32 i = 0; i < mCollisionDetails.size(); i++)
  550. {
  551. shape_inst->animate(mCollisionDetails[i]);
  552. if (shape_inst->castRay(start, end, info, mCollisionDetails[i]))
  553. {
  554. info->object = this;
  555. if (info->t < shortest.t)
  556. shortest = *info;
  557. }
  558. }
  559. }
  560. }
  561. if (info->object == this)
  562. {
  563. // Copy out the shortest time...
  564. *info = shortest;
  565. return true;
  566. }
  567. }
  568. return false;
  569. }
  570. U32 afxModel::unique_anim_tag_counter = 1;
  571. #define BAD_ANIM_ID 999999999
  572. U32 afxModel::setAnimClip(const char* clip, F32 pos, F32 rate, F32 trans)
  573. {
  574. if (!shape_inst)
  575. return 0;
  576. TSShape* shape = shape_inst->getShape();
  577. S32 seq_id = shape->findSequence(clip);
  578. if (seq_id == -1)
  579. {
  580. Con::errorf("afxModel::setAnimClip() -- failed to find a sequence matching the name, \"%s\".", clip);
  581. return 0;
  582. }
  583. // JTF Note: test if this blend implementation is working
  584. if (shape->sequences[seq_id].isBlend())
  585. {
  586. BlendThread blend_clip;
  587. blend_clip.tag = ((unique_anim_tag_counter++) | 0x80000000);
  588. blend_clip.thread = shape_inst->addThread();
  589. shape_inst->setSequence(blend_clip.thread, seq_id, pos);
  590. shape_inst->setTimeScale(blend_clip.thread, rate);
  591. blend_clips.push_back(blend_clip);
  592. return blend_clip.tag;
  593. }
  594. if (!main_seq_thread)
  595. {
  596. main_seq_thread = shape_inst->addThread();
  597. shape_inst->setTimeScale(main_seq_thread, seq_rate_factor*rate);
  598. shape_inst->setSequence(main_seq_thread, seq_id, pos);
  599. seq_animates_vis = shape->sequences[seq_id].visMatters.testAll();
  600. }
  601. else
  602. {
  603. shape_inst->setTimeScale(main_seq_thread, seq_rate_factor*rate);
  604. F32 transTime = (trans < 0) ? 0.25 : trans;
  605. if (transTime > 0.0f)
  606. shape_inst->transitionToSequence(main_seq_thread, seq_id, pos, transTime, true);
  607. else
  608. shape_inst->setSequence(main_seq_thread, seq_id, pos);
  609. seq_animates_vis = shape->sequences[seq_id].visMatters.testAll();
  610. }
  611. last_anim_tag = unique_anim_tag_counter++;
  612. return last_anim_tag;
  613. }
  614. void afxModel::resetAnimation(U32 tag)
  615. {
  616. // check if this is a blended clip
  617. if ((tag & 0x80000000) != 0)
  618. {
  619. for (S32 i = 0; i < blend_clips.size(); i++)
  620. {
  621. if (blend_clips[i].tag == tag)
  622. {
  623. if (blend_clips[i].thread)
  624. {
  625. //Con::printf("DESTROY THREAD %d of %d tag=%d" , i, blend_clips.size(), tag & 0x7fffffff);
  626. shape_inst->destroyThread(blend_clips[i].thread);
  627. }
  628. blend_clips.erase_fast(i);
  629. break;
  630. }
  631. }
  632. return;
  633. }
  634. if (tag != 0 && tag == last_anim_tag)
  635. {
  636. // restore original non-animated state
  637. if (main_seq_id == -1)
  638. {
  639. shape_inst->destroyThread(main_seq_thread);
  640. main_seq_thread = 0;
  641. }
  642. // restore original sequence
  643. else
  644. {
  645. shape_inst->setTimeScale(main_seq_thread, seq_rate_factor*mDataBlock->seq_rate);
  646. shape_inst->transitionToSequence(main_seq_thread, main_seq_id , 0.0f, 0.25f, true);
  647. }
  648. last_anim_tag = 0;
  649. }
  650. }
  651. F32 afxModel::getAnimClipDuration(const char* clip)
  652. {
  653. if (!shape_inst)
  654. return 0.0f;
  655. TSShape* shape = shape_inst->getShape();
  656. S32 seq_id = shape->findSequence(clip);
  657. return (seq_id != -1) ? shape->sequences[seq_id].duration : 0.0f;
  658. }
  659. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//