zhouenguang 3 年之前
父节点
当前提交
b9cf54d6b6
共有 5 个文件被更改,包括 155 次插入131 次删除
  1. 110 97
      src/XAnimationController.js
  2. 18 9
      src/XAvatarLoader.js
  3. 20 18
      src/XAvatarManager.js
  4. 3 3
      src/XverseAvatar.js
  5. 4 4
      src/Xverse_Room.js

+ 110 - 97
src/XAnimationController.js

@@ -4,7 +4,7 @@ import Logger from "./Logger.js"
 
 const logger = new Logger('AnimationController')
 export default class XAnimationController {
-    constructor(e) {
+    constructor(avatar) {
         this.iBodyAnim = void 0,
         this.animations = [],
         this.defaultAnimation = "Idle",
@@ -18,87 +18,97 @@ export default class XAnimationController {
         this.activeFaceAnimation = void 0,
         this.iFaceAnim = void 0,
         this.onPlayObservable = new Observable,
-        this.playAnimation = (i,o,s=0,c,d,_)=>new Promise((b,k)=>{
-            // zeg 传入i为任意动画名即可播放该动画,比如"GiftClap"
-            // window.room.avatarManager.avatars.get(window.room.userId).playAnimation({"animationName": "GiftClap", "loop":true})
-            if (this._isPlaying(i, s) || (this._registerAnimInfo(i, o, s, c, d, _),
-            !this._isAnimate()))
-                return b(null);
-            this._prerocess(i, o),
-            this._avatar.avatarManager.loadAnimation(this._avatar.avatarType, i).then(j=>{
-                if (!j)
-                    return k(new AvatarAnimationError("animation group does not exist"));
-                const skeleton = this._mappingSkeleton(j);
-                if (!skeleton)
-                    return k(new AvatarAnimationError("mapping animation failed"));
-                if (skeleton && this._isAnimationValid(skeleton))
-                    return skeleton.dispose(),
-                    k(new AvatarAnimationError("mapping animation failed"));
-                if (this.enableSkLod && this.skeletonMask(skeleton, s),
-                this.detachAnimation(s),
-                s == 0 ? this.iBodyAnim.animGroup = skeleton : s == 1 && (this.iFaceAnim.animGroup = skeleton),
-                !this._playAnimation(s))
-                    return k(new AvatarAnimationError("[Engine] play animation failed, animtion resource does not match current character"));
-                this._playEffect(),
-                this.postObserver = skeleton.onAnimationEndObservable.addOnce(()=>(this._postprocess(s),
-                b(null)))
-            }
-            )
-        }
-        ),
-        this.stopAnimation = (i=0)=>{
-            var o, s, c, d;
-            switch (i) {
-            case 0:
-                this.iBodyAnim && this.iBodyAnim.animGroup && ((o = this.iBodyAnim) == null || o.animGroup.stop());
-                break;
-            case 1:
-                this.iFaceAnim && this.iFaceAnim.animGroup && ((s = this.iFaceAnim) == null || s.animGroup.stop());
-                break;
-            case 2:
-                this.iBodyAnim && this.iBodyAnim.animGroup && ((c = this.iBodyAnim) == null || c.animGroup.stop()),
-                this.iFaceAnim && this.iFaceAnim.animGroup && ((d = this.iFaceAnim) == null || d.animGroup.stop());
-                break
-            }
+
+        this._avatar = avatar,
+        this._scene = avatar.avatarManager.scene,
+        this.animationExtras.push(action.Cheering.animName),
+        this._boneMap = new Map
+    }
+    
+    // s: :0 身体动画 :1 脸部动画
+    playAnimation = (animationName, isLoop, s=0, c, d, _)=>new Promise((resolve, reject)=>{
+        // zeg 传入i为任意动画名即可播放该动画,比如"GiftClap"
+        // window.room.avatarManager.avatars.get(window.room.userId).playAnimation({"animationName": "GiftClap", "loop":true})
+        if (
+            this._isPlaying(animationName, s) || 
+            (this._registerAnimInfo(animationName, isLoop, s, c, d, _), !this._isAnimate())
+        ) 
+        return resolve(null);
+
+        this._prerocess(animationName, isLoop),
+        this._avatar.avatarManager.loadAnimation(this._avatar.avatarType, animationName).then(animation => {
+            if (!animation) return reject(new AvatarAnimationError("animation group does not exist"));
+
+            const skeleton = this._mappingSkeleton(animation);
+            if (!skeleton) return reject(new AvatarAnimationError("mapping animation failed"))
+            
+            if (skeleton && this._isAnimationValid(skeleton))
+                return skeleton.dispose(),
+                reject(new AvatarAnimationError("mapping animation failed"));
+
+            this.enableSkLod && this.skeletonMask(skeleton, s)
+            this.detachAnimation(s)
+            s == 0 ? this.iBodyAnim.animGroup = skeleton : s == 1 && (this.iFaceAnim.animGroup = skeleton)
+
+            if (!this._playAnimation(s)) 
+            return reject(new AvatarAnimationError("[Engine] play animation failed, animtion resource does not match current character"));
+            
+            this._playEffect(),
+            this.postObserver = skeleton.onAnimationEndObservable.addOnce(()=>(
+                this._postprocess(s),
+                resolve(null)
+            ))
+        })
+    })
+    
+    stopAnimation = (i=0)=>{
+        var o, s, c, d;
+        switch (i) {
+        case 0:
+            this.iBodyAnim && this.iBodyAnim.animGroup && ((o = this.iBodyAnim) == null || o.animGroup.stop());
+            break;
+        case 1:
+            this.iFaceAnim && this.iFaceAnim.animGroup && ((s = this.iFaceAnim) == null || s.animGroup.stop());
+            break;
+        case 2:
+            this.iBodyAnim && this.iBodyAnim.animGroup && ((c = this.iBodyAnim) == null || c.animGroup.stop()),
+            this.iFaceAnim && this.iFaceAnim.animGroup && ((d = this.iFaceAnim) == null || d.animGroup.stop());
+            break
         }
-        ,
-        this.pauseAnimation = (i=0)=>{
-            var o, s, c, d;
-            switch (i) {
-            case 0:
-                this.iBodyAnim && this.iBodyAnim.animGroup && ((o = this.iBodyAnim) == null || o.animGroup.pause());
-                break;
-            case 1:
-                this.iFaceAnim && this.iFaceAnim.animGroup && ((s = this.iFaceAnim) == null || s.animGroup.pause());
-                break;
-            case 2:
-                this.iBodyAnim && this.iBodyAnim.animGroup && ((c = this.iBodyAnim) == null || c.animGroup.pause()),
-                this.iFaceAnim && this.iFaceAnim.animGroup && ((d = this.iFaceAnim) == null || d.animGroup.pause());
-                break
-            }
+    }
+    
+    pauseAnimation = (i=0)=>{
+        var o, s, c, d;
+        switch (i) {
+        case 0:
+            this.iBodyAnim && this.iBodyAnim.animGroup && ((o = this.iBodyAnim) == null || o.animGroup.pause());
+            break;
+        case 1:
+            this.iFaceAnim && this.iFaceAnim.animGroup && ((s = this.iFaceAnim) == null || s.animGroup.pause());
+            break;
+        case 2:
+            this.iBodyAnim && this.iBodyAnim.animGroup && ((c = this.iBodyAnim) == null || c.animGroup.pause()),
+            this.iFaceAnim && this.iFaceAnim.animGroup && ((d = this.iFaceAnim) == null || d.animGroup.pause());
+            break
         }
-        ,
-        this.resetAnimation = (i=0)=>{
-            var o, s, c, d;
-            switch (i) {
-            case 0:
-                this.iBodyAnim && this.iBodyAnim.animGroup && ((o = this.iBodyAnim) == null || o.animGroup.reset());
-                break;
-            case 1:
-                this.iFaceAnim && this.iFaceAnim.animGroup && ((s = this.iFaceAnim) == null || s.animGroup.reset());
-                break;
-            case 2:
-                this.iBodyAnim && this.iBodyAnim.animGroup && ((c = this.iBodyAnim) == null || c.animGroup.reset()),
-                this.iFaceAnim && this.iFaceAnim.animGroup && ((d = this.iFaceAnim) == null || d.animGroup.reset());
-                break
-            }
+    }
+
+    resetAnimation = (i=0)=>{
+        var o, s, c, d;
+        switch (i) {
+        case 0:
+            this.iBodyAnim && this.iBodyAnim.animGroup && ((o = this.iBodyAnim) == null || o.animGroup.reset());
+            break;
+        case 1:
+            this.iFaceAnim && this.iFaceAnim.animGroup && ((s = this.iFaceAnim) == null || s.animGroup.reset());
+            break;
+        case 2:
+            this.iBodyAnim && this.iBodyAnim.animGroup && ((c = this.iBodyAnim) == null || c.animGroup.reset()),
+            this.iFaceAnim && this.iFaceAnim.animGroup && ((d = this.iFaceAnim) == null || d.animGroup.reset());
+            break
         }
-        ,
-        this._avatar = e,
-        this._scene = e.avatarManager.scene,
-        this.animationExtras.push(action.Cheering.animName),
-        this._boneMap = new Map
     }
+
     _isPlaying(e, i) {
         return i == 0 && this.iBodyAnim != null && this.iBodyAnim.animGroup && e == this.iBodyAnim.name ? !0 : !!(i == 1 && this.iFaceAnim != null && this.iFaceAnim.animGroup && e == this.iFaceAnim.name)
     }
@@ -175,23 +185,23 @@ export default class XAnimationController {
     getAnimation(e, i) {
         return avatarLoader.animations.get(getAnimationKey(i, e))
     }
-    _mappingSkeleton(e) {
-        if (e) {
-            const i = e.clone(e.name, o=>{
-                var c, d, _;
-                const s = o.name.split(" ").length > 2 ? o.name.split(" ")[2] : o.name;
-                if (this._boneMap.size === ((c = this._avatar.skeleton) == null ? void 0 : c.bones.length))
-                    return this._boneMap.get(s);
+    _mappingSkeleton(animation) {
+        if (animation) {
+            const skeleton = animation.clone(animation.name, o=>{
+                const name = o.name.split(" ").length > 2 ? o.name.split(" ")[2] : o.name;
+                if (this._boneMap.size === (this._avatar.skeleton && this._avatar.skeleton.bones.length)) return this._boneMap.get(name);
                 {
-                    const b = (_ = (d = this._avatar.skeleton) == null ? void 0 : d.bones.find(k=>k.name === o.name || k.name === o.name.split(" ")[2])) == null ? void 0 : _.getTransformNode();
-                    return b && (b.name = s,
-                    this._boneMap.set(s, b)),
+                    let bone = this._avatar.skeleton && this._avatar.skeleton.bones.find(bone => bone.name === o.name || bone.name === o.name.split(" ")[2])
+                    const b = bone && bone.getTransformNode();
+                    return b && (
+                        b.name = name,
+                        this._boneMap.set(name, b)
+                    ),
                     b
                 }
-            }
-            );
-            return i._parentContainer = e._parentContainer,
-            i
+            });
+            return skeleton._parentContainer = animation._parentContainer,
+            skeleton
         } else
             return
     }
@@ -246,11 +256,14 @@ export default class XAnimationController {
         )
     }
     _playAnimation(e=0) {
-        var i, o;
-        return e == 0 && this.iBodyAnim && ((i = this.iBodyAnim) == null ? void 0 : i.animGroup) ? (this.onPlayObservable.notifyObservers(this._scene),
-        this.iBodyAnim.animGroup.start(this.loop, this.iBodyAnim.playSpeed, this.iBodyAnim.startFrame, this.iBodyAnim.endFrame, !1),
-        !0) : e == 1 && this.iFaceAnim && ((o = this.iFaceAnim) == null ? void 0 : o.animGroup) ? (this.iFaceAnim.animGroup.start(this.loop, this.iFaceAnim.playSpeed, this.iFaceAnim.startFrame, this.iFaceAnim.endFrame, !1),
-        !0) : !1
+        return e == 0 && this.iBodyAnim && this.iBodyAnim.animGroup ? (
+            this.onPlayObservable.notifyObservers(this._scene),
+            this.iBodyAnim.animGroup.start(this.loop, this.iBodyAnim.playSpeed, this.iBodyAnim.startFrame, this.iBodyAnim.endFrame, !1),
+            !0
+        ) : e == 1 && this.iFaceAnim && this.iFaceAnim.animGroup ? (
+                this.iFaceAnim.animGroup.start(this.loop, this.iFaceAnim.playSpeed, this.iFaceAnim.startFrame, this.iFaceAnim.endFrame, !1),
+                !0
+            ) : !1
     }
     _postprocess(e) {
         var o, s;

+ 18 - 9
src/XAvatarLoader.js

@@ -174,9 +174,9 @@ export default class XAvatarLoader {
     _getSourceKey(e, i) {
         return i && avatarSetting.lod[i] ? e + avatarSetting.lod[i].fileName.split(".")[0] : e
     }
-    _getAnimPath(e, i) {
-        let o = this.aniPath.get(i + "_animations_" + i.split("_")[1]);
-        return o || (o = this.aniPath.get(i + "_" + e)),
+    _getAnimPath(animationName, avatarType) {
+        let o = this.aniPath.get(avatarType + "_animations_" + avatarType.split("_")[1]);
+        return o || (o = this.aniPath.get(avatarType + "_" + animationName)),
         o
     }
     load(e, i, o, s) {
@@ -191,12 +191,21 @@ export default class XAvatarLoader {
         ),
         o
     }
-    loadAnimRes(e, i, o) {
-        const s = this._getAnimPath(i, o)
-          , c = getAnimationKey(i, o);
-        return s && this.containers.get(s) ? Promise.resolve(this.containers.get(s)) : s ? this._loadGlbFromBlob(e, c, s).then(d=>d.animationGroups.length == 0 ? (this.containers.delete(c),
-        d.dispose(),
-        Promise.reject("container does not contains animation data")) : d) : Promise.reject("no such url")
+    loadAnimRes(sceneManager, animationName, avatarType) {
+        const aniModelPath = this._getAnimPath(animationName, avatarType)
+          , aniKey = getAnimationKey(animationName, avatarType);
+        return aniModelPath && this.containers.get(aniModelPath) 
+        ? Promise.resolve(this.containers.get(aniModelPath)) 
+        : aniModelPath
+            ? this._loadGlbFromBlob(sceneManager, aniKey, aniModelPath).then(d=>
+                d.animationGroups.length == 0 
+                ? (
+                    this.containers.delete(aniKey),
+                    d.dispose(),
+                    Promise.reject("container does not contains animation data")
+                ) 
+                : d) 
+            : Promise.reject("no such url")
     }
     loadGlb(e, i, o) {
         let s = this.getMeshUrl(this._getSourceKey(i, o));

+ 20 - 18
src/XAvatarManager.js

@@ -561,24 +561,26 @@ export default class XAvatarManager {
         return new Promise((r,n)=>(avatarLoader.avaliableAnimation.set(t, e),
         r()))
     }
-    loadAnimation(e, t) {
-        return new Promise((r,n)=>avatarLoader.loadAnimRes(this.sceneManager, t, e).then(o=>{
-            if (o) {
-                let a;
-                const s = this.avatarLoader.animations;
-                return o.animationGroups.forEach(l=>{
-                    l.stop(),
-                    l.name === t && (a = l,
-                    a.pContainer = o),
-                    s.set(getAnimationKey(l.name, e), l)
-                }
-                ),
-                this._clipContainerRes(o),
-                o.xReferenceCount++,
-                r(a)
-            } else
-                return n(new ContainerLoadingFailedError("[Engine] container failed instanciates failed"))
-        }
+    loadAnimation(avatarType, animationName) {
+        return new Promise((resolve, reject)=>
+            avatarLoader.loadAnimRes(this.sceneManager, animationName, avatarType).then(o=>{
+                if (o) {
+                    let a;
+                    const avatarAnimations = this.avatarLoader.animations;
+                    return o.animationGroups.forEach(animation=>{
+                        animation.stop(),
+                        animation.name === animationName && (
+                            a = animation,
+                            a.pContainer = o
+                        ),
+                        avatarAnimations.set(getAnimationKey(animation.name, avatarType), animation)
+                    }),
+                    this._clipContainerRes(o),
+                    o.xReferenceCount++,
+                    resolve(a)
+                } else
+                    return reject(new ContainerLoadingFailedError("[Engine] container failed instanciates failed"))
+            }
         ))
     }
     loadDecoration(e, t, r) {

+ 3 - 3
src/XverseAvatar.js

@@ -37,9 +37,9 @@ export default class XverseAvatar extends EventEmitter {
         this._avatarId = avatarId,
         this.group = group,
 
-        this._room.modelManager.getAvatarModelList().then(avatarModelList=>{
-            const l = avatarModelList.find(u=>u.id === avatarId);
-            l && (this._avatarModel = l)
+        this._room.modelManager.getAvatarModelList().then(list=>{
+            const avatarModel = list.find(u=>u.id === avatarId);
+            avatarModel && (this._avatarModel = avatarModel)
         })
     }
 

+ 4 - 4
src/Xverse_Room.js

@@ -28,7 +28,7 @@ import NewUserStateType from "./enum/NewUserStateType.js";
 
 const logger = new Logger("xverse-room");
 export default class Xverse_Room extends EventEmitter {
-  constructor(e) {
+  constructor(options) {
     super();
     this.disableAutoTurn = !1;
     this.options;
@@ -67,12 +67,12 @@ export default class Xverse_Room extends EventEmitter {
     this.firstFrameTimestamp;
     this.moveToExtra = "";
 
-    this.options = e
+    this.options = options
     this.options.wsServerUrl || (this.options.wsServerUrl = SERVER_URLS.DEV)
-    this.modelManager = ModelManager.getInstance(e.appId, e.releaseId)
+    this.modelManager = ModelManager.getInstance(options.appId, options.releaseId)
     this.updateReporter()
 
-    const n = e,
+    const n = options,
       { canvas: t } = n,
       r = Oe(n, ["canvas"]);