Prechádzať zdrojové kódy

Merge pull request #865 from Palmer-JC/master

merge() refactored into 2 methods
David Catuhe 9 rokov pred
rodič
commit
ba039d92ba
1 zmenil súbory, kde vykonal 53 pridanie a 120 odobranie
  1. 53 120
      src/Mesh/babylon.mesh.vertexData.ts

+ 53 - 120
src/Mesh/babylon.mesh.vertexData.ts

@@ -231,135 +231,59 @@
         }
 
         public merge(other: VertexData): void {
-            var index: number;
             if (other.indices) {
                 if (!this.indices) {
                     this.indices = [];
                 }
 
                 var offset = this.positions ? this.positions.length / 3 : 0;
-                for (index = 0; index < other.indices.length; index++) {
+                for (var index = 0; index < other.indices.length; index++) {
                     //TODO check type - if Int32Array!
                     (<number[]>this.indices).push(other.indices[index] + offset);
                 }
             }
 
-            if (other.positions) {
-                if (!this.positions) {
-                    this.positions = [];
-                }
-
-                for (index = 0; index < other.positions.length; index++) {
-                    (<number[]>this.positions).push(other.positions[index]);
-                }
-            }
-
-            if (other.normals) {
-                if (!this.normals) {
-                    this.normals = [];
-                }
-                for (index = 0; index < other.normals.length; index++) {
-                    (<number[]>this.normals).push(other.normals[index]);
-                }
-            }
-
-            if (other.uvs) {
-                if (!this.uvs) {
-                    this.uvs = [];
-                }
-                for (index = 0; index < other.uvs.length; index++) {
-                    (<number[]>this.uvs).push(other.uvs[index]);
-                }
-            }
-
-            if (other.uvs2) {
-                if (!this.uvs2) {
-                    this.uvs2 = [];
-                }
-                for (index = 0; index < other.uvs2.length; index++) {
-                    (<number[]>this.uvs2).push(other.uvs2[index]);
-                }
-            }
-
-            if (other.uvs3) {
-                if (!this.uvs3) {
-                    this.uvs3 = [];
-                }
-                for (index = 0; index < other.uvs3.length; index++) {
-                    (<number[]>this.uvs3).push(other.uvs3[index]);
-                }
-            }
-
-            if (other.uvs4) {
-                if (!this.uvs4) {
-                    this.uvs4 = [];
-                }
-                for (index = 0; index < other.uvs4.length; index++) {
-                    (<number[]>this.uvs4).push(other.uvs4[index]);
-                }
-            }
-
-            if (other.uvs5) {
-                if (!this.uvs5) {
-                    this.uvs5 = [];
-                }
-                for (index = 0; index < other.uvs5.length; index++) {
-                    (<number[]>this.uvs5).push(other.uvs5[index]);
-                }
-            }
-
-            if (other.uvs6) {
-                if (!this.uvs6) {
-                    this.uvs6 = [];
-                }
-                for (index = 0; index < other.uvs6.length; index++) {
-                    (<number[]>this.uvs6).push(other.uvs6[index]);
-                }
-            }
+            this.positions = this._mergeElement(this.positions, other.positions);
+            this.normals   = this._mergeElement(this.normals  , other.normals  );
+            this.uvs       = this._mergeElement(this.uvs      , other.uvs      );
+            this.uvs2      = this._mergeElement(this.uvs2     , other.uvs2     );
+            this.uvs3      = this._mergeElement(this.uvs3     , other.uvs3     );
+            this.uvs4      = this._mergeElement(this.uvs4     , other.uvs4     );
+            this.uvs5      = this._mergeElement(this.uvs5     , other.uvs5     );
+            this.uvs6      = this._mergeElement(this.uvs6     , other.uvs6     );
+            this.colors    = this._mergeElement(this.colors   , other.colors   );
+            this.matricesIndices      = this._mergeElement(this.matricesIndices     , other.matricesIndices     );
+            this.matricesWeights      = this._mergeElement(this.matricesWeights     , other.matricesWeights     );
+            this.matricesIndicesExtra = this._mergeElement(this.matricesIndicesExtra, other.matricesIndicesExtra);
+            this.matricesWeightsExtra = this._mergeElement(this.matricesWeightsExtra, other.matricesWeightsExtra);
+        }
 
-            if (other.matricesIndices) {
-                if (!this.matricesIndices) {
-                    this.matricesIndices = [];
-                }
-                for (index = 0; index < other.matricesIndices.length; index++) {
-                    (<number[]>this.matricesIndices).push(other.matricesIndices[index]);
-                }
-            }
+        private _mergeElement(source : number[] | Float32Array, other : number[] | Float32Array) : number[] | Float32Array{
+            if (!other ) return source;
+            if (!source) return other;
 
-            if (other.matricesWeights) {
-                if (!this.matricesWeights) {
-                    this.matricesWeights = [];
-                }
-                for (index = 0; index < other.matricesWeights.length; index++) {
-                    (<number[]>this.matricesWeights).push(other.matricesWeights[index]);
-                }
-            }
+            var len = other.length + source.length;
+            var isSrcTypedArray = source instanceof Float32Array;
+            var isOthTypedArray = other  instanceof Float32Array;
 
-            if (other.matricesIndicesExtra) {
-                if (!this.matricesIndicesExtra) {
-                    this.matricesIndicesExtra = [];
-                }
-                for (index = 0; index < other.matricesIndicesExtra.length; index++) {
-                    (<number[]>this.matricesIndicesExtra).push(other.matricesIndicesExtra[index]);
-                }
-            }
+            // use non-loop method when the source is Float32Array
+            if(isSrcTypedArray) {
+               var ret32 = new Float32Array(len);
+               ret32.set(source);
+               ret32.set(other, source.length);
+               return ret32;
 
-            if (other.matricesWeightsExtra) {
-                if (!this.matricesWeightsExtra) {
-                    this.matricesWeightsExtra = [];
-                }
-                for (index = 0; index < other.matricesWeightsExtra.length; index++) {
-                    (<number[]>this.matricesWeightsExtra).push(other.matricesWeightsExtra[index]);
-                }
-            }
+            // source is number[], when other is also use concat
+            }else if (!isOthTypedArray) {
+                return (<number[]> source).concat(<number[]> other);
 
-            if (other.colors) {
-                if (!this.colors) {
-                    this.colors = [];
-                }
-                for (index = 0; index < other.colors.length; index++) {
-                    (<number[]>this.colors).push(other.colors[index]);
+            // source is a number[], but other is a Float32Array, loop required
+            } else {
+                var ret = (<number[]> source).slice(0); // copy source to a separate array
+                for (var i = 0, len = other.length; i < len; i++){
+                    ret.push(other[i]);
                 }
+                return ret;
             }
         }
 
@@ -411,6 +335,15 @@
                 serializationObject.matricesWeights = this.matricesWeights;
             }
 
+            if (this.matricesIndicesExtra) {
+                serializationObject.matricesIndicesExtra = this.matricesIndicesExtra;
+                serializationObject.matricesIndicesExtra._isExpanded = true;
+            }
+
+            if (this.matricesWeightsExtra) {
+                serializationObject.matricesWeightsExtra = this.matricesWeightsExtra;
+            }
+
             serializationObject.indices = this.indices;
 
             return serializationObject;
@@ -599,7 +532,7 @@
             }
 
 
-            // uvs            
+            // uvs
             var u: number;
             var v: number;
             for (p = 0; p < pathArray.length; p++) {
@@ -616,7 +549,7 @@
             var l1: number = lg[p] - 1;           		// path1 length
             var l2: number = lg[p + 1] - 1;         	// path2 length
             var min: number = (l1 < l2) ? l1 : l2;   	// current path stop index
-            var shft: number = idx[1] - idx[0];         // shift 
+            var shft: number = idx[1] - idx[0];         // shift
             var path1nb: number = closeArray ? lg.length : lg.length - 1;     // number of path1 to iterate	on
 
             while (pi <= min && p < path1nb) {       	//  stay under min and don't go over next to last path
@@ -664,7 +597,7 @@
                     normals[indexLast + 2] = normals[indexFirst + 2];
                 }
             }
-            
+
             // sides
             VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
 
@@ -858,7 +791,7 @@
             return vertexData;
         }
 
