浏览代码

Merge pull request #2199 from Kesshi/position-cache

moved position array cache from mesh to geometry to save memory
David Catuhe 8 年之前
父节点
当前提交
0f5aad1ddf
共有 3 个文件被更改,包括 48 次插入21 次删除
  1. 4 1
      src/Mesh/babylon.abstractMesh.ts
  2. 31 4
      src/Mesh/babylon.geometry.ts
  3. 13 16
      src/Mesh/babylon.mesh.ts

+ 4 - 1
src/Mesh/babylon.abstractMesh.ts

@@ -294,7 +294,6 @@
         private _absolutePosition = Vector3.Zero();
         private _collisionsTransformMatrix = Matrix.Zero();
         private _collisionsScalingMatrix = Matrix.Zero();
-        public _positions: Vector3[];
         private _isDirty = false;
         public _masterMesh: AbstractMesh;
 
@@ -315,6 +314,10 @@
 
         public _lightSources = new Array<Light>();
 
+        public get _positions(): Vector3[] {
+            return null;
+        }
+
         // Loading properties
         public _waitingActions: any;
         public _waitingFreezeWorldMatrix: boolean;

+ 31 - 4
src/Mesh/babylon.geometry.ts

@@ -23,6 +23,9 @@
         public _softwareSkinningRenderId: number;
         private _vertexArrayObjects: { [key: string]: WebGLVertexArrayObject; };
 
+        // Cache
+        public _positions: Vector3[];
+
         /**
          *  The Bias Vector to apply on the bounding elements (box/sphere), the max extend is computed as v += v * bias.x + bias.y, the min is computed as v -= v * bias.x + bias.y
          * @returns The Bias Vector
@@ -134,13 +137,13 @@
                 this._totalVertices = data.length / stride;
 
                 this.updateExtend(data, stride);
+                this._resetPointsArrayCache();
 
                 var meshes = this._meshes;
                 var numOfMeshes = meshes.length;
 
                 for (var index = 0; index < numOfMeshes; index++) {
                     var mesh = meshes[index];
-                    mesh._resetPointsArrayCache();
                     mesh._boundingInfo = new BoundingInfo(this._extend.minimum, this._extend.maximum);
                     mesh._createGlobalSubMesh();
                     mesh.computeWorldMatrix(true);
@@ -192,10 +195,10 @@
 
             var meshes = this._meshes;
             var numOfMeshes = meshes.length;
+            this._resetPointsArrayCache();
 
             for (var index = 0; index < numOfMeshes; index++) {
                 var mesh = meshes[index];
-                mesh._resetPointsArrayCache();
                 if (updateExtends) {
                     mesh._boundingInfo = new BoundingInfo(this._extend.minimum, this._extend.maximum);
 
@@ -424,8 +427,6 @@
                     buffer.references = numOfMeshes;
 
                 if (kind === VertexBuffer.PositionKind) {
-                    mesh._resetPointsArrayCache();
-
                     if (!this._extend) {
                         this.updateExtend(this._vertexBuffers[kind].getData());
                     }
@@ -531,6 +532,32 @@
             }
         }
 
+        // Cache
+        public _resetPointsArrayCache(): void
+        {
+            this._positions = null;
+        }
+
+        public _generatePointsArray(): boolean
+        {
+            if (this._positions)
+                return true;
+
+            this._positions = [];
+
+            var data = this.getVerticesData(VertexBuffer.PositionKind);
+
+            if (!data) {
+                return false;
+            }
+
+            for (var index = 0; index < data.length; index += 3) {
+                this._positions.push(Vector3.FromArray(data, index));
+            }
+
+            return true;
+        }
+
         public isDisposed(): boolean {
             return this._isDisposed;
         }

+ 13 - 16
src/Mesh/babylon.mesh.ts

@@ -1391,28 +1391,25 @@
         }
 
         // Cache
+        public get _positions(): Vector3[] {
+            if (this._geometry) {
+                return this._geometry._positions;
+            }
+            return null;
+        }
+
         public _resetPointsArrayCache(): Mesh {
-            this._positions = null;
+            if (this._geometry) {
+                this._geometry._resetPointsArrayCache();
+            }
             return this;
         }
 
         public _generatePointsArray(): boolean {
-            if (this._positions)
-                return true;
-
-            this._positions = [];
-
-            var data = this.getVerticesData(VertexBuffer.PositionKind);
-
-            if (!data) {
-                return false;
-            }
-
-            for (var index = 0; index < data.length; index += 3) {
-                this._positions.push(Vector3.FromArray(data, index));
+            if (this._geometry) {
+                return this._geometry._generatePointsArray();
             }
-
-            return true;
+            return false;
         }
 
         /**