浏览代码

Fix morphTarget nullablility

Gary Hsu 7 年之前
父节点
当前提交
aab8715a36
共有 3 个文件被更改,包括 50 次插入32 次删除
  1. 15 5
      src/Mesh/babylon.mesh.ts
  2. 18 14
      src/Morph/babylon.morphTarget.ts
  3. 17 13
      src/Morph/babylon.morphTargetManager.ts

+ 15 - 5
src/Mesh/babylon.mesh.ts

@@ -2182,13 +2182,23 @@
 
                 for (var index = 0; index < morphTargetManager.numInfluencers; index++) {
                     var morphTarget = morphTargetManager.getActiveTarget(index);
-                    this.geometry.setVerticesData(VertexBuffer.PositionKind + index, morphTarget.getPositions(), false, 3);
 
-                    if (morphTarget.hasNormals) {
-                        this.geometry.setVerticesData(VertexBuffer.NormalKind + index, morphTarget.getNormals(), false, 3);
+                    const positions = morphTarget.getPositions(); 
+                    if (!positions) {
+                        Tools.Error("Invalid morph target. Target must have positions.");
+                        return;
                     }
-                    if (morphTarget.hasTangents) {
-                        this.geometry.setVerticesData(VertexBuffer.TangentKind + index, morphTarget.getTangents(), false, 3);
+
+                    this.geometry.setVerticesData(VertexBuffer.PositionKind + index, positions, false, 3);
+
+                    const normals = morphTarget.getNormals();
+                    if (normals) {
+                        this.geometry.setVerticesData(VertexBuffer.NormalKind + index, normals, false, 3);
+                    }
+
+                    const tangents = morphTarget.getTangents();
+                    if (tangents) {
+                        this.geometry.setVerticesData(VertexBuffer.TangentKind + index, tangents, false, 3);
                     }
                 }
             } else {

+ 18 - 14
src/Morph/babylon.morphTarget.ts

@@ -2,9 +2,9 @@ module BABYLON {
     export class MorphTarget {
         public animations = new Array<Animation>();
 
-        private _positions: Float32Array;
-        private _normals: Float32Array;
-        private _tangents: Float32Array;
+        private _positions: Nullable<FloatArray> = null;
+        private _normals: Nullable<FloatArray> = null;
+        private _tangents: Nullable<FloatArray> = null;
         private _influence: number;
 
         public onInfluenceChanged = new Observable<boolean>();
@@ -30,35 +30,39 @@ module BABYLON {
             this.influence = influence;
         }
 
+        public get hasPositions(): boolean {
+            return !!this._positions;
+        }
+
         public get hasNormals(): boolean {
-            return this._normals !== undefined;
+            return !!this._normals;
         }
 
         public get hasTangents(): boolean {
-            return this._tangents !== undefined;
+            return !!this._tangents;
         }
 
-        public setPositions(data: Float32Array | number[]) {
-            this._positions = new Float32Array(data);
+        public setPositions(data: Nullable<FloatArray>) {
+            this._positions = data;
         }
 
-        public getPositions(): Float32Array {
+        public getPositions(): Nullable<FloatArray> {
             return this._positions;
         }
 
-        public setNormals(data: Float32Array | number[]) {
-            this._normals = new Float32Array(data);
+        public setNormals(data: Nullable<FloatArray>) {
+            this._normals = data;
         }
 
-        public getNormals(): Float32Array {
+        public getNormals(): Nullable<FloatArray> {
             return this._normals;
         }
 
-        public setTangents(data: Float32Array | number[]) {
-            this._tangents = new Float32Array(data);
+        public setTangents(data: Nullable<FloatArray>) {
+            this._tangents = data;
         }
 
-        public getTangents(): Float32Array {
+        public getTangents(): Nullable<FloatArray> {
             return this._tangents;
         }
 

+ 17 - 13
src/Morph/babylon.morphTargetManager.ts

@@ -60,20 +60,13 @@ module BABYLON {
         public getTarget(index: number): MorphTarget {
             return this._targets[index];
         }
-       
-        public addTarget(target: MorphTarget): void {
-            if (this._vertexCount) {
-                if (this._vertexCount !== target.getPositions().length / 3) {
-                    Tools.Error("Incompatible target. Targets must all have the same vertices count.");
-                    return;
-                }
-            }
 
+        public addTarget(target: MorphTarget): void {
             this._targets.push(target);
             this._targetObservable.push(target.onInfluenceChanged.add(needUpdate => {
                 this._syncActiveTargets(needUpdate);
             }));
-            this._syncActiveTargets(true);        
+            this._syncActiveTargets(true);
         }
 
         public removeTarget(target: MorphTarget): void {
@@ -82,7 +75,6 @@ module BABYLON {
                 this._targets.splice(index, 1);
 
                 target.onInfluenceChanged.remove(this._targetObservable.splice(index, 1)[0]);
-                this._vertexCount = 0;
                 this._syncActiveTargets(true);
             }
         }
@@ -106,9 +98,10 @@ module BABYLON {
 
         private _syncActiveTargets(needUpdate: boolean): void {
             let influenceCount = 0;
-            this._activeTargets.reset();            
+            this._activeTargets.reset();
             this._supportsNormals = true;
             this._supportsTangents = true;
+            this._vertexCount = 0;
             for (var target of this._targets) {
                 if (target.influence > 0) {
                     this._activeTargets.push(target);
@@ -117,8 +110,19 @@ module BABYLON {
                     this._supportsNormals = this._supportsNormals && target.hasNormals;
                     this._supportsTangents = this._supportsTangents && target.hasTangents;
 
+                    const positions = target.getPositions();
+                    if (!positions) {
+                        Tools.Error("Invalid target. Target must positions.");
+                        return;
+                    }
+
+                    const vertexCount = positions.length / 3;
                     if (this._vertexCount === 0) {
-                        this._vertexCount = target.getPositions().length / 3;
+                        this._vertexCount = vertexCount;
+                    }
+                    else if (this._vertexCount !== vertexCount) {
+                        Tools.Error("Incompatible target. Targets must all have the same vertices count.");
+                        return;
                     }
                 }
             }
@@ -130,7 +134,7 @@ module BABYLON {
             for (var index = 0; index < influenceCount; index++) {
                 this._influences[index] = this._tempInfluences[index];
             }
-            
+
             if (needUpdate && this._scene) {
                 // Flag meshes as dirty to resync with the active targets
                 for (var mesh of this._scene.meshes) {