XAnimationController.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. const avatarLoader = new XAvatarLoader
  2. , log$J = new Logger$1("AnimationController");
  3. class XAnimationController {
  4. constructor(e) {
  5. E(this, "iBodyAnim");
  6. E(this, "animations", []);
  7. E(this, "defaultAnimation", "Idle");
  8. E(this, "onPlay", "Idle");
  9. E(this, "loop", !0);
  10. E(this, "animationExtras", []);
  11. E(this, "enableBlend", !1);
  12. E(this, "enableSkLod", !1);
  13. E(this, "_boneMap", new Map);
  14. E(this, "_lodMask", new Map);
  15. E(this, "activeFaceAnimation");
  16. E(this, "iFaceAnim");
  17. E(this, "_scene");
  18. E(this, "_avatar");
  19. E(this, "onPlayObservable", new Observable);
  20. E(this, "postObserver");
  21. E(this, "playAnimation", (e,t,r=0,n,o,a)=>new Promise((s,l)=>{
  22. if (this._isPlaying(e, r) || (this._registerAnimInfo(e, t, r, n, o, a),
  23. !this._isAnimate()))
  24. return s(null);
  25. this._prerocess(e, t),
  26. this._avatar.avatarManager.loadAnimation(this._avatar.avatarType, e).then(u=>{
  27. if (!u)
  28. return l(new AvatarAnimationError("animation group does not exist"));
  29. const c = this._mappingSkeleton(u);
  30. if (!c)
  31. return l(new AvatarAnimationError("mapping animation failed"));
  32. if (c && this._isAnimationValid(c))
  33. return c.dispose(),
  34. l(new AvatarAnimationError("mapping animation failed"));
  35. if (this.enableSkLod && this.skeletonMask(c, r),
  36. this.detachAnimation(r),
  37. r == 0 ? this.iBodyAnim.animGroup = c : r == 1 && (this.iFaceAnim.animGroup = c),
  38. !this._playAnimation(r))
  39. return l(new AvatarAnimationError("[Engine] play animation failed, animtion resource does not match current character"));
  40. this._playEffect(),
  41. this.postObserver = c.onAnimationEndObservable.addOnce(()=>(this._postprocess(r),
  42. s(null)))
  43. }
  44. )
  45. }
  46. ));
  47. E(this, "stopAnimation", (e=0)=>{
  48. var t, r, n, o;
  49. switch (e) {
  50. case 0:
  51. this.iBodyAnim && this.iBodyAnim.animGroup && ((t = this.iBodyAnim) == null || t.animGroup.stop());
  52. break;
  53. case 1:
  54. this.iFaceAnim && this.iFaceAnim.animGroup && ((r = this.iFaceAnim) == null || r.animGroup.stop());
  55. break;
  56. case 2:
  57. this.iBodyAnim && this.iBodyAnim.animGroup && ((n = this.iBodyAnim) == null || n.animGroup.stop()),
  58. this.iFaceAnim && this.iFaceAnim.animGroup && ((o = this.iFaceAnim) == null || o.animGroup.stop());
  59. break
  60. }
  61. }
  62. );
  63. E(this, "pauseAnimation", (e=0)=>{
  64. var t, r, n, o;
  65. switch (e) {
  66. case 0:
  67. this.iBodyAnim && this.iBodyAnim.animGroup && ((t = this.iBodyAnim) == null || t.animGroup.pause());
  68. break;
  69. case 1:
  70. this.iFaceAnim && this.iFaceAnim.animGroup && ((r = this.iFaceAnim) == null || r.animGroup.pause());
  71. break;
  72. case 2:
  73. this.iBodyAnim && this.iBodyAnim.animGroup && ((n = this.iBodyAnim) == null || n.animGroup.pause()),
  74. this.iFaceAnim && this.iFaceAnim.animGroup && ((o = this.iFaceAnim) == null || o.animGroup.pause());
  75. break
  76. }
  77. }
  78. );
  79. E(this, "resetAnimation", (e=0)=>{
  80. var t, r, n, o;
  81. switch (e) {
  82. case 0:
  83. this.iBodyAnim && this.iBodyAnim.animGroup && ((t = this.iBodyAnim) == null || t.animGroup.reset());
  84. break;
  85. case 1:
  86. this.iFaceAnim && this.iFaceAnim.animGroup && ((r = this.iFaceAnim) == null || r.animGroup.reset());
  87. break;
  88. case 2:
  89. this.iBodyAnim && this.iBodyAnim.animGroup && ((n = this.iBodyAnim) == null || n.animGroup.reset()),
  90. this.iFaceAnim && this.iFaceAnim.animGroup && ((o = this.iFaceAnim) == null || o.animGroup.reset());
  91. break
  92. }
  93. }
  94. );
  95. this._avatar = e,
  96. this._scene = e.avatarManager.scene,
  97. this.animationExtras.push(action.Cheering.animName),
  98. this._boneMap = new Map
  99. }
  100. _isPlaying(e, t) {
  101. return t == 0 && this.iBodyAnim != null && this.iBodyAnim.animGroup && e == this.iBodyAnim.name ? !0 : !!(t == 1 && this.iFaceAnim != null && this.iFaceAnim.animGroup && e == this.iFaceAnim.name)
  102. }
  103. activeAnimation(e=0) {
  104. var t, r;
  105. switch (e) {
  106. case 0:
  107. return (t = this.iBodyAnim) == null ? void 0 : t.animGroup;
  108. case 1:
  109. return (r = this.iFaceAnim) == null ? void 0 : r.animGroup;
  110. default:
  111. return
  112. }
  113. }
  114. enableAnimationBlend(e=.1, t=0) {
  115. var r, n, o, a;
  116. if (t == 0 && ((r = this.iBodyAnim) == null ? void 0 : r.animGroup))
  117. for (const s of (n = this.iBodyAnim) == null ? void 0 : n.animGroup.targetedAnimations)
  118. s.animation.enableBlending = !0,
  119. s.animation.blendingSpeed = e;
  120. else if (t == 0 && ((o = this.iFaceAnim) == null ? void 0 : o.animGroup))
  121. for (const s of (a = this.iFaceAnim) == null ? void 0 : a.animGroup.targetedAnimations)
  122. s.animation.enableBlending = !0,
  123. s.animation.blendingSpeed = e
  124. }
  125. disableAnimationBlend(e=0) {
  126. var t, r, n, o;
  127. if (e == 0 && ((t = this.iBodyAnim) == null ? void 0 : t.animGroup))
  128. for (const a of (r = this.iBodyAnim) == null ? void 0 : r.animGroup.targetedAnimations)
  129. a.animation.enableBlending = !1;
  130. else if (e == 0 && ((n = this.iFaceAnim) == null ? void 0 : n.animGroup))
  131. for (const a of (o = this.iFaceAnim) == null ? void 0 : o.animGroup.targetedAnimations)
  132. a.animation.enableBlending = !1
  133. }
  134. skeletonMask(e, t=0) {
  135. if (t == 0) {
  136. const r = this._lodMask.get(this._avatar.distLevel);
  137. if (r)
  138. for (let n = 0; n < e.targetedAnimations.length; ++n)
  139. r.includes(e.targetedAnimations[n].target.name) || (e.targetedAnimations.splice(n, 1),
  140. n--);
  141. return !0
  142. }
  143. return !1
  144. }
  145. detachAnimation(e=2) {
  146. var t, r;
  147. switch (e) {
  148. case 0:
  149. this.iBodyAnim && this.iBodyAnim.animGroup && (this.iBodyAnim.animGroup._parentContainer.xReferenceCount && this.iBodyAnim.animGroup._parentContainer.xReferenceCount--,
  150. this.iBodyAnim.animGroup.stop(),
  151. this.iBodyAnim.animGroup.dispose(),
  152. this.iBodyAnim.animGroup = void 0);
  153. break;
  154. case 1:
  155. this.iFaceAnim && this.iFaceAnim.animGroup && (this.iFaceAnim.animGroup._parentContainer.xReferenceCount && this.iFaceAnim.animGroup._parentContainer.xReferenceCount--,
  156. this.iFaceAnim.animGroup.stop(),
  157. this.iFaceAnim.animGroup.dispose(),
  158. this.iFaceAnim.animGroup = void 0);
  159. break;
  160. case 2:
  161. this.iBodyAnim && this.iBodyAnim.animGroup && (this.iBodyAnim.animGroup._parentContainer.xReferenceCount && this.iBodyAnim.animGroup._parentContainer.xReferenceCount--,
  162. (t = this.iBodyAnim) == null || t.animGroup.stop(),
  163. (r = this.iBodyAnim) == null || r.animGroup.dispose(),
  164. this.iBodyAnim.animGroup = void 0),
  165. this.iFaceAnim && this.iFaceAnim.animGroup && (this.iFaceAnim.animGroup._parentContainer.xReferenceCount && this.iFaceAnim.animGroup._parentContainer.xReferenceCount--,
  166. this.iFaceAnim.animGroup.stop(),
  167. this.iFaceAnim.animGroup.dispose(),
  168. this.iFaceAnim.animGroup = void 0);
  169. break
  170. }
  171. }
  172. blendAnimation() {}
  173. getAnimation(e, t) {
  174. return avatarLoader.animations.get(getAnimationKey(t, e))
  175. }
  176. _mappingSkeleton(e) {
  177. if (e) {
  178. const t = e.clone(e.name, r=>{
  179. var o, a, s;
  180. const n = r.name.split(" ").length > 2 ? r.name.split(" ")[2] : r.name;
  181. if (this._boneMap.size === ((o = this._avatar.skeleton) == null ? void 0 : o.bones.length))
  182. return this._boneMap.get(n);
  183. {
  184. const l = (s = (a = this._avatar.skeleton) == null ? void 0 : a.bones.find(u=>u.name === r.name || u.name === r.name.split(" ")[2])) == null ? void 0 : s.getTransformNode();
  185. return l && (l.name = n,
  186. this._boneMap.set(n, l)),
  187. l
  188. }
  189. }
  190. );
  191. return t._parentContainer = e._parentContainer,
  192. t
  193. } else
  194. return
  195. }
  196. removeAnimation(e) {
  197. const t = avatarLoader.containers.get(e.name);
  198. t && (t.dispose(),
  199. avatarLoader.containers.delete(e.name),
  200. avatarLoader.animations.delete(getAnimationKey(e.name, e.skType)))
  201. }
  202. _setPosition(e, t) {
  203. this._avatar.priority === 0 && this._avatar.isRender && e === this.defaultAnimation && e != this.onPlay && !this._avatar.isSelected && this._avatar.setPosition(this._avatar.position, !0)
  204. }
  205. _registerAnimInfo(e, t, r=0, n, o, a) {
  206. const s = {
  207. name: e,
  208. skType: this._avatar.avatarType,
  209. loop: t,
  210. playSpeed: n,
  211. currentFrame: 0,
  212. startFrame: o,
  213. endFrame: a
  214. };
  215. r == 0 ? this.iBodyAnim == null ? this.iBodyAnim = s : (this.iBodyAnim.name = e,
  216. this.iBodyAnim.skType = this._avatar.avatarType,
  217. this.iBodyAnim.loop = t,
  218. this.iBodyAnim.playSpeed = n,
  219. this.iBodyAnim.currentFrame = 0,
  220. this.iBodyAnim.startFrame = o,
  221. this.iBodyAnim.endFrame = a) : r == 1 && (this.iFaceAnim == null ? this.iFaceAnim = s : (this.iFaceAnim.name = e,
  222. this.iFaceAnim.skType = this._avatar.avatarType,
  223. this.iFaceAnim.loop = t,
  224. this.iFaceAnim.playSpeed = n,
  225. this.iFaceAnim.currentFrame = 0,
  226. this.iFaceAnim.startFrame = o,
  227. this.iFaceAnim.endFrame = a)),
  228. this.onPlay = e,
  229. this.loop = t
  230. }
  231. _isAnimate() {
  232. var e;
  233. return !(!this._avatar.isRender || !this._avatar.skeleton || ((e = this._avatar.rootNode) == null ? void 0 : e.getChildMeshes().length) == 0)
  234. }
  235. _prerocess(e, t) {
  236. this._avatar.isRayCastEnable && this._setPosition(e, t),
  237. this._avatar.priority === 0 && log$J.info(`start play animation: ${e} on avatar ${this._avatar.id}`)
  238. }
  239. _playEffect() {
  240. this.animationExtras.indexOf(this.iBodyAnim.name) != -1 && action.Cheering.attachPair.forEach(t=>{
  241. this._avatar.attachExtraProp(t.obj, t.bone, new Vector3(t.offset.x,t.offset.y,t.offset.z), new Vector3(t.rotate.x,t.rotate.y,t.rotate.z)),
  242. this._avatar.showExtra(t.obj)
  243. }
  244. )
  245. }
  246. _playAnimation(e=0) {
  247. var t, r;
  248. return e == 0 && this.iBodyAnim && ((t = this.iBodyAnim) == null ? void 0 : t.animGroup) ? (this.onPlayObservable.notifyObservers(this._scene),
  249. this.iBodyAnim.animGroup.start(this.loop, this.iBodyAnim.playSpeed, this.iBodyAnim.startFrame, this.iBodyAnim.endFrame, !1),
  250. !0) : e == 1 && this.iFaceAnim && ((r = this.iFaceAnim) == null ? void 0 : r.animGroup) ? (this.iFaceAnim.animGroup.start(this.loop, this.iFaceAnim.playSpeed, this.iFaceAnim.startFrame, this.iFaceAnim.endFrame, !1),
  251. !0) : !1
  252. }
  253. _postprocess(e) {
  254. var r, n;
  255. let t;
  256. e == 0 ? t = (r = this.iBodyAnim) == null ? void 0 : r.name : e == 1 && (t = (n = this.iFaceAnim) == null ? void 0 : n.name),
  257. t === action.Cheering.animName && this._avatar.disposeExtra()
  258. }
  259. _isAnimationValid(e) {
  260. for (let t = 0; t < e.targetedAnimations.length; ++t)
  261. if (e.targetedAnimations[t].target)
  262. return !1;
  263. return !0
  264. }
  265. }