llpuppetmotion.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. /**
  2. * @file llpuppetmotion.cpp
  3. * @brief Implementation of the LLPuppetMotion class.
  4. *
  5. * $LicenseInfo:firstyear=2021&license=viewergpl$
  6. *
  7. * Copyright (c) 2021-2022, 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 "llviewerprecompiledheaders.h"
  33. #include <array>
  34. #include <set>
  35. #include "llpuppetmotion.h"
  36. #include "llcharacter.h"
  37. #include "llcorehttputil.h"
  38. #include "llmessage.h"
  39. #include "llquantize.h" // For F32_to_U16() and U16_to_F32()
  40. #include "llsdutil_math.h" // For ll_sd_from_vector3()
  41. #include "llagent.h"
  42. #include "llpuppetmodule.h"
  43. #include "llviewercontrol.h"
  44. #include "llviewerregion.h"
  45. #include "llvoavatarself.h"
  46. ///////////////////////////////////////////////////////////////////////////////
  47. // LLPuppetEvent class: resides in llpuppetevent.cpp in LL's viewer sources,
  48. // but is only used here, so... HB
  49. ///////////////////////////////////////////////////////////////////////////////
  50. // *HACK: move this somewhere better.
  51. constexpr U32 PUPPET_MAX_EVENT_BYTES = 200;
  52. U8 PUPPET_WRITE_BUFFER[PUPPET_MAX_EVENT_BYTES];
  53. // Helper functions
  54. static size_t pack_vec3(U8* wptr, LLVector3 vec)
  55. {
  56. // Pack F32 components into 16 bits
  57. vec.quantize16(-LL_MAX_PELVIS_OFFSET, LL_MAX_PELVIS_OFFSET,
  58. -LL_MAX_PELVIS_OFFSET, LL_MAX_PELVIS_OFFSET);
  59. U16 x = F32_to_U16(vec.mV[VX], -LL_MAX_PELVIS_OFFSET,
  60. LL_MAX_PELVIS_OFFSET);
  61. U16 y = F32_to_U16(vec.mV[VY], -LL_MAX_PELVIS_OFFSET,
  62. LL_MAX_PELVIS_OFFSET);
  63. U16 z = F32_to_U16(vec.mV[VZ], -LL_MAX_PELVIS_OFFSET,
  64. LL_MAX_PELVIS_OFFSET);
  65. htonmemcpy(wptr, &x, MVT_U16, sizeof(U16));
  66. htonmemcpy(wptr + sizeof(U16), &y, MVT_U16, sizeof(U16));
  67. htonmemcpy(wptr + 2 * sizeof(U16), &z, MVT_U16, sizeof(U16));
  68. return 3 * sizeof(U16);
  69. }
  70. static size_t pack_quat(U8* wptr, LLQuaternion quat)
  71. {
  72. // A Quaternion is a 4D object but the group isomorphic with rotations is
  73. // limited to the surface of the unit hypersphere (radius = 1).
  74. // Consequently the quaternions we care about have only three degrees of
  75. // freedom and we can store them in three floats. To do this we always make
  76. // sure the real component (W) is positive by negating the Quaternion as
  77. // necessary and then we store only the imaginary part (XYZ). The real
  78. // part can be obtained with the formula: W = sqrt(1.0 - X*X + Y*Y + Z*Z)
  79. if (quat.mQ[VW] < 0.f)
  80. {
  81. // Negate the quaternion to keep its real part positive
  82. quat = -1.f * quat;
  83. }
  84. // Pack F32 components into 16 bits
  85. quat.quantize16(-LL_MAX_PELVIS_OFFSET, LL_MAX_PELVIS_OFFSET);
  86. U16 x = F32_to_U16(quat.mQ[VX], -LL_MAX_PELVIS_OFFSET,
  87. LL_MAX_PELVIS_OFFSET);
  88. U16 y = F32_to_U16(quat.mQ[VY], -LL_MAX_PELVIS_OFFSET,
  89. LL_MAX_PELVIS_OFFSET);
  90. U16 z = F32_to_U16(quat.mQ[VZ], -LL_MAX_PELVIS_OFFSET,
  91. LL_MAX_PELVIS_OFFSET);
  92. // Store the imaginary part
  93. htonmemcpy(wptr, &x, MVT_U16, sizeof(U16));
  94. htonmemcpy(wptr + sizeof(U16), &y, MVT_U16, sizeof(U16));
  95. htonmemcpy(wptr + 2 * sizeof(U16), &z, MVT_U16, sizeof(U16));
  96. return 3 * sizeof(U16);
  97. }
  98. static size_t unpack_vec3(U8* wptr, LLVector3& vec)
  99. {
  100. U16 x, y, z; // F32 data is packed in 16 bits
  101. htonmemcpy(&x, wptr, MVT_U16, sizeof(U16));
  102. htonmemcpy(&y, wptr + sizeof(U16), MVT_U16, sizeof(U16));
  103. htonmemcpy(&z, wptr + 2 * sizeof(U16), MVT_U16, sizeof(U16));
  104. vec.mV[VX] = U16_to_F32(x, -LL_MAX_PELVIS_OFFSET, LL_MAX_PELVIS_OFFSET);
  105. vec.mV[VY] = U16_to_F32(y, -LL_MAX_PELVIS_OFFSET, LL_MAX_PELVIS_OFFSET);
  106. vec.mV[VZ] = U16_to_F32(z, -LL_MAX_PELVIS_OFFSET, LL_MAX_PELVIS_OFFSET);
  107. return 3 * sizeof(U16);
  108. }
  109. static size_t unpack_quat(U8* wptr, LLQuaternion& quat)
  110. {
  111. U16 x, y, z; // F32 data is packed in 16 bits
  112. htonmemcpy(&x, wptr, MVT_U16, sizeof(U16));
  113. htonmemcpy(&y, wptr + sizeof(U16), MVT_U16, sizeof(U16));
  114. htonmemcpy(&z, wptr + 2 * sizeof(U16), MVT_U16, sizeof(U16));
  115. quat.mQ[VX] = U16_to_F32(x, -LL_MAX_PELVIS_OFFSET, LL_MAX_PELVIS_OFFSET);
  116. quat.mQ[VY] = U16_to_F32(y, -LL_MAX_PELVIS_OFFSET, LL_MAX_PELVIS_OFFSET);
  117. quat.mQ[VZ] = U16_to_F32(z, -LL_MAX_PELVIS_OFFSET, LL_MAX_PELVIS_OFFSET);
  118. // A packed Quaternion only includes the imaginary part (XYZ) and the
  119. // real part (W) is obtained with the formula:
  120. // W = sqrt(1.0 - X*X + Y*Y + Z*Z)
  121. F32 imaginary_length_squared = quat.mQ[VX] * quat.mQ[VX] +
  122. quat.mQ[VY] * quat.mQ[VY] +
  123. quat.mQ[VZ] * quat.mQ[VZ];
  124. // DANGER: make sure we do not try to take the sqrt of a negative number.
  125. if (imaginary_length_squared > 1.f)
  126. {
  127. quat.mQ[VW] = 0.f;
  128. F32 scale = 1.f / sqrtf(imaginary_length_squared);
  129. quat.mQ[VX] *= scale;
  130. quat.mQ[VY] *= scale;
  131. quat.mQ[VZ] *= scale;
  132. }
  133. else
  134. {
  135. quat.mQ[VW] = sqrtf(1.f - imaginary_length_squared);
  136. }
  137. return 3 * sizeof(U16);
  138. }
  139. // LLPuppetJointEvent class
  140. void LLPuppetJointEvent::interpolate(F32 del, const LLPuppetJointEvent& a,
  141. const LLPuppetJointEvent& b)
  142. {
  143. // Copy all of A just in case B is incompatible
  144. mRotation = a.mRotation;
  145. mPosition = a.mPosition;
  146. mScale = a.mScale;
  147. mJointID = a.mJointID;
  148. mMask = a.mMask;
  149. // Interpolate
  150. del = llclamp(del, 0.f, 1.f); // Keep del in range [0,1]
  151. U8 mask = mMask & LLIK::MASK_ROT;
  152. if (mask && (mMask & LLIK::MASK_ROT) == (b.mMask & LLIK::MASK_ROT))
  153. {
  154. mRotation = slerp(del, a.mRotation, b.mRotation);
  155. }
  156. mask = mMask & LLIK::MASK_POS;
  157. if (mask && (mMask & LLIK::MASK_POS) == (b.mMask & LLIK::MASK_POS))
  158. {
  159. mPosition = (1.f - del) * a.mPosition + del * b.mPosition;
  160. }
  161. if ((mMask & LLIK::CONFIG_FLAG_LOCAL_SCALE) &&
  162. (b.mMask & LLIK::CONFIG_FLAG_LOCAL_SCALE))
  163. {
  164. mScale = (1.f - del) * a.mScale + del * b.mScale;
  165. }
  166. }
  167. size_t LLPuppetJointEvent::getSize() const
  168. {
  169. constexpr U32 BYTES_PER_VEC_3 = 3 * sizeof(F32);
  170. size_t num_bytes = sizeof(S16) + sizeof(S8); // mJointID, mMask
  171. if (mMask & LLIK::MASK_ROT)
  172. {
  173. num_bytes += BYTES_PER_VEC_3;
  174. }
  175. if (mMask & LLIK::MASK_POS)
  176. {
  177. num_bytes += BYTES_PER_VEC_3;
  178. }
  179. if (mMask & LLIK::CONFIG_FLAG_LOCAL_SCALE)
  180. {
  181. num_bytes += BYTES_PER_VEC_3;
  182. }
  183. return num_bytes;
  184. }
  185. size_t LLPuppetJointEvent::pack(U8* wptr) const
  186. {
  187. // Stuff everything into a binary blob to save overhead.
  188. size_t offset = 0;
  189. htonmemcpy(wptr, &mJointID, MVT_S16, sizeof(S16));
  190. offset += sizeof(S16);
  191. htonmemcpy(wptr + offset, &mMask, MVT_U8, sizeof(U8));
  192. offset += sizeof(U8);
  193. // Pack these into the buffer in the same order as the flags.
  194. if (mMask & LLIK::MASK_ROT)
  195. {
  196. offset += pack_quat(wptr + offset, mRotation);
  197. }
  198. if (mMask & LLIK::MASK_POS)
  199. {
  200. offset += pack_vec3(wptr + offset, mPosition);
  201. }
  202. if (mMask & LLIK::CONFIG_FLAG_LOCAL_SCALE)
  203. {
  204. offset += pack_vec3(wptr + offset, mScale);
  205. }
  206. return offset;
  207. }
  208. size_t LLPuppetJointEvent::unpack(U8* wptr)
  209. {
  210. htonmemcpy(&mJointID, wptr, MVT_S16, sizeof(S16));
  211. size_t offset = sizeof(S16);
  212. htonmemcpy(&mMask, wptr + offset, MVT_U8, sizeof(U8));
  213. offset += sizeof(U8);
  214. // Unpack in the same order as the flags.
  215. if (mMask & LLIK::MASK_ROT)
  216. {
  217. offset += unpack_quat(wptr + offset, mRotation);
  218. }
  219. if (mMask & LLIK::MASK_POS)
  220. {
  221. offset += unpack_vec3(wptr + offset, mPosition);
  222. }
  223. if (mMask & LLIK::CONFIG_FLAG_LOCAL_SCALE)
  224. {
  225. offset += unpack_vec3(wptr + offset, mScale);
  226. }
  227. return offset;
  228. }
  229. S32 LLPuppetEvent::getMinEventSize() const
  230. {
  231. // Time, num and the size of the event buffer.
  232. S32 min_sz = sizeof(S32) + sizeof(S16) + sizeof(U32);
  233. if (mJointEvents.size() > 0)
  234. {
  235. min_sz = (S32)(mJointEvents.begin()->getSize());
  236. }
  237. return min_sz;
  238. }
  239. // A PuppetEvent contains a timestamp and one or more joints with one or more
  240. // actions applied to it. Return value is true if we packed all joints into
  241. // this event.
  242. bool LLPuppetEvent::pack(LLDataPackerBinaryBuffer& buffer, S32& out_num_joints)
  243. {
  244. S16 num_joints = 0;
  245. size_t buffer_size = buffer.getBufferSize() - buffer.getCurrentSize();
  246. bool result = true;
  247. static std::array<U8, PUPPET_MAX_EVENT_BYTES> scratch_buffer;
  248. // Accounting for time and num first plus an extra S32 for binary data size
  249. size_t len = sizeof(S32) + sizeof(S16) + sizeof(S32);
  250. size_t buf_sz = 0;
  251. U8* wptr = scratch_buffer.data();
  252. joint_deq_t::iterator iter = mJointEvents.begin();
  253. while (iter != mJointEvents.end())
  254. {
  255. if (len + buf_sz + iter->getSize() > buffer_size)
  256. {
  257. result = false;
  258. break;
  259. }
  260. size_t offset = iter->pack(wptr);
  261. ++num_joints;
  262. wptr += offset;
  263. buf_sz += offset;
  264. mJointEvents.pop_front();
  265. iter = mJointEvents.begin();
  266. }
  267. len += buf_sz;
  268. buffer.packS32(mTimestamp, "time");
  269. buffer.packU16(num_joints, "num");
  270. buffer.packBinaryData(scratch_buffer.data(), buf_sz, "data");
  271. out_num_joints = num_joints;
  272. return result;
  273. }
  274. bool LLPuppetEvent::unpack(LLDataPackerBinaryBuffer& buffer)
  275. {
  276. if (!buffer.unpackS32(mTimestamp, "time"))
  277. {
  278. LL_DEBUGS("Puppetry") << "Unable to unpack timestamp from puppetry packet."
  279. << LL_ENDL;
  280. return false;
  281. }
  282. U16 num_joints = 0;
  283. if (!buffer.unpackU16(num_joints, "num"))
  284. {
  285. LL_DEBUGS("Puppetry") << "Unable to unpack expected joint count from puppetry packet."
  286. << LL_ENDL;
  287. return false;
  288. }
  289. static std::array<U8, PUPPET_MAX_EVENT_BYTES> scratch_buffer;
  290. S32 buff_sz = scratch_buffer.size();
  291. if (!buffer.unpackBinaryData(scratch_buffer.data(), buff_sz, "data"))
  292. {
  293. LL_DEBUGS("Puppetry") << "Unable to unpack puppetry payload data from puppetry packet."
  294. << LL_ENDL;
  295. return false;
  296. }
  297. U8* wptr = scratch_buffer.data();
  298. S32 offset = 0;
  299. S32 index = 0;
  300. for ( ; index < num_joints && offset < buff_sz; ++index)
  301. {
  302. LLPuppetJointEvent jev;
  303. offset += jev.unpack(wptr + offset);
  304. mJointEvents.push_back(jev);
  305. }
  306. if (index != num_joints)
  307. {
  308. LL_DEBUGS("Puppetry") << "Unexpected joint count unpacking puppetry, expecting "
  309. << num_joints << ", only read " << index
  310. << LL_ENDL;
  311. return false;
  312. }
  313. if (offset != buff_sz)
  314. {
  315. LL_DEBUGS("Puppetry") << "Unread data in buffer. " << buff_sz
  316. << " bytes received, but only " << offset
  317. << " bytes used." << LL_ENDL;
  318. return false;
  319. }
  320. return true;
  321. }
  322. ///////////////////////////////////////////////////////////////////////////////
  323. // LLPuppetMotion utilities
  324. ///////////////////////////////////////////////////////////////////////////////
  325. // *HACK: hard-coded joint_ids
  326. constexpr U16 CHEST_ID = 6;
  327. constexpr S16 WRIST_LEFT_ID = 61;
  328. constexpr S16 WRIST_RIGHT_ID = 80;
  329. // Other constants
  330. // This is the largest possible size event:
  331. constexpr U32 PUPPET_MAX_MSG_BYTES = 255;
  332. constexpr F32 PUPPET_BROADCAST_INTERVAL = 0.05f; // Time in seconds
  333. constexpr S32 POSED_JOINT_EXPIRY_PERIOD = 3000; // In milliseconds
  334. // Static members
  335. bool LLPuppetMotion::sIsPuppetryEnabled = false;
  336. size_t LLPuppetMotion::sPuppeteerEventMaxSize = 0;
  337. ///////////////////////////////////////////////////////////////////////////////
  338. // LLPuppetMotion class proper
  339. ///////////////////////////////////////////////////////////////////////////////
  340. LLPuppetMotion::LLPuppetMotion(const LLUUID& id)
  341. : LLMotion(id),
  342. mMotionPriority(LLJoint::PUPPET_PRIORITY),
  343. mNextJointStateExpiry(S32_MAX),
  344. mRemoteToLocalClockOffset(F32_MIN),
  345. mArmSpan(2.f),
  346. mIsSelf(false)
  347. {
  348. mName = "puppet_motion";
  349. mBroadcastTimer.resetWithExpiry(PUPPET_BROADCAST_INTERVAL);
  350. }
  351. //virtual
  352. bool LLPuppetMotion::needsUpdate() const
  353. {
  354. return !mExpressionEvents.empty() || !mEventQueues.empty() ||
  355. LLMotion::needsUpdate();
  356. }
  357. //virtual
  358. LLMotion::LLMotionInitStatus LLPuppetMotion::onInitialize(LLCharacter* charp)
  359. {
  360. if (!charp) // Paranoia
  361. {
  362. return STATUS_FAILURE;
  363. }
  364. mIsSelf = ((LLVOAvatar*)charp)->isSelf();
  365. LLJoint* jointp = charp->getJoint(LL_JOINT_KEY_PELVIS);
  366. if (!jointp)
  367. {
  368. return STATUS_FAILURE;
  369. }
  370. S16 joint_id = jointp->getJointNum();
  371. mIKSolver.setRootID(joint_id);
  372. collectJoints(jointp);
  373. mIKSolver.addWristID(WRIST_LEFT_ID);
  374. mIKSolver.addWristID(WRIST_RIGHT_ID);
  375. // Compute arms reach
  376. measureArmSpan();
  377. // Generate reference rotation
  378. mIKSolver.resetSkeleton();
  379. // *HACK: whitelist of sub-bases: joints that have only child Chains and
  380. // should always be Chain endpoints, never in the middle of a Chain.
  381. std::set<S16> ids;
  382. ids.insert(CHEST_ID);
  383. mIKSolver.setSubBaseIds(ids);
  384. #if 0
  385. // *HACK: whitelist of sub-roots. This prevents the spine from being
  386. // included in the IK solution, effectively preventing the spine from
  387. // moving.
  388. ids.clear();
  389. ids.insert(CHEST_ID);
  390. mIKSolver.setSubRootIds(ids);
  391. #endif
  392. return STATUS_SUCCESS;
  393. }
  394. void LLPuppetMotion::clearAll()
  395. {
  396. mEventQueues.clear();
  397. mOutgoingEvents.clear();
  398. mJointStateExpiries.clear();
  399. mJointsToRemoveFromPose.clear();
  400. mIKSolver.resetSkeleton();
  401. }
  402. void LLPuppetMotion::addExpressionEvent(const LLPuppetJointEvent& event)
  403. {
  404. // We used to collect these events in a map, keyed by joint_id, but now we
  405. // just collect them onto a vector and process them FIFO later.
  406. mExpressionEvents.emplace_back(event);
  407. }
  408. void LLPuppetMotion::addJointToSkeletonData(LLSD& skeleton_sd, LLJoint* joint,
  409. const LLVector3& parent_rel_pos,
  410. const LLVector3& tip_rel_end_pos)
  411. {
  412. LLSD bone_sd;
  413. S16 joint_id = joint->getJointNum();
  414. bone_sd["id"] = joint_id;
  415. if (joint->getParent())
  416. {
  417. bone_sd["parent_id"] = joint->getParent()->getJointNum();
  418. bone_sd["parent_relative_position"] = ll_sd_from_vector3(parent_rel_pos);
  419. }
  420. bone_sd["tip_relative_end_position"] = ll_sd_from_vector3(tip_rel_end_pos);
  421. if (joint->getNumChildren() > 0)
  422. {
  423. bone_sd["children"] = LLSD::emptyArray();
  424. for (LLJoint::child_list_t::iterator it = joint->mChildren.begin(),
  425. end = joint->mChildren.end();
  426. it != end; ++it)
  427. {
  428. if ((*it)->isBone())
  429. {
  430. LLJoint* child = *it;
  431. bone_sd["children"].append(child->getJointNum());
  432. break;
  433. }
  434. }
  435. }
  436. skeleton_sd[joint->getName()] = bone_sd;
  437. }
  438. LLSD LLPuppetMotion::getSkeletonData()
  439. {
  440. LLSD skeleton_sd;
  441. for (state_map_t::iterator it = mJointStates.begin(),
  442. end = mJointStates.end();
  443. it != end; ++it)
  444. {
  445. LLPointer<LLJointState>& jstatep = it->second;
  446. LLJoint* jointp = jstatep->getJoint();
  447. if (!jointp) // Paranoia
  448. {
  449. continue;
  450. }
  451. LLVector3 local_pos_in_parent_frame =
  452. jointp->getPosition().scaledVec(jointp->getScale());
  453. LLVector3 bone_in_local_frame =
  454. jointp->getEnd().scaledVec(jointp->getScale());
  455. addJointToSkeletonData(skeleton_sd, jointp, local_pos_in_parent_frame,
  456. bone_in_local_frame);
  457. }
  458. skeleton_sd["scale"] = mArmSpan;
  459. return skeleton_sd;
  460. }
  461. void LLPuppetMotion::updateSkeletonGeometry()
  462. {
  463. LLIKConstraintFactory* factoryp = LLIKConstraintFactory::getInstance();
  464. for (state_map_t::iterator it = mJointStates.begin(),
  465. end = mJointStates.end();
  466. it != end; ++it)
  467. {
  468. LLPointer<LLJointState>& jstatep = it->second;
  469. if (!jstatep) // Paranoia
  470. {
  471. continue;
  472. }
  473. LLJoint* jointp = jstatep->getJoint();
  474. if (jointp) // Paranoia
  475. {
  476. LLIK::Constraint::ptr_t constraint =
  477. factoryp->getConstrForJoint(jointp->getName());
  478. mIKSolver.resetJointGeometry(it->first, constraint);
  479. }
  480. }
  481. measureArmSpan();
  482. }
  483. void LLPuppetMotion::rememberPosedJoint(S16 joint_id,
  484. LLPointer<LLJointState> jstatep,
  485. S32 now)
  486. {
  487. S32 expiry = now + POSED_JOINT_EXPIRY_PERIOD;
  488. expiries_map_t::iterator itr = mJointStateExpiries.find(joint_id);
  489. if (itr == mJointStateExpiries.end())
  490. {
  491. // Always bump remembered joints to HIGHEST_PRIORITY
  492. jstatep->setPriority(LLJoint::USE_MOTION_PRIORITY);
  493. mJointStateExpiries[joint_id] = JointStateExpiry(jstatep, expiry);
  494. addJointState(jstatep);
  495. // Check for and remove mentions of jstatep in mJointsToRemoveFromPose
  496. size_t i = 0;
  497. while (i < mJointsToRemoveFromPose.size())
  498. {
  499. if (mJointsToRemoveFromPose[i] == jstatep)
  500. {
  501. size_t last_index = mJointsToRemoveFromPose.size() - 1;
  502. if (i < last_index)
  503. {
  504. mJointsToRemoveFromPose[i] =
  505. mJointsToRemoveFromPose[last_index];
  506. }
  507. mJointsToRemoveFromPose.pop_back();
  508. }
  509. else
  510. {
  511. ++i;
  512. }
  513. }
  514. }
  515. else
  516. {
  517. itr->second.mExpiry = expiry;
  518. }
  519. if (expiry < mNextJointStateExpiry)
  520. {
  521. mNextJointStateExpiry = expiry;
  522. }
  523. }
  524. void LLPuppetMotion::reportRootRelativePosition(S16 joint_id, S32 request_id)
  525. {
  526. // Sanity checks.
  527. if (!mJointStates.count(joint_id))
  528. {
  529. return;
  530. }
  531. LLPointer<LLJointState>& rstatep = mJointStates[0];
  532. LLPointer<LLJointState>& jstatep = mJointStates[joint_id];
  533. if (rstatep.isNull() || jstatep.isNull())
  534. {
  535. return;
  536. }
  537. LLJoint* rootp = rstatep->getJoint();
  538. LLJoint* jointp = jstatep->getJoint();
  539. if (!rootp || !jointp || rootp == jointp) // No reporting for root
  540. {
  541. return;
  542. }
  543. const std::string name = jointp->getName();
  544. const LLVector3& rpos = rootp->getWorldPosition();
  545. const LLQuaternion& rrot = rootp->getWorldRotation();
  546. LLVector3 jpos = jointp->getWorldPosition();
  547. LLVector3 jend = jointp->getEnd();
  548. jend.rotVec(jointp->getWorldRotation());
  549. jpos += jend;
  550. LLVector3 opos = jpos - rpos; // Vector from root to joint end.
  551. opos.rotVec(~rrot); // Remove root rotation.
  552. LLVector3 opos2 = opos * (2.f / mArmSpan);
  553. const LLQuaternion& rlrot = rootp->getRotation();
  554. LL_DEBUGS("PuppetrySpam") << "PostIK report for: " << name << " ("
  555. << joint_id << ") 1m scale position: ("
  556. << opos.mV[0] << ", " << opos.mV[1] << ", "
  557. << opos.mV[2] << ") - Arm span scale: "
  558. << opos2.mV[0] << ", " << opos2.mV[1] << ", "
  559. << opos2.mV[2];
  560. LLVector3 reuler;
  561. rlrot.getEulerAngles(&reuler.mV[0], &reuler.mV[1], &reuler.mV[2]);
  562. reuler *= RAD_TO_DEG;
  563. LL_CONT << ") - Parent relative Euler rotation (" << reuler.mV[0] << ", "
  564. << reuler.mV[1] << ", " << reuler.mV[2] << ")" << LL_ENDL;
  565. LLSD data;
  566. data["joint_id"] = joint_id;
  567. data["name"] = name;
  568. data["position"] = LLSDMap();
  569. data["position"]["one_meter"] = ll_sd_from_vector3(opos);
  570. data["position"]["armspan"] = ll_sd_from_vector3(opos2);
  571. data["rotation"] = ll_sd_from_quaternion(rlrot);
  572. if (request_id > -1)
  573. {
  574. data["reqid"] = request_id;
  575. }
  576. gEventPumps.obtain("JointReport").post(data);
  577. }
  578. // Note: this only ever called when mIsSelf is true and configs not empty
  579. void LLPuppetMotion::solveIKAndHarvestResults(ik_map_t& configs, S32 now)
  580. {
  581. LLPuppetModule* modulep = LLPuppetModule::getInstance();
  582. bool local_puppetry = !modulep->getEcho();
  583. if (local_puppetry)
  584. {
  585. // Do not actually apply puppetry when local agent is in mouselook
  586. ECameraMode camera_mode = gAgent.getCameraMode();
  587. local_puppetry = camera_mode != CAMERA_MODE_MOUSELOOK &&
  588. camera_mode != CAMERA_MODE_CUSTOMIZE_AVATAR;
  589. }
  590. bool is_sending = modulep->isSending();
  591. if (!local_puppetry && !is_sending)
  592. {
  593. return;
  594. }
  595. bool config_changed = mIKSolver.updateJointConfigs(configs);
  596. if (config_changed)
  597. {
  598. mIKSolver.solve();
  599. }
  600. #if 0 // ATM we still need to constantly re-send unchanged Puppetry data so
  601. // we DO NOT bail early here... yet.
  602. else
  603. {
  604. // *TODO: figure out how to send partial updates, and how to explicitly
  605. // clear joint settings in the Puppetry stream.
  606. return;
  607. }
  608. #endif
  609. LLPuppetEvent broadcast_event;
  610. const LLIK::Solver::joint_list_t& active_joints =
  611. mIKSolver.getActiveJoints();
  612. for (auto joint : active_joints)
  613. {
  614. S16 id = joint->getID();
  615. U8 flags = joint->getHarvestFlags();
  616. if (local_puppetry)
  617. {
  618. LLPointer<LLJointState>& jstatep = mJointStates[id];
  619. jstatep->setUsage(U32(flags & LLIK::MASK_JOINT_STATE_USAGE));
  620. if (flags & LLIK::CONFIG_FLAG_LOCAL_POS)
  621. {
  622. jstatep->setPosition(joint->getPreScaledLocalPos());
  623. }
  624. if (flags & LLIK::CONFIG_FLAG_LOCAL_ROT)
  625. {
  626. jstatep->setRotation(joint->getLocalRot());
  627. }
  628. if (flags & LLIK::CONFIG_FLAG_LOCAL_SCALE)
  629. {
  630. jstatep->setScale(joint->getLocalScale());
  631. }
  632. rememberPosedJoint(id, jstatep, now);
  633. }
  634. if (is_sending)
  635. {
  636. LLPuppetJointEvent joint_event;
  637. joint_event.setJointID(id);
  638. joint_event.setReferenceFrame(LLPuppetJointEvent::PARENT_FRAME);
  639. if (flags & LLIK::CONFIG_FLAG_LOCAL_POS)
  640. {
  641. // We send positions with correct scale, so they can be applied
  642. // on the receiving end without modification.
  643. joint_event.setPosition(joint->getPreScaledLocalPos());
  644. }
  645. if (flags & LLIK::CONFIG_FLAG_LOCAL_ROT)
  646. {
  647. joint_event.setRotation(joint->getLocalRot());
  648. }
  649. if (flags & LLIK::CONFIG_FLAG_LOCAL_SCALE)
  650. {
  651. joint_event.setScale(joint->getLocalScale());
  652. }
  653. if (flags & LLIK::CONFIG_FLAG_DISABLE_CONSTRAINT)
  654. {
  655. joint_event.disableConstraint();
  656. }
  657. broadcast_event.addJointEvent(joint_event);
  658. }
  659. }
  660. if (is_sending)
  661. {
  662. broadcast_event.updateTimestamp();
  663. queueOutgoingEvent(broadcast_event);
  664. }
  665. }
  666. void LLPuppetMotion::applyEvent(const LLPuppetJointEvent& event, U64 now,
  667. ik_map_t& configs)
  668. {
  669. S16 joint_id = event.getJointID();
  670. if (mJointStates.count(joint_id))
  671. {
  672. LLIK::Joint::Config config;
  673. bool something_changed = false;
  674. U8 mask = event.getMask();
  675. bool local = bool(mask & LLIK::CONFIG_FLAG_LOCAL_ROT);
  676. if (mask & LLIK::MASK_ROT)
  677. {
  678. if (local)
  679. {
  680. config.setLocalRot(event.getRotation());
  681. }
  682. else
  683. {
  684. config.setTargetRot(event.getRotation());
  685. }
  686. something_changed = true;
  687. }
  688. if (mask & LLIK::MASK_POS)
  689. {
  690. if (local)
  691. {
  692. config.setLocalPos(event.getPosition());
  693. }
  694. else
  695. {
  696. // Do not forget to scale by half mArmSpan
  697. config.setTargetPos(event.getPosition() * (0.5f * mArmSpan));
  698. }
  699. something_changed = true;
  700. }
  701. if (mask & LLIK::CONFIG_FLAG_DISABLE_CONSTRAINT)
  702. {
  703. config.disableConstraint();
  704. something_changed = true;
  705. }
  706. if (mask & LLIK::CONFIG_FLAG_ENABLE_REPORTING)
  707. {
  708. config.enableReporting(event.getRequestID());
  709. }
  710. if (something_changed)
  711. {
  712. configs[joint_id] = config;
  713. }
  714. }
  715. }
  716. // Note: if we get here mIsSelf must be true
  717. void LLPuppetMotion::updateFromExpression(S32 now)
  718. {
  719. if (mExpressionEvents.empty())
  720. {
  721. return;
  722. }
  723. bool reporting = false;
  724. ik_map_t configs;
  725. for (const auto& event : mExpressionEvents)
  726. {
  727. S16 joint_id = event.getJointID();
  728. if (!mJointStates.count(joint_id))
  729. {
  730. continue;
  731. }
  732. LLIK::Joint::Config config;
  733. bool something_changed = false;
  734. U8 mask = event.getMask();
  735. if (mask & LLIK::MASK_ROT)
  736. {
  737. if (mask & LLIK::CONFIG_FLAG_LOCAL_ROT)
  738. {
  739. config.setLocalRot(event.getRotation());
  740. }
  741. else
  742. {
  743. config.setTargetRot(event.getRotation());
  744. }
  745. something_changed = true;
  746. }
  747. if (mask & LLIK::MASK_POS)
  748. {
  749. if (mask & LLIK::CONFIG_FLAG_LOCAL_POS)
  750. {
  751. config.setLocalPos(event.getPosition());
  752. }
  753. else
  754. {
  755. // Do not forget to scale by half mArmSpan
  756. config.setTargetPos(event.getPosition() * (0.5f * mArmSpan));
  757. }
  758. something_changed = true;
  759. }
  760. if (mask & LLIK::CONFIG_FLAG_DISABLE_CONSTRAINT)
  761. {
  762. config.disableConstraint();
  763. something_changed = true;
  764. }
  765. if (mask & LLIK::CONFIG_FLAG_ENABLE_REPORTING)
  766. {
  767. reporting = true;
  768. }
  769. if (something_changed)
  770. {
  771. LLIK::Solver::joint_config_map_t::iterator itr =
  772. configs.find(joint_id);
  773. if (itr == configs.end())
  774. {
  775. configs.emplace(joint_id, config);
  776. }
  777. else
  778. {
  779. itr->second.updateFrom(config);
  780. }
  781. }
  782. }
  783. if (!configs.empty())
  784. {
  785. solveIKAndHarvestResults(configs, now);
  786. }
  787. if (reporting && LLPuppetModule::getInstance()->getEcho())
  788. {
  789. for (const auto& event : mExpressionEvents)
  790. {
  791. S16 joint_id = event.getJointID();
  792. // No reporting for the root joint or unknown joints
  793. if (joint_id && mJointStates.count(joint_id) &&
  794. (event.getMask() & LLIK::CONFIG_FLAG_ENABLE_REPORTING))
  795. {
  796. reportRootRelativePosition(joint_id, event.getRequestID());
  797. }
  798. }
  799. }
  800. mExpressionEvents.clear();
  801. }
  802. void LLPuppetMotion::applyBroadcastEvent(const LLPuppetJointEvent& event,
  803. S32 now)
  804. {
  805. S16 joint_id = event.getJointID();
  806. state_map_t::iterator it = mJointStates.find(joint_id);
  807. if (it == mJointStates.end())
  808. {
  809. return;
  810. }
  811. LLPointer<LLJointState>& jstatep = it->second;
  812. if (!jstatep) // Paranoia
  813. {
  814. return;
  815. }
  816. U8 flags = event.getMask();
  817. jstatep->setUsage(U32(flags & LLIK::MASK_JOINT_STATE_USAGE));
  818. // Note: we assume broadcast event always in parent-frame, e.g.
  819. // (flags & LLIK::MASK_TARGET) == 0
  820. if (flags & LLIK::CONFIG_FLAG_LOCAL_POS)
  821. {
  822. // We expect received position to be scaled correctly so it can be
  823. // applied without modification.
  824. jstatep->setPosition(event.getPosition());
  825. }
  826. if (flags & LLIK::CONFIG_FLAG_LOCAL_ROT)
  827. {
  828. jstatep->setRotation(event.getRotation());
  829. }
  830. if (flags & LLIK::CONFIG_FLAG_LOCAL_SCALE)
  831. {
  832. jstatep->setScale(event.getScale());
  833. }
  834. rememberPosedJoint(joint_id, jstatep, now);
  835. }
  836. void LLPuppetMotion::updateFromBroadcast(S32 now)
  837. {
  838. LLPuppetModule* modulep = LLPuppetModule::getInstance();
  839. bool accept_broadcast = modulep->isReceiving();
  840. if (accept_broadcast && mIsSelf)
  841. {
  842. ECameraMode camera_mode = gAgent.getCameraMode();
  843. accept_broadcast = camera_mode != CAMERA_MODE_MOUSELOOK &&
  844. camera_mode != CAMERA_MODE_CUSTOMIZE_AVATAR;
  845. }
  846. if (!accept_broadcast)
  847. {
  848. // Drop unapplied data.
  849. mEventQueues.clear();
  850. return;
  851. }
  852. // We walk the queue looking for the two bounding events: the last previous
  853. // and the next pending: we will interpolate between them. If we do not
  854. // find bounding events we will use whatever we have got.
  855. evqueues_map_t::iterator queue_itr = mEventQueues.begin();
  856. while (queue_itr != mEventQueues.end())
  857. {
  858. event_queue_t& queue = queue_itr->second.getEventQueue();
  859. event_queue_t::iterator event_itr = queue.begin();
  860. while (event_itr != queue.end())
  861. {
  862. S32 timestamp = event_itr->first;
  863. const LLPuppetJointEvent& event = event_itr->second;
  864. if (timestamp > now)
  865. {
  866. // First available event is in the future; we have no choice
  867. // but to apply what we have
  868. applyBroadcastEvent(event, now);
  869. break;
  870. }
  871. // Event is in the past --> check next event
  872. ++event_itr;
  873. if (event_itr == queue.end())
  874. {
  875. // We are at the end of the queue
  876. constexpr S32 STALE_QUEUE_DURATION = 3000;
  877. if (timestamp < now - STALE_QUEUE_DURATION)
  878. {
  879. // This queue is stale; the "remembered pose" will be
  880. // purged elewhere
  881. queue.clear();
  882. }
  883. else
  884. {
  885. // Presumeably we already interpolated close to this event
  886. // but just in case we didn't quite reach it yet: apply
  887. applyBroadcastEvent(event, now);
  888. }
  889. break;
  890. }
  891. S32 next_time = event_itr->first;
  892. if (next_time < now)
  893. {
  894. // Event is stale --> drop it
  895. queue.pop_front();
  896. event_itr = queue.begin();
  897. continue;
  898. }
  899. // Next event is in the future, which means we have found the two
  900. // events that straddle 'now' --> create an interpolated event and
  901. // apply that.
  902. F32 del = (F32)(now - timestamp) / (F32)(next_time - timestamp);
  903. LLPuppetJointEvent interpolated_event;
  904. const LLPuppetJointEvent& next_event = event_itr->second;
  905. interpolated_event.interpolate(del, event, next_event);
  906. applyBroadcastEvent(interpolated_event, now);
  907. break;
  908. }
  909. if (queue.empty())
  910. {
  911. queue_itr = mEventQueues.erase(queue_itr);
  912. }
  913. else
  914. {
  915. ++queue_itr;
  916. }
  917. }
  918. }
  919. // Note: we expect Puppetry data to be in the "normalized-frame" where the
  920. // arm-span is 2.0 units. We will scale the inbound data by half mArmSpan.
  921. void LLPuppetMotion::measureArmSpan()
  922. {
  923. // "arm span" is twice the y-component of the longest arm
  924. F32 reach_left = mIKSolver.computeReach(CHEST_ID, WRIST_LEFT_ID).mV[VY];
  925. F32 reach_right = mIKSolver.computeReach(CHEST_ID, WRIST_RIGHT_ID).mV[VY];
  926. mArmSpan = 2.f * llmax(fabsf(reach_left), fabsf(reach_right));
  927. }
  928. void LLPuppetMotion::queueEvent(const LLPuppetEvent& puppet_event)
  929. {
  930. // Adjust the timestamp for local clock and push into the future to
  931. // allow interpolation
  932. S32 remote_timestamp = puppet_event.getTimestamp();
  933. S32 now = (S32)(LLFrameTimer::getElapsedSeconds() * 1000.0);
  934. S32 clock_skew = now - remote_timestamp;
  935. if (mRemoteToLocalClockOffset == std::numeric_limits<F32>::min())
  936. {
  937. mRemoteToLocalClockOffset = (F32)(clock_skew);
  938. }
  939. else
  940. {
  941. // Compute a running average
  942. constexpr F32 DEL = 0.05f;
  943. mRemoteToLocalClockOffset = (1.f - DEL) * mRemoteToLocalClockOffset +
  944. DEL * clock_skew;
  945. }
  946. S32 local_timestamp = remote_timestamp + S32(mRemoteToLocalClockOffset);
  947. // Split puppet_event into joint-specific streams
  948. for (const auto& joint_event : puppet_event.mJointEvents)
  949. {
  950. S16 joint_id = joint_event.getJointID();
  951. if (!mJointStates.count(joint_id))
  952. {
  953. // Ignore this unknown joint_id
  954. continue;
  955. }
  956. DelayedEventQueue& queue = mEventQueues[joint_id];
  957. queue.addEvent(remote_timestamp, local_timestamp, joint_event);
  958. }
  959. }
  960. //virtual
  961. bool LLPuppetMotion::onUpdate(F32 time, U8* joint_mask)
  962. {
  963. if (!sIsPuppetryEnabled || mJointStates.empty())
  964. {
  965. return false;
  966. }
  967. // On each update we push mStopTimestamp into the future. If the updates
  968. // stop happening then this Motion will be stopped.
  969. if (!mStopped)
  970. {
  971. constexpr F32 INACTIVITY_TIMEOUT = 2.f; // In seconds
  972. mStopTimestamp = mActivationTimestamp + time + INACTIVITY_TIMEOUT;
  973. }
  974. S32 now = S32(LLFrameTimer::getElapsedSeconds() * 1000.0);
  975. if (mIsSelf)
  976. {
  977. // *TODO: combine the two event maps into one vector of targets
  978. // LLIK::Solver::joint_config_map_t targets;
  979. updateFromExpression(now);
  980. pumpOutgoingEvents();
  981. if (LLPuppetModule::getInstance()->getEcho())
  982. {
  983. // Check for updates from server if we are echoing from there
  984. updateFromBroadcast(now);
  985. }
  986. }
  987. else
  988. {
  989. // Some other agent: just update from any incoming data
  990. updateFromBroadcast(now);
  991. }
  992. U32 jcount = mJointsToRemoveFromPose.size();
  993. if (jcount)
  994. {
  995. for (U32 i = 0; i < jcount; ++i)
  996. {
  997. LLPointer<LLJointState>& jstatep = mJointsToRemoveFromPose[i];
  998. if (jstatep)
  999. {
  1000. jstatep->setUsage(0);
  1001. mPose.removeJointState(jstatep);
  1002. }
  1003. }
  1004. mJointsToRemoveFromPose.clear();
  1005. }
  1006. // Expire joints that have not been updated in a while
  1007. if (now > mNextJointStateExpiry)
  1008. {
  1009. mNextJointStateExpiry = S32_MAX;
  1010. expiries_map_t::iterator it = mJointStateExpiries.begin();
  1011. while (it != mJointStateExpiries.end())
  1012. {
  1013. JointStateExpiry& jstate_expiryp = it->second;
  1014. if (now > jstate_expiryp.mExpiry)
  1015. {
  1016. // Instead of removing the joint from mPose during this
  1017. // onUpdate(), we set its priority LOW and clear its local
  1018. // rotation which will reset the avatar's joint... If no
  1019. // other animations contribute to it. We will remove it from
  1020. // mPose next onUpdate().
  1021. jstate_expiryp.mState->setPriority(LLJoint::LOW_PRIORITY);
  1022. jstate_expiryp.mState->setRotation(LLQuaternion::DEFAULT);
  1023. mJointsToRemoveFromPose.push_back(jstate_expiryp.mState);
  1024. it = mJointStateExpiries.erase(it);
  1025. }
  1026. else
  1027. {
  1028. if (jstate_expiryp.mExpiry < mNextJointStateExpiry)
  1029. {
  1030. mNextJointStateExpiry = jstate_expiryp.mExpiry;
  1031. }
  1032. ++it;
  1033. }
  1034. }
  1035. }
  1036. // We must return true else LLMotionController will stop and purge this
  1037. // motion.
  1038. //
  1039. // *TODO ? Figure out when to return false so that LLMotionController can
  1040. // reduce its idle load. Also will need to plumb LLPuppetModule to be able
  1041. // to reintroduce this motion to the controller when puppetry restarts.
  1042. return true;
  1043. }
  1044. //virtual
  1045. bool LLPuppetMotion::onActivate()
  1046. {
  1047. mStopTimestamp = 0.f;
  1048. return true;
  1049. }
  1050. //virtual
  1051. void LLPuppetMotion::onDeactivate()
  1052. {
  1053. mPose.removeAllJointStates();
  1054. mJointsToRemoveFromPose.clear();
  1055. for (state_map_t::iterator it = mJointStates.begin(),
  1056. end = mJointStates.end();
  1057. it != end; ++it)
  1058. {
  1059. LLPointer<LLJointState>& jstatep = it->second;
  1060. if (jstatep)
  1061. {
  1062. jstatep->setUsage(0);
  1063. }
  1064. }
  1065. // Clear solver memory.
  1066. LLIK::Solver::joint_config_map_t empty_configs;
  1067. mIKSolver.updateJointConfigs(empty_configs);
  1068. }
  1069. void LLPuppetMotion::collectJoints(LLJoint* joint)
  1070. {
  1071. // The PuppetMotion controller starts with the passed joint and recurses
  1072. // into its children, collecting all the joints and putting them under
  1073. // control of this motion controller.
  1074. if (!joint->isBone())
  1075. {
  1076. return;
  1077. }
  1078. S16 parent_id = joint->getParent()->getJointNum();
  1079. // BEGIN HACK: bypass mSpine joints
  1080. //
  1081. // mTorso 6
  1082. // |
  1083. // mSpine4 5
  1084. // |
  1085. // mSpine3 4
  1086. // |
  1087. // mChest 3
  1088. // |
  1089. // mSpine2 2
  1090. // |
  1091. // mSpine1 1
  1092. // |
  1093. // mPelvis 0
  1094. while (joint->getName().rfind("mSpine", 0) == 0)
  1095. {
  1096. for (LLJoint::child_list_t::iterator itr = joint->mChildren.begin();
  1097. itr != joint->mChildren.end(); ++itr)
  1098. {
  1099. if ((*itr)->isBone())
  1100. {
  1101. joint = *itr;
  1102. break;
  1103. }
  1104. }
  1105. }
  1106. // END HACK
  1107. LLPointer<LLJointState> jstatep = new LLJointState(joint);
  1108. S16 joint_id = joint->getJointNum();
  1109. mJointStates[joint_id] = jstatep;
  1110. LLIK::Constraint::ptr_t constraint =
  1111. LLIKConstraintFactory::getInstance()->getConstrForJoint(joint->getName());
  1112. mIKSolver.addJoint(joint_id, parent_id, joint, constraint);
  1113. // Recurse through the children of this joint and add them to our joint
  1114. // control list
  1115. for (LLJoint::child_list_t::iterator it = joint->mChildren.begin(),
  1116. end = joint->mChildren.end();
  1117. it != end; ++it)
  1118. {
  1119. collectJoints(*it);
  1120. }
  1121. }
  1122. void LLPuppetMotion::pumpOutgoingEvents()
  1123. {
  1124. if (mBroadcastTimer.hasExpired())
  1125. {
  1126. packEvents();
  1127. mBroadcastTimer.resetWithExpiry(PUPPET_BROADCAST_INTERVAL);
  1128. }
  1129. }
  1130. void LLPuppetMotion::queueOutgoingEvent(const LLPuppetEvent& event)
  1131. {
  1132. mOutgoingEvents.emplace_back(event);
  1133. }
  1134. void LLPuppetMotion::packEvents()
  1135. {
  1136. if (mOutgoingEvents.empty())
  1137. {
  1138. return;
  1139. }
  1140. if (!sIsPuppetryEnabled || sPuppeteerEventMaxSize < 30)
  1141. {
  1142. llwarns_once << "Puppetry enabled=" << sIsPuppetryEnabled
  1143. << " - event_window=" << sPuppeteerEventMaxSize
  1144. << llendl;
  1145. mOutgoingEvents.clear();
  1146. return;
  1147. }
  1148. std::array<U8, PUPPET_MAX_MSG_BYTES> puppet_pack_buffer;
  1149. LLDataPackerBinaryBuffer data_packer(puppet_pack_buffer.data(),
  1150. sPuppeteerEventMaxSize);
  1151. // Send the agent and session information.
  1152. LLMessageSystem* msg = gMessageSystemp;
  1153. msg->newMessageFast(_PREHASH_AgentAnimation);
  1154. msg->nextBlockFast(_PREHASH_AgentData);
  1155. msg->addUUIDFast(_PREHASH_AgentID, gAgentID);
  1156. msg->addUUIDFast(_PREHASH_SessionID, gAgentSessionID);
  1157. S32 msgblock_count = 0;
  1158. S32 joint_count = 0;
  1159. update_deq_t::iterator event = mOutgoingEvents.begin();
  1160. while (event != mOutgoingEvents.end())
  1161. {
  1162. data_packer.reset();
  1163. // While the datapacker can fit at least some of the current event in
  1164. // the buffer...
  1165. while (event != mOutgoingEvents.end() &&
  1166. data_packer.getCurrentSize() +
  1167. event->getMinEventSize() < data_packer.getBufferSize())
  1168. {
  1169. S32 packed_joints = 0;
  1170. bool all_done = event->pack(data_packer, packed_joints);
  1171. joint_count += packed_joints;
  1172. ++msgblock_count;
  1173. if (!all_done)
  1174. {
  1175. // Pack was not able to fit everything into this buffer it is
  1176. // full so time to send it.
  1177. break;
  1178. }
  1179. ++event;
  1180. }
  1181. // If datapacker has some data, we should put it into the message and
  1182. // perhaps send it.
  1183. if (data_packer.getCurrentSize() > 0)
  1184. {
  1185. if (msg->getCurrentSendTotal() +
  1186. data_packer.getCurrentSize() + 16 >= MTUBYTES)
  1187. {
  1188. LL_DEBUGS("PuppetrySpam") << "Message would overflow MTU, sending message with "
  1189. << msgblock_count << " blocks and "
  1190. << joint_count << " joints."
  1191. << LL_ENDL;
  1192. // Send the old message and get a new one ready.
  1193. gAgent.sendMessage();
  1194. joint_count = 0;
  1195. msgblock_count = 0;
  1196. // Create the next message header
  1197. msg->newMessageFast(_PREHASH_AgentAnimation);
  1198. msg->nextBlockFast(_PREHASH_AgentData);
  1199. msg->addUUIDFast(_PREHASH_AgentID, gAgentID);
  1200. msg->addUUIDFast(_PREHASH_SessionID, gAgentSessionID);
  1201. }
  1202. msg->nextBlockFast(_PREHASH_PhysicalAvatarEventList);
  1203. msg->addBinaryDataFast(_PREHASH_TypeData,
  1204. puppet_pack_buffer.data(),
  1205. data_packer.getCurrentSize());
  1206. }
  1207. }
  1208. mOutgoingEvents.clear();
  1209. if (msgblock_count)
  1210. {
  1211. LL_DEBUGS("PuppetrySpam") << "Sending message with "
  1212. << msgblock_count << " blocks and "
  1213. << joint_count << " joints." << LL_ENDL;
  1214. // There are some events that were not sent above. Send them along.
  1215. gAgent.sendMessage();
  1216. }
  1217. else
  1218. {
  1219. // Clean up the message we started
  1220. msg->clearMessage();
  1221. }
  1222. }
  1223. void LLPuppetMotion::unpackEvents(LLMessageSystem* mesgsys, int blocknum)
  1224. {
  1225. std::array<U8, PUPPET_MAX_MSG_BYTES> puppet_pack_buffer;
  1226. LLDataPackerBinaryBuffer data_packer(puppet_pack_buffer.data(),
  1227. PUPPET_MAX_MSG_BYTES);
  1228. data_packer.reset();
  1229. S32 data_size = mesgsys->getSizeFast(_PREHASH_PhysicalAvatarEventList,
  1230. blocknum, _PREHASH_TypeData);
  1231. mesgsys->getBinaryDataFast(_PREHASH_PhysicalAvatarEventList,
  1232. _PREHASH_TypeData, puppet_pack_buffer.data(),
  1233. data_size , blocknum,PUPPET_MAX_MSG_BYTES);
  1234. LLPuppetEvent event;
  1235. if (event.unpack(data_packer))
  1236. {
  1237. queueEvent(event);
  1238. }
  1239. else
  1240. {
  1241. llwarns_sparse << "Invalid puppetry packet received. Rejecting !"
  1242. << llendl;
  1243. }
  1244. // HACK: set mPose weight < 1.f to trigger non-idle updates in
  1245. // MotionController
  1246. if (mPose.getWeight() == 1.f && mPose.getNumJointStates() == 0)
  1247. {
  1248. mPose.setWeight(0.999f);
  1249. }
  1250. }
  1251. //static
  1252. void LLPuppetMotion::setPuppetryEnabled(bool enabled, size_t event_size)
  1253. {
  1254. bool was_enabled = sIsPuppetryEnabled;
  1255. sPuppeteerEventMaxSize = llmin(event_size, size_t(255));
  1256. sIsPuppetryEnabled = enabled && sPuppeteerEventMaxSize > 0 &&
  1257. gSavedSettings.getBool("PuppetryAllowed");
  1258. if (sIsPuppetryEnabled)
  1259. {
  1260. llinfos << "Puppetry is enabled with event window of " << event_size
  1261. << " bytes." << llendl;
  1262. return;
  1263. }
  1264. // Unload any running puppetry plugin... HB
  1265. if (was_enabled)
  1266. {
  1267. LLPuppetModule* modulep = LLPuppetModule::getInstance();
  1268. if (modulep->havePuppetModule())
  1269. {
  1270. modulep->setSending(false);
  1271. modulep->setEcho(false);
  1272. modulep->clearLeapModule();
  1273. }
  1274. }
  1275. llinfos << "Puppetry is disabled." << llendl;
  1276. }
  1277. //static
  1278. void LLPuppetMotion::updatePuppetryEnabling()
  1279. {
  1280. if (sIsPuppetryEnabled != gSavedSettings.getBool("PuppetryAllowed"))
  1281. {
  1282. // If enablement changed, refresh the status.
  1283. requestPuppetryStatus(gAgent.getRegion());
  1284. }
  1285. }
  1286. //static
  1287. void LLPuppetMotion::requestPuppetryStatus(LLViewerRegion* regionp)
  1288. {
  1289. if (!regionp) return;
  1290. // Turn off puppetry while we ask the simulator
  1291. setPuppetryEnabled(false, 0);
  1292. if (!gSavedSettings.getBool("PuppetryAllowed"))
  1293. {
  1294. return; // Forget it: the user does not want puppetry at all ! HB
  1295. }
  1296. std::string cap = regionp->getCapability("Puppetry");
  1297. if (cap.empty())
  1298. {
  1299. return;
  1300. }
  1301. gCoros.launch("RequestPuppetryStatusCoro",
  1302. [cap]()
  1303. {
  1304. requestPuppetryStatusCoro(cap);
  1305. });
  1306. }
  1307. void LLPuppetMotion::requestPuppetryStatusCoro(const std::string& capurl)
  1308. {
  1309. LLCoreHttpUtil::HttpCoroutineAdapter adapter("requestPuppetryStatusCoro");
  1310. LLCore::HttpOptions::ptr_t options(new LLCore::HttpOptions);
  1311. options->setFollowRedirects(true);
  1312. S32 retry_count = 0;
  1313. LLSD result;
  1314. LLCore::HttpStatus status;
  1315. while (true)
  1316. {
  1317. result = adapter.getAndSuspend(capurl, options);
  1318. status =
  1319. LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(result);
  1320. if (status.getType() == HTTP_NOT_FOUND)
  1321. {
  1322. // There seems to be a case at first login where the simulator is
  1323. // slow getting all of the caps connected for the agent. It has
  1324. // given us back the cap URL but returns a 404 when we try and hit
  1325. // it. Pause, take a breath and give it another shot.
  1326. if (++retry_count >= 3)
  1327. {
  1328. llwarns << "Failed to get puppetry information." << llendl;
  1329. return;
  1330. }
  1331. llcoro::suspendUntilTimeout(0.25f);
  1332. }
  1333. else if (!status)
  1334. {
  1335. llwarns << "Failed to get puppetry information." << llendl;
  1336. return;
  1337. }
  1338. else
  1339. {
  1340. break; // Success
  1341. }
  1342. }
  1343. size_t event_size = result["event_size"].asInteger();
  1344. // Maybe turn on puppetry (depending on user choice) and set the event size
  1345. LLPuppetMotion::setPuppetryEnabled(true, event_size);
  1346. if (!sIsPuppetryEnabled)
  1347. {
  1348. return;
  1349. }
  1350. LLPuppetModule::getInstance()->parsePuppetryResponse(result);
  1351. LLViewerRegion* regionp = gAgent.getRegion();
  1352. if (!isAgentAvatarValid() || !regionp ||
  1353. !regionp->getRegionFlag(REGION_FLAGS_ENABLE_ANIMATION_TRACKING))
  1354. {
  1355. return;
  1356. }
  1357. F32 period = DEFAULT_ATTACHMENT_UPDATE_PERIOD;
  1358. if (result.has("update_period"))
  1359. {
  1360. period = result["update_period"].asReal();
  1361. }
  1362. gAgentAvatarp->setAttachmentUpdatePeriod(period);
  1363. }
  1364. // LLPuppetMotion::DelayedEventQueue sub-class
  1365. void LLPuppetMotion::DelayedEventQueue::addEvent(S32 remote_timestamp,
  1366. S32 local_timestamp,
  1367. const LLPuppetJointEvent& event)
  1368. {
  1369. if (mLastRemoteTimestamp != -1)
  1370. {
  1371. // Dynamically measure mEventPeriod and mEventJitter
  1372. constexpr F32 DEL = 0.1f;
  1373. S32 this_period = remote_timestamp - mLastRemoteTimestamp;
  1374. mEventJitter = (1.f - DEL) * mEventJitter +
  1375. DEL * fabsf(mEventPeriod - (F32)this_period);
  1376. // mEventPeriod is a running average of the period between events
  1377. mEventPeriod = (1.f - DEL) * mEventPeriod + DEL * this_period;
  1378. }
  1379. mLastRemoteTimestamp = remote_timestamp;
  1380. // We push event into the future so we have something to interpolate toward
  1381. // while we wait for the next
  1382. S32 delayed_timestamp = local_timestamp + S32(mEventPeriod + mEventJitter);
  1383. mQueue.emplace_back(delayed_timestamp, event);
  1384. }