afxModel.cpp 25 KB

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