Browse Source

Added multiMultiMaterials option to MergeMeshes

= 6 years ago
parent
commit
0534d39075
2 changed files with 51 additions and 5 deletions
  1. 1 0
      dist/preview release/what's new.md
  2. 50 5
      src/Meshes/mesh.ts

+ 1 - 0
dist/preview release/what's new.md

@@ -96,6 +96,7 @@
 - Added per solid particle culling possibility : `solidParticle.isInFrustum()`  ([jerome](https://github.com/jbousquie))
 - Added per solid particle culling possibility : `solidParticle.isInFrustum()`  ([jerome](https://github.com/jbousquie))
 - Added transparency support to `GlowLayer` ([Sebavan](https://github.com/Sebavan))
 - Added transparency support to `GlowLayer` ([Sebavan](https://github.com/Sebavan))
 - Added option `forceDisposeChildren` to multiMaterial.dispose ([danjpar](https://github.com/danjpar))
 - Added option `forceDisposeChildren` to multiMaterial.dispose ([danjpar](https://github.com/danjpar))
+- Added option `multiMultiMaterials` to mesh.mergeMeshes ([danjpar](https://github.com/danjpar))
 
 
 ### OBJ Loader
 ### OBJ Loader
 - Add color vertex support (not part of standard) ([brianzinn](https://github.com/brianzinn))
 - Add color vertex support (not part of standard) ([brianzinn](https://github.com/brianzinn))

+ 50 - 5
src/Meshes/mesh.ts

@@ -18,6 +18,7 @@ import { BoundingInfo } from "../Culling/boundingInfo";
 import { BoundingSphere } from "../Culling/boundingSphere";
 import { BoundingSphere } from "../Culling/boundingSphere";
 import { Effect } from "../Materials/effect";
 import { Effect } from "../Materials/effect";
 import { Material } from "../Materials/material";
 import { Material } from "../Materials/material";
+import { MultiMaterial } from "../Materials/multiMaterial";
 import { SceneLoaderFlags } from "../Loading/sceneLoaderFlags";
 import { SceneLoaderFlags } from "../Loading/sceneLoaderFlags";
 import { Skeleton } from "../Bones/skeleton";
 import { Skeleton } from "../Bones/skeleton";
 import { MorphTargetManager } from "../Morph/morphTargetManager";
 import { MorphTargetManager } from "../Morph/morphTargetManager";
@@ -3586,9 +3587,10 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
      * @param allow32BitsIndices when the sum of the vertices > 64k, this must be set to true
      * @param allow32BitsIndices when the sum of the vertices > 64k, this must be set to true
      * @param meshSubclass when set, vertices inserted into this Mesh.  Meshes can then be merged into a Mesh sub-class.
      * @param meshSubclass when set, vertices inserted into this Mesh.  Meshes can then be merged into a Mesh sub-class.
      * @param subdivideWithSubMeshes when true (false default), subdivide mesh to his subMesh array with meshes source.
      * @param subdivideWithSubMeshes when true (false default), subdivide mesh to his subMesh array with meshes source.
+     * @param multiMultiMaterials when true (false default), subdivide mesh and accept multiple multi materials, ignores subdivideWithSubMeshes.
      * @returns a new mesh
      * @returns a new mesh
      */
      */
-    public static MergeMeshes(meshes: Array<Mesh>, disposeSource = true, allow32BitsIndices?: boolean, meshSubclass?: Mesh, subdivideWithSubMeshes?: boolean): Nullable<Mesh> {
+    public static MergeMeshes(meshes: Array<Mesh>, disposeSource = true, allow32BitsIndices?: boolean, meshSubclass?: Mesh, subdivideWithSubMeshes?: boolean, multiMultiMaterials?: boolean): Nullable<Mesh> {
         var index: number;
         var index: number;
         if (!allow32BitsIndices) {
         if (!allow32BitsIndices) {
             var totalVertices = 0;
             var totalVertices = 0;
@@ -3605,7 +3607,15 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
                 }
                 }
             }
             }
         }
         }
-
+        if (multiMultiMaterials) {
+            var newMultiMaterial: Nullable<MultiMaterial> = null;
+            var subIndex: number;
+            var matIndex: number;
+            subdivideWithSubMeshes = false;
+        }
+        var maxValue = 0;
+        var materialArray: Array<Material> = new Array<Material>();
+        var materialIndexArray: Array<number> = new Array<number>();
         // Merge
         // Merge
         var vertexData: Nullable<VertexData> = null;
         var vertexData: Nullable<VertexData> = null;
         var otherVertexData: VertexData;
         var otherVertexData: VertexData;
@@ -3623,10 +3633,35 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
                     vertexData = otherVertexData;
                     vertexData = otherVertexData;
                     source = meshes[index];
                     source = meshes[index];
                 }
                 }
-
                 if (subdivideWithSubMeshes) {
                 if (subdivideWithSubMeshes) {
                     indiceArray.push(meshes[index].getTotalIndices());
                     indiceArray.push(meshes[index].getTotalIndices());
                 }
                 }
+                if (multiMultiMaterials) {
+                    if (meshes[index].material) {
+                        var material = meshes[index].material;
+                        if (material instanceof MultiMaterial) {
+                            for (matIndex = 0; matIndex < material.subMaterials.length; matIndex++) {
+                                materialArray.push(<Material>material.subMaterials[matIndex]);
+                            }
+                            for (subIndex = 0; subIndex < meshes[index].subMeshes.length; subIndex++) {
+                                materialIndexArray.push(meshes[index].subMeshes[subIndex].materialIndex + maxValue);
+                                indiceArray.push(meshes[index].subMeshes[subIndex].indexCount);
+                            }
+                        } else {
+                            materialArray.push(<Material>material);
+                            for (subIndex = 0; subIndex < meshes[index].subMeshes.length; subIndex++) {
+                                materialIndexArray.push(0 + maxValue);
+                                indiceArray.push(meshes[index].subMeshes[subIndex].indexCount);
+                            }
+                        }
+                    } else {
+                        for (subIndex = 0; subIndex < meshes[index].subMeshes.length; subIndex++) {
+                            materialIndexArray.push(0);
+                            indiceArray.push(meshes[index].subMeshes[subIndex].indexCount);
+                        }
+                    }
+                    maxValue = materialArray.length;
+                }
             }
             }
         }
         }
 
 
@@ -3639,7 +3674,6 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
         (<VertexData>vertexData).applyToMesh(meshSubclass);
         (<VertexData>vertexData).applyToMesh(meshSubclass);
 
 
         // Setting properties
         // Setting properties
-        meshSubclass.material = source.material;
         meshSubclass.checkCollisions = source.checkCollisions;
         meshSubclass.checkCollisions = source.checkCollisions;
 
 
         // Cleaning
         // Cleaning
@@ -3652,7 +3686,7 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
         }
         }
 
 
         // Subdivide
         // Subdivide
-        if (subdivideWithSubMeshes) {
+        if (subdivideWithSubMeshes || multiMultiMaterials) {
 
 
             //-- removal of global submesh
             //-- removal of global submesh
             meshSubclass.releaseSubMeshes();
             meshSubclass.releaseSubMeshes();
@@ -3667,6 +3701,17 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
             }
             }
         }
         }
 
 
+        if (multiMultiMaterials) {
+            newMultiMaterial = new MultiMaterial(source.name + "_merged", source.getScene());
+            newMultiMaterial.subMaterials = materialArray;
+            for (subIndex = 0; subIndex < meshSubclass.subMeshes.length; subIndex++) {
+                meshSubclass.subMeshes[subIndex].materialIndex = materialIndexArray[subIndex];
+            }
+            meshSubclass.material = newMultiMaterial;
+        } else {
+            meshSubclass.material = source.material;
+        }
+
         return meshSubclass;
         return meshSubclass;
     }
     }