afxModel.cpp 25 KB

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