瀏覽代碼

Merge pull request #5443 from kcoley/deserializeMorphTargets

Deserialize morph target animations from animation groups
David Catuhe 6 年之前
父節點
當前提交
c908196be5
共有 4 個文件被更改,包括 50 次插入3 次删除
  1. 1 0
      dist/preview release/what's new.md
  2. 11 3
      src/Animations/babylon.animationGroup.ts
  3. 20 0
      src/Morph/babylon.morphTarget.ts
  4. 18 0
      src/babylon.scene.ts

+ 1 - 0
dist/preview release/what's new.md

@@ -58,6 +58,7 @@
 - Make sure that `Material.markAsDirty` and all the `markXXXDirty` methods early out when `scene.blockMaterialDirtyMechanism` is true. ([barroij](https://github.com/barroij))
 - Add updateUpVectorFromRotation to target camera to allow the up vector to be computed from rotation ([TrevorDev](https://github.com/TrevorDev))
 - Added opacity texture support to `GridMaterial` ([Deltakosh](https://github.com/deltakosh))
+- Added support for deserializing morph target animations in animation groups
 
 ### glTF Loader
 

+ 11 - 3
src/Animations/babylon.animationGroup.ts

@@ -405,10 +405,18 @@ module BABYLON {
                 var targetedAnimation = parsedAnimationGroup.targetedAnimations[i];
                 var animation = Animation.Parse(targetedAnimation.animation);
                 var id = targetedAnimation.targetId;
-                var targetNode = scene.getNodeByID(id);
+                if (targetedAnimation.animation.property === "influence") { // morph target animation
+                    let morphTarget = scene.getMorphTargetById(id);
+                    if (morphTarget) {
+                        animationGroup.addTargetedAnimation(animation, morphTarget);
+                    }
+                }
+                else {
+                    var targetNode = scene.getNodeByID(id);
 
-                if (targetNode != null) {
-                    animationGroup.addTargetedAnimation(animation, targetNode);
+                    if (targetNode != null) {
+                        animationGroup.addTargetedAnimation(animation, targetNode);
+                    }
                 }
             }
 

+ 20 - 0
src/Morph/babylon.morphTarget.ts

@@ -43,6 +43,12 @@ module BABYLON {
             }
         }
 
+        /**
+         * Gets or sets the id of the morph Target
+         */
+        @serialize()
+        public id: string;
+
         private _animationPropertiesOverride: Nullable<AnimationPropertiesOverride> = null;
 
         /**
@@ -169,6 +175,9 @@ module BABYLON {
             serializationObject.influence = this.influence;
 
             serializationObject.positions = Array.prototype.slice.call(this.getPositions());
+            if (this.id != null) {
+                serializationObject.id = this.id;
+            }
             if (this.hasNormals) {
                 serializationObject.normals = Array.prototype.slice.call(this.getNormals());
             }
@@ -182,6 +191,14 @@ module BABYLON {
             return serializationObject;
         }
 
+        /**
+         * Returns the string "MorphTarget"
+         * @returns "MorphTarget"
+         */
+        public getClassName(): string {
+            return "MorphTarget";
+        }
+
         // Statics
 
         /**
@@ -194,6 +211,9 @@ module BABYLON {
 
             result.setPositions(serializationObject.positions);
 
+            if (serializationObject.id != null) {
+                result.id = serializationObject.id;
+            }
             if (serializationObject.normals) {
                 result.setNormals(serializationObject.normals);
             }

+ 18 - 0
src/babylon.scene.ts

@@ -3959,6 +3959,24 @@ module BABYLON {
         }
 
         /**
+         * Gets a morph target using a given id (if many are found, this function will pick the first one)
+         * @param id defines the id to search for
+         * @return the found morph target or null if not found at all.
+         */
+        public getMorphTargetById(id: string): Nullable<MorphTarget> {
+            for (let managerIndex = 0; managerIndex < this.morphTargetManagers.length; ++managerIndex) {
+                const morphTargetManager = this.morphTargetManagers[managerIndex];
+                for (let index = 0; index < morphTargetManager.numTargets; ++index) {
+                    const target = morphTargetManager.getTarget(index);
+                    if (target.id === id) {
+                        return target;
+                    }
+                }
+            }
+            return null;
+        }
+
+        /**
          * Gets a boolean indicating if the given mesh is active
          * @param mesh defines the mesh to look for
          * @returns true if the mesh is in the active list