UnityMol  0.9.6-875
UnityMol viewer / In developement
Splitting.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4 
5 public class Splitting {
6  private static int vertexLimit = 65000;
7  private int[] triangles;
8  private Vector3[] vertices;
9  private Vector3[] normals;
10  private Color32[] colors;
11  private int currentIndex;
12  private List<Mesh> meshes;
13  private int lastIndex;
14 
15  public List<Mesh> Split(MeshData mData) {
16  triangles = mData.triangles;
17  vertices = mData.vertices;
18  normals = mData.normals;
19  colors = mData.colors;
20  meshes = new List<Mesh>();
21 
22  if(UI.UIData.isGLIC)
23  vertexLimit = 59520;
24 
25  // Small meshes don't need to be split
26  if(mData.vertices.Length < vertexLimit) {
27  Debug.Log("Vertices size : "+vertices.Length);
28  Debug.Log(" triangle size : "+ triangles.Length);
29  Mesh mesh = new Mesh();
30  mesh.vertices = vertices;
31  mesh.triangles = triangles;
32  mesh.normals = normals;
33  mesh.colors32 = colors;
34  meshes.Add(mesh);
35  return meshes;
36  }
37 
38  lastIndex = triangles.Length;
39 
40  while(currentIndex < lastIndex) {
41  FillMesh();
42  }
43 
44  return meshes;
45  }
46 
47 
48 
49 
50  private void FillMesh() {
51  List<int> tris = new List<int>();
52  List<Vector3> verts = new List<Vector3>();
53  List<Vector3> norms = new List<Vector3>();
54  List<Color32> cols = new List<Color32>();
55  Dictionary<int, int> dict = new Dictionary<int, int>();
56  int currentVertex = 0;
57 
58  // Should not matter, as it should never be used unless dict.TryGetValue() succeeds
59  int index = -1;
60  int vIndex;
61  bool useColors = (colors != null);
62 
63  bool roomLeft = true;
64 
65  while( roomLeft && (currentIndex < lastIndex) ) {
66  // The dictionary may contain the correct index for this vertex
67  // in the context of the current mesh. That means the vertex already exists
68  // in the local mesh.
69  if(dict.TryGetValue(triangles[currentIndex], out index)) {
70  tris.Add(index);
71  } else {
72  // If not, we add it. The index of the vertex in the original
73  // mesh is the key, and the value is currentVertex
74  dict.Add(triangles[currentIndex], currentVertex);
75 
76  // We add the vertex, color and normal pointed to by the global index
77  vIndex = triangles[currentIndex];
78  verts.Add(vertices[vIndex]);
79  norms.Add(normals[vIndex]);
80  if(useColors)
81  cols.Add(colors[vIndex]);
82 
83  // But we reference it with the local index.
84  tris.Add(currentVertex);
85  currentVertex++;
86  }
87  // We don't want to exceed the vertex limit, which is something like 65534, but
88  // we must also take care not to exit the function in the middle of a triangle.
89  if( (currentVertex > vertexLimit) && ( (currentIndex+1) % 3 == 0) )
90  //if(currentVertex > 10000)
91  roomLeft = false;
92 
93  // Next item in triangles
94  currentIndex ++;
95 
96  }
97 
98  Mesh mesh = new Mesh();
99  mesh.vertices = verts.ToArray();
100  mesh.triangles = tris.ToArray();
101  mesh.normals = norms.ToArray();
102  mesh.colors32 = cols.ToArray();
103 
104  meshes.Add(mesh);
105  }
106 }
Vector3[] normals
Definition: MeshData.cs:7
List< Mesh > meshes
Definition: Splitting.cs:12
Color32[] colors
Definition: MeshData.cs:8
int currentIndex
Definition: Splitting.cs:11
List< Mesh > Split(MeshData mData)
Definition: Splitting.cs:15
Color32[] colors
Definition: Splitting.cs:10
int[] triangles
Definition: MeshData.cs:5
Vector3[] vertices
Definition: MeshData.cs:6
Vector3[] vertices
Definition: Splitting.cs:8
!WiP Includes FLAGS of GUI.
Definition: UIData.cs:78
static bool isGLIC
Definition: UIData.cs:189
int[] triangles
Definition: Splitting.cs:7
void FillMesh()
Definition: Splitting.cs:50
int lastIndex
Definition: Splitting.cs:13
Vector3[] normals
Definition: Splitting.cs:9
static int vertexLimit
Definition: Splitting.cs:6
Definition: GUIDisplay.cs:66