afxModel.cpp 25 KB

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