XAnimationController.js 12 KB

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