afxModel.cpp 25 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. sequence = ST_NULLSTRING;
  48. seq_rate = 1.0f;
  49. seq_offset = 0.0f;
  50. alpha_mult = 1.0f;
  51. use_vertex_alpha = false;
  52. force_on_material_flags = 0;
  53. force_off_material_flags = 0;
  54. texture_filtering = true;
  55. fog_mult = 1.0f;
  56. remap_txr_tags = ST_NULLSTRING;
  57. remap_buffer = 0;
  58. overrideLightingOptions = false;
  59. receiveSunLight = true;
  60. receiveLMLighting = true;
  61. useAdaptiveSelfIllumination = false;
  62. useCustomAmbientLighting = false;
  63. customAmbientForSelfIllumination = false;
  64. customAmbientLighting = LinearColorF(0.0f, 0.0f, 0.0f);
  65. shadowSize = 128;
  66. shadowMaxVisibleDistance = 80.0f;
  67. shadowProjectionDistance = 10.0f;
  68. shadowSphereAdjust = 1.0;
  69. mShapeAsset.registerRefreshNotify(this);
  70. }
  71. afxModelData::afxModelData(const afxModelData& other, bool temp_clone) : GameBaseData(other, temp_clone)
  72. {
  73. mShapeAsset = other.mShapeAsset;
  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. mShapeAsset.unregisterRefreshNotify();
  102. }
  103. bool afxModelData::preload(bool server, String &errorStr)
  104. {
  105. if (Parent::preload(server, errorStr) == false)
  106. return false;
  107. // don't need to do this stuff on the server
  108. if (server)
  109. return true;
  110. if (getShape())
  111. {
  112. // just parse up the string and collect the remappings in txr_tag_remappings.
  113. if (remap_txr_tags != ST_NULLSTRING)
  114. {
  115. txr_tag_remappings.clear();
  116. if (remap_buffer)
  117. dFree(remap_buffer);
  118. remap_buffer = dStrdup(remap_txr_tags);
  119. char* remap_token = dStrtok(remap_buffer, " \t");
  120. while (remap_token != NULL)
  121. {
  122. char* colon = dStrchr(remap_token, ':');
  123. if (colon)
  124. {
  125. *colon = '\0';
  126. txr_tag_remappings.increment();
  127. txr_tag_remappings.last().old_tag = remap_token;
  128. txr_tag_remappings.last().new_tag = colon+1;
  129. }
  130. remap_token = dStrtok(NULL, " \t");
  131. }
  132. }
  133. // this little hack messes things up when remapping texture tags
  134. if (txr_tag_remappings.size() == 0)
  135. {
  136. // this little hack forces the textures to preload
  137. TSShapeInstance* pDummy = new TSShapeInstance(getShape());
  138. delete pDummy;
  139. }
  140. }
  141. else
  142. {
  143. errorStr = String::ToString("afxModelData::load: Failed to load shape \"%s\"", _getShapeAssetId());
  144. return false;
  145. }
  146. return true;
  147. }
  148. #define myOffset(field) Offset(field, afxModelData)
  149. void afxModelData::initPersistFields()
  150. {
  151. docsURL;
  152. addGroup("Shapes");
  153. INITPERSISTFIELD_SHAPEASSET_REFACTOR(Shape, afxModelData, "The name of a .dts format file to use for the model.");
  154. endGroup("Shapes");
  155. addGroup("Animation");
  156. addField("sequence", TypeString, myOffset(sequence),
  157. "The name of an animation sequence to play in the model.");
  158. addFieldV("sequenceRate", TypeRangedF32, myOffset(seq_rate), &CommonValidators::F32Range,
  159. "The rate of playback for the sequence.");
  160. addFieldV("sequenceOffset", TypeRangedF32, myOffset(seq_offset), &CommonValidators::F32Range,
  161. "An offset in seconds indicating a starting point for the animation sequence "
  162. "specified by the sequence field. A rate of 1.0 (rather than sequenceRate) is used "
  163. "to convert from seconds to the thread offset.");
  164. endGroup("Animation");
  165. addGroup("Rendering");
  166. addFieldV("alphaMult", TypeRangedF32, myOffset(alpha_mult), &CommonValidators::PositiveFloat,
  167. "An alpha multiplier used to set maximum opacity of the model.");
  168. addFieldV("fogMult", TypeRangedF32, myOffset(fog_mult), &CommonValidators::PositiveFloat, "");
  169. addField("remapTextureTags", TypeString, myOffset(remap_txr_tags),
  170. "Rename one or more texture tags in the model. Texture tags are what link a "
  171. "model's textures to materials.\n"
  172. "Field should be a string containing space-separated remapping tokens. A remapping "
  173. "token is two names separated by a colon, ':'. The first name should be a texture-tag "
  174. "that exists in the model, while the second is a new name to replace it. The string "
  175. "can have any number of remapping tokens as long as the total string length does not "
  176. "exceed 255.");
  177. endGroup("Rendering");
  178. addGroup("Deprecated");
  179. addField("useVertexAlpha", TypeBool, myOffset(use_vertex_alpha),
  180. "deprecated");
  181. addField("forceOnMaterialFlags", TypeS32, myOffset(force_on_material_flags),
  182. "deprecated");
  183. addField("forceOffMaterialFlags", TypeS32, myOffset(force_off_material_flags),
  184. "deprecated");
  185. addField("textureFiltering", TypeBool, myOffset(texture_filtering),
  186. "deprecated");
  187. addField("overrideLightingOptions", TypeBool, myOffset(overrideLightingOptions),
  188. "deprecated");
  189. addField("receiveSunLight", TypeBool, myOffset(receiveSunLight),
  190. "");
  191. addField("receiveLMLighting", TypeBool, myOffset(receiveLMLighting),
  192. "deprecated");
  193. addField("useAdaptiveSelfIllumination", TypeBool, myOffset(useAdaptiveSelfIllumination),
  194. "deprecated");
  195. addField("useCustomAmbientLighting", TypeBool, myOffset(useCustomAmbientLighting),
  196. "deprecated");
  197. addField("customAmbientSelfIllumination", TypeBool, myOffset(customAmbientForSelfIllumination),
  198. "deprecated");
  199. addField("customAmbientLighting", TypeColorF, myOffset(customAmbientLighting),
  200. "deprecated");
  201. addField("shadowSize", TypeS32, myOffset(shadowSize),
  202. "deprecated");
  203. addField("shadowMaxVisibleDistance", TypeF32, myOffset(shadowMaxVisibleDistance),
  204. "deprecated");
  205. addField("shadowProjectionDistance", TypeF32, myOffset(shadowProjectionDistance),
  206. "deprecated");
  207. addField("shadowSphereAdjust", TypeF32, myOffset(shadowSphereAdjust),
  208. "deprecated");
  209. endGroup("Deprecated");
  210. Parent::initPersistFields();
  211. // Material Flags
  212. Con::setIntVariable("$MaterialFlags::S_Wrap", TSMaterialList::S_Wrap);
  213. Con::setIntVariable("$MaterialFlags::T_Wrap", TSMaterialList::T_Wrap);
  214. Con::setIntVariable("$MaterialFlags::Translucent", TSMaterialList::Translucent);
  215. Con::setIntVariable("$MaterialFlags::Additive", TSMaterialList::Additive);
  216. Con::setIntVariable("$MaterialFlags::Subtractive", TSMaterialList::Subtractive);
  217. Con::setIntVariable("$MaterialFlags::SelfIlluminating", TSMaterialList::SelfIlluminating);
  218. Con::setIntVariable("$MaterialFlags::NeverEnvMap", TSMaterialList::NeverEnvMap);
  219. Con::setIntVariable("$MaterialFlags::NoMipMap", TSMaterialList::NoMipMap);
  220. Con::setIntVariable("$MaterialFlags::MipMap_ZeroBorder", TSMaterialList::MipMap_ZeroBorder);
  221. Con::setIntVariable("$MaterialFlags::AuxiliaryMap", TSMaterialList::AuxiliaryMap);
  222. #if defined(AFX_CAP_AFXMODEL_TYPE)
  223. Con::setIntVariable("$TypeMasks::afxModelObjectType", afxModelObjectType);
  224. #endif
  225. }
  226. void afxModelData::packData(BitStream* stream)
  227. {
  228. Parent::packData(stream);
  229. PACKDATA_ASSET_REFACTOR(Shape);
  230. stream->writeString(sequence);
  231. stream->write(seq_rate);
  232. stream->write(seq_offset);
  233. stream->write(alpha_mult);
  234. stream->write(use_vertex_alpha);
  235. stream->write(force_on_material_flags);
  236. stream->write(force_off_material_flags);
  237. stream->writeFlag(texture_filtering);
  238. stream->write(fog_mult);
  239. stream->writeString(remap_txr_tags);
  240. stream->writeFlag(overrideLightingOptions);
  241. stream->writeFlag(receiveSunLight);
  242. stream->writeFlag(useAdaptiveSelfIllumination);
  243. stream->writeFlag(useCustomAmbientLighting);
  244. stream->writeFlag(customAmbientForSelfIllumination);
  245. stream->write(customAmbientLighting);
  246. stream->writeFlag(receiveLMLighting);
  247. stream->write(shadowSize);
  248. stream->write(shadowMaxVisibleDistance);
  249. stream->write(shadowProjectionDistance);
  250. stream->write(shadowSphereAdjust);
  251. }
  252. void afxModelData::unpackData(BitStream* stream)
  253. {
  254. Parent::unpackData(stream);
  255. UNPACKDATA_ASSET_REFACTOR(Shape);
  256. sequence = stream->readSTString();
  257. stream->read(&seq_rate);
  258. stream->read(&seq_offset);
  259. stream->read(&alpha_mult);
  260. stream->read(&use_vertex_alpha);
  261. stream->read(&force_on_material_flags);
  262. stream->read(&force_off_material_flags);
  263. texture_filtering = stream->readFlag();
  264. stream->read(&fog_mult);
  265. remap_txr_tags = stream->readSTString();
  266. overrideLightingOptions = stream->readFlag();
  267. receiveSunLight = stream->readFlag();
  268. useAdaptiveSelfIllumination = stream->readFlag();
  269. useCustomAmbientLighting = stream->readFlag();
  270. customAmbientForSelfIllumination = stream->readFlag();
  271. stream->read(&customAmbientLighting);
  272. receiveLMLighting = stream->readFlag();
  273. stream->read(&shadowSize);
  274. stream->read(&shadowMaxVisibleDistance);
  275. stream->read(&shadowProjectionDistance);
  276. stream->read(&shadowSphereAdjust);
  277. }
  278. void afxModelData::onPerformSubstitutions()
  279. {
  280. if (!getShape())
  281. {
  282. Con::errorf("afxModelData::onPerformSubstitutions: Failed to load shape \"%s\"", _getShapeAssetId());
  283. return;
  284. }
  285. }
  286. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  287. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  288. // afxModel
  289. IMPLEMENT_CO_NETOBJECT_V1(afxModel);
  290. ConsoleDocClass( afxModel,
  291. "@brief A Model effect as defined by an afxModelData datablock.\n\n"
  292. "A Model effect is a lightweight client-only geometry object useful for effect-driven "
  293. "props.\n"
  294. "@ingroup afxEffects\n"
  295. "@ingroup AFX\n"
  296. );
  297. afxModel::afxModel()
  298. {
  299. mTypeMask |= DynamicShapeObjectType;
  300. #if defined(AFX_CAP_AFXMODEL_TYPE)
  301. mTypeMask |= afxModelObjectType;
  302. #endif
  303. shape_inst = 0;
  304. main_seq_thread = 0;
  305. main_seq_id = -1;
  306. seq_rate_factor = 1.0f;
  307. last_anim_tag = 0;
  308. seq_animates_vis = false;
  309. fade_amt = 1.0f;
  310. is_visible = true;
  311. sort_priority = 0;
  312. mDataBlock = NULL;
  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->getShape())
  342. mObjBox = mDataBlock->getShape()->mBounds;
  343. else
  344. mObjBox = Box3F(Point3F(-1, -1, -1), Point3F(1, 1, 1));
  345. // setup the shape instance and sequence
  346. if (mDataBlock->getShape())
  347. {
  348. if (/*isClientObject() && */mDataBlock->txr_tag_remappings.size() > 0)
  349. {
  350. // temporarily substitute material tags with alternates
  351. TSMaterialList* mat_list = mDataBlock->getShape()->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->getShape());
  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->getShape()->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->getShape())
  438. {
  439. // Scan out the collision hulls...
  440. static const String sCollisionStr( "collision-" );
  441. for (U32 i = 0; i < mDataBlock->getShape()->details.size(); i++)
  442. {
  443. const String &name = mDataBlock->getShape()->names[mDataBlock->getShape()->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->getShape()->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->getShape()->details.size(); i++)
  464. {
  465. const String &name = mDataBlock->getShape()->names[mDataBlock->getShape()->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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//