private void MergeChunkMeshes() { MeshProcessor processor = this.meshProcessor; TileSystem system = this.tileSystem; Matrix4x4 correctionMatrix = system.transform.worldToLocalMatrix; this.combineChunkMeshes = new MeshData[this.combineChunkRows, this.combineChunkColumns]; for (int chunkRow = 0; chunkRow < this.combineChunkRows; ++chunkRow) { for (int chunkColumn = 0; chunkColumn < this.combineChunkColumns; ++chunkColumn) { MeshData chunkMesh = null; int startRow = chunkRow * this.combineChunkHeight; int startColumn = chunkColumn * this.combineChunkWidth; int endRow = Mathf.Min(system.RowCount, startRow + this.combineChunkHeight); int endColumn = Mathf.Min(system.ColumnCount, startColumn + this.combineChunkWidth); for (int row = startRow; row < endRow; ++row) { for (int column = startColumn; column < endColumn; ++column) { MeshData tileMesh = this.map[row, column]; if (tileMesh == null) { continue; } if (chunkMesh == null) { chunkMesh = tileMesh; processor.Mount(chunkMesh); } else { processor.AppendMesh(tileMesh); } } } if (chunkMesh != null) { processor.Apply(); this.combineChunkMeshes[chunkRow, chunkColumn] = chunkMesh; chunkMesh.ApplyTransform(correctionMatrix); if (chunkMesh.HasTangents) { chunkMesh.CalculateTangents(); } } this.ReportProgress(); } } this.map = null; }
/// <summary> /// Convert mesh data into meshes on a per material basis. /// </summary> /// <param name="processor">Reuse an existing mesh processor, or alternatively /// specify <c>null</c> to create a new one.</param> /// <returns> /// Array of <c>Mesh</c> objects. /// </returns> public Mesh[] ToMeshPerMaterial(MeshProcessor processor) { if (processor == null) { processor = new MeshProcessor(); } Mesh[] meshes = new Mesh[this.Submeshes.Count]; int meshIndex = 0; // Array of vertex indices which is used to extract vertices // into separate mesh objects. int[] vertexMap = new int[this.Vertices.Length]; int vertexMapIndex = 0; foreach (var material in this.Submeshes.Keys) { int[] triangles = this.Submeshes[material].ToArray(); // Clear previously extracted vertices. processor.Reset(); for (int i = 0; i < vertexMap.Length; ++i) { vertexMap[i] = -1; } vertexMapIndex = 0; // Extract unique vertices from mesh. for (int ti = 0; ti < triangles.Length; ++ti) { int vi = triangles[ti]; // Has this vertex already been extracted? if (vertexMap[vi] == -1) { vertexMap[vi] = vertexMapIndex++; processor.Vertices.Add(this.Vertices[vi]); if (this.HasNormals) { processor.Normals.Add(this.Normals[vi]); } if (this.HasUV) { processor.Uv.Add(this.Uv[vi]); } if (this.HasUV2) { processor.Uv2.Add(this.Uv2[vi]); } if (this.HasColors32) { processor.Colors32.Add(this.Colors32[vi]); } if (this.HasTangents) { processor.Tangents.Add(this.Tangents[vi]); } } // Triangle index must be remapped. triangles[ti] = vertexMap[vi]; } // Form mesh from extracted vertex data. var mesh = new Mesh(); meshes[meshIndex++] = mesh; mesh.vertices = processor.Vertices.ToArray(); if (this.HasNormals) { mesh.normals = processor.Normals.ToArray(); } if (this.HasUV) { mesh.uv = processor.Uv.ToArray(); } if (this.HasUV2) { mesh.uv2 = processor.Uv2.ToArray(); } if (this.HasColors32) { mesh.colors32 = processor.Colors32.ToArray(); } if (this.HasTangents) { mesh.tangents = processor.Tangents.ToArray(); } mesh.triangles = triangles; mesh.RecalculateBounds(); } return(meshes); }