llkeyframemotion.cpp 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794
  1. /**
  2. * @file llkeyframemotion.cpp
  3. * @brief Implementation of LLKeyframeMotion 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 "llkeyframemotion.h"
  35. #include "llanimationstates.h"
  36. #include "llassetstorage.h"
  37. #include "llcharacter.h"
  38. #include "llcriticaldamp.h"
  39. #include "lldatapacker.h"
  40. #include "lldir.h"
  41. #include "llfilesystem.h"
  42. #include "llmath.h"
  43. #include "llquantize.h"
  44. // Static members
  45. std::string LLKeyframeMotion::sStaticAnimsDir;
  46. LLKeyframeDataCache::data_map_t LLKeyframeDataCache::sKeyframeDataMap;
  47. constexpr F32 JOINT_LENGTH_K = 0.7f;
  48. constexpr S32 MAX_ITERATIONS = 20;
  49. constexpr S32 MIN_ITERATIONS = 1;
  50. constexpr S32 MIN_ITERATION_COUNT = 2;
  51. constexpr F32 MAX_PIXEL_AREA_CONSTRAINTS = 80000.f;
  52. constexpr F32 MIN_PIXEL_AREA_CONSTRAINTS = 1000.f;
  53. constexpr F32 MIN_ACCELERATION_SQUARED = 0.0005f * 0.0005f;
  54. // Normally 10, but the "clap" SL anim is bogus and got 11 constraints
  55. constexpr S32 MAX_CONSTRAINTS = 11;
  56. //-----------------------------------------------------------------------------
  57. // LLKeyframeMotion::JointMotionList sub-class
  58. //-----------------------------------------------------------------------------
  59. LLKeyframeMotion::JointMotionList::JointMotionList()
  60. : mDuration(0.f),
  61. mLoopInPoint(0.f),
  62. mLoopOutPoint(0.f),
  63. mEaseInDuration(0.f),
  64. mEaseOutDuration(0.f),
  65. mBasePriority(LLJoint::LOW_PRIORITY),
  66. mMaxPriority(LLJoint::LOW_PRIORITY),
  67. mHandPose(LLHandMotion::HAND_POSE_SPREAD),
  68. mLoop(false)
  69. {
  70. }
  71. LLKeyframeMotion::JointMotionList::~JointMotionList()
  72. {
  73. std::for_each(mConstraints.begin(), mConstraints.end(), DeletePointer());
  74. mConstraints.clear();
  75. std::for_each(mJointMotionArray.begin(), mJointMotionArray.end(),
  76. DeletePointer());
  77. mJointMotionArray.clear();
  78. }
  79. U32 LLKeyframeMotion::JointMotionList::dumpDiagInfo()
  80. {
  81. S32 total_size = sizeof(JointMotionList);
  82. for (U32 i = 0, count = getNumJointMotions(); i < count; ++i)
  83. {
  84. JointMotion* joint_motionp = mJointMotionArray[i];
  85. llinfos << "\tJoint " << joint_motionp->mJointName << llendl;
  86. if (joint_motionp->mUsage & LLJointState::SCALE)
  87. {
  88. llinfos << " " << joint_motionp->mScaleCurve.mNumKeys
  89. << " scale keys at "
  90. << joint_motionp->mScaleCurve.mNumKeys * sizeof(ScaleKey)
  91. << " bytes" << llendl;
  92. total_size += joint_motionp->mScaleCurve.mNumKeys *
  93. sizeof(ScaleKey);
  94. }
  95. if (joint_motionp->mUsage & LLJointState::ROT)
  96. {
  97. llinfos << " " << joint_motionp->mRotationCurve.mNumKeys
  98. << " rotation keys at "
  99. << joint_motionp->mRotationCurve.mNumKeys *
  100. sizeof(RotationKey) << " bytes" << llendl;
  101. total_size += joint_motionp->mRotationCurve.mNumKeys *
  102. sizeof(RotationKey);
  103. }
  104. if (joint_motionp->mUsage & LLJointState::POS)
  105. {
  106. llinfos << " " << joint_motionp->mPositionCurve.mNumKeys
  107. << " position keys at "
  108. << joint_motionp->mPositionCurve.mNumKeys *
  109. sizeof(PositionKey) << " bytes" << llendl;
  110. total_size += joint_motionp->mPositionCurve.mNumKeys *
  111. sizeof(PositionKey);
  112. }
  113. }
  114. llinfos << "Size: " << total_size << " bytes" << llendl;
  115. return total_size;
  116. }
  117. //-----------------------------------------------------------------------------
  118. // LLKeyframeMotion::ScaleCurve sub-class
  119. //-----------------------------------------------------------------------------
  120. LLKeyframeMotion::ScaleCurve::ScaleCurve()
  121. {
  122. mInterpolationType = LLKeyframeMotion::IT_LINEAR;
  123. mNumKeys = 0;
  124. }
  125. LLKeyframeMotion::ScaleCurve::~ScaleCurve()
  126. {
  127. mKeys.clear();
  128. mNumKeys = 0;
  129. }
  130. LLVector3 LLKeyframeMotion::ScaleCurve::getValue(F32 time, F32 duration)
  131. {
  132. LLVector3 value;
  133. if (mKeys.empty())
  134. {
  135. return value;
  136. }
  137. key_map_t::iterator right = mKeys.lower_bound(time);
  138. if (right == mKeys.end())
  139. {
  140. // Past last key
  141. value = (--right)->second.mScale;
  142. }
  143. else if (right == mKeys.begin() || right->first == time)
  144. {
  145. // Before first key or exactly on a key
  146. value = right->second.mScale;
  147. }
  148. else
  149. {
  150. // Between two keys
  151. key_map_t::iterator left = right;
  152. F32 index_before = (--left)->first;
  153. F32 index_after = right->first;
  154. if (index_after > index_before)
  155. {
  156. ScaleKey& scale_before = left->second;
  157. ScaleKey& scale_after = right->second;
  158. if (right == mKeys.end())
  159. {
  160. scale_after = mLoopInKey;
  161. index_after = duration;
  162. }
  163. F32 u = (time - index_before) / (index_after - index_before);
  164. value = interp(u, scale_before, scale_after);
  165. }
  166. else
  167. {
  168. llwarns << "Out of order indexes." << llendl;
  169. value = right->second.mScale;
  170. }
  171. }
  172. return value;
  173. }
  174. LLVector3 LLKeyframeMotion::ScaleCurve::interp(F32 u, ScaleKey& before,
  175. ScaleKey& after)
  176. {
  177. if (mInterpolationType == IT_STEP)
  178. {
  179. return before.mScale;
  180. }
  181. else
  182. {
  183. return lerp(before.mScale, after.mScale, u);
  184. }
  185. }
  186. //-----------------------------------------------------------------------------
  187. // LLKeyframeMotion::RotationCurve sub-class
  188. //-----------------------------------------------------------------------------
  189. LLKeyframeMotion::RotationCurve::RotationCurve()
  190. {
  191. mInterpolationType = LLKeyframeMotion::IT_LINEAR;
  192. mNumKeys = 0;
  193. }
  194. LLKeyframeMotion::RotationCurve::~RotationCurve()
  195. {
  196. mKeys.clear();
  197. mNumKeys = 0;
  198. }
  199. LLQuaternion LLKeyframeMotion::RotationCurve::getValue(F32 time, F32 duration)
  200. {
  201. if (mKeys.empty())
  202. {
  203. return LLQuaternion::DEFAULT;
  204. }
  205. LLQuaternion value;
  206. key_map_t::iterator right = mKeys.lower_bound(time);
  207. if (right == mKeys.end())
  208. {
  209. // Past last key
  210. value = (--right)->second.mRotation;
  211. }
  212. else if (right == mKeys.begin() || right->first == time)
  213. {
  214. // Before first key or exactly on a key
  215. value = right->second.mRotation;
  216. }
  217. else
  218. {
  219. // Between two keys
  220. key_map_t::iterator left = right;
  221. F32 index_before = (--left)->first;
  222. F32 index_after = right->first;
  223. if (index_after > index_before)
  224. {
  225. RotationKey& rot_before = left->second;
  226. RotationKey& rot_after = right->second;
  227. if (right == mKeys.end())
  228. {
  229. rot_after = mLoopInKey;
  230. index_after = duration;
  231. }
  232. F32 u = (time - index_before) / (index_after - index_before);
  233. value = interp(u, rot_before, rot_after);
  234. }
  235. else
  236. {
  237. llwarns << "Out of order indexes." << llendl;
  238. value = right->second.mRotation;
  239. }
  240. }
  241. return value;
  242. }
  243. LLQuaternion LLKeyframeMotion::RotationCurve::interp(F32 u,
  244. RotationKey& before,
  245. RotationKey& after)
  246. {
  247. if (mInterpolationType == IT_STEP)
  248. {
  249. return before.mRotation;
  250. }
  251. else
  252. {
  253. return nlerp(u, before.mRotation, after.mRotation);
  254. }
  255. }
  256. //-----------------------------------------------------------------------------
  257. // LLKeyframeMotion::PositionCurve sub-class
  258. //-----------------------------------------------------------------------------
  259. LLKeyframeMotion::PositionCurve::PositionCurve()
  260. {
  261. mInterpolationType = LLKeyframeMotion::IT_LINEAR;
  262. mNumKeys = 0;
  263. }
  264. LLKeyframeMotion::PositionCurve::~PositionCurve()
  265. {
  266. mKeys.clear();
  267. mNumKeys = 0;
  268. }
  269. LLVector3 LLKeyframeMotion::PositionCurve::getValue(F32 time, F32 duration)
  270. {
  271. LLVector3 value;
  272. if (mKeys.empty())
  273. {
  274. return value;
  275. }
  276. key_map_t::iterator right = mKeys.lower_bound(time);
  277. if (right == mKeys.end())
  278. {
  279. // Past last key
  280. value = (--right)->second.mPosition;
  281. }
  282. else if (right == mKeys.begin() || right->first == time)
  283. {
  284. // Before first key or exactly on a key
  285. value = right->second.mPosition;
  286. }
  287. else
  288. {
  289. // Between two keys
  290. key_map_t::iterator left = right;
  291. F32 index_before = (--left)->first;
  292. F32 index_after = right->first;
  293. PositionKey& pos_before = left->second;
  294. PositionKey& pos_after = right->second;
  295. if (index_after > index_before)
  296. {
  297. if (right == mKeys.end())
  298. {
  299. pos_after = mLoopInKey;
  300. index_after = duration;
  301. }
  302. F32 u = (time - index_before) / (index_after - index_before);
  303. value = interp(u, pos_before, pos_after);
  304. }
  305. else
  306. {
  307. llwarns << "Out of order indexes." << llendl;
  308. value = right->second.mPosition;
  309. }
  310. }
  311. llassert(value.isFinite());
  312. return value;
  313. }
  314. LLVector3 LLKeyframeMotion::PositionCurve::interp(F32 u, PositionKey& before,
  315. PositionKey& after)
  316. {
  317. if (mInterpolationType == IT_STEP)
  318. {
  319. return before.mPosition;
  320. }
  321. return lerp(before.mPosition, after.mPosition, u);
  322. }
  323. //-----------------------------------------------------------------------------
  324. // LLKeyframeMotion::JointMotion sub-class
  325. //-----------------------------------------------------------------------------
  326. void LLKeyframeMotion::JointMotion::update(LLJointState* joint_state, F32 time,
  327. F32 duration)
  328. {
  329. // This value being 0 is the cause of:
  330. // https://jira.lindenlab.com/browse/SL-22678
  331. // but I have not managed to get a stack to see how it got here. Testing
  332. // for 0 here will stop the crash.
  333. if (!joint_state)
  334. {
  335. return;
  336. }
  337. U32 usage = joint_state->getUsage();
  338. // Update scale component of joint state
  339. if ((usage & LLJointState::SCALE) && mScaleCurve.mNumKeys)
  340. {
  341. joint_state->setScale(mScaleCurve.getValue(time, duration));
  342. }
  343. // Update rotation component of joint state
  344. if ((usage & LLJointState::ROT) && mRotationCurve.mNumKeys)
  345. {
  346. joint_state->setRotation(mRotationCurve.getValue(time, duration));
  347. }
  348. // Update position component of joint state
  349. if ((usage & LLJointState::POS) && mPositionCurve.mNumKeys)
  350. {
  351. joint_state->setPosition(mPositionCurve.getValue(time, duration));
  352. }
  353. }
  354. //-----------------------------------------------------------------------------
  355. // LLKeyframeMotion class
  356. //-----------------------------------------------------------------------------
  357. LLKeyframeMotion::LLKeyframeMotion(const LLUUID& id)
  358. : LLMotion(id),
  359. mCharacter(NULL),
  360. mJointMotionList(NULL),
  361. mPelvisp(NULL),
  362. mLastSkeletonSerialNum(0),
  363. mLastUpdateTime(0.f),
  364. mLastLoopedTime(0.f),
  365. mAssetStatus(ASSET_UNDEFINED)
  366. {
  367. }
  368. LLKeyframeMotion::~LLKeyframeMotion()
  369. {
  370. std::for_each(mConstraints.begin(), mConstraints.end(), DeletePointer());
  371. mConstraints.clear();
  372. }
  373. LLPointer<LLJointState>& LLKeyframeMotion::getJointState(U32 index)
  374. {
  375. if (LL_UNLIKELY(index >= mJointStates.size()))
  376. {
  377. llerrs << "Index " << index << " out of range for motion: "
  378. << getName() << " - Maximum was: " << mJointStates.size() - 1
  379. << llendl;
  380. }
  381. return mJointStates[index];
  382. }
  383. LLJoint* LLKeyframeMotion::getJoint(U32 index)
  384. {
  385. if (LL_UNLIKELY(index >= mJointStates.size()))
  386. {
  387. llwarns_once << "Index " << index << " out of range for motion: "
  388. << getName() << " - Maximum is: "
  389. << mJointStates.size() - 1 << llendl;
  390. return NULL;
  391. }
  392. return mJointStates[index]->getJoint();
  393. }
  394. LLMotion::LLMotionInitStatus LLKeyframeMotion::onInitialize(LLCharacter* chr)
  395. {
  396. mCharacter = chr;
  397. // Asset already loaded ?
  398. switch (mAssetStatus)
  399. {
  400. case ASSET_NEEDS_FETCH:
  401. {
  402. // Request asset
  403. if (!gAssetStoragep)
  404. {
  405. llwarns << "No asset storage system. Aborted." << llendl;
  406. mAssetStatus = ASSET_FETCH_FAILED;
  407. return STATUS_FAILURE;
  408. }
  409. if (mID.isNull())
  410. {
  411. llwarns_once << "Attempt to fetch animation " << mName
  412. << " with a null Id. Aborted." << llendl;
  413. mAssetStatus = ASSET_FETCH_FAILED;
  414. return STATUS_FAILURE;
  415. }
  416. mAssetStatus = ASSET_FETCHED;
  417. LLUUID* character_id = new LLUUID(mCharacter->getID());
  418. gAssetStoragep->getAssetData(mID, LLAssetType::AT_ANIMATION,
  419. onLoadComplete, (void*)character_id,
  420. false);
  421. return STATUS_HOLD;
  422. }
  423. case ASSET_FETCHED:
  424. return STATUS_HOLD;
  425. case ASSET_FETCH_FAILED:
  426. return STATUS_FAILURE;
  427. case ASSET_LOADED:
  428. return STATUS_SUCCESS;
  429. default:
  430. // We do not know what state the asset is in yet, so keep going
  431. // check keyframe cache first then static cache then asset request
  432. break;
  433. }
  434. LLKeyframeMotion::JointMotionList* joint_motion_list =
  435. LLKeyframeDataCache::getKeyframeData(getID());
  436. if (joint_motion_list)
  437. {
  438. // Motion already existed in cache, so grab it
  439. mJointMotionList = joint_motion_list;
  440. U32 count = mJointMotionList->getNumJointMotions();
  441. mJointStates.reserve(count);
  442. // Do not forget to allocate joint states. Set up joint states to point
  443. // to character joints.
  444. for (U32 i = 0; i < count; ++i)
  445. {
  446. JointMotion* joint_motion = mJointMotionList->getJointMotion(i);
  447. if (!joint_motion)
  448. {
  449. llwarns << "NULL joint motion found !" << llendl;
  450. continue;
  451. }
  452. LLJoint* joint = NULL;
  453. U32 joint_key = joint_motion->mJointKey;
  454. if (joint_key)
  455. {
  456. joint = mCharacter->getJoint(joint_key);
  457. }
  458. if (joint)
  459. {
  460. LLPointer<LLJointState> joint_state = new LLJointState;
  461. mJointStates.push_back(joint_state);
  462. joint_state->setJoint(joint);
  463. joint_state->setUsage(joint_motion->mUsage);
  464. joint_state->setPriority(joint_motion->mPriority);
  465. }
  466. else
  467. {
  468. // Add dummy joint state with no associated joint
  469. mJointStates.push_back(new LLJointState);
  470. }
  471. }
  472. mAssetStatus = ASSET_LOADED;
  473. setupPose();
  474. return STATUS_SUCCESS;
  475. }
  476. // Check that everything is properly initialized...
  477. if (sStaticAnimsDir.empty())
  478. {
  479. sStaticAnimsDir = gDirUtil.getFullPath(LL_PATH_CHARACTER, "anims");
  480. sStaticAnimsDir += LL_DIR_DELIM_STR;
  481. }
  482. bool success = false;
  483. size_t anim_file_size = 0;
  484. U8* anim_data = NULL;
  485. // We first search in the static animation assets bundled with the viewer
  486. std::string filename = sStaticAnimsDir + mID.asString() + ".lla";
  487. llstat stat;
  488. if (!LLFile::stat(filename, &stat)) // If the file exists
  489. {
  490. anim_file_size = stat.st_size;
  491. if (anim_file_size > 0)
  492. {
  493. LLFILE* fp = LLFile::open(filename, "rb");
  494. if (fp)
  495. {
  496. anim_data = new (std::nothrow) U8[anim_file_size];
  497. if (anim_data)
  498. {
  499. success = fread(anim_data, 1,
  500. anim_file_size, fp) == anim_file_size;
  501. }
  502. else
  503. {
  504. LLMemory::allocationFailed();
  505. llwarns << "Failed to allocate data buffer (size: "
  506. << anim_file_size << " bytes) for animation: " << mID
  507. << llendl;
  508. }
  509. }
  510. LLFile::close(fp);
  511. }
  512. }
  513. if (success)
  514. {
  515. LL_DEBUGS("KeyFrameMotion") << "Loaded keyframe data from static anim file: "
  516. << filename << LL_ENDL;
  517. }
  518. else
  519. // If not a valid static asset, then try the cache...
  520. {
  521. // Load named file by concatenating the character prefix with the
  522. // motion name. Load data into a buffer to be parsed.
  523. LLFileSystem* anim_file = new LLFileSystem(mID);
  524. if (!anim_file || !anim_file->getSize())
  525. {
  526. delete anim_file;
  527. // Request asset over network on next call to load
  528. mAssetStatus = ASSET_NEEDS_FETCH;
  529. return STATUS_HOLD;
  530. }
  531. anim_file_size = anim_file->getSize();
  532. anim_data = new (std::nothrow) U8[anim_file_size];
  533. if (anim_data)
  534. {
  535. success = anim_file->read(anim_data, anim_file_size);
  536. }
  537. else
  538. {
  539. LLMemory::allocationFailed();
  540. llwarns << "Failed to allocate data buffer (size: "
  541. << anim_file_size << " bytes) for animation: " << mID
  542. << llendl;
  543. }
  544. delete anim_file;
  545. }
  546. if (!success)
  547. {
  548. llwarns << "Cannot open animation file " << mID << llendl;
  549. mAssetStatus = ASSET_FETCH_FAILED;
  550. return STATUS_FAILURE;
  551. }
  552. LL_DEBUGS("KeyFrameMotion") << "Loading keyframe data for: " << getName()
  553. << ":" << getID() << " (" << anim_file_size
  554. << " bytes)" << LL_ENDL;
  555. LLDataPackerBinaryBuffer dp(anim_data, anim_file_size);
  556. if (!deserialize(dp, mID))
  557. {
  558. llwarns << "Failed to decode asset for animation " << getName() << ": "
  559. << getID() << llendl;
  560. mAssetStatus = ASSET_FETCH_FAILED;
  561. return STATUS_FAILURE;
  562. }
  563. delete[] anim_data;
  564. mAssetStatus = ASSET_LOADED;
  565. return STATUS_SUCCESS;
  566. }
  567. bool LLKeyframeMotion::setupPose()
  568. {
  569. if (!mJointMotionList || !mCharacter)
  570. {
  571. return false;
  572. }
  573. // Add all valid joint states to the pose
  574. for (U32 jm = 0, count = mJointMotionList->getNumJointMotions();
  575. jm < count; ++jm)
  576. {
  577. LLPointer<LLJointState> joint_state = getJointState(jm);
  578. if (joint_state->getJoint())
  579. {
  580. addJointState(joint_state);
  581. }
  582. }
  583. // Initialize joint constraints
  584. for (JointMotionList::constraint_list_t::iterator
  585. iter = mJointMotionList->mConstraints.begin();
  586. iter != mJointMotionList->mConstraints.end(); ++iter)
  587. {
  588. JointConstraintSharedData* jcsd = *iter;
  589. JointConstraint* constraintp = new JointConstraint(jcsd);
  590. initializeConstraint(constraintp);
  591. mConstraints.push_front(constraintp);
  592. }
  593. if (mJointMotionList->mConstraints.size())
  594. {
  595. mPelvisp = mCharacter->getJoint(LL_JOINT_KEY_PELVIS);
  596. if (!mPelvisp)
  597. {
  598. return false;
  599. }
  600. }
  601. // Setup loop keys
  602. setLoopIn(mJointMotionList->mLoopInPoint);
  603. setLoopOut(mJointMotionList->mLoopOutPoint);
  604. return true;
  605. }
  606. bool LLKeyframeMotion::onActivate()
  607. {
  608. if (!mJointMotionList || !mCharacter)
  609. {
  610. return false;
  611. }
  612. // If the keyframe anim has an associated emote, trigger it.
  613. const LLUUID& emote_id = mJointMotionList->mEmoteID;
  614. if (emote_id.notNull())
  615. {
  616. // Do not start emote if already active to avoid recursion
  617. if (!mCharacter->isMotionActive(emote_id))
  618. {
  619. mCharacter->startMotion(emote_id);
  620. }
  621. }
  622. mLastLoopedTime = 0.f;
  623. return true;
  624. }
  625. bool LLKeyframeMotion::onUpdate(F32 time, U8* joint_mask)
  626. {
  627. if (!mJointMotionList)
  628. {
  629. return false;
  630. }
  631. if (time - mLastUpdateTime < 0.f)
  632. {
  633. mLastUpdateTime = mLastLoopedTime = time;
  634. LL_DEBUGS("KeyFrameMotion") << "Negative time passed; time delta zeroed."
  635. << LL_ENDL;
  636. }
  637. if (mJointMotionList->mLoop)
  638. {
  639. if (mJointMotionList->mDuration == 0.f)
  640. {
  641. time = 0.f;
  642. mLastLoopedTime = 0.f;
  643. }
  644. else if (mStopped)
  645. {
  646. mLastLoopedTime = llmin(mJointMotionList->mDuration,
  647. mLastLoopedTime + time - mLastUpdateTime);
  648. }
  649. else if (time > mJointMotionList->mLoopOutPoint)
  650. {
  651. if (mJointMotionList->mLoopOutPoint -
  652. mJointMotionList->mLoopInPoint == 0.f)
  653. {
  654. mLastLoopedTime = mJointMotionList->mLoopOutPoint;
  655. }
  656. else
  657. {
  658. mLastLoopedTime = mJointMotionList->mLoopInPoint +
  659. fmod(time - mJointMotionList->mLoopOutPoint,
  660. mJointMotionList->mLoopOutPoint -
  661. mJointMotionList->mLoopInPoint);
  662. }
  663. }
  664. else
  665. {
  666. mLastLoopedTime = time;
  667. }
  668. }
  669. else
  670. {
  671. mLastLoopedTime = time;
  672. }
  673. applyKeyframes(mLastLoopedTime);
  674. applyConstraints(mLastLoopedTime, joint_mask);
  675. mLastUpdateTime = time;
  676. return mLastLoopedTime <= mJointMotionList->mDuration;
  677. }
  678. void LLKeyframeMotion::applyKeyframes(F32 time)
  679. {
  680. if (!mJointMotionList || !mCharacter)
  681. {
  682. return;
  683. }
  684. U32 count = mJointMotionList->getNumJointMotions();
  685. if (count > mJointStates.size())
  686. {
  687. llwarns_once << "More joint states (" << count
  688. << ") than joint motion list members ("
  689. << mJointStates.size() << "). Aborting update." << llendl;
  690. return;
  691. }
  692. for (U32 i = 0; i < count; ++i)
  693. {
  694. JointMotion* joint_motion = mJointMotionList->getJointMotion(i);
  695. if (!joint_motion)
  696. {
  697. llwarns << "NULL joint motion found !" << llendl;
  698. continue;
  699. }
  700. joint_motion->update(mJointStates[i], time,
  701. mJointMotionList->mDuration);
  702. }
  703. static const std::string hand_pose = "Hand Pose";
  704. static const std::string hand_pose_prio = "Hand Pose Priority";
  705. LLJoint::JointPriority* pose_priority =
  706. (LLJoint::JointPriority*)mCharacter->getAnimationData(hand_pose_prio);
  707. if (pose_priority)
  708. {
  709. if (mJointMotionList->mMaxPriority >= *pose_priority)
  710. {
  711. mCharacter->setAnimationData(hand_pose,
  712. &mJointMotionList->mHandPose);
  713. mCharacter->setAnimationData(hand_pose_prio,
  714. &mJointMotionList->mMaxPriority);
  715. }
  716. }
  717. else
  718. {
  719. mCharacter->setAnimationData(hand_pose, &mJointMotionList->mHandPose);
  720. mCharacter->setAnimationData(hand_pose_prio,
  721. &mJointMotionList->mMaxPriority);
  722. }
  723. }
  724. // *TODO: investigate replacing spring simulation with critically damped motion
  725. void LLKeyframeMotion::applyConstraints(F32 time, U8* joint_mask)
  726. {
  727. // Re-init constraints if skeleton has changed
  728. if (mCharacter->getSkeletonSerialNum() != mLastSkeletonSerialNum)
  729. {
  730. mLastSkeletonSerialNum = mCharacter->getSkeletonSerialNum();
  731. for (constraint_list_t::iterator iter = mConstraints.begin();
  732. iter != mConstraints.end(); ++iter)
  733. {
  734. JointConstraint* constraintp = *iter;
  735. initializeConstraint(constraintp);
  736. }
  737. }
  738. // Apply constraints
  739. for (constraint_list_t::iterator iter = mConstraints.begin();
  740. iter != mConstraints.end(); ++iter)
  741. {
  742. JointConstraint* constraintp = *iter;
  743. applyConstraint(constraintp, time, joint_mask);
  744. }
  745. }
  746. void LLKeyframeMotion::onDeactivate()
  747. {
  748. for (constraint_list_t::iterator iter = mConstraints.begin(),
  749. end = mConstraints.end();
  750. iter != end; ++iter)
  751. {
  752. JointConstraint* constraintp = *iter;
  753. deactivateConstraint(constraintp);
  754. }
  755. }
  756. // Time is in seconds since character creation
  757. void LLKeyframeMotion::setStopTime(F32 time)
  758. {
  759. LLMotion::setStopTime(time);
  760. if (mJointMotionList && mJointMotionList->mLoop &&
  761. mJointMotionList->mLoopOutPoint != mJointMotionList->mDuration)
  762. {
  763. F32 start_loop_time = mActivationTimestamp + mJointMotionList->mLoopInPoint;
  764. F32 loop_fraction_time;
  765. if (mJointMotionList->mLoopOutPoint == mJointMotionList->mLoopInPoint)
  766. {
  767. loop_fraction_time = 0.f;
  768. }
  769. else
  770. {
  771. loop_fraction_time = fmod(time - start_loop_time,
  772. mJointMotionList->mLoopOutPoint -
  773. mJointMotionList->mLoopInPoint);
  774. }
  775. mStopTimestamp = llmax(time,
  776. time - loop_fraction_time +
  777. mJointMotionList->mDuration -
  778. mJointMotionList->mLoopInPoint -
  779. getEaseOutDuration());
  780. }
  781. }
  782. void LLKeyframeMotion::initializeConstraint(JointConstraint* constraint)
  783. {
  784. JointConstraintSharedData* jcsd = constraint->mSharedData;
  785. LLVector3 source_pos =
  786. mCharacter->getVolumePos(jcsd->mSourceConstraintVolId,
  787. jcsd->mSourceConstraintOffset);
  788. LLJoint* cur_joint = getJoint(jcsd->mJointStateIndices[0]);
  789. if (!cur_joint)
  790. {
  791. return;
  792. }
  793. F32 src_pos_offset = dist_vec(source_pos, cur_joint->getWorldPosition());
  794. constraint->mTotalLength = constraint->mJointLengths[0] =
  795. dist_vec(cur_joint->getParent()->getWorldPosition(), source_pos);
  796. // Grab joint lengths
  797. for (S32 joint_num = 1, count = jcsd->mChainLength;
  798. joint_num < count; ++joint_num)
  799. {
  800. cur_joint =
  801. getJointState(jcsd->mJointStateIndices[joint_num])->getJoint();
  802. if (!cur_joint)
  803. {
  804. return;
  805. }
  806. constraint->mJointLengths[joint_num] =
  807. dist_vec(cur_joint->getWorldPosition(),
  808. cur_joint->getParent()->getWorldPosition());
  809. constraint->mTotalLength += constraint->mJointLengths[joint_num];
  810. }
  811. // Store fraction of total chain length so we know how to shear the entire
  812. // chain towards the goal position
  813. for (S32 joint_num = 1, count = jcsd->mChainLength;
  814. joint_num < count; ++joint_num)
  815. {
  816. constraint->mJointLengthFractions[joint_num] =
  817. constraint->mJointLengths[joint_num] / constraint->mTotalLength;
  818. }
  819. // Add last step in chain, from final joint to constraint position
  820. constraint->mTotalLength += src_pos_offset;
  821. constraint->mSourceVolume =
  822. mCharacter->findCollisionVolume(jcsd->mSourceConstraintVolId);
  823. constraint->mTargetVolume =
  824. mCharacter->findCollisionVolume(jcsd->mTargetConstraintVolId);
  825. }
  826. void LLKeyframeMotion::activateConstraint(JointConstraint* constraint)
  827. {
  828. JointConstraintSharedData* jcsd = constraint->mSharedData;
  829. constraint->mActive = true;
  830. // Grab ground position if we need to
  831. if (jcsd->mConstraintTargetType == CONSTRAINT_TARGET_TYPE_GROUND)
  832. {
  833. LLVector3 source_pos =
  834. mCharacter->getVolumePos(jcsd->mSourceConstraintVolId,
  835. jcsd->mSourceConstraintOffset);
  836. LLVector3 ground_pos_agent;
  837. mCharacter->getGround(source_pos, ground_pos_agent,
  838. constraint->mGroundNorm);
  839. constraint->mGroundPos =
  840. mCharacter->getPosGlobalFromAgent(ground_pos_agent +
  841. jcsd->mTargetConstraintOffset);
  842. }
  843. for (S32 joint_num = 1, count = jcsd->mChainLength;
  844. joint_num < count; ++joint_num)
  845. {
  846. LLJoint* cur_joint = getJoint(jcsd->mJointStateIndices[joint_num]);
  847. if (!cur_joint)
  848. {
  849. return;
  850. }
  851. constraint->mPositions[joint_num] =
  852. (cur_joint->getWorldPosition() - mPelvisp->getWorldPosition()) *
  853. ~mPelvisp->getWorldRotation();
  854. }
  855. constraint->mWeight = 1.f;
  856. }
  857. void LLKeyframeMotion::deactivateConstraint(JointConstraint* constraintp)
  858. {
  859. if (constraintp->mSourceVolume)
  860. {
  861. constraintp->mSourceVolume->mUpdateXform = false;
  862. }
  863. if (constraintp->mSharedData->mConstraintTargetType !=
  864. CONSTRAINT_TARGET_TYPE_GROUND)
  865. {
  866. if (constraintp->mTargetVolume)
  867. {
  868. constraintp->mTargetVolume->mUpdateXform = false;
  869. }
  870. }
  871. constraintp->mActive = false;
  872. }
  873. void LLKeyframeMotion::applyConstraint(JointConstraint* constraint, F32 time,
  874. U8* joint_mask)
  875. {
  876. JointConstraintSharedData* jcsd = constraint->mSharedData;
  877. if (!jcsd) return;
  878. S32 chain_length = jcsd->mChainLength;
  879. LLVector3 positions[MAX_CHAIN_LENGTH];
  880. const F32* joint_lengths = constraint->mJointLengths;
  881. LLVector3 velocities[MAX_CHAIN_LENGTH - 1];
  882. LLQuaternion old_rots[MAX_CHAIN_LENGTH];
  883. S32 joint_num;
  884. if (time < jcsd->mEaseInStartTime)
  885. {
  886. return;
  887. }
  888. if (time > jcsd->mEaseOutStopTime)
  889. {
  890. if (constraint->mActive)
  891. {
  892. deactivateConstraint(constraint);
  893. }
  894. return;
  895. }
  896. if (!constraint->mActive || time < jcsd->mEaseInStopTime)
  897. {
  898. activateConstraint(constraint);
  899. }
  900. LLJoint* root_joint = getJoint(jcsd->mJointStateIndices[chain_length]);
  901. if (!root_joint)
  902. {
  903. return;
  904. }
  905. LLVector3 root_pos = root_joint->getWorldPosition();
  906. root_joint->getParent()->getWorldRotation();
  907. // Apply underlying keyframe animation to get nominal "kinematic" joint
  908. // positions
  909. for (joint_num = 0; joint_num <= chain_length; ++joint_num)
  910. {
  911. LLJoint* cur_joint = getJoint(jcsd->mJointStateIndices[joint_num]);
  912. if (!cur_joint)
  913. {
  914. return;
  915. }
  916. if (joint_mask[cur_joint->getJointNum()] >= (0xff >> (7 - getPriority())))
  917. {
  918. // Skip constraint
  919. return;
  920. }
  921. old_rots[joint_num] = cur_joint->getRotation();
  922. cur_joint->setRotation(getJointState(jcsd->mJointStateIndices[joint_num])->getRotation());
  923. }
  924. LLVector3 keyframe_source_pos =
  925. mCharacter->getVolumePos(jcsd->mSourceConstraintVolId,
  926. jcsd->mSourceConstraintOffset);
  927. LLVector3 target_pos;
  928. switch (jcsd->mConstraintTargetType)
  929. {
  930. case CONSTRAINT_TARGET_TYPE_GROUND:
  931. target_pos =
  932. mCharacter->getPosAgentFromGlobal(constraint->mGroundPos);
  933. break;
  934. case CONSTRAINT_TARGET_TYPE_BODY:
  935. target_pos =
  936. mCharacter->getVolumePos(jcsd->mTargetConstraintVolId,
  937. jcsd->mTargetConstraintOffset);
  938. break;
  939. default:
  940. break;
  941. }
  942. if (jcsd->mConstraintType == CONSTRAINT_TYPE_PLANE)
  943. {
  944. LLVector3 norm;
  945. switch (jcsd->mConstraintTargetType)
  946. {
  947. case CONSTRAINT_TARGET_TYPE_GROUND:
  948. norm = constraint->mGroundNorm;
  949. break;
  950. case CONSTRAINT_TARGET_TYPE_BODY:
  951. {
  952. LLJoint* target_jointp =
  953. mCharacter->findCollisionVolume(jcsd->mTargetConstraintVolId);
  954. if (target_jointp)
  955. {
  956. // *FIX: do proper normal calculation for stretched spheres
  957. // (inverse transpose)
  958. norm = target_pos - target_jointp->getWorldPosition();
  959. }
  960. if (norm.isExactlyZero())
  961. {
  962. LLJoint* source_jointp =
  963. mCharacter->findCollisionVolume(jcsd->mSourceConstraintVolId);
  964. norm = -1.f * jcsd->mSourceConstraintOffset;
  965. if (source_jointp)
  966. {
  967. norm = norm * source_jointp->getWorldRotation();
  968. }
  969. }
  970. norm.normalize();
  971. break;
  972. }
  973. default:
  974. norm.clear();
  975. break;
  976. }
  977. target_pos = keyframe_source_pos +
  978. (norm * ((target_pos - keyframe_source_pos) * norm));
  979. }
  980. if (chain_length != 0 &&
  981. dist_vec_squared(root_pos, target_pos) * 0.95f >
  982. constraint->mTotalLength * constraint->mTotalLength)
  983. {
  984. constraint->mWeight = lerp(constraint->mWeight, 0.f,
  985. LLCriticalDamp::getInterpolant(0.1f));
  986. }
  987. else
  988. {
  989. constraint->mWeight = lerp(constraint->mWeight, 1.f,
  990. LLCriticalDamp::getInterpolant(0.3f));
  991. }
  992. F32 weight = 1.f;
  993. if (jcsd->mEaseOutStopTime != 0.f)
  994. {
  995. weight = constraint->mWeight *
  996. llmin(clamp_rescale(time, jcsd->mEaseInStartTime,
  997. jcsd->mEaseInStopTime, 0.f, 1.f),
  998. clamp_rescale(time, jcsd->mEaseOutStartTime,
  999. jcsd->mEaseOutStopTime, 1.f, 0.f));
  1000. }
  1001. LLVector3 source_to_target = target_pos - keyframe_source_pos;
  1002. if (chain_length)
  1003. {
  1004. LLJoint* end_joint = getJoint(jcsd->mJointStateIndices[0]);
  1005. if (!end_joint)
  1006. {
  1007. return;
  1008. }
  1009. LLQuaternion end_rot = end_joint->getWorldRotation();
  1010. // Slam start and end of chain to the proper positions (rest of chain
  1011. // stays put)
  1012. positions[0] = lerp(keyframe_source_pos, target_pos, weight);
  1013. positions[chain_length] = root_pos;
  1014. // Grab keyframe-specified positions of joints
  1015. for (joint_num = 1; joint_num < chain_length; ++joint_num)
  1016. {
  1017. LLJoint* cur_joint = getJoint(jcsd->mJointStateIndices[joint_num]);
  1018. if (!cur_joint)
  1019. {
  1020. return;
  1021. }
  1022. LLVector3 kinematic_pos =
  1023. cur_joint->getWorldPosition() +
  1024. (source_to_target *
  1025. constraint->mJointLengthFractions[joint_num]);
  1026. // Convert intermediate joint positions to world coordinates
  1027. positions[joint_num] = constraint->mPositions[joint_num] *
  1028. mPelvisp->getWorldRotation() +
  1029. mPelvisp->getWorldPosition();
  1030. F32 tc = 1.f / clamp_rescale(constraint->mFixupDistanceRMS, 0.f,
  1031. 0.5f, 0.2f, 8.f);
  1032. positions[joint_num] = lerp(positions[joint_num], kinematic_pos,
  1033. LLCriticalDamp::getInterpolant(tc,
  1034. false));
  1035. }
  1036. S32 max_iter_count =
  1037. ll_roundp(clamp_rescale(mCharacter->getPixelArea(),
  1038. MAX_PIXEL_AREA_CONSTRAINTS,
  1039. MIN_PIXEL_AREA_CONSTRAINTS,
  1040. (F32)MAX_ITERATIONS,
  1041. (F32)MIN_ITERATIONS));
  1042. for (S32 iteration_count = 0; iteration_count < max_iter_count;
  1043. ++iteration_count)
  1044. {
  1045. S32 num_joints_finished = 0;
  1046. for (joint_num = 1; joint_num < chain_length; ++joint_num)
  1047. {
  1048. // Constraint to child
  1049. LLVector3 acceleration =
  1050. (positions[joint_num - 1] - positions[joint_num]) *
  1051. (dist_vec(positions[joint_num], positions[joint_num - 1]) -
  1052. joint_lengths[joint_num - 1]) * JOINT_LENGTH_K;
  1053. // Constraint to parent
  1054. acceleration += (positions[joint_num + 1] -
  1055. positions[joint_num]) *
  1056. (dist_vec(positions[joint_num + 1],
  1057. positions[joint_num]) -
  1058. joint_lengths[joint_num]) * JOINT_LENGTH_K;
  1059. if (acceleration.lengthSquared() < MIN_ACCELERATION_SQUARED)
  1060. {
  1061. ++num_joints_finished;
  1062. }
  1063. velocities[joint_num - 1] = velocities[joint_num - 1] * 0.7f;
  1064. positions[joint_num] += velocities[joint_num - 1] +
  1065. acceleration * 0.5f;
  1066. velocities[joint_num - 1] += acceleration;
  1067. }
  1068. if (iteration_count >= MIN_ITERATION_COUNT &&
  1069. num_joints_finished == chain_length - 1)
  1070. {
  1071. break;
  1072. }
  1073. }
  1074. for (joint_num = chain_length; joint_num > 0; --joint_num)
  1075. {
  1076. LLJoint* cur_joint = getJoint(jcsd->mJointStateIndices[joint_num]);
  1077. if (!cur_joint)
  1078. {
  1079. return;
  1080. }
  1081. LLJoint* child_joint =
  1082. getJoint(jcsd->mJointStateIndices[joint_num - 1]);
  1083. if (!child_joint)
  1084. {
  1085. return;
  1086. }
  1087. LLQuaternion parent_rot =
  1088. cur_joint->getParent()->getWorldRotation();
  1089. LLQuaternion cur_rot = cur_joint->getWorldRotation();
  1090. LLQuaternion fixup_rot;
  1091. LLVector3 target_at = positions[joint_num - 1] -
  1092. positions[joint_num];
  1093. // At bottom of chain, use point on collision volume, not joint
  1094. // position
  1095. LLVector3 current_at;
  1096. if (joint_num == 1)
  1097. {
  1098. current_at =
  1099. mCharacter->getVolumePos(jcsd->mSourceConstraintVolId,
  1100. jcsd->mSourceConstraintOffset) -
  1101. cur_joint->getWorldPosition();
  1102. }
  1103. else
  1104. {
  1105. current_at = child_joint->getPosition() * cur_rot;
  1106. }
  1107. fixup_rot.shortestArc(current_at, target_at);
  1108. LLQuaternion tgt_rot = (cur_rot * fixup_rot) * ~parent_rot;
  1109. if (weight != 1.f)
  1110. {
  1111. LLQuaternion cur_rot =
  1112. getJointState(jcsd->mJointStateIndices[joint_num])->getRotation();
  1113. tgt_rot = nlerp(weight, cur_rot, tgt_rot);
  1114. }
  1115. getJointState(jcsd->mJointStateIndices[joint_num])->setRotation(tgt_rot);
  1116. cur_joint->setRotation(tgt_rot);
  1117. }
  1118. // End local rotation
  1119. LLQuaternion end_loc_rot = end_rot *
  1120. ~end_joint->getParent()->getWorldRotation();
  1121. if (weight == 1.f)
  1122. {
  1123. getJointState(jcsd->mJointStateIndices[0])->setRotation(end_loc_rot);
  1124. }
  1125. else
  1126. {
  1127. LLQuaternion cur_rot =
  1128. getJointState(jcsd->mJointStateIndices[0])->getRotation();
  1129. getJointState(jcsd->mJointStateIndices[0])->setRotation(nlerp(weight,
  1130. cur_rot,
  1131. end_loc_rot));
  1132. }
  1133. // Save simulated positions in pelvis-space and calculate total fixup
  1134. // distance
  1135. constraint->mFixupDistanceRMS = 0.f;
  1136. F32 delta_time = llmax(0.02f, fabsf(time - mLastUpdateTime));
  1137. for (joint_num = 1; joint_num < chain_length; ++joint_num)
  1138. {
  1139. LLVector3 new_pos =
  1140. (positions[joint_num] - mPelvisp->getWorldPosition()) *
  1141. ~mPelvisp->getWorldRotation();
  1142. constraint->mFixupDistanceRMS +=
  1143. dist_vec_squared(new_pos, constraint->mPositions[joint_num]) /
  1144. delta_time;
  1145. constraint->mPositions[joint_num] = new_pos;
  1146. }
  1147. constraint->mFixupDistanceRMS *= 1.f / (constraint->mTotalLength *
  1148. (F32)(chain_length - 1));
  1149. constraint->mFixupDistanceRMS = sqrtf(constraint->mFixupDistanceRMS);
  1150. // Reset old joint rots
  1151. for (joint_num = 0; joint_num <= chain_length; ++joint_num)
  1152. {
  1153. LLJoint* cur_joint = getJoint(jcsd->mJointStateIndices[joint_num]);
  1154. if (!cur_joint)
  1155. {
  1156. return;
  1157. }
  1158. cur_joint->setRotation(old_rots[joint_num]);
  1159. }
  1160. }
  1161. // Simple positional constraint (pelvis only)
  1162. else if (getJointState(jcsd->mJointStateIndices[0])->getUsage() &
  1163. LLJointState::POS)
  1164. {
  1165. LLVector3 delta = source_to_target * weight;
  1166. LLPointer<LLJointState> cur_jt_state =
  1167. getJointState(jcsd->mJointStateIndices[0]);
  1168. if (cur_jt_state->getJoint() && cur_jt_state->getJoint()->getParent())
  1169. {
  1170. LLQuaternion parent_rot =
  1171. cur_jt_state->getJoint()->getParent()->getWorldRotation();
  1172. delta = delta * ~parent_rot;
  1173. cur_jt_state->setPosition(cur_jt_state->getJoint()->getPosition() +
  1174. delta);
  1175. }
  1176. }
  1177. }
  1178. // NOTE: 'allow_invalid_joints' should be true when handling existing content,
  1179. // to avoid breakage. During upload, we should be more restrictive and reject
  1180. // such animations.
  1181. bool LLKeyframeMotion::deserialize(LLDataPacker& dp, const LLUUID& asset_id,
  1182. bool allow_invalid_joints)
  1183. {
  1184. // Check version
  1185. U16 version;
  1186. if (!dp.unpackU16(version, "version"))
  1187. {
  1188. llwarns << "Cannot read version number for motion " << asset_id
  1189. << llendl;
  1190. return false;
  1191. }
  1192. U16 sub_version;
  1193. if (!dp.unpackU16(sub_version, "sub_version"))
  1194. {
  1195. llwarns << "Cannot read sub-version number for motion " << asset_id
  1196. << llendl;
  1197. return false;
  1198. }
  1199. bool old_version = false;
  1200. if (version == 0 && sub_version == 1)
  1201. {
  1202. old_version = true;
  1203. }
  1204. else if (version != KEYFRAME_MOTION_VERSION ||
  1205. sub_version != KEYFRAME_MOTION_SUBVERSION)
  1206. {
  1207. llwarns << "Bad animation version " << version << "." << sub_version
  1208. << " for motion " << asset_id << llendl;
  1209. llassert(false);
  1210. return false;
  1211. }
  1212. // Get base priority
  1213. S32 temp_priority;
  1214. if (!dp.unpackS32(temp_priority, "base_priority"))
  1215. {
  1216. llwarns << "Cannot read animation base priority for motion "
  1217. << asset_id << llendl;
  1218. return false;
  1219. }
  1220. mJointMotionList = new LLKeyframeMotion::JointMotionList;
  1221. mJointMotionList->mBasePriority = (LLJoint::JointPriority)temp_priority;
  1222. if (mJointMotionList->mBasePriority >= LLJoint::ADDITIVE_PRIORITY)
  1223. {
  1224. mJointMotionList->mBasePriority =
  1225. (LLJoint::JointPriority)((S32)LLJoint::ADDITIVE_PRIORITY - 1);
  1226. mJointMotionList->mMaxPriority = mJointMotionList->mBasePriority;
  1227. }
  1228. else if (mJointMotionList->mBasePriority < LLJoint::USE_MOTION_PRIORITY)
  1229. {
  1230. llwarns << "Bad animation base priority "
  1231. << mJointMotionList->mBasePriority << " for motion "
  1232. << asset_id << llendl;
  1233. delete mJointMotionList;
  1234. mJointMotionList = NULL;
  1235. return false;
  1236. }
  1237. // Get duration
  1238. if (!dp.unpackF32(mJointMotionList->mDuration, "duration"))
  1239. {
  1240. llwarns << "Cannot read duration for motion " << asset_id << llendl;
  1241. delete mJointMotionList;
  1242. mJointMotionList = NULL;
  1243. return false;
  1244. }
  1245. if (mJointMotionList->mDuration > ABSOLUTE_MAX_ANIM_DURATION ||
  1246. !llfinite(mJointMotionList->mDuration))
  1247. {
  1248. llwarns << "Invalid animation duration for motion " << asset_id
  1249. << llendl;
  1250. delete mJointMotionList;
  1251. mJointMotionList = NULL;
  1252. return false;
  1253. }
  1254. // Get emote (optional)
  1255. if (!dp.unpackString(mJointMotionList->mEmoteName, "emote_name"))
  1256. {
  1257. llwarns << "Cannot read optional emote animation name for motion "
  1258. << asset_id << llendl;
  1259. delete mJointMotionList;
  1260. mJointMotionList = NULL;
  1261. return false;
  1262. }
  1263. if (!mJointMotionList->mEmoteName.empty())
  1264. {
  1265. if (mJointMotionList->mEmoteName == mID.asString())
  1266. {
  1267. llwarns << "Malformed animation mEmoteName==mID for motion "
  1268. << asset_id << llendl;
  1269. delete mJointMotionList;
  1270. mJointMotionList = NULL;
  1271. return false;
  1272. }
  1273. // "Closed_Mouth" is a very popular emote name we should ignore
  1274. if (mJointMotionList->mEmoteName == "Closed_Mouth")
  1275. {
  1276. mJointMotionList->mEmoteName.clear();
  1277. }
  1278. else
  1279. {
  1280. mJointMotionList->mEmoteID =
  1281. gAnimLibrary.stringToAnimState(mJointMotionList->mEmoteName);
  1282. if (mJointMotionList->mEmoteID.isNull())
  1283. {
  1284. llwarns << "Unknown emote name for motion " << asset_id
  1285. << llendl;
  1286. mJointMotionList->mEmoteName.clear();
  1287. }
  1288. }
  1289. }
  1290. // Get loop
  1291. if (!dp.unpackF32(mJointMotionList->mLoopInPoint, "loop_in_point") ||
  1292. !llfinite(mJointMotionList->mLoopInPoint))
  1293. {
  1294. llwarns << "Cannot read loop point for motion " << asset_id << llendl;
  1295. delete mJointMotionList;
  1296. mJointMotionList = NULL;
  1297. return false;
  1298. }
  1299. if (!dp.unpackF32(mJointMotionList->mLoopOutPoint, "loop_out_point") ||
  1300. !llfinite(mJointMotionList->mLoopOutPoint))
  1301. {
  1302. llwarns << "Cannot read loop point for motion " << asset_id << llendl;
  1303. delete mJointMotionList;
  1304. mJointMotionList = NULL;
  1305. return false;
  1306. }
  1307. S32 temp;
  1308. if (!dp.unpackS32(temp, "loop"))
  1309. {
  1310. llwarns << "Cannot read loop flag for motion " << asset_id << llendl;
  1311. delete mJointMotionList;
  1312. mJointMotionList = NULL;
  1313. return false;
  1314. }
  1315. mJointMotionList->mLoop = (bool)temp;
  1316. // *HACK: to alter Female_land loop setting, while current behavior won't
  1317. // be changed server-side in SL.
  1318. if (mJointMotionList->mLoop && asset_id == ANIM_AGENT_FEMALE_LAND)
  1319. {
  1320. LL_DEBUGS("KeyFrameMotion") << "Female landing animation looping disabled."
  1321. << LL_ENDL;
  1322. mJointMotionList->mLoop = false;
  1323. }
  1324. // Get easeIn and easeOut
  1325. if (!dp.unpackF32(mJointMotionList->mEaseInDuration, "ease_in_duration") ||
  1326. !llfinite(mJointMotionList->mEaseInDuration))
  1327. {
  1328. llwarns << "Cannot read ease-in duration for motion " << asset_id
  1329. << llendl;
  1330. delete mJointMotionList;
  1331. mJointMotionList = NULL;
  1332. return false;
  1333. }
  1334. if (!dp.unpackF32(mJointMotionList->mEaseOutDuration,
  1335. "ease_out_duration") ||
  1336. !llfinite(mJointMotionList->mEaseOutDuration))
  1337. {
  1338. llwarns << "Cannot read ease-out duration for motion " << asset_id
  1339. << llendl;
  1340. delete mJointMotionList;
  1341. mJointMotionList = NULL;
  1342. return false;
  1343. }
  1344. // Get hand pose
  1345. U32 word;
  1346. if (!dp.unpackU32(word, "hand_pose"))
  1347. {
  1348. llwarns << "Cannot read hand pose for motion " << asset_id << llendl;
  1349. delete mJointMotionList;
  1350. mJointMotionList = NULL;
  1351. return false;
  1352. }
  1353. if (word > LLHandMotion::NUM_HAND_POSES)
  1354. {
  1355. llwarns << "Invalid LLHandMotion::eHandPose index: " << word
  1356. << " for motion " << asset_id << llendl;
  1357. delete mJointMotionList;
  1358. mJointMotionList = NULL;
  1359. return false;
  1360. }
  1361. mJointMotionList->mHandPose = (LLHandMotion::eHandPose)word;
  1362. // Get number of joint motions
  1363. U32 num_motions = 0;
  1364. U32 rotation_duplicates = 0;
  1365. U32 position_duplicates = 0;
  1366. if (!dp.unpackU32(num_motions, "num_joints"))
  1367. {
  1368. llwarns << "Cannot read number of joints for motion " << asset_id
  1369. << llendl;
  1370. delete mJointMotionList;
  1371. mJointMotionList = NULL;
  1372. return false;
  1373. }
  1374. if (num_motions == 0)
  1375. {
  1376. llwarns << "No joint in animation for motion " << asset_id
  1377. << llendl;
  1378. delete mJointMotionList;
  1379. mJointMotionList = NULL;
  1380. return false;
  1381. }
  1382. else if (num_motions > LL_CHARACTER_MAX_ANIMATED_JOINTS)
  1383. {
  1384. llwarns << "Too many joints in animation for motion " << asset_id
  1385. << llendl;
  1386. delete mJointMotionList;
  1387. mJointMotionList = NULL;
  1388. return false;
  1389. }
  1390. mJointMotionList->mJointMotionArray.clear();
  1391. mJointMotionList->mJointMotionArray.reserve(num_motions);
  1392. mJointStates.clear();
  1393. mJointStates.reserve(num_motions);
  1394. // Initialize joint motions
  1395. for (U32 i = 0; i < num_motions; ++i)
  1396. {
  1397. JointMotion* joint_motion = new JointMotion;
  1398. mJointMotionList->mJointMotionArray.push_back(joint_motion);
  1399. std::string joint_name;
  1400. if (!dp.unpackString(joint_name, "joint_name"))
  1401. {
  1402. llwarns << "Cannot read joint name for motion " << asset_id
  1403. << llendl;
  1404. delete mJointMotionList;
  1405. mJointMotionList = NULL;
  1406. return false;
  1407. }
  1408. if (joint_name == "mScreen" || joint_name == "mRoot")
  1409. {
  1410. llwarns << "Attempted to animate special '" << joint_name
  1411. << "' joint for motion " << asset_id << llendl;
  1412. delete mJointMotionList;
  1413. mJointMotionList = NULL;
  1414. return false;
  1415. }
  1416. // Find the corresponding joint
  1417. U32 joint_key = LLJoint::getAliasedJointKey(joint_name);
  1418. LLJoint* joint = mCharacter->getJoint(joint_key);
  1419. if (joint)
  1420. {
  1421. // Canonical name in case this is an alias.
  1422. joint_name = joint->getName();
  1423. S32 joint_num = joint->getJointNum();
  1424. if (joint_num < 0 ||
  1425. joint_num >= (S32)LL_CHARACTER_MAX_ANIMATED_JOINTS)
  1426. {
  1427. llwarns << "Joint number " << joint_num
  1428. << " is outside of legal range [0-"
  1429. << LL_CHARACTER_MAX_ANIMATED_JOINTS
  1430. << "] and will be omitted from animation for joint: "
  1431. << joint->getName() << " - Motion " << asset_id
  1432. << llendl;
  1433. joint = NULL;
  1434. }
  1435. }
  1436. else
  1437. {
  1438. llwarns << "Joint not found: " << joint_name << llendl;
  1439. if (!allow_invalid_joints)
  1440. {
  1441. delete mJointMotionList;
  1442. mJointMotionList = NULL;
  1443. return false;
  1444. }
  1445. }
  1446. joint_motion->mJointName = joint_name;
  1447. joint_motion->mJointKey = joint_key;
  1448. LLPointer<LLJointState> joint_state = new LLJointState;
  1449. mJointStates.push_back(joint_state);
  1450. joint_state->setJoint(joint); // note: can accept NULL
  1451. joint_state->setUsage(0);
  1452. // Get joint priority
  1453. S32 joint_priority;
  1454. if (!dp.unpackS32(joint_priority, "joint_priority"))
  1455. {
  1456. llwarns << "Cannot read joint priority for motion " << asset_id
  1457. << llendl;
  1458. delete mJointMotionList;
  1459. mJointMotionList = NULL;
  1460. return false;
  1461. }
  1462. if (joint_priority < LLJoint::USE_MOTION_PRIORITY)
  1463. {
  1464. llwarns << "joint priority unknown - too low for motion "
  1465. << asset_id << llendl;
  1466. delete mJointMotionList;
  1467. mJointMotionList = NULL;
  1468. return false;
  1469. }
  1470. joint_motion->mPriority = (LLJoint::JointPriority)joint_priority;
  1471. if (joint_priority != LLJoint::USE_MOTION_PRIORITY &&
  1472. joint_priority > mJointMotionList->mMaxPriority)
  1473. {
  1474. mJointMotionList->mMaxPriority =
  1475. (LLJoint::JointPriority)joint_priority;
  1476. }
  1477. joint_state->setPriority((LLJoint::JointPriority)joint_priority);
  1478. // Scan rotation curve header
  1479. if (!dp.unpackS32(joint_motion->mRotationCurve.mNumKeys,
  1480. "num_rot_keys") ||
  1481. joint_motion->mRotationCurve.mNumKeys < 0)
  1482. {
  1483. llwarns << "Cannot read number of rotation keys for motion "
  1484. << asset_id << llendl;
  1485. delete mJointMotionList;
  1486. mJointMotionList = NULL;
  1487. return false;
  1488. }
  1489. joint_motion->mRotationCurve.mInterpolationType = IT_LINEAR;
  1490. if (joint_motion->mRotationCurve.mNumKeys != 0)
  1491. {
  1492. joint_state->setUsage(joint_state->getUsage() | LLJointState::ROT);
  1493. }
  1494. // Scan rotation curve keys
  1495. RotationCurve *rCurve = &joint_motion->mRotationCurve;
  1496. for (S32 k = 0; k < joint_motion->mRotationCurve.mNumKeys; ++k)
  1497. {
  1498. F32 time;
  1499. if (old_version)
  1500. {
  1501. if (!dp.unpackF32(time, "time") || !llfinite(time))
  1502. {
  1503. llwarns << "Cannot read rotation key (" << k
  1504. << ") for motion " << asset_id << llendl;
  1505. delete mJointMotionList;
  1506. mJointMotionList = NULL;
  1507. return false;
  1508. }
  1509. }
  1510. else
  1511. {
  1512. U16 time_short;
  1513. if (!dp.unpackU16(time_short, "time"))
  1514. {
  1515. llwarns << "Cannot read rotation key (" << k
  1516. << ") for motion " << asset_id << llendl;
  1517. delete mJointMotionList;
  1518. mJointMotionList = NULL;
  1519. return false;
  1520. }
  1521. time = U16_to_F32(time_short, 0.f,
  1522. mJointMotionList->mDuration);
  1523. if (time < 0 || time > mJointMotionList->mDuration)
  1524. {
  1525. llwarns << "Invalid frame time for motion " << asset_id
  1526. << llendl;
  1527. delete mJointMotionList;
  1528. mJointMotionList = NULL;
  1529. return false;
  1530. }
  1531. }
  1532. RotationKey rot_key;
  1533. rot_key.mTime = time;
  1534. LLVector3 rot_angles;
  1535. bool success = true;
  1536. if (old_version)
  1537. {
  1538. success = dp.unpackVector3(rot_angles, "rot_angles") &&
  1539. rot_angles.isFinite();
  1540. LLQuaternion::Order ro = StringToOrder("ZYX");
  1541. rot_key.mRotation = mayaQ(rot_angles.mV[VX], rot_angles.mV[VY],
  1542. rot_angles.mV[VZ], ro);
  1543. }
  1544. else
  1545. {
  1546. U16 x, y, z;
  1547. success &= dp.unpackU16(x, "rot_angle_x");
  1548. success &= dp.unpackU16(y, "rot_angle_y");
  1549. success &= dp.unpackU16(z, "rot_angle_z");
  1550. LLVector3 rot_vec(U16_to_F32(x, -1.f, 1.f),
  1551. U16_to_F32(y, -1.f, 1.f),
  1552. U16_to_F32(z, -1.f, 1.f));
  1553. rot_key.mRotation.unpackFromVector3(rot_vec);
  1554. }
  1555. if (!rot_key.mRotation.isFinite())
  1556. {
  1557. llwarns << "Non-finite angle in rotation key for motion "
  1558. << asset_id << llendl;
  1559. success = false;
  1560. }
  1561. if (!success)
  1562. {
  1563. llwarns << "Cannot read rotation key (" << k << ") for motion "
  1564. << asset_id << llendl;
  1565. delete mJointMotionList;
  1566. mJointMotionList = NULL;
  1567. return false;
  1568. }
  1569. rCurve->mKeys[time] = rot_key;
  1570. }
  1571. if (joint_motion->mRotationCurve.mNumKeys >
  1572. (S32)joint_motion->mRotationCurve.mKeys.size())
  1573. {
  1574. ++rotation_duplicates;
  1575. }
  1576. // Scan position curve header
  1577. if (!dp.unpackS32(joint_motion->mPositionCurve.mNumKeys,
  1578. "num_pos_keys") ||
  1579. joint_motion->mPositionCurve.mNumKeys < 0)
  1580. {
  1581. llwarns << "Cannot read number of position keys for motion "
  1582. << asset_id << llendl;
  1583. delete mJointMotionList;
  1584. mJointMotionList = NULL;
  1585. return false;
  1586. }
  1587. joint_motion->mPositionCurve.mInterpolationType = IT_LINEAR;
  1588. if (joint_motion->mPositionCurve.mNumKeys != 0)
  1589. {
  1590. joint_state->setUsage(joint_state->getUsage() | LLJointState::POS);
  1591. }
  1592. // Scan position curve keys
  1593. PositionCurve* curvep = &joint_motion->mPositionCurve;
  1594. bool is_pelvis = joint_motion->mJointKey == LL_JOINT_KEY_PELVIS;
  1595. for (S32 k = 0; k < joint_motion->mPositionCurve.mNumKeys; ++k)
  1596. {
  1597. U16 time_short;
  1598. PositionKey pos_key;
  1599. if (old_version)
  1600. {
  1601. if (!dp.unpackF32(pos_key.mTime, "time") ||
  1602. !llfinite(pos_key.mTime))
  1603. {
  1604. llwarns << "Cannot read position key (" << k
  1605. << ") for motion " << asset_id << llendl;
  1606. delete mJointMotionList;
  1607. mJointMotionList = NULL;
  1608. return false;
  1609. }
  1610. }
  1611. else
  1612. {
  1613. if (!dp.unpackU16(time_short, "time"))
  1614. {
  1615. llwarns << "Cannot read position key (" << k
  1616. << ") for motion " << asset_id << llendl;
  1617. delete mJointMotionList;
  1618. mJointMotionList = NULL;
  1619. return false;
  1620. }
  1621. pos_key.mTime = U16_to_F32(time_short, 0.f,
  1622. mJointMotionList->mDuration);
  1623. }
  1624. bool success = true;
  1625. if (old_version)
  1626. {
  1627. success = dp.unpackVector3(pos_key.mPosition, "pos");
  1628. pos_key.mPosition.mV[VX] = llclamp(pos_key.mPosition.mV[VX],
  1629. -LL_MAX_PELVIS_OFFSET,
  1630. LL_MAX_PELVIS_OFFSET);
  1631. pos_key.mPosition.mV[VY] = llclamp(pos_key.mPosition.mV[VY],
  1632. -LL_MAX_PELVIS_OFFSET,
  1633. LL_MAX_PELVIS_OFFSET);
  1634. pos_key.mPosition.mV[VZ] = llclamp(pos_key.mPosition.mV[VZ],
  1635. -LL_MAX_PELVIS_OFFSET,
  1636. LL_MAX_PELVIS_OFFSET);
  1637. }
  1638. else
  1639. {
  1640. U16 x, y, z;
  1641. success &= dp.unpackU16(x, "pos_x");
  1642. success &= dp.unpackU16(y, "pos_y");
  1643. success &= dp.unpackU16(z, "pos_z");
  1644. pos_key.mPosition.mV[VX] = U16_to_F32(x, -LL_MAX_PELVIS_OFFSET,
  1645. LL_MAX_PELVIS_OFFSET);
  1646. pos_key.mPosition.mV[VY] = U16_to_F32(y, -LL_MAX_PELVIS_OFFSET,
  1647. LL_MAX_PELVIS_OFFSET);
  1648. pos_key.mPosition.mV[VZ] = U16_to_F32(z, -LL_MAX_PELVIS_OFFSET,
  1649. LL_MAX_PELVIS_OFFSET);
  1650. }
  1651. if (!(pos_key.mPosition.isFinite()))
  1652. {
  1653. llwarns << "Non-finite position in key" << llendl;
  1654. success = false;
  1655. }
  1656. if (!success)
  1657. {
  1658. llwarns << "Cannot read position key (" << k
  1659. << ") for motion " << asset_id << llendl;
  1660. delete mJointMotionList;
  1661. mJointMotionList = NULL;
  1662. return false;
  1663. }
  1664. curvep->mKeys[pos_key.mTime] = pos_key;
  1665. if (is_pelvis)
  1666. {
  1667. mJointMotionList->mPelvisBBox.addPoint(pos_key.mPosition);
  1668. }
  1669. }
  1670. if (joint_motion->mPositionCurve.mNumKeys >
  1671. (S32)joint_motion->mPositionCurve.mKeys.size())
  1672. {
  1673. ++position_duplicates;
  1674. }
  1675. joint_motion->mUsage = joint_state->getUsage();
  1676. }
  1677. if (rotation_duplicates)
  1678. {
  1679. llinfos << "Motion " << asset_id
  1680. << " had duplicated rotation keys that were removed" << llendl;
  1681. }
  1682. if (position_duplicates)
  1683. {
  1684. llinfos << "Motion " << asset_id
  1685. << " had duplicated position keys that were removed" << llendl;
  1686. }
  1687. // Get number of constraints
  1688. S32 num_constraints = 0;
  1689. if (!dp.unpackS32(num_constraints, "num_constraints"))
  1690. {
  1691. llwarns << "Cannot read the number of constraints for motion "
  1692. << asset_id << llendl;
  1693. delete mJointMotionList;
  1694. mJointMotionList = NULL;
  1695. return false;
  1696. }
  1697. if (num_constraints > MAX_CONSTRAINTS || num_constraints < 0)
  1698. {
  1699. llwarns << "Bad number of constraints: " << num_constraints
  1700. << " - Motion " << asset_id << llendl;
  1701. delete mJointMotionList;
  1702. mJointMotionList = NULL;
  1703. return false;
  1704. }
  1705. // Get constraints
  1706. std::string str;
  1707. for (S32 i = 0; i < num_constraints; ++i)
  1708. {
  1709. // Read in constraint data
  1710. JointConstraintSharedData* constraintp = new JointConstraintSharedData;
  1711. U8 byte = 0;
  1712. if (!dp.unpackU8(byte, "chain_length"))
  1713. {
  1714. llwarns << "Cannot read constraint chain length for motion "
  1715. << asset_id << llendl;
  1716. delete constraintp;
  1717. delete mJointMotionList;
  1718. mJointMotionList = NULL;
  1719. return false;
  1720. }
  1721. constraintp->mChainLength = (S32)byte;
  1722. if ((U32)constraintp->mChainLength >
  1723. mJointMotionList->getNumJointMotions())
  1724. {
  1725. llwarns << "Invalid constraint chain length for motion "
  1726. << asset_id << llendl;
  1727. delete constraintp;
  1728. delete mJointMotionList;
  1729. mJointMotionList = NULL;
  1730. return false;
  1731. }
  1732. if (!dp.unpackU8(byte, "constraint_type"))
  1733. {
  1734. llwarns << "Cannot read constraint type for motion " << asset_id
  1735. << llendl;
  1736. delete constraintp;
  1737. delete mJointMotionList;
  1738. mJointMotionList = NULL;
  1739. return false;
  1740. }
  1741. if (byte >= NUM_CONSTRAINT_TYPES)
  1742. {
  1743. llwarns << "Invalid constraint type for motion " << asset_id
  1744. << llendl;
  1745. delete constraintp;
  1746. delete mJointMotionList;
  1747. mJointMotionList = NULL;
  1748. return false;
  1749. }
  1750. constraintp->mConstraintType = (EConstraintType)byte;
  1751. constexpr S32 BIN_DATA_LENGTH = 16;
  1752. U8 bin_data[BIN_DATA_LENGTH + 1];
  1753. if (!dp.unpackBinaryDataFixed(bin_data, BIN_DATA_LENGTH,
  1754. "source_volume"))
  1755. {
  1756. llwarns << "Cannot read source volume name for motion " << asset_id
  1757. << llendl;
  1758. delete constraintp;
  1759. delete mJointMotionList;
  1760. mJointMotionList = NULL;
  1761. return false;
  1762. }
  1763. bin_data[BIN_DATA_LENGTH] = 0; // Ensure null termination
  1764. str = (char*)bin_data;
  1765. constraintp->mSourceConstraintVolId =
  1766. mCharacter->getCollisionVolumeID(str);
  1767. if (constraintp->mSourceConstraintVolId == -1)
  1768. {
  1769. llwarns << "Not a valid source constraint volume: " << str
  1770. << " - Motion " << asset_id << llendl;
  1771. delete constraintp;
  1772. delete mJointMotionList;
  1773. mJointMotionList = NULL;
  1774. return false;
  1775. }
  1776. if (!dp.unpackVector3(constraintp->mSourceConstraintOffset,
  1777. "source_offset"))
  1778. {
  1779. llwarns << "Cannot read constraint source offset for motion "
  1780. << asset_id << llendl;
  1781. delete constraintp;
  1782. delete mJointMotionList;
  1783. mJointMotionList = NULL;
  1784. return false;
  1785. }
  1786. if (!(constraintp->mSourceConstraintOffset.isFinite()))
  1787. {
  1788. llwarns << "Non-finite constraint source offset for motion "
  1789. << asset_id << llendl;
  1790. delete constraintp;
  1791. delete mJointMotionList;
  1792. mJointMotionList = NULL;
  1793. return false;
  1794. }
  1795. if (!dp.unpackBinaryDataFixed(bin_data, BIN_DATA_LENGTH,
  1796. "target_volume"))
  1797. {
  1798. llwarns << "Cannot read target volume name for motion "
  1799. << asset_id << llendl;
  1800. delete constraintp;
  1801. delete mJointMotionList;
  1802. mJointMotionList = NULL;
  1803. return false;
  1804. }
  1805. bin_data[BIN_DATA_LENGTH] = 0; // Ensure null termination
  1806. str = (char*)bin_data;
  1807. if (str == "GROUND")
  1808. {
  1809. // Constrain to ground
  1810. constraintp->mConstraintTargetType = CONSTRAINT_TARGET_TYPE_GROUND;
  1811. }
  1812. else
  1813. {
  1814. constraintp->mConstraintTargetType = CONSTRAINT_TARGET_TYPE_BODY;
  1815. constraintp->mTargetConstraintVolId =
  1816. mCharacter->getCollisionVolumeID(str);
  1817. if (constraintp->mSourceConstraintVolId == -1)
  1818. {
  1819. llwarns << "Not a valid target constraint volume: " << str
  1820. << " - Motion " << asset_id << llendl;
  1821. delete constraintp;
  1822. delete mJointMotionList;
  1823. mJointMotionList = NULL;
  1824. return false;
  1825. }
  1826. }
  1827. if (!dp.unpackVector3(constraintp->mTargetConstraintOffset,
  1828. "target_offset"))
  1829. {
  1830. llwarns << "Cannot read constraint target offset for motion "
  1831. << asset_id << llendl;
  1832. delete constraintp;
  1833. delete mJointMotionList;
  1834. mJointMotionList = NULL;
  1835. return false;
  1836. }
  1837. if (!(constraintp->mTargetConstraintOffset.isFinite()))
  1838. {
  1839. llwarns << "Non-finite constraint target offset for motion "
  1840. << asset_id << llendl;
  1841. delete constraintp;
  1842. delete mJointMotionList;
  1843. mJointMotionList = NULL;
  1844. return false;
  1845. }
  1846. if (!dp.unpackVector3(constraintp->mTargetConstraintDir, "target_dir"))
  1847. {
  1848. llwarns << "Cannot read constraint target direction" << llendl;
  1849. delete constraintp;
  1850. delete mJointMotionList;
  1851. mJointMotionList = NULL;
  1852. return false;
  1853. }
  1854. if (!(constraintp->mTargetConstraintDir.isFinite()))
  1855. {
  1856. llwarns << "Non-finite constraint target direction for motion "
  1857. << asset_id << llendl;
  1858. delete constraintp;
  1859. delete mJointMotionList;
  1860. mJointMotionList = NULL;
  1861. return false;
  1862. }
  1863. if (!constraintp->mTargetConstraintDir.isExactlyZero())
  1864. {
  1865. constraintp->mUseTargetOffset = true;
  1866. #if 0
  1867. constraintp->mTargetConstraintDir *=
  1868. constraintp->mSourceConstraintOffset.length();
  1869. #endif
  1870. }
  1871. if (!dp.unpackF32(constraintp->mEaseInStartTime, "ease_in_start") ||
  1872. !llfinite(constraintp->mEaseInStartTime))
  1873. {
  1874. llwarns << "Cannot read constraint ease in start time for motion "
  1875. << asset_id << llendl;
  1876. delete constraintp;
  1877. delete mJointMotionList;
  1878. mJointMotionList = NULL;
  1879. return false;
  1880. }
  1881. if (!dp.unpackF32(constraintp->mEaseInStopTime, "ease_in_stop") ||
  1882. !llfinite(constraintp->mEaseInStopTime))
  1883. {
  1884. llwarns << "Cannot read constraint ease in stop time for motion "
  1885. << asset_id << llendl;
  1886. delete constraintp;
  1887. delete mJointMotionList;
  1888. mJointMotionList = NULL;
  1889. return false;
  1890. }
  1891. if (!dp.unpackF32(constraintp->mEaseOutStartTime, "ease_out_start") ||
  1892. !llfinite(constraintp->mEaseOutStartTime))
  1893. {
  1894. llwarns << "Cannot read constraint ease out start time for motion "
  1895. << asset_id << llendl;
  1896. delete constraintp;
  1897. delete mJointMotionList;
  1898. mJointMotionList = NULL;
  1899. return false;
  1900. }
  1901. if (!dp.unpackF32(constraintp->mEaseOutStopTime, "ease_out_stop") ||
  1902. !llfinite(constraintp->mEaseOutStopTime))
  1903. {
  1904. llwarns << "Cannot read constraint ease out stop time for motion "
  1905. << asset_id << llendl;
  1906. delete constraintp;
  1907. delete mJointMotionList;
  1908. mJointMotionList = NULL;
  1909. return false;
  1910. }
  1911. mJointMotionList->mConstraints.push_front(constraintp);
  1912. // Note: mChainLength is size-limited (as it comes from a byte)
  1913. constraintp->mJointStateIndices =
  1914. new S32[constraintp->mChainLength + 1];
  1915. // Get joint to which this collision volume is attached
  1916. LLJoint* joint =
  1917. mCharacter->findCollisionVolume(constraintp->mSourceConstraintVolId);
  1918. if (!joint)
  1919. {
  1920. delete constraintp;
  1921. delete mJointMotionList;
  1922. mJointMotionList = NULL;
  1923. return false;
  1924. }
  1925. for (S32 i = 0, count = constraintp->mChainLength + 1; i < count; ++i)
  1926. {
  1927. LLJoint* parent = joint->getParent();
  1928. if (!parent)
  1929. {
  1930. llwarns << "Joint with no parent: " << joint->getName()
  1931. << " Emote: " << mJointMotionList->mEmoteName
  1932. << llendl;
  1933. delete constraintp;
  1934. delete mJointMotionList;
  1935. mJointMotionList = NULL;
  1936. return false;
  1937. }
  1938. joint = parent;
  1939. constraintp->mJointStateIndices[i] = -1;
  1940. for (U32 j = 0, count2 = mJointMotionList->getNumJointMotions();
  1941. j < count2; ++j)
  1942. {
  1943. LLJoint* constraint_joint = getJoint(j);
  1944. if (!constraint_joint)
  1945. {
  1946. llwarns << "Invalid joint " << j << llendl;
  1947. delete constraintp;
  1948. delete mJointMotionList;
  1949. mJointMotionList = NULL;
  1950. return false;
  1951. }
  1952. if (constraint_joint == joint)
  1953. {
  1954. constraintp->mJointStateIndices[i] = (S32)j;
  1955. break;
  1956. }
  1957. }
  1958. if (constraintp->mJointStateIndices[i] < 0)
  1959. {
  1960. llwarns << "No joint index for constraint " << i
  1961. << " for motion " << asset_id << llendl;
  1962. delete constraintp;
  1963. delete mJointMotionList;
  1964. mJointMotionList = NULL;
  1965. return false;
  1966. }
  1967. }
  1968. }
  1969. // *FIX: support cleanup of old keyframe data
  1970. LLKeyframeDataCache::addKeyframeData(getID(), mJointMotionList);
  1971. mAssetStatus = ASSET_LOADED;
  1972. setupPose();
  1973. return true;
  1974. }
  1975. bool LLKeyframeMotion::serialize(LLDataPacker& dp) const
  1976. {
  1977. if (!mJointMotionList || !mCharacter)
  1978. {
  1979. llwarns << "Trying to set serialize a motion without a motion list"
  1980. << llendl;
  1981. llassert(false);
  1982. return false;
  1983. }
  1984. bool success = true;
  1985. success &= dp.packU16(KEYFRAME_MOTION_VERSION, "version");
  1986. success &= dp.packU16(KEYFRAME_MOTION_SUBVERSION, "sub_version");
  1987. success &= dp.packS32(mJointMotionList->mBasePriority, "base_priority");
  1988. success &= dp.packF32(mJointMotionList->mDuration, "duration");
  1989. success &= dp.packString(mJointMotionList->mEmoteName, "emote_name");
  1990. success &= dp.packF32(mJointMotionList->mLoopInPoint, "loop_in_point");
  1991. success &= dp.packF32(mJointMotionList->mLoopOutPoint, "loop_out_point");
  1992. success &= dp.packS32(mJointMotionList->mLoop, "loop");
  1993. success &= dp.packF32(mJointMotionList->mEaseInDuration,
  1994. "ease_in_duration");
  1995. success &= dp.packF32(mJointMotionList->mEaseOutDuration,
  1996. "ease_out_duration");
  1997. success &= dp.packU32(mJointMotionList->mHandPose, "hand_pose");
  1998. success &= dp.packU32(mJointMotionList->getNumJointMotions(),
  1999. "num_joints");
  2000. LL_DEBUGS("KeyFrameMotion") << "Serialized: version: "
  2001. << KEYFRAME_MOTION_VERSION
  2002. << " - sub_version: "
  2003. << KEYFRAME_MOTION_SUBVERSION
  2004. << " - base_priority: "
  2005. << mJointMotionList->mBasePriority
  2006. << " - duration: "
  2007. << mJointMotionList->mDuration
  2008. << " - emote_name: "
  2009. << mJointMotionList->mEmoteName
  2010. << " - loop_in_point: "
  2011. << mJointMotionList->mLoopInPoint
  2012. << " - loop_out_point: "
  2013. << mJointMotionList->mLoopOutPoint
  2014. << " - loop: " << mJointMotionList->mLoop
  2015. << " - ease_in_duration: "
  2016. << mJointMotionList->mEaseInDuration
  2017. << " - ease_out_duration: "
  2018. << mJointMotionList->mEaseOutDuration
  2019. << " - hand_pose: "
  2020. << mJointMotionList->mHandPose
  2021. << " - num_joints: "
  2022. << mJointMotionList->getNumJointMotions()
  2023. << LL_ENDL;
  2024. for (U32 i = 0; i < mJointMotionList->getNumJointMotions(); ++i)
  2025. {
  2026. JointMotion* joint_motionp = mJointMotionList->getJointMotion(i);
  2027. if (!joint_motionp)
  2028. {
  2029. llwarns << "NULL joint motion found !" << llendl;
  2030. continue;
  2031. }
  2032. success &= dp.packString(joint_motionp->mJointName, "joint_name");
  2033. success &= dp.packS32(joint_motionp->mPriority, "joint_priority");
  2034. success &= dp.packS32(joint_motionp->mRotationCurve.mNumKeys,
  2035. "num_rot_keys");
  2036. LL_DEBUGS("KeyFrameMotion") << "Joint: " << joint_motionp->mJointName
  2037. << LL_ENDL;
  2038. for (RotationCurve::key_map_t::iterator
  2039. iter = joint_motionp->mRotationCurve.mKeys.begin(),
  2040. end = joint_motionp->mRotationCurve.mKeys.end();
  2041. iter != end; ++iter)
  2042. {
  2043. RotationKey& rot_key = iter->second;
  2044. U16 time_short = F32_to_U16(rot_key.mTime, 0.f,
  2045. mJointMotionList->mDuration);
  2046. success &= dp.packU16(time_short, "time");
  2047. LLVector3 rot_angles = rot_key.mRotation.packToVector3();
  2048. rot_angles.quantize16(-1.f, 1.f, -1.f, 1.f);
  2049. U16 x = F32_to_U16(rot_angles.mV[VX], -1.f, 1.f);
  2050. U16 y = F32_to_U16(rot_angles.mV[VY], -1.f, 1.f);
  2051. U16 z = F32_to_U16(rot_angles.mV[VZ], -1.f, 1.f);
  2052. success &= dp.packU16(x, "rot_angle_x");
  2053. success &= dp.packU16(y, "rot_angle_y");
  2054. success &= dp.packU16(z, "rot_angle_z");
  2055. LL_DEBUGS("KeyFrameMotion") << " Rot: t=" << rot_key.mTime
  2056. << " - rotation=" << rot_angles.mV[VX]
  2057. << "," << rot_angles.mV[VY] << ","
  2058. << rot_angles.mV[VZ] << LL_ENDL;
  2059. }
  2060. success &= dp.packS32(joint_motionp->mPositionCurve.mNumKeys,
  2061. "num_pos_keys");
  2062. for (PositionCurve::key_map_t::iterator
  2063. iter = joint_motionp->mPositionCurve.mKeys.begin(),
  2064. end = joint_motionp->mPositionCurve.mKeys.end();
  2065. iter != end; ++iter)
  2066. {
  2067. PositionKey& pos_key = iter->second;
  2068. U16 time_short = F32_to_U16(pos_key.mTime, 0.f,
  2069. mJointMotionList->mDuration);
  2070. success &= dp.packU16(time_short, "time");
  2071. pos_key.mPosition.quantize16(-LL_MAX_PELVIS_OFFSET,
  2072. LL_MAX_PELVIS_OFFSET,
  2073. -LL_MAX_PELVIS_OFFSET,
  2074. LL_MAX_PELVIS_OFFSET);
  2075. U16 x = F32_to_U16(pos_key.mPosition.mV[VX], -LL_MAX_PELVIS_OFFSET,
  2076. LL_MAX_PELVIS_OFFSET);
  2077. U16 y = F32_to_U16(pos_key.mPosition.mV[VY], -LL_MAX_PELVIS_OFFSET,
  2078. LL_MAX_PELVIS_OFFSET);
  2079. U16 z = F32_to_U16(pos_key.mPosition.mV[VZ], -LL_MAX_PELVIS_OFFSET,
  2080. LL_MAX_PELVIS_OFFSET);
  2081. success &= dp.packU16(x, "pos_x");
  2082. success &= dp.packU16(y, "pos_y");
  2083. success &= dp.packU16(z, "pos_z");
  2084. LL_DEBUGS("KeyFrameMotion") << " Pos: t=" << pos_key.mTime
  2085. << " - position="
  2086. << pos_key.mPosition.mV[VX] << ","
  2087. << pos_key.mPosition.mV[VY] << ","
  2088. << pos_key.mPosition.mV[VZ] << LL_ENDL;
  2089. }
  2090. }
  2091. success &= dp.packS32(mJointMotionList->mConstraints.size(),
  2092. "num_constraints");
  2093. LL_DEBUGS("KeyFrameMotion") << "num_constraints: "
  2094. << mJointMotionList->mConstraints.size()
  2095. << LL_ENDL;
  2096. for (JointMotionList::constraint_list_t::const_iterator iter =
  2097. mJointMotionList->mConstraints.begin();
  2098. iter != mJointMotionList->mConstraints.end(); ++iter)
  2099. {
  2100. JointConstraintSharedData* jcsd = *iter;
  2101. success &= dp.packU8(jcsd->mChainLength, "chain_length");
  2102. success &= dp.packU8(jcsd->mConstraintType, "constraint_type");
  2103. char source_volume[16];
  2104. snprintf(source_volume, sizeof(source_volume), "%s",
  2105. mCharacter->findCollisionVolume(jcsd->mSourceConstraintVolId)->getName().c_str());
  2106. success &= dp.packBinaryDataFixed((U8*)source_volume, 16,
  2107. "source_volume");
  2108. success &= dp.packVector3(jcsd->mSourceConstraintOffset,
  2109. "source_offset");
  2110. char target_volume[16];
  2111. if (jcsd->mConstraintTargetType ==
  2112. CONSTRAINT_TARGET_TYPE_GROUND)
  2113. {
  2114. snprintf(target_volume, sizeof(target_volume), "%s", "GROUND");
  2115. }
  2116. else
  2117. {
  2118. snprintf(target_volume, sizeof(target_volume), "%s",
  2119. mCharacter->findCollisionVolume(jcsd->mTargetConstraintVolId)->getName().c_str());
  2120. }
  2121. success &= dp.packBinaryDataFixed((U8*)target_volume, 16,
  2122. "target_volume");
  2123. success &= dp.packVector3(jcsd->mTargetConstraintOffset,
  2124. "target_offset");
  2125. success &= dp.packVector3(jcsd->mTargetConstraintDir, "target_dir");
  2126. success &= dp.packF32(jcsd->mEaseInStartTime, "ease_in_start");
  2127. success &= dp.packF32(jcsd->mEaseInStopTime, "ease_in_stop");
  2128. success &= dp.packF32(jcsd->mEaseOutStartTime, "ease_out_start");
  2129. success &= dp.packF32(jcsd->mEaseOutStopTime, "ease_out_stop");
  2130. LL_DEBUGS("KeyFrameMotion") << " chain_length: " << jcsd->mChainLength
  2131. << " - constraint_type: "
  2132. << (S32)jcsd->mConstraintType
  2133. << " - source_volume: " << source_volume
  2134. << " - source_offset: "
  2135. << jcsd->mSourceConstraintOffset
  2136. << " - target_volume: " << target_volume
  2137. << " - target_offset: "
  2138. << jcsd->mTargetConstraintOffset
  2139. << " - target_dir: "
  2140. << jcsd->mTargetConstraintDir
  2141. << " - ease_in_start: "
  2142. << jcsd->mEaseInStartTime
  2143. << " - ease_in_stop: "
  2144. << jcsd->mEaseInStopTime
  2145. << " - ease_out_start: "
  2146. << jcsd->mEaseOutStartTime
  2147. << " - ease_out_stop: "
  2148. << jcsd->mEaseOutStopTime << LL_ENDL;
  2149. }
  2150. return success;
  2151. }
  2152. U32 LLKeyframeMotion::getFileSize()
  2153. {
  2154. // Serialize into a dummy buffer to calculate required size
  2155. LLDataPackerBinaryBuffer dp;
  2156. serialize(dp);
  2157. return dp.getCurrentSize();
  2158. }
  2159. bool LLKeyframeMotion::dumpToFile(const std::string& name)
  2160. {
  2161. if (!isLoaded())
  2162. {
  2163. llwarns << "Animation not loaded. Cannot write: " << name << llendl;
  2164. return false;
  2165. }
  2166. std::string filename;
  2167. if (!name.empty())
  2168. {
  2169. filename = name;
  2170. }
  2171. else if (!getName().empty())
  2172. {
  2173. filename = getName();
  2174. }
  2175. else
  2176. {
  2177. filename = getID().asString();
  2178. }
  2179. std::string extension = gDirUtil.getExtension(filename);
  2180. if (extension != "anim" && extension != "tmp")
  2181. {
  2182. filename += ".anim";
  2183. }
  2184. if (gDirUtil.getDirName(filename).empty())
  2185. {
  2186. filename = gDirUtil.getFullPath(LL_PATH_CACHE, filename);
  2187. }
  2188. if (LLFile::isfile(filename))
  2189. {
  2190. llwarns << filename << " already exists. Not overwriting; aborted."
  2191. << llendl;
  2192. return false;
  2193. }
  2194. bool success = false;
  2195. LL_DEBUGS("KeyFrameMotion") << "Dumping " << filename << LL_ENDL;
  2196. S32 file_size = getFileSize();
  2197. if (file_size > 0)
  2198. {
  2199. U8* buffer = new U8[file_size];
  2200. LLDataPackerBinaryBuffer dp(buffer, file_size);
  2201. if (serialize(dp))
  2202. {
  2203. LLFile outfile(filename, "w+b");
  2204. if (outfile)
  2205. {
  2206. success = outfile.write(buffer, file_size) == file_size;
  2207. }
  2208. }
  2209. delete[] buffer;
  2210. }
  2211. return success;
  2212. }
  2213. const LLBBoxLocal& LLKeyframeMotion::getPelvisBBox()
  2214. {
  2215. return mJointMotionList->mPelvisBBox;
  2216. }
  2217. void LLKeyframeMotion::setPriority(S32 priority)
  2218. {
  2219. if (mJointMotionList)
  2220. {
  2221. S32 priority_delta = priority - mJointMotionList->mBasePriority;
  2222. mJointMotionList->mBasePriority = (LLJoint::JointPriority)priority;
  2223. mJointMotionList->mMaxPriority = mJointMotionList->mBasePriority;
  2224. for (U32 i = 0, count = mJointMotionList->getNumJointMotions();
  2225. i < count; ++i)
  2226. {
  2227. JointMotion* joint_motion = mJointMotionList->getJointMotion(i);
  2228. if (!joint_motion)
  2229. {
  2230. llwarns << "NULL joint motion found !" << llendl;
  2231. continue;
  2232. }
  2233. joint_motion->mPriority =
  2234. (LLJoint::JointPriority)llclamp((S32)joint_motion->mPriority +
  2235. priority_delta,
  2236. (S32)LLJoint::LOW_PRIORITY,
  2237. (S32)LLJoint::HIGHEST_PRIORITY);
  2238. getJointState(i)->setPriority(joint_motion->mPriority);
  2239. }
  2240. }
  2241. }
  2242. void LLKeyframeMotion::setEmote(const LLUUID& emote_id)
  2243. {
  2244. if (!mJointMotionList)
  2245. {
  2246. return;
  2247. }
  2248. const char* emote_name = gAnimLibrary.animStateToString(emote_id);
  2249. if (emote_name)
  2250. {
  2251. mJointMotionList->mEmoteID = emote_id;
  2252. mJointMotionList->mEmoteName = emote_name;
  2253. }
  2254. else
  2255. {
  2256. mJointMotionList->mEmoteID.setNull();
  2257. mJointMotionList->mEmoteName.clear();
  2258. }
  2259. }
  2260. void LLKeyframeMotion::setEaseIn(F32 ease_in)
  2261. {
  2262. if (mJointMotionList)
  2263. {
  2264. mJointMotionList->mEaseInDuration = llmax(ease_in, 0.f);
  2265. }
  2266. }
  2267. void LLKeyframeMotion::setEaseOut(F32 ease_in)
  2268. {
  2269. if (mJointMotionList)
  2270. {
  2271. mJointMotionList->mEaseOutDuration = llmax(ease_in, 0.f);
  2272. }
  2273. }
  2274. void LLKeyframeMotion::flushKeyframeCache()
  2275. {
  2276. #if 0 // TODO: Make this safe to do
  2277. LLKeyframeDataCache::clear();
  2278. #endif
  2279. }
  2280. void LLKeyframeMotion::setLoop(bool loop)
  2281. {
  2282. if (mJointMotionList)
  2283. {
  2284. mJointMotionList->mLoop = loop;
  2285. mSendStopTimestamp = F32_MAX;
  2286. }
  2287. }
  2288. void LLKeyframeMotion::setLoopIn(F32 in_point)
  2289. {
  2290. if (mJointMotionList)
  2291. {
  2292. mJointMotionList->mLoopInPoint = in_point;
  2293. // Set up loop keys
  2294. for (U32 i = 0, count = mJointMotionList->getNumJointMotions();
  2295. i < count; ++i)
  2296. {
  2297. JointMotion* joint_motion = mJointMotionList->getJointMotion(i);
  2298. if (!joint_motion)
  2299. {
  2300. llwarns << "NULL joint motion found !" << llendl;
  2301. continue;
  2302. }
  2303. PositionCurve* pos_curve = &joint_motion->mPositionCurve;
  2304. RotationCurve* rot_curve = &joint_motion->mRotationCurve;
  2305. ScaleCurve* scale_curve = &joint_motion->mScaleCurve;
  2306. pos_curve->mLoopInKey.mTime = mJointMotionList->mLoopInPoint;
  2307. rot_curve->mLoopInKey.mTime = mJointMotionList->mLoopInPoint;
  2308. scale_curve->mLoopInKey.mTime = mJointMotionList->mLoopInPoint;
  2309. pos_curve->mLoopInKey.mPosition =
  2310. pos_curve->getValue(mJointMotionList->mLoopInPoint,
  2311. mJointMotionList->mDuration);
  2312. rot_curve->mLoopInKey.mRotation =
  2313. rot_curve->getValue(mJointMotionList->mLoopInPoint,
  2314. mJointMotionList->mDuration);
  2315. scale_curve->mLoopInKey.mScale =
  2316. scale_curve->getValue(mJointMotionList->mLoopInPoint,
  2317. mJointMotionList->mDuration);
  2318. }
  2319. }
  2320. }
  2321. void LLKeyframeMotion::setLoopOut(F32 out_point)
  2322. {
  2323. if (mJointMotionList)
  2324. {
  2325. mJointMotionList->mLoopOutPoint = out_point;
  2326. // Set up loop keys
  2327. for (U32 i = 0, count = mJointMotionList->getNumJointMotions();
  2328. i < count; ++i)
  2329. {
  2330. JointMotion* joint_motion = mJointMotionList->getJointMotion(i);
  2331. if (!joint_motion)
  2332. {
  2333. llwarns << "NULL joint motion found !" << llendl;
  2334. continue;
  2335. }
  2336. PositionCurve* pos_curve = &joint_motion->mPositionCurve;
  2337. RotationCurve* rot_curve = &joint_motion->mRotationCurve;
  2338. ScaleCurve* scale_curve = &joint_motion->mScaleCurve;
  2339. pos_curve->mLoopOutKey.mTime = mJointMotionList->mLoopOutPoint;
  2340. rot_curve->mLoopOutKey.mTime = mJointMotionList->mLoopOutPoint;
  2341. scale_curve->mLoopOutKey.mTime = mJointMotionList->mLoopOutPoint;
  2342. pos_curve->mLoopOutKey.mPosition =
  2343. pos_curve->getValue(mJointMotionList->mLoopOutPoint,
  2344. mJointMotionList->mDuration);
  2345. rot_curve->mLoopOutKey.mRotation =
  2346. rot_curve->getValue(mJointMotionList->mLoopOutPoint,
  2347. mJointMotionList->mDuration);
  2348. scale_curve->mLoopOutKey.mScale =
  2349. scale_curve->getValue(mJointMotionList->mLoopOutPoint,
  2350. mJointMotionList->mDuration);
  2351. }
  2352. }
  2353. }
  2354. void LLKeyframeMotion::onLoadComplete(const LLUUID& asset_uuid,
  2355. LLAssetType::EType, void* user_data,
  2356. S32 status, LLExtStat)
  2357. {
  2358. LLUUID* id = (LLUUID*)user_data;
  2359. std::vector<LLCharacter*>::iterator char_iter =
  2360. LLCharacter::sInstances.begin();
  2361. std::vector<LLCharacter*>::iterator char_end =
  2362. LLCharacter::sInstances.end();
  2363. while (char_iter != char_end && (*char_iter)->getID() != *id)
  2364. {
  2365. ++char_iter;
  2366. }
  2367. delete id;
  2368. if (char_iter == char_end)
  2369. {
  2370. return;
  2371. }
  2372. LLCharacter* character = *char_iter;
  2373. // Look for an existing instance of this motion
  2374. LLMotion* motionp = character->findMotion(asset_uuid);
  2375. LLKeyframeMotion* kfmotionp = motionp ? motionp->asKeyframeMotion() : NULL;
  2376. if (kfmotionp)
  2377. {
  2378. if (status == 0)
  2379. {
  2380. if (kfmotionp->mAssetStatus == ASSET_LOADED)
  2381. {
  2382. // Asset already loaded
  2383. return;
  2384. }
  2385. LLFileSystem file(asset_uuid);
  2386. S32 size = file.getSize();
  2387. if (size <= 0)
  2388. {
  2389. llwarns << "Empty file for asset Id: " << asset_uuid << llendl;
  2390. return;
  2391. }
  2392. U8* buffer = new U8[size];
  2393. file.read((U8*)buffer, size);
  2394. LL_DEBUGS("KeyFrameMotion") << "Loading keyframe data for: "
  2395. << kfmotionp->getName() << ":"
  2396. << kfmotionp->getID() << " (" << size
  2397. << " bytes)" << LL_ENDL;
  2398. LLDataPackerBinaryBuffer dp(buffer, size);
  2399. if (kfmotionp->deserialize(dp, asset_uuid))
  2400. {
  2401. kfmotionp->mAssetStatus = ASSET_LOADED;
  2402. }
  2403. else
  2404. {
  2405. llwarns << "Failed to decode asset for animation "
  2406. << kfmotionp->getName() << ":" << kfmotionp->getID()
  2407. << llendl;
  2408. kfmotionp->mAssetStatus = ASSET_FETCH_FAILED;
  2409. }
  2410. delete[] buffer;
  2411. }
  2412. else
  2413. {
  2414. llwarns << "Failed to load asset for animation "
  2415. << kfmotionp->getName() << ":" << kfmotionp->getID()
  2416. << llendl;
  2417. kfmotionp->mAssetStatus = ASSET_FETCH_FAILED;
  2418. }
  2419. }
  2420. else
  2421. {
  2422. llwarns << "No existing motion for asset data, Id: " << asset_uuid
  2423. << llendl;
  2424. }
  2425. }
  2426. //--------------------------------------------------------------------
  2427. // LLKeyframeDataCache class
  2428. //--------------------------------------------------------------------
  2429. //static
  2430. void LLKeyframeDataCache::removeKeyframeData(const LLUUID& id)
  2431. {
  2432. data_map_t::iterator found_data = sKeyframeDataMap.find(id);
  2433. if (found_data != sKeyframeDataMap.end())
  2434. {
  2435. delete found_data->second;
  2436. sKeyframeDataMap.erase(found_data);
  2437. }
  2438. }
  2439. //static
  2440. LLKeyframeMotion::JointMotionList* LLKeyframeDataCache::getKeyframeData(const LLUUID& id)
  2441. {
  2442. data_map_t::iterator it = sKeyframeDataMap.find(id);
  2443. return it != sKeyframeDataMap.end() ? it->second : NULL;
  2444. }
  2445. //static
  2446. void LLKeyframeDataCache::clear()
  2447. {
  2448. llinfos << "Total cached entries: " << sKeyframeDataMap.size() << llendl;
  2449. for (auto it = sKeyframeDataMap.begin(), end = sKeyframeDataMap.end();
  2450. it != end; ++it)
  2451. {
  2452. delete it->second;
  2453. }
  2454. sKeyframeDataMap.clear();
  2455. llinfos << "Cache cleared." << llendl;
  2456. }
  2457. //static
  2458. void LLKeyframeDataCache::dumpDiagInfo()
  2459. {
  2460. llinfos << "-----------------------------------------------------"
  2461. << llendl;
  2462. llinfos << " Global Motion Table" << llendl;
  2463. llinfos << "-----------------------------------------------------"
  2464. << llendl;
  2465. // Keep track of totals
  2466. U32 total_size = 0;
  2467. // Print each loaded motion list, and its memory usage
  2468. for (data_map_t::iterator it = sKeyframeDataMap.begin(),
  2469. end = sKeyframeDataMap.end();
  2470. it != end; ++it)
  2471. {
  2472. LLKeyframeMotion::JointMotionList* motionlistp = it->second;
  2473. llinfos << "Motion: " << it->first << llendl;
  2474. total_size += motionlistp->dumpDiagInfo();
  2475. }
  2476. llinfos << "-----------------------------------------------------"
  2477. << llendl;
  2478. llinfos << "Total: " << (S32)sKeyframeDataMap.size() << "motions - Size: "
  2479. << total_size << "Kb" << llendl;
  2480. llinfos << "-----------------------------------------------------"
  2481. << llendl;
  2482. }
  2483. //-----------------------------------------------------------------------------
  2484. // JointConstraint class
  2485. //-----------------------------------------------------------------------------
  2486. LLKeyframeMotion::JointConstraint::JointConstraint(JointConstraintSharedData* jcsd)
  2487. : mSharedData(jcsd),
  2488. mSourceVolume(NULL),
  2489. mTargetVolume(NULL),
  2490. mWeight(0.f),
  2491. mTotalLength(0.f),
  2492. mFixupDistanceRMS(0.f),
  2493. mActive(false)
  2494. {
  2495. for (S32 i = 0; i < MAX_CHAIN_LENGTH; ++i)
  2496. {
  2497. mJointLengths[i] = 0.f;
  2498. mJointLengthFractions[i] = 0.f;
  2499. }
  2500. }