XAnimationController.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import {avatarLoader} from "./XAvatarLoader.js"
  2. import AvatarAnimationError from "./error/AvatarAnimationError.js"
  3. import Logger from "./Logger.js"
  4. import EMotionType from "./enum/EMotionType.js"
  5. const logger = new Logger('AnimationController')
  6. export default class XAnimationController {
  7. constructor(o) {
  8. this.iBodyAnim = void 0,
  9. this.animations = [],
  10. this.defaultAnimation = "Idle",
  11. this.onPlay = void 0,
  12. this.loop = !0,
  13. this.animationExtras = [],
  14. this.enableBlend = !1,
  15. this.enableSkLod = !1,
  16. this._boneMap = new Map,
  17. this._lodMask = new Map,
  18. this.activeFaceAnimation = void 0,
  19. this.iFaceAnim = void 0,
  20. this.onPlayObservable = new BABYLON.Observable,
  21. this.stateObserver = void 0,
  22. this.speed = 0,
  23. this.moveThresh = .001,
  24. this.motionMap = new Map,
  25. this.playAnimation = (s,c,_=0,b,k,j)=>new Promise(($,_e)=>{
  26. const et = this.motionMap.get(s);
  27. if (et && (s = et),
  28. this._isPlaying(s, _))
  29. return $(null);
  30. if (GEngine.engineOption.bDisableSkeletonAnim)
  31. return setTimeout(()=>{
  32. this.onPlayObservable.notifyObservers(this._scene)
  33. }
  34. , 10),
  35. this.detachAnimation(_),
  36. $(null);
  37. if (this._registerAnimInfo(s, c, _, b, k, j),
  38. !this._isAnimate())
  39. return $(null);
  40. this._prerocess(s, c),
  41. this._avatar.avatarManager.loadAnimation(s).then(tt=>{
  42. if (!tt)
  43. return _e(new AvatarAnimationError("animation group does not exist"));
  44. if (!this._avatar.skeleton)
  45. return $(null);
  46. const rt = this._mappingSkeleton(tt);
  47. if (!rt)
  48. return _e(new AvatarAnimationError(`mapping animation failed: when ${s} attach to ${this._avatar.avatarType}`));
  49. if (rt && this._isAnimationValid(rt))
  50. return rt.dispose(),
  51. _e();
  52. if (this.enableSkLod && this.skeletonMask(rt, _),
  53. this.detachAnimation(_),
  54. _ == 0 ? this.iBodyAnim.animGroup = rt : _ == 1 && (this.iFaceAnim.animGroup = rt),
  55. !this._playAnimation(_))
  56. return _e(new AvatarAnimationError("[Engine] play animation failed, animtion resource does not match current character"));
  57. this.postObserver = rt.onAnimationEndObservable.addOnce(()=>(this._postprocess(_),
  58. $(null)))
  59. }
  60. ).catch(tt=>{
  61. _e(tt)
  62. }
  63. )
  64. }
  65. ),
  66. this.stopAnimation = (s=0)=>{
  67. var c, _, b, k;
  68. switch (s) {
  69. case 0:
  70. this.iBodyAnim && this.iBodyAnim.animGroup && ((c = this.iBodyAnim) == null || c.animGroup.stop());
  71. break;
  72. case 1:
  73. this.iFaceAnim && this.iFaceAnim.animGroup && ((_ = this.iFaceAnim) == null || _.animGroup.stop());
  74. break;
  75. case 2:
  76. this.iBodyAnim && this.iBodyAnim.animGroup && ((b = this.iBodyAnim) == null || b.animGroup.stop()),
  77. this.iFaceAnim && this.iFaceAnim.animGroup && ((k = this.iFaceAnim) == null || k.animGroup.stop());
  78. break
  79. }
  80. }
  81. ,
  82. this.pauseAnimation = (s=0)=>{
  83. var c, _, b, k;
  84. switch (s) {
  85. case 0:
  86. this.iBodyAnim && this.iBodyAnim.animGroup && ((c = this.iBodyAnim) == null || c.animGroup.pause());
  87. break;
  88. case 1:
  89. this.iFaceAnim && this.iFaceAnim.animGroup && ((_ = this.iFaceAnim) == null || _.animGroup.pause());
  90. break;
  91. case 2:
  92. this.iBodyAnim && this.iBodyAnim.animGroup && ((b = this.iBodyAnim) == null || b.animGroup.pause()),
  93. this.iFaceAnim && this.iFaceAnim.animGroup && ((k = this.iFaceAnim) == null || k.animGroup.pause());
  94. break
  95. }
  96. }
  97. ,
  98. this.resetAnimation = (s=0)=>{
  99. var c, _, b, k;
  100. switch (s) {
  101. case 0:
  102. this.iBodyAnim && this.iBodyAnim.animGroup && ((c = this.iBodyAnim) == null || c.animGroup.reset());
  103. break;
  104. case 1:
  105. this.iFaceAnim && this.iFaceAnim.animGroup && ((_ = this.iFaceAnim) == null || _.animGroup.reset());
  106. break;
  107. case 2:
  108. this.iBodyAnim && this.iBodyAnim.animGroup && ((b = this.iBodyAnim) == null || b.animGroup.reset()),
  109. this.iFaceAnim && this.iFaceAnim.animGroup && ((k = this.iFaceAnim) == null || k.animGroup.reset());
  110. break
  111. }
  112. }
  113. ,
  114. this._avatar = o,
  115. this._scene = o.avatarManager.scene,
  116. this.animationExtras.push(action.Cheering.animName),
  117. this._boneMap = new Map
  118. }
  119. ReceiveTick() {
  120. var o;
  121. this.stateObserver = (o = this._avatar.avatarManager.sceneManager) == null ? void 0 : o.Scene.onBeforeRenderObservable.add(()=>{
  122. this._avatar.movementComponent.enable && this._tickMoveState(this._avatar.movementComponent.currentMotionType)
  123. }
  124. )
  125. }
  126. RemoveTick() {
  127. this.stateObserver && this._scene.onBeforeRenderObservable.remove(this.stateObserver)
  128. }
  129. BindingAnimationId(o, s) {
  130. this.motionMap.set(o, s)
  131. }
  132. _tickMoveState(o) {
  133. switch (o) {
  134. case EMotionType.Idle:
  135. (this.onPlay == this.motionMap.get("Walking") || this.onPlay == this.motionMap.get("Running")) && this.playAnimation(this.motionMap.get("Idle"), !0);
  136. break;
  137. case EMotionType.Walk:
  138. this.playAnimation(this.motionMap.get("Walking"), !0);
  139. break;
  140. case EMotionType.Run:
  141. this.playAnimation(this.motionMap.get("Running"), !0);
  142. break
  143. }
  144. }
  145. _isPlaying(o, s) {
  146. return s == 0 && this.iBodyAnim != null && this.iBodyAnim.animGroup && o == this.iBodyAnim.name ? !0 : !!(s == 1 && this.iFaceAnim != null && this.iFaceAnim.animGroup && o == this.iFaceAnim.name)
  147. }
  148. activeAnimation(o=0) {
  149. var s, c;
  150. switch (o) {
  151. case 0:
  152. return (s = this.iBodyAnim) == null ? void 0 : s.animGroup;
  153. case 1:
  154. return (c = this.iFaceAnim) == null ? void 0 : c.animGroup;
  155. default:
  156. return
  157. }
  158. }
  159. enableAnimationBlend(o=.1, s=0) {
  160. var c, _, b, k;
  161. if (s == 0 && ((c = this.iBodyAnim) == null ? void 0 : c.animGroup))
  162. for (const j of (_ = this.iBodyAnim) == null ? void 0 : _.animGroup.targetedAnimations)
  163. j.animation.enableBlending = !0,
  164. j.animation.blendingSpeed = o;
  165. else if (s == 0 && ((b = this.iFaceAnim) == null ? void 0 : b.animGroup))
  166. for (const j of (k = this.iFaceAnim) == null ? void 0 : k.animGroup.targetedAnimations)
  167. j.animation.enableBlending = !0,
  168. j.animation.blendingSpeed = o
  169. }
  170. disableAnimationBlend(o=0) {
  171. var s, c, _, b;
  172. if (o == 0 && ((s = this.iBodyAnim) == null ? void 0 : s.animGroup))
  173. for (const k of (c = this.iBodyAnim) == null ? void 0 : c.animGroup.targetedAnimations)
  174. k.animation.enableBlending = !1;
  175. else if (o == 0 && ((_ = this.iFaceAnim) == null ? void 0 : _.animGroup))
  176. for (const k of (b = this.iFaceAnim) == null ? void 0 : b.animGroup.targetedAnimations)
  177. k.animation.enableBlending = !1
  178. }
  179. skeletonMask(o, s=0) {
  180. if (s == 0) {
  181. const c = this._lodMask.get(this._avatar.distLevel);
  182. if (c)
  183. for (let _ = 0; _ < o.targetedAnimations.length; ++_)
  184. c.includes(o.targetedAnimations[_].target.name) || (o.targetedAnimations.splice(_, 1),
  185. _--);
  186. return !0
  187. }
  188. return !1
  189. }
  190. detachAnimation(o=2) {
  191. var s, c;
  192. switch (o) {
  193. case 0:
  194. this.iBodyAnim && this.iBodyAnim.animGroup && (this.iBodyAnim.animGroup._parentContainer && this.iBodyAnim.animGroup._parentContainer.xReferenceCount && this.iBodyAnim.animGroup._parentContainer.xReferenceCount--,
  195. this.iBodyAnim.animGroup.stop(),
  196. this.iBodyAnim.animGroup.dispose(),
  197. this.iBodyAnim.animGroup = void 0);
  198. break;
  199. case 1:
  200. this.iFaceAnim && this.iFaceAnim.animGroup && (this.iFaceAnim.animGroup._parentContainer && this.iFaceAnim.animGroup._parentContainer.xReferenceCount && this.iFaceAnim.animGroup._parentContainer.xReferenceCount--,
  201. this.iFaceAnim.animGroup.stop(),
  202. this.iFaceAnim.animGroup.dispose(),
  203. this.iFaceAnim.animGroup = void 0);
  204. break;
  205. case 2:
  206. this.iBodyAnim && this.iBodyAnim.animGroup && (this.iBodyAnim.animGroup._parentContainer && this.iBodyAnim.animGroup._parentContainer.xReferenceCount && this.iBodyAnim.animGroup._parentContainer.xReferenceCount--,
  207. (s = this.iBodyAnim) == null || s.animGroup.stop(),
  208. (c = this.iBodyAnim) == null || c.animGroup.dispose(),
  209. this.iBodyAnim.animGroup = void 0),
  210. this.iFaceAnim && this.iFaceAnim.animGroup && (this.iFaceAnim.animGroup._parentContainer && this.iFaceAnim.animGroup._parentContainer.xReferenceCount && this.iFaceAnim.animGroup._parentContainer.xReferenceCount--,
  211. this.iFaceAnim.animGroup.stop(),
  212. this.iFaceAnim.animGroup.dispose(),
  213. this.iFaceAnim.animGroup = void 0);
  214. break
  215. }
  216. this._scene.onDisposeObservable.clear(),
  217. this._scene.getEngine().onContextRestoredObservable.clear()
  218. }
  219. blendAnimation() {}
  220. getAnimation(o, s) {}
  221. _mappingSkeleton(o) {
  222. if (o) {
  223. const s = o.clone(o.name, c=>{
  224. var b, k, j;
  225. const _ = c.name.split(" ").length > 2 ? c.name.split(" ")[2] : c.name;
  226. if (this._boneMap.size === ((b = this._avatar.skeleton) == null ? void 0 : b.bones.length))
  227. return this._boneMap.get(_);
  228. {
  229. const $ = (j = (k = this._avatar.skeleton) == null ? void 0 : k.bones.find(_e=>_e.name === c.name || _e.name === c.name.split(" ")[2])) == null ? void 0 : j.getTransformNode();
  230. return $ && ($.name = _,
  231. this._boneMap.set(_, $)),
  232. $
  233. }
  234. }
  235. );
  236. return s._parentContainer = o._parentContainer,
  237. s
  238. } else
  239. return
  240. }
  241. removeAnimation(o) {
  242. const s = avatarLoader.containers.get(o.name);
  243. s && (s.dispose(),
  244. avatarLoader.containers.delete(o.name))
  245. }
  246. _setPosition(o, s) {
  247. this._avatar.priority === 0 && this._avatar.isRender && o === this.defaultAnimation && o != this.onPlay && !this._avatar.isSelected && this._avatar.movementComponent.updatePosition(this._avatar.position, !0)
  248. }
  249. _registerAnimInfo(o, s, c=0, _, b, k) {
  250. const j = {
  251. name: o,
  252. skType: this._avatar.avatarType,
  253. loop: s,
  254. playSpeed: _,
  255. currentFrame: 0,
  256. startFrame: b,
  257. endFrame: k
  258. };
  259. c == 0 ? this.iBodyAnim == null ? this.iBodyAnim = j : (this.iBodyAnim.name = o,
  260. this.iBodyAnim.skType = this._avatar.avatarType,
  261. this.iBodyAnim.loop = s,
  262. this.iBodyAnim.playSpeed = _,
  263. this.iBodyAnim.currentFrame = 0,
  264. this.iBodyAnim.startFrame = b,
  265. this.iBodyAnim.endFrame = k) : c == 1 && (this.iFaceAnim == null ? this.iFaceAnim = j : (this.iFaceAnim.name = o,
  266. this.iFaceAnim.skType = this._avatar.avatarType,
  267. this.iFaceAnim.loop = s,
  268. this.iFaceAnim.playSpeed = _,
  269. this.iFaceAnim.currentFrame = 0,
  270. this.iFaceAnim.startFrame = b,
  271. this.iFaceAnim.endFrame = k)),
  272. this.onPlay = o,
  273. this.loop = s
  274. }
  275. _isAnimate() {
  276. var o;
  277. return !(!this._avatar.skeleton || ((o = this._avatar.rootNode) == null ? void 0 : o.getChildMeshes().length) == 0)
  278. }
  279. _prerocess(o, s) {
  280. this._avatar.isRayCastEnable && this._setPosition(o, s),
  281. this._avatar.priority === 0 && logger.info(`start play animation: ${o} on avatar ${this._avatar.id}`)
  282. }
  283. _playAnimation(o=0) {
  284. var s, c;
  285. if(o == 0 && this.iBodyAnim && ((s = this.iBodyAnim) == null ? void 0 : s.animGroup) ){
  286. this.onPlayObservable.notifyObservers(this._scene);
  287. try {
  288. this.iBodyAnim.animGroup.start(this.loop, this.iBodyAnim.playSpeed, this.iBodyAnim.startFrame, this.iBodyAnim.endFrame, !1);
  289. } catch (error) { }
  290. return !0
  291. }
  292. else{
  293. if(o == 1 && this.iFaceAnim && ((c = this.iFaceAnim) == null ? void 0 : c.animGroup)){
  294. this.iFaceAnim.animGroup.start(this.loop, this.iFaceAnim.playSpeed, this.iFaceAnim.startFrame, this.iFaceAnim.endFrame, !1);
  295. return !0
  296. }
  297. else{
  298. return !1
  299. }
  300. }
  301. // return o == 0 && this.iBodyAnim && ((s = this.iBodyAnim) == null ? void 0 : s.animGroup) ? (this.onPlayObservable.notifyObservers(this._scene),
  302. // this.iBodyAnim.animGroup.start(this.loop, this.iBodyAnim.playSpeed, this.iBodyAnim.startFrame, this.iBodyAnim.endFrame, !1),
  303. // !0) : o == 1 && this.iFaceAnim && ((c = this.iFaceAnim) == null ? void 0 : c.animGroup) ? (this.iFaceAnim.animGroup.start(this.loop, this.iFaceAnim.playSpeed, this.iFaceAnim.startFrame, this.iFaceAnim.endFrame, !1),
  304. // !0) : !1
  305. }
  306. _postprocess(o) {
  307. var s, c;
  308. o == 0 ? (s = this.iBodyAnim) == null || s.name : o == 1 && ((c = this.iFaceAnim) == null || c.name)
  309. }
  310. _isAnimationValid(o) {
  311. for (let s = 0; s < o.targetedAnimations.length; ++s)
  312. if (o.targetedAnimations[s].target)
  313. return !1;
  314. return !0
  315. }
  316. }