123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- import {avatarLoader} from "./XAvatarLoader.js"
- import AvatarAnimationError from "./error/AvatarAnimationError.js"
- import Logger from "./Logger.js"
- import EMotionType from "./enum/EMotionType.js"
- const logger = new Logger('AnimationController')
- export default class XAnimationController {
- constructor(o) {
- this.iBodyAnim = void 0,
- this.animations = [],
- this.defaultAnimation = "Idle",
- this.onPlay = void 0,
- this.loop = !0,
- this.animationExtras = [],
- this.enableBlend = !1,
- this.enableSkLod = !1,
- this._boneMap = new Map,
- this._lodMask = new Map,
- this.activeFaceAnimation = void 0,
- this.iFaceAnim = void 0,
- this.onPlayObservable = new BABYLON.Observable,
- this.stateObserver = void 0,
- this.speed = 0,
- this.moveThresh = .001,
- this.motionMap = new Map,
- this.playAnimation = (s,c,_=0,b,k,j)=>new Promise(($,_e)=>{
- const et = this.motionMap.get(s);
- if (et && (s = et),
- this._isPlaying(s, _))
- return $(null);
- if (GEngine.engineOption.bDisableSkeletonAnim)
- return setTimeout(()=>{
- this.onPlayObservable.notifyObservers(this._scene)
- }
- , 10),
- this.detachAnimation(_),
- $(null);
- if (this._registerAnimInfo(s, c, _, b, k, j),
- !this._isAnimate())
- return $(null);
- this._prerocess(s, c),
- this._avatar.avatarManager.loadAnimation(s).then(tt=>{
- if (!tt)
- return _e(new AvatarAnimationError("animation group does not exist"));
- if (!this._avatar.skeleton)
- return $(null);
- const rt = this._mappingSkeleton(tt);
- if (!rt)
- return _e(new AvatarAnimationError(`mapping animation failed: when ${s} attach to ${this._avatar.avatarType}`));
- if (rt && this._isAnimationValid(rt))
- return rt.dispose(),
- _e();
- if (this.enableSkLod && this.skeletonMask(rt, _),
- this.detachAnimation(_),
- _ == 0 ? this.iBodyAnim.animGroup = rt : _ == 1 && (this.iFaceAnim.animGroup = rt),
- !this._playAnimation(_))
- return _e(new AvatarAnimationError("[Engine] play animation failed, animtion resource does not match current character"));
- this.postObserver = rt.onAnimationEndObservable.addOnce(()=>(this._postprocess(_),
- $(null)))
- }
- ).catch(tt=>{
- _e(tt)
- }
- )
- }
- ),
- this.stopAnimation = (s=0)=>{
- var c, _, b, k;
- switch (s) {
- case 0:
- this.iBodyAnim && this.iBodyAnim.animGroup && ((c = this.iBodyAnim) == null || c.animGroup.stop());
- break;
- case 1:
- this.iFaceAnim && this.iFaceAnim.animGroup && ((_ = this.iFaceAnim) == null || _.animGroup.stop());
- break;
- case 2:
- this.iBodyAnim && this.iBodyAnim.animGroup && ((b = this.iBodyAnim) == null || b.animGroup.stop()),
- this.iFaceAnim && this.iFaceAnim.animGroup && ((k = this.iFaceAnim) == null || k.animGroup.stop());
- break
- }
- }
- ,
- this.pauseAnimation = (s=0)=>{
- var c, _, b, k;
- switch (s) {
- case 0:
- this.iBodyAnim && this.iBodyAnim.animGroup && ((c = this.iBodyAnim) == null || c.animGroup.pause());
- break;
- case 1:
- this.iFaceAnim && this.iFaceAnim.animGroup && ((_ = this.iFaceAnim) == null || _.animGroup.pause());
- break;
- case 2:
- this.iBodyAnim && this.iBodyAnim.animGroup && ((b = this.iBodyAnim) == null || b.animGroup.pause()),
- this.iFaceAnim && this.iFaceAnim.animGroup && ((k = this.iFaceAnim) == null || k.animGroup.pause());
- break
- }
- }
- ,
- this.resetAnimation = (s=0)=>{
- var c, _, b, k;
- switch (s) {
- case 0:
- this.iBodyAnim && this.iBodyAnim.animGroup && ((c = this.iBodyAnim) == null || c.animGroup.reset());
- break;
- case 1:
- this.iFaceAnim && this.iFaceAnim.animGroup && ((_ = this.iFaceAnim) == null || _.animGroup.reset());
- break;
- case 2:
- this.iBodyAnim && this.iBodyAnim.animGroup && ((b = this.iBodyAnim) == null || b.animGroup.reset()),
- this.iFaceAnim && this.iFaceAnim.animGroup && ((k = this.iFaceAnim) == null || k.animGroup.reset());
- break
- }
- }
- ,
- this._avatar = o,
- this._scene = o.avatarManager.scene,
- this.animationExtras.push(action.Cheering.animName),
- this._boneMap = new Map
- }
- ReceiveTick() {
- var o;
- this.stateObserver = (o = this._avatar.avatarManager.sceneManager) == null ? void 0 : o.Scene.onBeforeRenderObservable.add(()=>{
- this._avatar.movementComponent.enable && this._tickMoveState(this._avatar.movementComponent.currentMotionType)
- }
- )
- }
- RemoveTick() {
- this.stateObserver && this._scene.onBeforeRenderObservable.remove(this.stateObserver)
- }
- BindingAnimationId(o, s) {
- this.motionMap.set(o, s)
- }
- _tickMoveState(o) {
- switch (o) {
- case EMotionType.Idle:
- (this.onPlay == this.motionMap.get("Walking") || this.onPlay == this.motionMap.get("Running")) && this.playAnimation(this.motionMap.get("Idle"), !0);
- break;
- case EMotionType.Walk:
- this.playAnimation(this.motionMap.get("Walking"), !0);
- break;
- case EMotionType.Run:
- this.playAnimation(this.motionMap.get("Running"), !0);
- break
- }
- }
- _isPlaying(o, s) {
- 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)
- }
- activeAnimation(o=0) {
- var s, c;
- switch (o) {
- case 0:
- return (s = this.iBodyAnim) == null ? void 0 : s.animGroup;
- case 1:
- return (c = this.iFaceAnim) == null ? void 0 : c.animGroup;
- default:
- return
- }
- }
- enableAnimationBlend(o=.1, s=0) {
- var c, _, b, k;
- if (s == 0 && ((c = this.iBodyAnim) == null ? void 0 : c.animGroup))
- for (const j of (_ = this.iBodyAnim) == null ? void 0 : _.animGroup.targetedAnimations)
- j.animation.enableBlending = !0,
- j.animation.blendingSpeed = o;
- else if (s == 0 && ((b = this.iFaceAnim) == null ? void 0 : b.animGroup))
- for (const j of (k = this.iFaceAnim) == null ? void 0 : k.animGroup.targetedAnimations)
- j.animation.enableBlending = !0,
- j.animation.blendingSpeed = o
- }
- disableAnimationBlend(o=0) {
- var s, c, _, b;
- if (o == 0 && ((s = this.iBodyAnim) == null ? void 0 : s.animGroup))
- for (const k of (c = this.iBodyAnim) == null ? void 0 : c.animGroup.targetedAnimations)
- k.animation.enableBlending = !1;
- else if (o == 0 && ((_ = this.iFaceAnim) == null ? void 0 : _.animGroup))
- for (const k of (b = this.iFaceAnim) == null ? void 0 : b.animGroup.targetedAnimations)
- k.animation.enableBlending = !1
- }
- skeletonMask(o, s=0) {
- if (s == 0) {
- const c = this._lodMask.get(this._avatar.distLevel);
- if (c)
- for (let _ = 0; _ < o.targetedAnimations.length; ++_)
- c.includes(o.targetedAnimations[_].target.name) || (o.targetedAnimations.splice(_, 1),
- _--);
- return !0
- }
- return !1
- }
- detachAnimation(o=2) {
- var s, c;
- switch (o) {
- case 0:
- this.iBodyAnim && this.iBodyAnim.animGroup && (this.iBodyAnim.animGroup._parentContainer && this.iBodyAnim.animGroup._parentContainer.xReferenceCount && this.iBodyAnim.animGroup._parentContainer.xReferenceCount--,
- this.iBodyAnim.animGroup.stop(),
- this.iBodyAnim.animGroup.dispose(),
- this.iBodyAnim.animGroup = void 0);
- break;
- case 1:
- this.iFaceAnim && this.iFaceAnim.animGroup && (this.iFaceAnim.animGroup._parentContainer && this.iFaceAnim.animGroup._parentContainer.xReferenceCount && this.iFaceAnim.animGroup._parentContainer.xReferenceCount--,
- this.iFaceAnim.animGroup.stop(),
- this.iFaceAnim.animGroup.dispose(),
- this.iFaceAnim.animGroup = void 0);
- break;
- case 2:
- this.iBodyAnim && this.iBodyAnim.animGroup && (this.iBodyAnim.animGroup._parentContainer && this.iBodyAnim.animGroup._parentContainer.xReferenceCount && this.iBodyAnim.animGroup._parentContainer.xReferenceCount--,
- (s = this.iBodyAnim) == null || s.animGroup.stop(),
- (c = this.iBodyAnim) == null || c.animGroup.dispose(),
- this.iBodyAnim.animGroup = void 0),
- this.iFaceAnim && this.iFaceAnim.animGroup && (this.iFaceAnim.animGroup._parentContainer && this.iFaceAnim.animGroup._parentContainer.xReferenceCount && this.iFaceAnim.animGroup._parentContainer.xReferenceCount--,
- this.iFaceAnim.animGroup.stop(),
- this.iFaceAnim.animGroup.dispose(),
- this.iFaceAnim.animGroup = void 0);
- break
- }
- this._scene.onDisposeObservable.clear(),
- this._scene.getEngine().onContextRestoredObservable.clear()
- }
- blendAnimation() {}
- getAnimation(o, s) {}
- _mappingSkeleton(o) {
- if (o) {
- const s = o.clone(o.name, c=>{
- var b, k, j;
- const _ = c.name.split(" ").length > 2 ? c.name.split(" ")[2] : c.name;
- if (this._boneMap.size === ((b = this._avatar.skeleton) == null ? void 0 : b.bones.length))
- return this._boneMap.get(_);
- {
- 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();
- return $ && ($.name = _,
- this._boneMap.set(_, $)),
- $
- }
- }
- );
- return s._parentContainer = o._parentContainer,
- s
- } else
- return
- }
- removeAnimation(o) {
- const s = avatarLoader.containers.get(o.name);
- s && (s.dispose(),
- avatarLoader.containers.delete(o.name))
- }
- _setPosition(o, s) {
- this._avatar.priority === 0 && this._avatar.isRender && o === this.defaultAnimation && o != this.onPlay && !this._avatar.isSelected && this._avatar.movementComponent.updatePosition(this._avatar.position, !0)
- }
- _registerAnimInfo(o, s, c=0, _, b, k) {
- const j = {
- name: o,
- skType: this._avatar.avatarType,
- loop: s,
- playSpeed: _,
- currentFrame: 0,
- startFrame: b,
- endFrame: k
- };
- c == 0 ? this.iBodyAnim == null ? this.iBodyAnim = j : (this.iBodyAnim.name = o,
- this.iBodyAnim.skType = this._avatar.avatarType,
- this.iBodyAnim.loop = s,
- this.iBodyAnim.playSpeed = _,
- this.iBodyAnim.currentFrame = 0,
- this.iBodyAnim.startFrame = b,
- this.iBodyAnim.endFrame = k) : c == 1 && (this.iFaceAnim == null ? this.iFaceAnim = j : (this.iFaceAnim.name = o,
- this.iFaceAnim.skType = this._avatar.avatarType,
- this.iFaceAnim.loop = s,
- this.iFaceAnim.playSpeed = _,
- this.iFaceAnim.currentFrame = 0,
- this.iFaceAnim.startFrame = b,
- this.iFaceAnim.endFrame = k)),
- this.onPlay = o,
- this.loop = s
- }
- _isAnimate() {
- var o;
- return !(!this._avatar.skeleton || ((o = this._avatar.rootNode) == null ? void 0 : o.getChildMeshes().length) == 0)
- }
- _prerocess(o, s) {
- this._avatar.isRayCastEnable && this._setPosition(o, s),
- this._avatar.priority === 0 && logger.info(`start play animation: ${o} on avatar ${this._avatar.id}`)
- }
- _playAnimation(o=0) {
- var s, c;
- if(o == 0 && this.iBodyAnim && ((s = this.iBodyAnim) == null ? void 0 : s.animGroup) ){
- this.onPlayObservable.notifyObservers(this._scene);
- try {
- this.iBodyAnim.animGroup.start(this.loop, this.iBodyAnim.playSpeed, this.iBodyAnim.startFrame, this.iBodyAnim.endFrame, !1);
- } catch (error) { }
- return !0
- }
- else{
- if(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);
- return !0
- }
- else{
- return !1
- }
- }
- // return o == 0 && this.iBodyAnim && ((s = this.iBodyAnim) == null ? void 0 : s.animGroup) ? (this.onPlayObservable.notifyObservers(this._scene),
- // this.iBodyAnim.animGroup.start(this.loop, this.iBodyAnim.playSpeed, this.iBodyAnim.startFrame, this.iBodyAnim.endFrame, !1),
- // !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),
- // !0) : !1
- }
- _postprocess(o) {
- var s, c;
- o == 0 ? (s = this.iBodyAnim) == null || s.name : o == 1 && ((c = this.iFaceAnim) == null || c.name)
- }
- _isAnimationValid(o) {
- for (let s = 0; s < o.targetedAnimations.length; ++s)
- if (o.targetedAnimations[s].target)
- return !1;
- return !0
- }
- }
|