llmotioncontroller.cpp 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. /**
  2. * @file llmotioncontroller.cpp
  3. * @brief Implementation of LLMotionController class.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include <algorithm>
  34. #include "llmotioncontroller.h"
  35. #include "llanimationstates.h"
  36. #include "llfasttimer.h"
  37. #include "llframetimer.h"
  38. #include "llkeyframemotion.h"
  39. #include "llmath.h"
  40. #include "llstl.h"
  41. #include "lltimer.h"
  42. // This is why LL_CHARACTER_MAX_ANIMATED_JOINTS needs to be a multiple of 4:
  43. constexpr S32 NUM_JOINT_SIGNATURE_STRIDES = LL_CHARACTER_MAX_ANIMATED_JOINTS / 4;
  44. constexpr U32 MAX_MOTION_INSTANCES = 32;
  45. uuid_vec_t LLMotionController::sMotionsToKill;
  46. F32 LLMotionController::sTimeFactorMultiplier = 1.f;
  47. LLMotionRegistry LLMotionController::sRegistry;
  48. //-----------------------------------------------------------------------------
  49. // LLMotionRegistry class
  50. //-----------------------------------------------------------------------------
  51. LLMotionRegistry::~LLMotionRegistry()
  52. {
  53. mMotionTable.clear();
  54. }
  55. bool LLMotionRegistry::registerMotion(const LLUUID& id,
  56. LLMotionConstructor constructor)
  57. {
  58. if (mMotionTable.count(id))
  59. {
  60. return false;
  61. }
  62. mMotionTable.emplace(id, constructor);
  63. return true;
  64. }
  65. void LLMotionRegistry::markBad(const LLUUID& id)
  66. {
  67. mMotionTable[id] = LLMotionConstructor(NULL);
  68. }
  69. LLMotion* LLMotionRegistry::createMotion(const LLUUID& id)
  70. {
  71. LLMotionConstructor constructor = get_if_there(mMotionTable, id,
  72. LLMotionConstructor(NULL));
  73. if (constructor)
  74. {
  75. return constructor(id);
  76. }
  77. // *FIX: need to replace with a better default scheme. RN
  78. return LLKeyframeMotion::create(id);
  79. }
  80. //-----------------------------------------------------------------------------
  81. // LLMotionController class
  82. //-----------------------------------------------------------------------------
  83. //static
  84. void LLMotionController::initClass()
  85. {
  86. // Let's avoid memory fragmentation over time...
  87. sMotionsToKill.reserve(MAX_MOTION_INSTANCES * 2);
  88. }
  89. //static
  90. void LLMotionController::dumpStats()
  91. {
  92. llinfos << "sMotionsToKill capacity reached: " << sMotionsToKill.capacity()
  93. << llendl;
  94. }
  95. LLMotionController::LLMotionController()
  96. : mTimeFactor(1.f),
  97. mTimeFactorMultiplier(sTimeFactorMultiplier),
  98. mCharacter(NULL),
  99. mAnimTime(0.f),
  100. mPrevTimerElapsed(0.f),
  101. mLastTime(0.0f),
  102. mHasRunOnce(false),
  103. mPaused(false),
  104. mPausedFrame(0),
  105. mTimeStep(0.f),
  106. mTimeStepCount(0),
  107. mLastInterp(0.f)
  108. {
  109. }
  110. LLMotionController::~LLMotionController()
  111. {
  112. deleteAllMotions();
  113. }
  114. void LLMotionController::incMotionCounts(S32& num_motions,
  115. S32& num_loading_motions,
  116. S32& num_loaded_motions,
  117. S32& num_active_motions,
  118. S32& num_deprecated_motions)
  119. {
  120. num_motions += mAllMotions.size();
  121. num_loading_motions += mLoadingMotions.size();
  122. num_loaded_motions += mLoadedMotions.size();
  123. num_active_motions += mActiveMotions.size();
  124. num_deprecated_motions += mDeprecatedMotions.size();
  125. }
  126. void LLMotionController::deleteAllMotions()
  127. {
  128. mLoadingMotions.clear();
  129. mLoadedMotions.clear();
  130. mActiveMotions.clear();
  131. for (motion_map_t::iterator it = mAllMotions.begin(),
  132. end = mAllMotions.end();
  133. it != end; ++it)
  134. {
  135. delete it->second;
  136. }
  137. mAllMotions.clear();
  138. for (motion_set_t::iterator it = mDeprecatedMotions.begin(),
  139. end = mDeprecatedMotions.end();
  140. it != end; ++it)
  141. {
  142. delete *it;
  143. }
  144. mDeprecatedMotions.clear();
  145. }
  146. void LLMotionController::purgeExcessMotions()
  147. {
  148. if (mLoadedMotions.size() > MAX_MOTION_INSTANCES)
  149. {
  150. // Clean up deprecated motions
  151. for (motion_set_t::iterator iter = mDeprecatedMotions.begin();
  152. iter != mDeprecatedMotions.end(); )
  153. {
  154. motion_set_t::iterator cur_iter = iter++;
  155. LLMotion* cur_motionp = *cur_iter;
  156. if (!isMotionActive(cur_motionp))
  157. {
  158. // Motion is deprecated so we know it is not cannonical, we can
  159. // safely remove the instance
  160. removeMotionInstance(cur_motionp);
  161. mDeprecatedMotions.erase(cur_iter);
  162. }
  163. }
  164. }
  165. S32 excess_motions = mLoadedMotions.size() - MAX_MOTION_INSTANCES;
  166. if (excess_motions > 0)
  167. {
  168. // Too many motions active this frame, kill all blenders
  169. mPoseBlender.clearBlenders();
  170. for (motion_set_t::iterator iter = mLoadedMotions.begin(),
  171. end = mLoadedMotions.end();
  172. iter != end; ++iter)
  173. {
  174. LLMotion* motionp = *iter;
  175. // Motion is not playing, mark for delete
  176. if (motionp && !isMotionActive(motionp))
  177. {
  178. sMotionsToKill.emplace_back(motionp->getID());
  179. }
  180. }
  181. }
  182. // Clean up all inactive, loaded motions
  183. excess_motions = sMotionsToKill.size();
  184. if (excess_motions > 0)
  185. {
  186. for (S32 i = 0; i < excess_motions; ++i)
  187. {
  188. // Look up the motion again by ID to get canonical instance and
  189. // kill it only if that one is inactive
  190. const LLUUID& motion_id = sMotionsToKill[i];
  191. LLMotion* motionp = findMotion(motion_id);
  192. if (motionp && !isMotionActive(motionp))
  193. {
  194. removeMotion(motion_id);
  195. }
  196. }
  197. sMotionsToKill.clear();
  198. }
  199. if (mLoadedMotions.size() > 2 * MAX_MOTION_INSTANCES)
  200. {
  201. LL_DEBUGS("Motion") << "> " << 2 * MAX_MOTION_INSTANCES
  202. << " Loaded Motions" << LL_ENDL;
  203. }
  204. }
  205. void LLMotionController::deactivateStoppedMotions()
  206. {
  207. // Since we are hidden, deactivate any stopped motions.
  208. for (motion_list_t::iterator iter = mActiveMotions.begin();
  209. iter != mActiveMotions.end(); )
  210. {
  211. LLMotion* motionp = *iter++;
  212. if (motionp && motionp->isStopped())
  213. {
  214. deactivateMotionInstance(motionp);
  215. }
  216. }
  217. }
  218. void LLMotionController::setTimeStep(F32 step)
  219. {
  220. mTimeStep = step;
  221. if (step != 0.f)
  222. {
  223. // Make sure timestamps conform to new quantum
  224. for (motion_list_t::iterator iter = mActiveMotions.begin(),
  225. end = mActiveMotions.end();
  226. iter != end; ++iter)
  227. {
  228. LLMotion* motionp = *iter;
  229. if (!motionp) continue;
  230. F32 activation_time = motionp->mActivationTimestamp;
  231. motionp->mActivationTimestamp =
  232. (F32)(llfloor(activation_time / step)) * step;
  233. bool stopped = motionp->isStopped();
  234. motionp->setStopTime((F32)(llfloor(motionp->getStopTime() /
  235. step)) * step);
  236. motionp->setStopped(stopped);
  237. motionp->mSendStopTimestamp =
  238. (F32)llfloor(motionp->mSendStopTimestamp / step) * step;
  239. }
  240. }
  241. }
  242. bool LLMotionController::registerMotion(const LLUUID& id,
  243. LLMotionConstructor constructor)
  244. {
  245. return sRegistry.registerMotion(id, constructor);
  246. }
  247. void LLMotionController::removeMotion(const LLUUID& id)
  248. {
  249. mAllMotions.erase(id);
  250. removeMotionInstance(findMotion(id));
  251. }
  252. // Removes instance of a motion from all runtime structures, but does not erase
  253. // entry by Id as this could be a duplicate instance; use removeMotion(id) to
  254. // remove all references to a given motion by Id.
  255. void LLMotionController::removeMotionInstance(LLMotion* motionp)
  256. {
  257. if (motionp)
  258. {
  259. llassert(findMotion(motionp->getID()) != motionp);
  260. if (motionp->isActive())
  261. {
  262. motionp->deactivate();
  263. }
  264. mLoadingMotions.erase(motionp);
  265. mLoadedMotions.erase(motionp);
  266. mActiveMotions.remove(motionp);
  267. delete motionp;
  268. }
  269. }
  270. LLMotion* LLMotionController::createMotion(const LLUUID& id)
  271. {
  272. if (id.isNull())
  273. {
  274. // Happens for hand animations (Bento mesh avatar with hand joints and
  275. // no hands anim defined ?). Just abort.
  276. return NULL;
  277. }
  278. // Do we have an instance of this motion for this character ?
  279. LLMotion* motion = findMotion(id);
  280. // If not, we need to create one
  281. if (!motion)
  282. {
  283. // Look up constructor and create it
  284. motion = sRegistry.createMotion(id);
  285. if (!motion)
  286. {
  287. return NULL;
  288. }
  289. // Look up name for default motions
  290. const char* motion_name = gAnimLibrary.animStateToString(id);
  291. if (motion_name)
  292. {
  293. motion->setName(motion_name);
  294. }
  295. // Initialize the new instance
  296. LLMotion::LLMotionInitStatus stat = motion->onInitialize(mCharacter);
  297. switch (stat)
  298. {
  299. case LLMotion::STATUS_FAILURE:
  300. llinfos << "Motion " << id << " init failed." << llendl;
  301. sRegistry.markBad(id);
  302. delete motion;
  303. return NULL;
  304. case LLMotion::STATUS_HOLD:
  305. mLoadingMotions.insert(motion);
  306. break;
  307. case LLMotion::STATUS_SUCCESS:
  308. // Add motion to our list
  309. mLoadedMotions.insert(motion);
  310. break;
  311. default:
  312. llerrs << "Invalid initialization status" << llendl;
  313. }
  314. mAllMotions.emplace(id, motion);
  315. }
  316. return motion;
  317. }
  318. bool LLMotionController::startMotion(const LLUUID& id, F32 start_offset)
  319. {
  320. // Do we have an instance of this motion for this character ?
  321. LLMotion* motion = findMotion(id);
  322. // Motion that is stopping will be allowed to stop but replaced by a new
  323. // instance of that motion
  324. if (motion && !mPaused && motion->canDeprecate() &&
  325. // Not LOD-ed out:
  326. motion->getFadeWeight() > 0.01f &&
  327. (motion->isBlending() || motion->getStopTime() != 0.f))
  328. {
  329. deprecateMotionInstance(motion);
  330. // Force creation of new instance
  331. motion = NULL;
  332. }
  333. // Create new motion instance
  334. if (!motion)
  335. {
  336. motion = createMotion(id);
  337. }
  338. if (!motion)
  339. {
  340. return false;
  341. }
  342. if (motion->canDeprecate() && isMotionActive(motion))
  343. {
  344. // If the motion is already active and allows deprecation, then let it
  345. // keep/ playing
  346. return true;
  347. }
  348. return activateMotionInstance(motion, mAnimTime - start_offset);
  349. }
  350. // If motion is already inactive, returns false
  351. bool LLMotionController::stopMotionLocally(const LLUUID& id, bool stop_now)
  352. {
  353. LLMotion* motion = findMotion(id);
  354. return stopMotionInstance(motion, stop_now || mPaused);
  355. }
  356. bool LLMotionController::stopMotionInstance(LLMotion* motion, bool stop_now)
  357. {
  358. if (!motion)
  359. {
  360. return false;
  361. }
  362. // If on active list, stop it
  363. if (isMotionActive(motion) && !motion->isStopped())
  364. {
  365. motion->setStopTime(mAnimTime);
  366. if (stop_now)
  367. {
  368. deactivateMotionInstance(motion);
  369. }
  370. return true;
  371. }
  372. if (isMotionLoading(motion))
  373. {
  374. motion->setStopped(true);
  375. return true;
  376. }
  377. return false;
  378. }
  379. void LLMotionController::updateRegularMotions()
  380. {
  381. updateMotionsByType(LLMotion::NORMAL_BLEND);
  382. }
  383. void LLMotionController::updateAdditiveMotions()
  384. {
  385. updateMotionsByType(LLMotion::ADDITIVE_BLEND);
  386. }
  387. void LLMotionController::resetJointSignatures()
  388. {
  389. const size_t bytes = sizeof(U8) * LL_CHARACTER_MAX_ANIMATED_JOINTS;
  390. memset(&mJointSignature[0][0], 0, bytes);
  391. memset(&mJointSignature[1][0], 0, bytes);
  392. }
  393. // minimal updates for active motions
  394. void LLMotionController::updateIdleMotion(LLMotion* motionp)
  395. {
  396. if (!motionp)
  397. {
  398. return;
  399. }
  400. if (motionp->isStopped() &&
  401. mAnimTime > motionp->getStopTime() + motionp->getEaseOutDuration())
  402. {
  403. deactivateMotionInstance(motionp);
  404. }
  405. else if (motionp->isStopped() && mAnimTime > motionp->getStopTime())
  406. {
  407. // is this the first iteration in the ease out phase?
  408. if (mLastTime <= motionp->getStopTime())
  409. {
  410. // store residual weight for this motion
  411. motionp->mResidualWeight = motionp->getPose()->getWeight();
  412. }
  413. }
  414. else if (mAnimTime > motionp->mSendStopTimestamp)
  415. {
  416. // Notify character of timed stop event on first iteration past
  417. // sendstoptimestamp. This will only be called when an animation stops
  418. // itself (runs out of time)
  419. if (mLastTime <= motionp->mSendStopTimestamp)
  420. {
  421. mCharacter->requestStopMotion(motionp);
  422. stopMotionInstance(motionp, false);
  423. }
  424. }
  425. else if (mAnimTime >= motionp->mActivationTimestamp)
  426. {
  427. if (mLastTime < motionp->mActivationTimestamp)
  428. {
  429. motionp->mResidualWeight = motionp->getPose()->getWeight();
  430. }
  431. }
  432. }
  433. // Call this instead of updateMotionsByType for hidden avatars
  434. void LLMotionController::updateIdleActiveMotions()
  435. {
  436. for (motion_list_t::iterator iter = mActiveMotions.begin();
  437. iter != mActiveMotions.end(); )
  438. {
  439. LLMotion* motionp = *iter++;
  440. if (motionp)
  441. {
  442. updateIdleMotion(motionp);
  443. }
  444. }
  445. }
  446. void LLMotionController::updateMotionsByType(LLMotion::LLMotionBlendType anim_type)
  447. {
  448. bool update_result = true;
  449. U8 last_joint_signature[LL_CHARACTER_MAX_ANIMATED_JOINTS];
  450. memset(&last_joint_signature, 0,
  451. sizeof(U8) * LL_CHARACTER_MAX_ANIMATED_JOINTS);
  452. // Iterate through active motions in chronological order
  453. for (motion_list_t::iterator iter = mActiveMotions.begin();
  454. iter != mActiveMotions.end(); )
  455. {
  456. LLMotion* motionp = *iter++;
  457. if (!motionp || motionp->getBlendType() != anim_type)
  458. {
  459. continue;
  460. }
  461. if (!motionp->needsUpdate())
  462. {
  463. // As far as the motion knows: it does not need an update but we
  464. // still update it if its "joint signature" causes a change to the
  465. // accumulated signature stored in 2D arraymJointSignature[][]
  466. // (whatever that means).
  467. bool update_motion = false;
  468. for (S32 i = 0; i < NUM_JOINT_SIGNATURE_STRIDES; ++i)
  469. {
  470. // First indice is 0 for positions.
  471. U32* current_signature = (U32*)&(mJointSignature[0][i * 4]);
  472. U32 test_signature =
  473. *(U32*)&(motionp->mJointSignature[0][i * 4]);
  474. if ((*current_signature | test_signature) > *current_signature)
  475. {
  476. *current_signature |= test_signature;
  477. update_motion = true;
  478. }
  479. // First indice is 1 for rotations.
  480. *((U32*)&last_joint_signature[i * 4]) =
  481. *(U32*)&(mJointSignature[1][i * 4]);
  482. current_signature = (U32*)&(mJointSignature[1][i * 4]);
  483. test_signature = *(U32*)&(motionp->mJointSignature[1][i * 4]);
  484. if ((*current_signature | test_signature) > *current_signature)
  485. {
  486. *current_signature |= test_signature;
  487. update_motion = true;
  488. }
  489. }
  490. if (!update_motion)
  491. {
  492. updateIdleMotion(motionp);
  493. continue;
  494. }
  495. }
  496. LLPose* posep = motionp->getPose();
  497. if (!posep)
  498. {
  499. llwarns_sparse << "NULL pose !" << llendl;
  500. continue;
  501. }
  502. // Only filter by LOD after running every animation at least once (to
  503. // prime the avatar state)
  504. if (mHasRunOnce &&
  505. motionp->getMinPixelArea() > mCharacter->getPixelArea())
  506. {
  507. motionp->fadeOut();
  508. // Should we notify the simulator that this motion should be
  509. // stopped (check even if skipped by LOD logic) ?
  510. if (mAnimTime > motionp->mSendStopTimestamp)
  511. {
  512. // Notify character of timed stop event on first iteration past
  513. // sendstoptimestamp. This will only be called when an
  514. // animation stops itself (runs out of time)
  515. if (mLastTime <= motionp->mSendStopTimestamp)
  516. {
  517. mCharacter->requestStopMotion(motionp);
  518. stopMotionInstance(motionp, false);
  519. }
  520. }
  521. if (motionp->getFadeWeight() < 0.01f)
  522. {
  523. if (motionp->isStopped() &&
  524. mAnimTime > motionp->getStopTime() + motionp->getEaseOutDuration())
  525. {
  526. posep->setWeight(0.f);
  527. deactivateMotionInstance(motionp);
  528. }
  529. continue;
  530. }
  531. }
  532. else
  533. {
  534. motionp->fadeIn();
  535. }
  536. //**********************
  537. // MOTION INACTIVE
  538. //**********************
  539. if (motionp->isStopped() &&
  540. mAnimTime > motionp->getStopTime() + motionp->getEaseOutDuration())
  541. {
  542. // This motion has gone on too long, deactivate it did we have a
  543. // chance to stop it ?
  544. if (mLastTime <= motionp->getStopTime())
  545. {
  546. // If not, let's stop it this time through and deactivate it the next
  547. posep->setWeight(motionp->getFadeWeight());
  548. motionp->onUpdate(motionp->getStopTime() - motionp->mActivationTimestamp,
  549. last_joint_signature);
  550. }
  551. else
  552. {
  553. posep->setWeight(0.f);
  554. deactivateMotionInstance(motionp);
  555. continue;
  556. }
  557. }
  558. //**********************
  559. // MOTION EASE OUT
  560. //**********************
  561. else if (motionp->isStopped() && mAnimTime > motionp->getStopTime())
  562. {
  563. // Is this the first iteration in the ease out phase?
  564. if (mLastTime <= motionp->getStopTime())
  565. {
  566. // store residual weight for this motion
  567. motionp->mResidualWeight = motionp->getPose()->getWeight();
  568. }
  569. if (motionp->getEaseOutDuration() == 0.f)
  570. {
  571. posep->setWeight(0.f);
  572. }
  573. else
  574. {
  575. posep->setWeight(motionp->getFadeWeight() *
  576. motionp->mResidualWeight *
  577. cubic_step(1.f -
  578. (mAnimTime -
  579. motionp->getStopTime()) /
  580. motionp->getEaseOutDuration()));
  581. }
  582. // Perform motion update
  583. {
  584. LL_FAST_TIMER(FTM_MOTION_ON_UPDATE);
  585. update_result = motionp->onUpdate(mAnimTime - motionp->mActivationTimestamp,
  586. last_joint_signature);
  587. }
  588. }
  589. //**********************
  590. // MOTION ACTIVE
  591. //**********************
  592. else if (mAnimTime >
  593. motionp->mActivationTimestamp + motionp->getEaseInDuration())
  594. {
  595. posep->setWeight(motionp->getFadeWeight());
  596. // Should we notify the simulator that this motion should be
  597. // stopped ?
  598. if (mAnimTime > motionp->mSendStopTimestamp)
  599. {
  600. // Notify character of timed stop event on first iteration past
  601. // sendstoptimestamp. This will only be called when an
  602. // animation stops itself (runs out of time)
  603. if (mLastTime <= motionp->mSendStopTimestamp)
  604. {
  605. mCharacter->requestStopMotion(motionp);
  606. stopMotionInstance(motionp, false);
  607. }
  608. }
  609. // perform motion update
  610. update_result =
  611. motionp->onUpdate(mAnimTime - motionp->mActivationTimestamp,
  612. last_joint_signature);
  613. }
  614. //**********************
  615. // MOTION EASE IN
  616. //**********************
  617. else if (mAnimTime >= motionp->mActivationTimestamp)
  618. {
  619. if (mLastTime < motionp->mActivationTimestamp)
  620. {
  621. motionp->mResidualWeight = motionp->getPose()->getWeight();
  622. }
  623. if (motionp->getEaseInDuration() == 0.f)
  624. {
  625. posep->setWeight(motionp->getFadeWeight());
  626. }
  627. else
  628. {
  629. // Perform motion update
  630. posep->setWeight(motionp->getFadeWeight() *
  631. motionp->mResidualWeight +
  632. (1.f - motionp->mResidualWeight) *
  633. cubic_step((mAnimTime -
  634. motionp->mActivationTimestamp) /
  635. motionp->getEaseInDuration()));
  636. }
  637. // perform motion update
  638. update_result = motionp->onUpdate(mAnimTime -
  639. motionp->mActivationTimestamp,
  640. last_joint_signature);
  641. }
  642. else
  643. {
  644. posep->setWeight(0.f);
  645. update_result = motionp->onUpdate(0.f, last_joint_signature);
  646. }
  647. // Allow motions to deactivate themselves
  648. if (!update_result &&
  649. (!motionp->isStopped() || motionp->getStopTime() > mAnimTime))
  650. {
  651. // Animation has stopped itself due to internal logic propagate
  652. // this to the network as not all viewers are guaranteed to have
  653. // access to the same logic.
  654. mCharacter->requestStopMotion(motionp);
  655. stopMotionInstance(motionp, false);
  656. }
  657. // Even if onupdate returns false, add this motion in to the blend one
  658. // last time
  659. mPoseBlender.addMotion(motionp);
  660. }
  661. }
  662. void LLMotionController::updateLoadingMotions()
  663. {
  664. // Query pending motions for completion
  665. for (motion_set_t::iterator iter = mLoadingMotions.begin();
  666. iter != mLoadingMotions.end(); )
  667. {
  668. LLMotion* motionp = *iter;
  669. if (!motionp) // Maybe it should not happen but I have seen it - MG
  670. {
  671. llwarns << "NULL motion loading found. Removing from list."
  672. << llendl;
  673. iter = mLoadingMotions.erase(iter);
  674. continue;
  675. }
  676. LLMotion::LLMotionInitStatus s = motionp->onInitialize(mCharacter);
  677. if (s == LLMotion::STATUS_SUCCESS)
  678. {
  679. // Add motion to our loaded motion list
  680. mLoadedMotions.insert(motionp);
  681. // Erase from loading motion list
  682. iter = mLoadingMotions.erase(iter);
  683. // This motion should be playing
  684. if (!motionp->isStopped())
  685. {
  686. activateMotionInstance(motionp, mAnimTime);
  687. }
  688. }
  689. else if (s == LLMotion::STATUS_FAILURE)
  690. {
  691. llwarns << "Motion " << motionp->getID() << " init failed."
  692. << llendl;
  693. sRegistry.markBad(motionp->getID());
  694. iter = mLoadingMotions.erase(iter);
  695. motion_set_t::iterator found_it = mDeprecatedMotions.find(motionp);
  696. if (found_it != mDeprecatedMotions.end())
  697. {
  698. mDeprecatedMotions.erase(found_it);
  699. }
  700. mAllMotions.erase(motionp->getID());
  701. delete motionp;
  702. }
  703. else // STATUS_HOLD
  704. {
  705. ++iter;
  706. }
  707. }
  708. }
  709. void LLMotionController::updateMotions(bool force_update)
  710. {
  711. bool use_quantum = mTimeStep != 0.f;
  712. // Always update mPrevTimerElapsed
  713. F32 cur_time = mTimer.getElapsedTimeF32();
  714. F32 delta_time = cur_time - mPrevTimerElapsed;
  715. if (delta_time < 0.f)
  716. {
  717. llwarns_sparse << "Negative time passed; zeroed." << llendl;
  718. delta_time = 0.f;
  719. }
  720. mPrevTimerElapsed = cur_time;
  721. mLastTime = mAnimTime;
  722. // Always cap the number of loaded motions
  723. purgeExcessMotions();
  724. // Update timing info for this time step.
  725. if (!mPaused)
  726. {
  727. F32 update_time = mAnimTime +
  728. delta_time * mTimeFactor * mTimeFactorMultiplier;
  729. if (use_quantum)
  730. {
  731. F32 time_interval = fmodf(update_time, mTimeStep);
  732. // always animate *ahead* of actual time
  733. #if 0
  734. S32 quantum_count = llmax(0,
  735. llfloor((update_time - time_interval) /
  736. mTimeStep)) + 1;
  737. #else
  738. S32 quantum_count = llmax(ll_roundp(update_time / mTimeStep),
  739. llceil(mAnimTime / mTimeStep));
  740. #endif
  741. if (quantum_count == mTimeStepCount)
  742. {
  743. // We are still in same time quantum as before, so just
  744. // interpolate and exit
  745. if (!mPaused)
  746. {
  747. F32 interp = time_interval / mTimeStep;
  748. mPoseBlender.interpolate(interp - mLastInterp);
  749. mLastInterp = interp;
  750. }
  751. updateLoadingMotions();
  752. return;
  753. }
  754. // Is calculating a new keyframe pose, make sure the last one gets
  755. // applied
  756. mPoseBlender.interpolate(1.f);
  757. mPoseBlender.clearBlenders();
  758. mTimeStepCount = quantum_count;
  759. mAnimTime = (F32)quantum_count * mTimeStep;
  760. mLastInterp = 0.f;
  761. }
  762. else
  763. {
  764. mAnimTime = update_time;
  765. }
  766. }
  767. updateLoadingMotions();
  768. resetJointSignatures();
  769. if (mPaused && !force_update)
  770. {
  771. updateIdleActiveMotions();
  772. }
  773. else
  774. {
  775. // Update additive motions
  776. updateAdditiveMotions();
  777. resetJointSignatures();
  778. // Update all regular motions
  779. updateRegularMotions();
  780. if (use_quantum)
  781. {
  782. mPoseBlender.blendAndCache(true);
  783. }
  784. else
  785. {
  786. mPoseBlender.blendAndApply();
  787. }
  788. }
  789. mHasRunOnce = true;
  790. }
  791. // minimal update (e.g. while hidden)
  792. void LLMotionController::updateMotionsMinimal()
  793. {
  794. // Always update mPrevTimerElapsed
  795. mPrevTimerElapsed = mTimer.getElapsedTimeF32();
  796. purgeExcessMotions();
  797. updateLoadingMotions();
  798. resetJointSignatures();
  799. deactivateStoppedMotions();
  800. mHasRunOnce = true;
  801. }
  802. bool LLMotionController::activateMotionInstance(LLMotion* motion, F32 time)
  803. {
  804. // It is not clear why the getWeight() line seems to be crashing this, but
  805. // hopefully this fixes it.
  806. if (!motion || !motion->getPose())
  807. {
  808. return false;
  809. }
  810. if (mLoadingMotions.count(motion) != 0)
  811. {
  812. // We want to start this motion, but we cannot yet, so flag it as
  813. // started
  814. motion->setStopped(false);
  815. // Report pending animations as activated
  816. return true;
  817. }
  818. motion->mResidualWeight = motion->getPose()->getWeight();
  819. // Set stop time based on given duration and ease out time
  820. if (motion->getDuration() != 0.f && !motion->getLoop())
  821. {
  822. F32 ease_out_time;
  823. F32 motion_duration;
  824. // Should we stop at the end of motion duration, or a bit earlier to
  825. // allow it to ease out while moving ?
  826. ease_out_time = motion->getEaseOutDuration();
  827. // Is the clock running when the motion is easing in ? If not
  828. // (POSTURE_EASE) then we need to wait that much longer before
  829. // triggering the stop.
  830. motion_duration = llmax(motion->getDuration() - ease_out_time, 0.f);
  831. motion->mSendStopTimestamp = time + motion_duration;
  832. }
  833. else
  834. {
  835. motion->mSendStopTimestamp = F32_MAX;
  836. }
  837. if (motion->isActive())
  838. {
  839. mActiveMotions.remove(motion);
  840. }
  841. mActiveMotions.push_front(motion);
  842. motion->activate(time);
  843. motion->onUpdate(0.f, mJointSignature[1]);
  844. if (mAnimTime >= motion->mSendStopTimestamp)
  845. {
  846. motion->setStopTime(motion->mSendStopTimestamp);
  847. if (motion->mResidualWeight == 0.0f)
  848. {
  849. // Bit of a hack; if newly activating a motion while easing out,
  850. // weight should be 1.
  851. motion->mResidualWeight = 1.f;
  852. }
  853. }
  854. return true;
  855. }
  856. bool LLMotionController::deactivateMotionInstance(LLMotion* motion)
  857. {
  858. if (!motion)
  859. {
  860. llwarns << "Attempted to deactivate a NULL motion (ignored) !"
  861. << llendl;
  862. return true;
  863. }
  864. motion->deactivate();
  865. motion_set_t::iterator found_it = mDeprecatedMotions.find(motion);
  866. if (found_it != mDeprecatedMotions.end())
  867. {
  868. // Deprecated motions need to be completely excised
  869. removeMotionInstance(motion);
  870. mDeprecatedMotions.erase(found_it);
  871. }
  872. else
  873. {
  874. // For motions that we are keeping, simply remove from active queue
  875. mActiveMotions.remove(motion);
  876. }
  877. return true;
  878. }
  879. void LLMotionController::deprecateMotionInstance(LLMotion* motion)
  880. {
  881. if (!motion)
  882. {
  883. llwarns << "Attempted to deprecate a NULL motion (ignored) !"
  884. << llendl;
  885. return;
  886. }
  887. mDeprecatedMotions.insert(motion);
  888. // Fade out deprecated motion
  889. stopMotionInstance(motion, false);
  890. // No longer canonical
  891. mAllMotions.erase(motion->getID());
  892. }
  893. LLMotion* LLMotionController::findMotion(const LLUUID& id) const
  894. {
  895. motion_map_t::const_iterator iter = mAllMotions.find(id);
  896. return iter == mAllMotions.end() ? NULL : iter->second;
  897. }
  898. void LLMotionController::deactivateAllMotions()
  899. {
  900. for (motion_map_t::iterator iter = mAllMotions.begin();
  901. iter != mAllMotions.end(); )
  902. {
  903. LLMotion* motionp = (iter++)->second;
  904. if (motionp)
  905. {
  906. deactivateMotionInstance(motionp);
  907. }
  908. }
  909. }
  910. void LLMotionController::pauseAllMotions()
  911. {
  912. if (!mPaused)
  913. {
  914. mPaused = true;
  915. mPausedFrame = LLFrameTimer::getFrameCount();
  916. }
  917. }
  918. bool LLMotionController::isReallyPaused() const
  919. {
  920. return mPaused && LLFrameTimer::getFrameCount() - mPausedFrame > 1;
  921. }
  922. void LLMotionController::flushAllMotions()
  923. {
  924. std::vector<std::pair<LLUUID, F32> > active_motions;
  925. active_motions.reserve(mActiveMotions.size());
  926. for (motion_list_t::iterator iter = mActiveMotions.begin(),
  927. end = mActiveMotions.end();
  928. iter != end; ++iter)
  929. {
  930. LLMotion* motionp = *iter;
  931. if (motionp)
  932. {
  933. F32 dtime = mAnimTime - motionp->mActivationTimestamp;
  934. active_motions.push_back(std::make_pair(motionp->getID(), dtime));
  935. // Do not call deactivateMotionInstance() because we are going to
  936. // reactivate it
  937. motionp->deactivate();
  938. }
  939. }
  940. mActiveMotions.clear();
  941. // Delete all motion instances
  942. deleteAllMotions();
  943. // Kill current hand pose that was previously called out by keyframe motion
  944. mCharacter->removeAnimationData("Hand Pose");
  945. // Restart motions
  946. for (std::vector<std::pair<LLUUID, F32> >::iterator
  947. iter = active_motions.begin(), end = active_motions.end();
  948. iter != end; ++iter)
  949. {
  950. startMotion(iter->first, iter->second);
  951. }
  952. }