Browse Source

Code cleanup

David `Deltakosh` Catuhe 5 years ago
parent
commit
fe47d2713d
1 changed files with 151 additions and 155 deletions
  1. 151 155
      src/Debug/skeletonViewer.ts

+ 151 - 155
src/Debug/skeletonViewer.ts

@@ -239,7 +239,7 @@ export class SkeletonViewer {
         }
         }
     }
     }
 
 
-    private _getLinesForBonesNoLength(bones: Bone[], meshMat: Matrix): void {
+    private _getLinesForBonesNoLength(bones: Bone[]): void {
         var len = bones.length;
         var len = bones.length;
         var boneNum = 0;
         var boneNum = 0;
 
 
@@ -273,192 +273,188 @@ export class SkeletonViewer {
 
 
     /** function to build and bind sphere joint points and spur bone representations. */
     /** function to build and bind sphere joint points and spur bone representations. */
     private _buildSpheresAndSpurs(spheresOnly = true): Promise<void> {
     private _buildSpheresAndSpurs(spheresOnly = true): Promise<void> {
-        if (this.debugMesh) {
-            this.debugMesh.dispose();
-        }
-        {
-            this._ready = false;
-            let scene = this.scene;
-            let bones: Bone[] = this.skeleton.bones;
-            let spheres: Mesh[] = [];
-            let spurs: Mesh[] = [];
-
-            return new Promise((resolve, reject) => {
-                try {
-                    this.dispose();
-                    if (this.options.pauseAnimations) {
-                        scene.animationsEnabled = false;
-                    }
-                    if (this.options.returnToRest) {
-                        this.skeleton.returnToRest();
-                    }
+        this.dispose();
 
 
-                    if (this.autoUpdateBonesMatrices) {
-                        this.skeleton.computeAbsoluteTransforms();
-                    }
+        this._ready = false;
+        let scene = this.scene;
+        let bones: Bone[] = this.skeleton.bones;
+        let spheres: Mesh[] = [];
+        let spurs: Mesh[] = [];
+
+        return new Promise((resolve, reject) => {
+            try {
+                if (this.options.pauseAnimations) {
+                    scene.animationsEnabled = false;
+                }
+
+                if (this.options.returnToRest) {
+                    this.skeleton.returnToRest();
+                }
 
 
-                    let longestBoneLength = Number.NEGATIVE_INFINITY;
-                    let getAbsoluteRestPose = function(bone: Nullable<Bone>, matrix: Matrix) {
+                if (this.autoUpdateBonesMatrices) {
+                    this.skeleton.computeAbsoluteTransforms();
+                }
+
+                let longestBoneLength = Number.NEGATIVE_INFINITY;
+                let getAbsoluteRestPose = function(bone: Nullable<Bone>, matrix: Matrix) {
                     if (bone == null) {
                     if (bone == null) {
                         matrix.copyFrom(Matrix.Identity());
                         matrix.copyFrom(Matrix.Identity());
                         return;
                         return;
                     }
                     }
-                        getAbsoluteRestPose(bone.getParent(), matrix);
-                        bone.getRestPose().multiplyToRef(matrix, matrix);
-                        return;
-                    };
+                    getAbsoluteRestPose(bone.getParent(), matrix);
+                    bone.getRestPose().multiplyToRef(matrix, matrix);
+                    return;
+                };
+
+                let displayOptions = this.options.displayOptions || {};
+
+                for (let i = 0; i < bones.length; i++) {
+                    let bone: Bone = bones[i];
+
+                    if (bone._index === null) {
+                        bone._index = i;
+                    }
+                    if (bone._index === -1) {
+                        continue;
+                    }
+
+                    let boneAbsoluteRestTransform = new Matrix();
+                    getAbsoluteRestPose(bone, boneAbsoluteRestTransform);
 
 
-                    let displayOptions = this.options.displayOptions || {};
+                    let anchorPoint = new Vector3();
+                    boneAbsoluteRestTransform.decompose(undefined, undefined, anchorPoint);
 
 
-                    for (let i = 0; i < bones.length; i++) {
-                        let bone: Bone = bones[i];
+                    bone.children.forEach((bc, i) => {
+                        let childAbsoluteRestTransform : Matrix = new Matrix();
+                        bc.getRestPose().multiplyToRef(boneAbsoluteRestTransform, childAbsoluteRestTransform);
+                        let childPoint = new Vector3();
+                        childAbsoluteRestTransform.decompose(undefined, undefined, childPoint);
 
 
-                        if (bone._index === null) {
-                            bone._index = i;
+                        let distanceFromParent = Vector3.Distance(anchorPoint, childPoint);
+
+                        if (distanceFromParent > longestBoneLength) {
+                            longestBoneLength = distanceFromParent;
                         }
                         }
-                        if (bone._index === -1) {
-                            continue;
+                        if (spheresOnly) {
+                            return;
                         }
                         }
 
 
-                        let boneAbsoluteRestTransform = new Matrix();
-                        getAbsoluteRestPose(bone, boneAbsoluteRestTransform);
-
-                        let anchorPoint = new Vector3();
-                        boneAbsoluteRestTransform.decompose(undefined, undefined, anchorPoint);
-
-                            bone.children.forEach((bc, i) => {
-                                let childAbsoluteRestTransform : Matrix = new Matrix();
-                                bc.getRestPose().multiplyToRef(boneAbsoluteRestTransform, childAbsoluteRestTransform);
-                                let childPoint = new Vector3();
-                                childAbsoluteRestTransform.decompose(undefined, undefined, childPoint);
-
-                                let distanceFromParent = Vector3.Distance(anchorPoint, childPoint);
-
-                                if (distanceFromParent > longestBoneLength) {
-                                    longestBoneLength = distanceFromParent;
-                                }
-                                if (spheresOnly) {
-                                    return;
-                                }
-
-                                let dir = childPoint.clone().subtract(anchorPoint.clone());
-                                let h = dir.length();
-                                let up = dir.normalize().scale(h);
-
-                                let midStep = displayOptions.midStep || 0.165;
-                                let midStepFactor = displayOptions.midStepFactor || 0.215;
-
-                                let up0 = up.scale(midStep);
-
-                                let spur = ShapeBuilder.ExtrudeShapeCustom(bc.name + ':spur',
-                                {
-                                    shape:  [
-                                                new Vector3(1, -1,  0),
-                                                new Vector3(1,  1,  0),
-                                                new Vector3(-1,  1,  0),
-                                                new Vector3(-1, -1,  0),
-                                                new Vector3(1, -1,  0)
-                                            ],
-                                    path:   [ Vector3.Zero(), up0, up ],
-                                    scaleFunction:
-                                            (i: number) => {
-                                                switch (i){
-                                                    case 0:
-                                                    case 2:
-                                                    return 0;
-                                                    case 1:
-                                                    return h * midStepFactor;
-                                                }
-                                                return 0;
-                                            },
-                                    sideOrientation: Mesh.DEFAULTSIDE,
-                                    updatable: true
-                                },  scene);
-
-                                let ind = spur.getIndices() || [];
-                                let mwk: number[] = [], mik: number[] = [];
-
-                                for (let i = 0; i < ind.length; i++) {
-                                    mwk.push(1, 0, 0, 0);
-                                    mik.push(bone.getIndex(), 0, 0, 0);
-                                }
-                                spur.convertToFlatShadedMesh();
-                                spur.position = anchorPoint.clone();
-
-                                spur.setVerticesData(VertexBuffer.MatricesWeightsKind, mwk, false);
-                                spur.setVerticesData(VertexBuffer.MatricesIndicesKind, mik, false);
-                                spurs.push(spur);
-
-                            });
-
-                        let sphereBaseSize = displayOptions.sphereBaseSize || 0.2;
-
-                        let sphere = SphereBuilder.CreateSphere(bone.name + ':sphere', {
-                            segments: 6,
-                            diameter: sphereBaseSize,
+                        let dir = childPoint.clone().subtract(anchorPoint.clone());
+                        let h = dir.length();
+                        let up = dir.normalize().scale(h);
+
+                        let midStep = displayOptions.midStep || 0.165;
+                        let midStepFactor = displayOptions.midStepFactor || 0.215;
+
+                        let up0 = up.scale(midStep);
+
+                        let spur = ShapeBuilder.ExtrudeShapeCustom(bc.name + ':spur',
+                        {
+                            shape:  [
+                                        new Vector3(1, -1,  0),
+                                        new Vector3(1,  1,  0),
+                                        new Vector3(-1,  1,  0),
+                                        new Vector3(-1, -1,  0),
+                                        new Vector3(1, -1,  0)
+                                    ],
+                            path:   [ Vector3.Zero(), up0, up ],
+                            scaleFunction:
+                                    (i: number) => {
+                                        switch (i){
+                                            case 0:
+                                            case 2:
+                                            return 0;
+                                            case 1:
+                                            return h * midStepFactor;
+                                        }
+                                        return 0;
+                                    },
+                            sideOrientation: Mesh.DEFAULTSIDE,
                             updatable: true
                             updatable: true
-                        }, scene);
+                        },  scene);
 
 
-                        let ind = sphere.getIndices() || [];
+                        let ind = spur.getIndices() || [];
                         let mwk: number[] = [], mik: number[] = [];
                         let mwk: number[] = [], mik: number[] = [];
 
 
                         for (let i = 0; i < ind.length; i++) {
                         for (let i = 0; i < ind.length; i++) {
                             mwk.push(1, 0, 0, 0);
                             mwk.push(1, 0, 0, 0);
                             mik.push(bone.getIndex(), 0, 0, 0);
                             mik.push(bone.getIndex(), 0, 0, 0);
                         }
                         }
+                        spur.convertToFlatShadedMesh();
+                        spur.position = anchorPoint.clone();
 
 
-                        sphere.setVerticesData(VertexBuffer.MatricesWeightsKind, mwk, false);
-                        sphere.setVerticesData(VertexBuffer.MatricesIndicesKind, mik, false);
-                        sphere.position = anchorPoint.clone();
-                        spheres.push(sphere);
+                        spur.setVerticesData(VertexBuffer.MatricesWeightsKind, mwk, false);
+                        spur.setVerticesData(VertexBuffer.MatricesIndicesKind, mik, false);
+                        spurs.push(spur);
 
 
-                    }
+                    });
 
 
-                    let skip = 0;
-                    let sphereScaleUnit = displayOptions.sphereScaleUnit || 2;
-                    let sphereFactor = displayOptions.sphereFactor || 0.85;
+                    let sphereBaseSize = displayOptions.sphereBaseSize || 0.2;
 
 
-                    for (let i = 0; i < bones.length; i++) {
-                        let bone: Nullable<Bone> = bones[i];
-                        if (bone.getIndex() === -1) {
-                            skip++;
-                            continue;
-                        }
-                        let sphere = spheres[i - skip];
-                        let scale = 1 / (sphereScaleUnit / longestBoneLength);
+                    let sphere = SphereBuilder.CreateSphere(bone.name + ':sphere', {
+                        segments: 6,
+                        diameter: sphereBaseSize,
+                        updatable: true
+                    }, scene);
 
 
-                        let _stepsOut = 0;
-                        let _b: Bone = (bone as Bone) || {};
+                    let ind = sphere.getIndices() || [];
+                    let mwk: number[] = [], mik: number[] = [];
 
 
-                        while ((_b.getParent()) && (_b.getParent() as Bone).getIndex() !== -1) {
-                            _stepsOut++;
-                            _b = (_b.getParent() as Bone);
-                        }
-                        sphere.scaling.scaleInPlace(scale * Math.pow(sphereFactor, _stepsOut));
+                    for (let i = 0; i < ind.length; i++) {
+                        mwk.push(1, 0, 0, 0);
+                        mik.push(bone.getIndex(), 0, 0, 0);
                     }
                     }
 
 
-                    this.debugMesh = Mesh.MergeMeshes(spheres.concat(spurs), true, true);
-                    if (this.debugMesh) {
-                        this.debugMesh.renderingGroupId = this.renderingGroupId;
-                        this.debugMesh.skeleton = this.skeleton;
-                        this.debugMesh.parent = this.mesh;
-                        this.debugMesh.computeBonesUsingShaders = this.options.computeBonesUsingShaders || true;
+                    sphere.setVerticesData(VertexBuffer.MatricesWeightsKind, mwk, false);
+                    sphere.setVerticesData(VertexBuffer.MatricesIndicesKind, mik, false);
+                    sphere.position = anchorPoint.clone();
+                    spheres.push(sphere);
+                }
+
+                let skip = 0;
+                let sphereScaleUnit = displayOptions.sphereScaleUnit || 2;
+                let sphereFactor = displayOptions.sphereFactor || 0.85;
+
+                for (let i = 0; i < bones.length; i++) {
+                    let bone: Nullable<Bone> = bones[i];
+                    if (bone.getIndex() === -1) {
+                        skip++;
+                        continue;
                     }
                     }
+                    let sphere = spheres[i - skip];
+                    let scale = 1 / (sphereScaleUnit / longestBoneLength);
+
+                    let _stepsOut = 0;
+                    let _b: Bone = (bone as Bone) || {};
 
 
-                    resolve();
-                }catch (err) {
-                    console.log(err);
-                    this._revert();
-                    this.dispose();
+                    while ((_b.getParent()) && (_b.getParent() as Bone).getIndex() !== -1) {
+                        _stepsOut++;
+                        _b = (_b.getParent() as Bone);
+                    }
+                    sphere.scaling.scaleInPlace(scale * Math.pow(sphereFactor, _stepsOut));
                 }
                 }
-            }).then(() => {
-                this._revert();
-                this.ready = true;
-            }).catch((err) => {
+
+                this.debugMesh = Mesh.MergeMeshes(spheres.concat(spurs), true, true);
+                if (this.debugMesh) {
+                    this.debugMesh.renderingGroupId = this.renderingGroupId;
+                    this.debugMesh.skeleton = this.skeleton;
+                    this.debugMesh.parent = this.mesh;
+                    this.debugMesh.computeBonesUsingShaders = this.options.computeBonesUsingShaders ?? true;
+                }
+
+                resolve();
+            } catch (err) {
                 console.log(err);
                 console.log(err);
+                this._revert();
                 this.dispose();
                 this.dispose();
-            });
-        }
+            }
+        }).then(() => {
+            this._revert();
+            this.ready = true;
+        }).catch((err) => {
+            console.log(err);
+            this.dispose();
+        });        
     }
     }
 
 
     /** Update the viewer to sync with current skeleton state, only used for the line display. */
     /** Update the viewer to sync with current skeleton state, only used for the line display. */
@@ -474,7 +470,7 @@ export class SkeletonViewer {
         let mesh = this.mesh._effectiveMesh;
         let mesh = this.mesh._effectiveMesh;
 
 
         if (this.skeleton.bones[0].length === undefined) {
         if (this.skeleton.bones[0].length === undefined) {
-            this._getLinesForBonesNoLength(this.skeleton.bones, mesh.getWorldMatrix());
+            this._getLinesForBonesNoLength(this.skeleton.bones);
         } else {
         } else {
             this._getLinesForBonesWithLength(this.skeleton.bones, mesh.getWorldMatrix());
             this._getLinesForBonesWithLength(this.skeleton.bones, mesh.getWorldMatrix());
         }
         }