Просмотр исходного кода

Animation ranges for Nodes

Added methods to allow for AnimationRanges on lights, cameras, & meshes.

These can come from a .babylon & also be serialized. Tested using
Blender exporter.  Need more testing there so, exporter not on this PR.

Also, minor changes for skeleton animation ranges.
jeff 10 лет назад
Родитель
Сommit
8c2227d6b4

+ 2 - 2
src/Bones/babylon.skeleton.ts

@@ -80,11 +80,11 @@
                 if (sourceBone){
                     ret = ret && this.bones[i].copyAnimationRange(sourceBone, name, frameOffset, rescaleAsRequired);
                 }else{
-                    BABYLON.Tools.Warn("copyAnimationRange: not same rig, missing source bone " + name);
+                    BABYLON.Tools.Warn("copyAnimationRange: not same rig, missing source bone " + boneName);
                     ret = false;
                 }
             }
-            // do not call createRange(), since it also is done to bones, which was already done
+            // do not call createAnimationRange(), since it also is done to bones, which was already done
             var range = source.getAnimationRange(name);
             this._ranges[name] = new AnimationRange(name, range.from + frameOffset, range.to + frameOffset);
             return ret;

+ 2 - 0
src/Cameras/babylon.camera.ts

@@ -607,6 +607,7 @@
             
             // Animations
             Animation.AppendSerializedAnimations(this, serializationObject);
+            serializationObject.ranges = this.serializeAnimationRanges();
 
             // Layer mask
             serializationObject.layerMask = this.layerMask;
@@ -718,6 +719,7 @@
 
                     camera.animations.push(Animation.Parse(parsedAnimation));
                 }
+                Node.ParseAnimationRanges(camera, parsedCamera, scene);
             }
 
             if (parsedCamera.autoAnimate) {

+ 1 - 0
src/Lights/babylon.light.ts

@@ -186,6 +186,7 @@
 
                     light.animations.push(Animation.Parse(parsedAnimation));
                 }
+                Node.ParseAnimationRanges(light, parsedLight, scene);
             }
 
             if (parsedLight.autoAnimate) {

+ 2 - 0
src/Mesh/babylon.mesh.ts

@@ -1477,6 +1477,7 @@
 
                     mesh.animations.push(Animation.Parse(parsedAnimation));
                 }
+                Node.ParseAnimationRanges(mesh, parsedMesh, scene);
             }
 
             if (parsedMesh.autoAnimate) {
@@ -1516,6 +1517,7 @@
 
                             instance.animations.push(Animation.Parse(parsedAnimation));
                         }
+                        Node.ParseAnimationRanges(instance, parsedMesh, scene);
                     }
                 }
             }

+ 2 - 0
src/Tools/babylon.sceneSerializer.ts

@@ -145,10 +145,12 @@
 
             // Animations
             Animation.AppendSerializedAnimations(instance, serializationInstance);
+            serializationInstance.ranges = instance.serializeAnimationRanges();
         }
 
         // Animations
         Animation.AppendSerializedAnimations(mesh, serializationObject);
+        serializationObject.ranges = mesh.serializeAnimationRanges();
 
         // Layer mask
         serializationObject.layerMask = mesh.layerMask;

+ 57 - 0
src/babylon.node.ts

@@ -11,6 +11,7 @@
         public state = "";
 
         public animations = new Array<Animation>();
+        private _ranges : { [name: string] : AnimationRange; } = {};
 
         public onReady: (node: Node) => void;
 
@@ -217,5 +218,61 @@
 
             return null;
         }
+        
+        public createAnimationRange(name: string, from: number, to: number): void {
+            // check name not already in use
+            if (! this._ranges[name]){
+                this._ranges[name] = new AnimationRange(name, from, to);
+                for (var i = 0, nAnimations = this.animations.length; i < nAnimations; i++) {
+                    if (this.animations[i]) {
+                        this.animations[i].createRange(name, from, to);
+                    }
+                }
+            }
+        }
+
+        public deleteAnimationRange(name: string, deleteFrames = true): void {
+            for (var i = 0, nAnimations = this.animations.length; i < nAnimations; i++) {
+                if (this.animations[i]) {
+                    this.animations[i].deleteRange(name, deleteFrames);
+                }
+            }
+            this._ranges[name] = undefined; // said much faster than 'delete this._range[name]' 
+        }
+
+        public getAnimationRange(name: string): AnimationRange {
+            return this._ranges[name];
+        }
+
+        public beginAnimation(name: string, loop?: boolean, speedRatio?: number, onAnimationEnd?: () => void): void {
+            var range = this.getAnimationRange(name);
+
+            if (!range) {
+                return null;
+            }
+
+            this._scene.beginAnimation(this, range.from, range.to, loop, speedRatio, onAnimationEnd);
+        }
+        
+        public serializeAnimationRanges(): any {
+            var serializationRanges = [];
+            for (var name in this._ranges) {
+                var range: any = {};
+                range.name = name;
+                range.from = this._ranges[name].from;
+                range.to   = this._ranges[name].to;
+                serializationRanges.push(range);
+            }
+            return serializationRanges;
+        }
+        
+        public static ParseAnimationRanges(node : Node, parsedNode: any, scene: Scene): void {
+            if (parsedNode.ranges){
+               for (var index = 0; index < parsedNode.ranges.length; index++) {
+                   var data = parsedNode.ranges[index];
+                   node.createAnimationRange(data.name, data.from, data.to);
+               }
+            }
+        }
     }
 }