Ver código fonte

Merge pull request #2682 from bghgary/bone-index-remap

Allow bone index remapping
David Catuhe 8 anos atrás
pai
commit
14859b60c8
2 arquivos alterados com 41 adições e 4 exclusões
  1. 8 3
      src/Bones/babylon.bone.ts
  2. 33 1
      src/Bones/babylon.skeleton.ts

+ 8 - 3
src/Bones/babylon.bone.ts

@@ -11,6 +11,10 @@ module BABYLON {
         public animations = new Array<Animation>();
         public length: number;
 
+        // Set this value to map this bone to a different index in the transform matrices.
+        // Set this value to -1 to exclude the bone from the transform matrices.
+        public _index: number;
+
         private _skeleton: Skeleton;
         private _localMatrix: Matrix;
         private _restPose: Matrix;
@@ -37,12 +41,13 @@ module BABYLON {
             }
         }
 
-        constructor(public name: string, skeleton: Skeleton, parentBone: Bone = null, matrix?: Matrix, restPose?: Matrix) {
+        constructor(public name: string, skeleton: Skeleton, parentBone: Bone = null, localMatrix?: Matrix, restPose?: Matrix, baseMatrix?: Matrix, index?: number) {
             super(name, skeleton.getScene());
             this._skeleton = skeleton;
-            this._localMatrix = matrix ? matrix : Matrix.Identity();
-            this._baseMatrix = this._localMatrix.clone();
+            this._localMatrix = localMatrix ? localMatrix : Matrix.Identity();
             this._restPose = restPose ? restPose : this._localMatrix.clone();
+            this._baseMatrix = baseMatrix ? baseMatrix : this._localMatrix.clone();
+            this._index = index === undefined ? undefined : index;
 
             skeleton.bones.push(this);
 

+ 33 - 1
src/Bones/babylon.skeleton.ts

@@ -232,7 +232,10 @@
                     }
                 }
 
-                bone.getInvertedAbsoluteTransform().multiplyToArray(bone.getWorldMatrix(), targetMatrix, index * 16);
+                if (bone._index !== -1) {
+                    var mappedIndex = bone._index === undefined ? index : bone._index;
+                    bone.getInvertedAbsoluteTransform().multiplyToArray(bone.getWorldMatrix(), targetMatrix, bone._index * 16);
+                }
             }
 
             this._identity.copyToArray(targetMatrix, this.bones.length * 16);
@@ -448,5 +451,34 @@
 
         }
 
+        public sortBones(): void {
+            var bones = new Array<Bone>();
+            var visited = new Array<boolean>(this.bones.length);
+            for (var index = 0; index < this.bones.length; index++) {
+                this._sortBones(index, bones, visited);
+            }
+
+            this.bones = bones;
+        }
+
+        private _sortBones(index: number, bones: Bone[], visited: boolean[]): void {
+            if (visited[index]) {
+                return;
+            }
+
+            visited[index] = true;
+
+            var bone = this.bones[index];
+            if (bone._index === undefined) {
+                bone._index = index;
+            }
+
+            var parentBone = bone.getParent();
+            if (parentBone) {
+                this._sortBones(this.bones.indexOf(parentBone), bones, visited);
+            }
+
+            bones.push(bone);
+        }
     }
 }