瀏覽代碼

SubMeshes simplification

SubMeshes are now being simplified one after the other.
Raanan Weber 10 年之前
父節點
當前提交
48677d47be

+ 10 - 6
Babylon/Mesh/babylon.mesh.js

@@ -872,14 +872,18 @@ var BABYLON;
          * successCallback optional success callback to be called after the simplification finished processing all settings.
          */
         Mesh.prototype.simplify = function (settings, parallelProcessing, simplificationType, successCallback) {
+            var _this = this;
             if (parallelProcessing === void 0) { parallelProcessing = true; }
             if (simplificationType === void 0) { simplificationType = BABYLON.SimplificationType.QUADRATIC; }
-            this.getScene().simplificationQueue.addTask({
-                settings: settings,
-                parallelProcessing: parallelProcessing,
-                mesh: this,
-                simplificationType: simplificationType,
-                successCallback: successCallback
+            this.subMeshes.forEach(function (submesh, index) {
+                _this.getScene().simplificationQueue.addTask({
+                    settings: settings,
+                    parallelProcessing: parallelProcessing,
+                    mesh: _this,
+                    submeshIndex: index,
+                    simplificationType: simplificationType,
+                    successCallback: successCallback
+                });
             });
         };
         // Statics

+ 10 - 7
Babylon/Mesh/babylon.mesh.ts

@@ -1070,13 +1070,16 @@
          * successCallback optional success callback to be called after the simplification finished processing all settings.
          */
         public simplify(settings: Array<ISimplificationSettings>, parallelProcessing: boolean = true, simplificationType: SimplificationType = SimplificationType.QUADRATIC, successCallback?: () => void) {
-            this.getScene().simplificationQueue.addTask({
-                settings: settings,
-                parallelProcessing: parallelProcessing,
-                mesh: this,
-                simplificationType: simplificationType,
-                successCallback: successCallback
-            });            
+            this.subMeshes.forEach((submesh, index) => {
+                this.getScene().simplificationQueue.addTask({
+                    settings: settings,
+                    parallelProcessing: parallelProcessing,
+                    mesh: this,
+                    submeshIndex:index,
+                    simplificationType: simplificationType,
+                    successCallback: successCallback
+                });  
+            }); 
         }
 
         // Statics

+ 16 - 10
Babylon/Mesh/babylon.meshSimplification.js

@@ -70,7 +70,7 @@ var BABYLON;
             switch (task.simplificationType) {
                 case 0 /* QUADRATIC */:
                 default:
-                    return new QuadraticErrorSimplification(task.mesh);
+                    return new QuadraticErrorSimplification(task.mesh, task.submeshIndex);
             }
         };
         return SimplificationQueue;
@@ -167,8 +167,10 @@ var BABYLON;
      * @author RaananW
      */
     var QuadraticErrorSimplification = (function () {
-        function QuadraticErrorSimplification(_mesh) {
+        function QuadraticErrorSimplification(_mesh, _submeshIndex) {
+            if (_submeshIndex === void 0) { _submeshIndex = 0; }
             this._mesh = _mesh;
+            this._submeshIndex = _submeshIndex;
             this.initialised = false;
             this.syncIterations = 5000;
             this.aggressiveness = 7;
@@ -177,7 +179,7 @@ var BABYLON;
         }
         QuadraticErrorSimplification.prototype.simplify = function (settings, successCallback) {
             var _this = this;
-            this.initWithMesh(this._mesh, function () {
+            this.initWithMesh(this._mesh, this._submeshIndex, function () {
                 _this.runDecimation(settings, successCallback);
             });
         };
@@ -298,7 +300,7 @@ var BABYLON;
                 }, 0);
             });
         };
-        QuadraticErrorSimplification.prototype.initWithMesh = function (mesh, callback) {
+        QuadraticErrorSimplification.prototype.initWithMesh = function (mesh, submeshIndex, callback) {
             var _this = this;
             if (!mesh)
                 return;
@@ -311,27 +313,31 @@ var BABYLON;
             var uvs = this._mesh.getVerticesData(BABYLON.VertexBuffer.UVKind);
             var colorsData = this._mesh.getVerticesData(BABYLON.VertexBuffer.ColorKind);
             var indices = mesh.getIndices();
+            var submesh = mesh.subMeshes[submeshIndex];
             var vertexInit = function (i) {
-                var vertex = new DecimationVertex(BABYLON.Vector3.FromArray(positionData, i * 3), BABYLON.Vector3.FromArray(normalData, i * 3), null, i);
+                var offset = i + submesh.verticesStart;
+                var vertex = new DecimationVertex(BABYLON.Vector3.FromArray(positionData, offset * 3), BABYLON.Vector3.FromArray(normalData, offset * 3), null, i);
                 if (_this._mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
-                    vertex.uv = BABYLON.Vector2.FromArray(uvs, i * 2);
+                    vertex.uv = BABYLON.Vector2.FromArray(uvs, offset * 2);
                 }
                 else if (_this._mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
-                    vertex.color = BABYLON.Color4.FromArray(colorsData, i * 4);
+                    vertex.color = BABYLON.Color4.FromArray(colorsData, offset * 4);
                 }
                 _this.vertices.push(vertex);
             };
-            var totalVertices = mesh.getTotalVertices();
+            //var totalVertices = mesh.getTotalVertices();
+            var totalVertices = submesh.verticesCount;
             BABYLON.AsyncLoop.SyncAsyncForLoop(totalVertices, this.syncIterations, vertexInit, function () {
                 var indicesInit = function (i) {
-                    var pos = i * 3;
+                    var offset = submesh.indexStart + i;
+                    var pos = offset * 3;
                     var i0 = indices[pos + 0];
                     var i1 = indices[pos + 1];
                     var i2 = indices[pos + 2];
                     var triangle = new DecimationTriangle([_this.vertices[i0].id, _this.vertices[i1].id, _this.vertices[i2].id]);
                     _this.triangles.push(triangle);
                 };
-                BABYLON.AsyncLoop.SyncAsyncForLoop(indices.length / 3, _this.syncIterations, indicesInit, function () {
+                BABYLON.AsyncLoop.SyncAsyncForLoop(submesh.indexCount / 3, _this.syncIterations, indicesInit, function () {
                     _this.init(callback);
                 });
             });

+ 15 - 11
Babylon/Mesh/babylon.meshSimplification.ts

@@ -34,7 +34,7 @@
         simplificationType: SimplificationType;
         mesh: Mesh;
         successCallback? : () => void;
-        submesh?: number;
+        submeshIndex: number;
         parallelProcessing: boolean;
     }
     
@@ -106,7 +106,7 @@
             switch (task.simplificationType) {
                 case SimplificationType.QUADRATIC:
                 default:
-                    return new QuadraticErrorSimplification(task.mesh);
+                    return new QuadraticErrorSimplification(task.mesh, task.submeshIndex);
             }
         }
     }
@@ -228,14 +228,14 @@
 
         public boundingBoxEpsilon: number;
 
-        constructor(private _mesh: Mesh) {
+        constructor(private _mesh: Mesh, private _submeshIndex:number = 0) {
             this.aggressiveness = 7;
             this.decimationIterations = 100;
             this.boundingBoxEpsilon = Engine.Epsilon;
         }
 
         public simplify(settings: ISimplificationSettings, successCallback: (simplifiedMeshes: Mesh) => void) {
-            this.initWithMesh(this._mesh,() => {
+            this.initWithMesh(this._mesh, this._submeshIndex, () => {
                 this.runDecimation(settings, successCallback);
             });
         }
@@ -370,7 +370,7 @@
                 });
         }
 
-        private initWithMesh(mesh: Mesh, callback: Function) {
+        private initWithMesh(mesh: Mesh, submeshIndex:number, callback: Function) {
             if (!mesh) return;
 
             this.vertices = [];
@@ -383,28 +383,32 @@
             var uvs = this._mesh.getVerticesData(VertexBuffer.UVKind);
             var colorsData = this._mesh.getVerticesData(VertexBuffer.ColorKind);
             var indices = mesh.getIndices();
+            var submesh = mesh.subMeshes[submeshIndex];
 
             var vertexInit = (i) => {
-                var vertex = new DecimationVertex(Vector3.FromArray(positionData, i * 3), Vector3.FromArray(normalData, i * 3), null, i);
+                var offset = i + submesh.verticesStart;
+                var vertex = new DecimationVertex(Vector3.FromArray(positionData, offset * 3), Vector3.FromArray(normalData, offset * 3), null, i);
                 if (this._mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
-                    vertex.uv = Vector2.FromArray(uvs, i * 2);
+                    vertex.uv = Vector2.FromArray(uvs, offset * 2);
                 } else if (this._mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
-                    vertex.color = Color4.FromArray(colorsData, i * 4);
+                    vertex.color = Color4.FromArray(colorsData, offset * 4);
                 }
                 this.vertices.push(vertex);
             };
-            var totalVertices = mesh.getTotalVertices();
+            //var totalVertices = mesh.getTotalVertices();
+            var totalVertices = submesh.verticesCount;
             AsyncLoop.SyncAsyncForLoop(totalVertices, this.syncIterations, vertexInit,() => {
 
                 var indicesInit = (i) => {
-                    var pos = i * 3;
+                    var offset = submesh.indexStart + i;
+                    var pos = offset * 3;
                     var i0 = indices[pos + 0];
                     var i1 = indices[pos + 1];
                     var i2 = indices[pos + 2];
                     var triangle = new DecimationTriangle([this.vertices[i0].id, this.vertices[i1].id, this.vertices[i2].id]);
                     this.triangles.push(triangle);
                 };
-                AsyncLoop.SyncAsyncForLoop(indices.length / 3, this.syncIterations, indicesInit,() => {
+                AsyncLoop.SyncAsyncForLoop(submesh.indexCount / 3, this.syncIterations, indicesInit,() => {
                     this.init(callback);
                 });
             });