Browse Source

Optimizing mesh simplification

After adding the optimize function, the needed fixes for the
simplification are added here.
This will fix the missing triangles problem (that is mainly caused by
non-optimized meshes).
The simplification will run faster as well, as the checks are not
simplified as well.
Raanan Weber 10 năm trước cách đây
mục cha
commit
15ae46c812

+ 13 - 7
Babylon/Mesh/babylon.meshSimplification.js

@@ -92,6 +92,7 @@ var BABYLON;
             this.error = new Array(4);
             this.error = new Array(4);
             this.deleted = false;
             this.deleted = false;
             this.isDirty = false;
             this.isDirty = false;
+            this.deletePending = false;
             this.borderFactor = 0;
             this.borderFactor = 0;
         }
         }
         return DecimationTriangle;
         return DecimationTriangle;
@@ -259,7 +260,16 @@ var BABYLON;
                                     continue;
                                     continue;
                                 if (_this.isFlipped(v1, i0, p, deleted1, t.borderFactor, delTr))
                                 if (_this.isFlipped(v1, i0, p, deleted1, t.borderFactor, delTr))
                                     continue;
                                     continue;
-                                if (delTr.length == 2 || delTr[0] === delTr[1]) {
+                                if (deleted0.indexOf(true) < 0 || deleted1.indexOf(true) < 0)
+                                    continue;
+                                var uniqueArray = [];
+                                delTr.forEach(function (deletedT) {
+                                    if (uniqueArray.indexOf(deletedT) === -1) {
+                                        deletedT.deletePending = true;
+                                        uniqueArray.push(deletedT);
+                                    }
+                                });
+                                if (uniqueArray.length % 2 != 0) {
                                     continue;
                                     continue;
                                 }
                                 }
                                 v0.normal = n;
                                 v0.normal = n;
@@ -268,10 +278,6 @@ var BABYLON;
                                 else if (v0.color)
                                 else if (v0.color)
                                     v0.color = color;
                                     v0.color = color;
                                 v0.q = v1.q.add(v0.q);
                                 v0.q = v1.q.add(v0.q);
-                                if (deleted0.indexOf(true) < 0 || deleted1.indexOf(true) < 0)
-                                    continue;
-                                if (p.equals(v0.position))
-                                    continue;
                                 v0.position = p;
                                 v0.position = p;
                                 var tStart = _this.references.length;
                                 var tStart = _this.references.length;
                                 deletedTriangles = _this.updateTriangles(v0.id, v0, deleted0, deletedTriangles);
                                 deletedTriangles = _this.updateTriangles(v0.id, v0, deleted0, deletedTriangles);
@@ -405,7 +411,7 @@ var BABYLON;
                     this.vertices[dst].normal = this.vertices[i].normal;
                     this.vertices[dst].normal = this.vertices[i].normal;
                     this.vertices[dst].uv = this.vertices[i].uv;
                     this.vertices[dst].uv = this.vertices[i].uv;
                     this.vertices[dst].color = this.vertices[i].color;
                     this.vertices[dst].color = this.vertices[i].color;
-                    newVerticesOrder.push(i);
+                    newVerticesOrder.push(dst);
                     dst++;
                     dst++;
                 }
                 }
             }
             }
@@ -505,7 +511,7 @@ var BABYLON;
                 var t = this.triangles[ref.triangleId];
                 var t = this.triangles[ref.triangleId];
                 if (t.deleted)
                 if (t.deleted)
                     continue;
                     continue;
-                if (deletedArray[i]) {
+                if (deletedArray[i] && t.deletePending) {
                     t.deleted = true;
                     t.deleted = true;
                     newDeleted++;
                     newDeleted++;
                     continue;
                     continue;

+ 16 - 6
Babylon/Mesh/babylon.meshSimplification.ts

@@ -129,11 +129,13 @@
         public deleted: boolean;
         public deleted: boolean;
         public isDirty: boolean;
         public isDirty: boolean;
         public borderFactor: number;
         public borderFactor: number;
+        public deletePending: boolean;
 
 
         constructor(public vertices: Array<number>) {
         constructor(public vertices: Array<number>) {
             this.error = new Array<number>(4);
             this.error = new Array<number>(4);
             this.deleted = false;
             this.deleted = false;
             this.isDirty = false;
             this.isDirty = false;
+            this.deletePending = false;
             this.borderFactor = 0;
             this.borderFactor = 0;
         }
         }
     }
     }
@@ -332,7 +334,18 @@
                                 if (this.isFlipped(v0, i1, p, deleted0, t.borderFactor, delTr)) continue;
                                 if (this.isFlipped(v0, i1, p, deleted0, t.borderFactor, delTr)) continue;
                                 if (this.isFlipped(v1, i0, p, deleted1, t.borderFactor, delTr)) continue;
                                 if (this.isFlipped(v1, i0, p, deleted1, t.borderFactor, delTr)) continue;
 
 
-                                if (delTr.length == 2 || delTr[0] === delTr[1]) {
+                                if (deleted0.indexOf(true) < 0 || deleted1.indexOf(true) < 0)
+                                    continue;
+
+                                var uniqueArray = [];
+                                delTr.forEach(function (deletedT) {
+                                    if (uniqueArray.indexOf(deletedT) === -1) {
+                                        deletedT.deletePending = true;
+                                        uniqueArray.push(deletedT);
+                                    }
+                                });
+
+                                if (uniqueArray.length % 2 != 0) {
                                     continue;
                                     continue;
                                 }
                                 }
 
 
@@ -343,9 +356,6 @@
                                     v0.color = color;
                                     v0.color = color;
                                 v0.q = v1.q.add(v0.q);
                                 v0.q = v1.q.add(v0.q);
 
 
-                                if (deleted0.indexOf(true) < 0 || deleted1.indexOf(true) < 0) continue;
-
-                                if (p.equals(v0.position)) continue;
                                 v0.position = p;
                                 v0.position = p;
 
 
                                 var tStart = this.references.length;
                                 var tStart = this.references.length;
@@ -488,7 +498,7 @@
                     this.vertices[dst].normal = this.vertices[i].normal;
                     this.vertices[dst].normal = this.vertices[i].normal;
                     this.vertices[dst].uv = this.vertices[i].uv;
                     this.vertices[dst].uv = this.vertices[i].uv;
                     this.vertices[dst].color = this.vertices[i].color;
                     this.vertices[dst].color = this.vertices[i].color;
-                    newVerticesOrder.push(i);
+                    newVerticesOrder.push(dst);
                     dst++;
                     dst++;
                 }
                 }
             }
             }
@@ -601,7 +611,7 @@
                 var ref = this.references[vertex.triangleStart + i];
                 var ref = this.references[vertex.triangleStart + i];
                 var t = this.triangles[ref.triangleId];
                 var t = this.triangles[ref.triangleId];
                 if (t.deleted) continue;
                 if (t.deleted) continue;
-                if (deletedArray[i]) {
+                if (deletedArray[i] && t.deletePending) {
                     t.deleted = true;
                     t.deleted = true;
                     newDeleted++;
                     newDeleted++;
                     continue;
                     continue;