-        // Cylinder and cone 
+        // Cylinder and cone
         public static CreateCylinder(options: { height?: number, diameterTop?: number, diameterBottom?: number, diameter?: number, tessellation?: number, subdivisions?: number, arc?: number, faceColors?: Color4[], faceUV?: Vector4[], hasRings?: boolean, enclose?: boolean, sideOrientation?: number }): VertexData {
             var height: number = options.height || 2;
             var diameterTop: number = (options.diameterTop === 0) ? 0 : options.diameterTop || options.diameter || 1;
@@ -871,7 +804,7 @@
             var sideOrientation: number = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
             var faceUV: Vector4[] = options.faceUV || new Array<Vector4>(3);
             var faceColors: Color4[] = options.faceColors;
-    
+
             // default face colors and UV if undefined
             var quadNb: number = (arc !== 1 && enclose) ? 2 : 0;
             var ringNb: number = (hasRings) ? subdivisions : 1;
@@ -1085,7 +1018,7 @@
                     }
                 }
             };
-            
+
             // add caps to geometry
             createCylinderCap(false);
             createCylinderCap(true);
@@ -2080,7 +2013,7 @@
                 normals[i3 * 3 + 1] += faceNormaly;
                 normals[i3 * 3 + 2] += faceNormalz;
             }
-            
+
             // last normalization of each normal
             for (index = 0; index < normals.length / 3; index++) {
                 faceNormalx = normals[index * 3];
@@ -2127,7 +2060,7 @@
                     break;
 
                 case Mesh.DOUBLESIDE:
-                    // positions 
+                    // positions
                     var lp: number = positions.length;
                     var l: number = lp / 3;
                     for (var p = 0; p < lp; p++) {