فهرست منبع

Making optimize async

It will now run async using the SyncAsync Loop from tools.
Raanan Weber 10 سال پیش
والد
کامیت
1bfb8fb85c
2فایلهای تغییر یافته به همراه121 افزوده شده و 50 حذف شده
  1. 57 24
      Babylon/Mesh/babylon.mesh.js
  2. 64 26
      Babylon/Mesh/babylon.mesh.ts

+ 57 - 24
Babylon/Mesh/babylon.mesh.js

@@ -893,37 +893,70 @@ var BABYLON;
         };
         //The function doesn't delete unused vertex data, it simply restructure the indices to ignore them.
         Mesh.prototype.optimize = function (successCallback) {
+            var _this = this;
             var indices = this.getIndices();
             var positions = this.getVerticesData(BABYLON.VertexBuffer.PositionKind);
-            //optimize each submesh individually.
-            this.subMeshes.forEach(function (subMesh) {
-                var total = subMesh.indexStart + subMesh.indexCount;
-                var i;
-                var subMeshPositions = [];
-                for (i = subMesh.indexStart; i < total; ++i) {
-                    subMeshPositions[indices[i]] = subMeshPositions[indices[i]] || (BABYLON.Vector3.FromArray(positions, indices[i]));
-                }
-                var dupes = [];
-                for (i = subMeshPositions.length - 1; i >= 1; --i) {
-                    var testedPosition = subMeshPositions[i];
-					if(!testedPosition) continue;
-                    for (var j = 0; j < i; ++j) {
-                        var againstPosition = subMeshPositions[j];
-						if(!againstPosition) continue; 
-                        if (testedPosition.equals(againstPosition)) {
-                            dupes[i] = j;
-                            break;
-                        }
+            var vectorPositions = [];
+            for (var pos = 0; pos < positions.length; pos = pos + 3) {
+                vectorPositions.push(BABYLON.Vector3.FromArray(positions, pos));
+            }
+            var dupes = [];
+            BABYLON.AsyncLoop.SyncAsyncForLoop(vectorPositions.length, 40, function (iteration) {
+                var realPos = vectorPositions.length - 1 - iteration;
+                var testedPosition = vectorPositions[realPos];
+                for (var j = 0; j < realPos; ++j) {
+                    var againstPosition = vectorPositions[j];
+                    if (testedPosition.equals(againstPosition)) {
+                        dupes[realPos] = j;
+                        break;
                     }
                 }
-                for (i = subMesh.indexStart; i < total; ++i) {
+            }, function () {
+                for (var i = 0; i < indices.length; ++i) {
                     indices[i] = dupes[indices[i]] || indices[i];
                 }
+                //indices are now reordered
+                var originalSubMeshes = _this.subMeshes.slice(0);
+                _this.setIndices(indices);
+                _this.subMeshes = originalSubMeshes;
+                if (successCallback) {
+                    successCallback(_this);
+                }
             });
-            //indices are now reordered
-            var originalSubMeshes = this.subMeshes.slice(0);
-            this.setIndices(indices);
-            this.subMeshes = originalSubMeshes;
+            //for (var i = vectorPositions.length - 1; i >= 1; --i) {
+            //    var testedPosition = vectorPositions[i];
+            //    for (var j = 0; j < i; ++j) {
+            //        var againstPosition = vectorPositions[j];
+            //        if (testedPosition.equals(againstPosition)) {
+            //            dupes[i] = j;
+            //            break;
+            //        }
+            //    }
+            //}
+            //optimize each submesh individually.
+            //this.subMeshes.forEach(subMesh => {
+            //    var total = subMesh.indexStart + subMesh.indexCount;
+            //    var i;
+            //    var subMeshPositions: Array<Vector3> = []
+            //    for (i = subMesh.indexStart; i < total; ++i) {
+            //        subMeshPositions[indices[i]] = subMeshPositions[indices[i]] || (BABYLON.Vector3.FromArray(positions, indices[i]));
+            //    }
+            //    var dupes = [];
+            //    //find duplicates
+            //    for (i = subMeshPositions.length - 1; i >= 1; --i) {
+            //        var testedPosition = subMeshPositions[i];
+            //        for (var j = 0; j < i; ++j) {
+            //            var againstPosition = subMeshPositions[j];
+            //            if (testedPosition.equals(againstPosition)) {
+            //                dupes[i] = j;
+            //                break;
+            //            }
+            //        }
+            //    }
+            //    for (i = subMesh.indexStart; i < total; ++i) {
+            //        indices[i] = dupes[indices[i]] || indices[i];
+            //    }
+            //});
         };
         // Statics
         Mesh.CreateRibbon = function (name, pathArray, closeArray, closePath, offset, scene, updatable, sideOrientation) {

+ 64 - 26
Babylon/Mesh/babylon.mesh.ts

@@ -1094,37 +1094,75 @@
         public optimize(successCallback?: (mesh?: Mesh) => void) {
             var indices = this.getIndices();
             var positions = this.getVerticesData(VertexBuffer.PositionKind);
-            
-            //optimize each submesh individually.
-            this.subMeshes.forEach(subMesh => {
-                var total = subMesh.indexStart + subMesh.indexCount;
-                var i;
-                var subMeshPositions: Array<Vector3> = []
-                for (i = subMesh.indexStart; i < total; ++i) {
-                    subMeshPositions[indices[i]] = subMeshPositions[indices[i]] || (BABYLON.Vector3.FromArray(positions, indices[i]));
-                }
-                var dupes = [];
-                //find duplicates
-                for (i = subMeshPositions.length - 1; i >= 1; --i) {
-                    var testedPosition = subMeshPositions[i];
-                    for (var j = 0; j < i; ++j) {
-                        var againstPosition = subMeshPositions[j];
-                        if (testedPosition.equals(againstPosition)) {
-                            dupes[i] = j;
-                            break;
-                        }
+            var vectorPositions = [];
+            for (var pos = 0; pos < positions.length; pos = pos + 3) {
+                vectorPositions.push(BABYLON.Vector3.FromArray(positions, pos));
+            }
+            var dupes = [];
+
+            AsyncLoop.SyncAsyncForLoop(vectorPositions.length, 40, function (iteration) {
+                var realPos = vectorPositions.length - 1 - iteration;
+                var testedPosition = vectorPositions[realPos];
+                for (var j = 0; j < realPos; ++j) {
+                    var againstPosition = vectorPositions[j];
+                    if (testedPosition.equals(againstPosition)) {
+                        dupes[realPos] = j;
+                        break;
                     }
                 }
-
-                
-                for (i = subMesh.indexStart; i < total; ++i) {
+            },  () => {
+                for (var i = 0; i < indices.length; ++i) {
                     indices[i] = dupes[indices[i]] || indices[i];
                 }
+
+                //indices are now reordered
+                var originalSubMeshes = this.subMeshes.slice(0);
+                this.setIndices(indices);
+                this.subMeshes = originalSubMeshes;
+                if (successCallback) {
+                    successCallback(this);
+                }
             });
-            //indices are now reordered
-            var originalSubMeshes = this.subMeshes.slice(0);
-            this.setIndices(indices);
-            this.subMeshes = originalSubMeshes;
+
+            //for (var i = vectorPositions.length - 1; i >= 1; --i) {
+            //    var testedPosition = vectorPositions[i];
+            //    for (var j = 0; j < i; ++j) {
+            //        var againstPosition = vectorPositions[j];
+            //        if (testedPosition.equals(againstPosition)) {
+            //            dupes[i] = j;
+            //            break;
+            //        }
+            //    }
+            //}
+            
+            //optimize each submesh individually.
+            //this.subMeshes.forEach(subMesh => {
+            //    var total = subMesh.indexStart + subMesh.indexCount;
+            //    var i;
+            //    var subMeshPositions: Array<Vector3> = []
+            //    for (i = subMesh.indexStart; i < total; ++i) {
+            //        subMeshPositions[indices[i]] = subMeshPositions[indices[i]] || (BABYLON.Vector3.FromArray(positions, indices[i]));
+            //    }
+            //    var dupes = [];
+            //    //find duplicates
+            //    for (i = subMeshPositions.length - 1; i >= 1; --i) {
+            //        var testedPosition = subMeshPositions[i];
+            //        for (var j = 0; j < i; ++j) {
+            //            var againstPosition = subMeshPositions[j];
+            //            if (testedPosition.equals(againstPosition)) {
+            //                dupes[i] = j;
+            //                break;
+            //            }
+            //        }
+            //    }
+
+                
+            //    for (i = subMesh.indexStart; i < total; ++i) {
+            //        indices[i] = dupes[indices[i]] || indices[i];
+            //    }
+            //});
+
+            
         }
 
         // Statics