UnityMol  0.9.6-875
UnityMol viewer / In developement
GeometryBuffer.cs
Go to the documentation of this file.
1 
66 using UnityEngine;
67 using System.Collections;
68 using System.Collections.Generic;
69 using Molecule.Model;
70 
71 public class GeometryBuffer {
72 
73  int vertextot= 0;
74  private List<ObjectData> objects;
75  public List<Vector3> vertices;
76  public List<Vector2> uvs;
77  public List<Vector3> normals;
78 
80  private class ObjectData {
81  public string name;
82  public List<GroupData> groups;
83  public List<FaceIndices> allFaces;
84  public ObjectData() {
85  groups = new List<GroupData>();
86  allFaces = new List<FaceIndices>();
87  }
88  }
89 
90 
91 
92 
93  private GroupData curgr;
94  private class GroupData {
95  public string name;
96  public string materialName;
97  public List<FaceIndices> faces;
98  public GroupData() {
99  faces = new List<FaceIndices>();
100  }
101  public bool isEmpty { get { return faces.Count == 0; } }
102  }
103 
104  public GeometryBuffer() {
105  objects = new List<ObjectData>();
106  ObjectData d = new ObjectData();
107  d.name = "default";
108  objects.Add(d);
109  current = d;
110 
111  GroupData g = new GroupData();
112  g.name = "default";
113  g.materialName = "default";
114  d.groups.Add(g);
115  curgr = g;
116 
117  vertices = new List<Vector3>();
118  uvs = new List<Vector2>();
119  normals = new List<Vector3>();
120  }
121 
122  public void PushObject(string name) {
123  //Debug.Log("Adding new object " + name + ". Current is empty: " + isEmpty);
124  if(isEmpty) objects.Remove(current);
125 
126  ObjectData n = new ObjectData();
127  n.name = name;
128  objects.Add(n);
129 
130  GroupData g = new GroupData();
131  g.name = "default";
132  g.materialName = "default";
133  n.groups.Add(g);
134 
135  curgr = g;
136  current = n;
137  }
138 
139  public void PushGroup(string name) {
140  if(curgr.isEmpty) current.groups.Remove(curgr);
141  GroupData g = new GroupData();
142  g.name = name;
143  g.materialName = "default";
144  current.groups.Add(g);
145  curgr = g;
146  }
147 
148  public void PushMaterialName(string name) {
149  Debug.Log("Pushing new material " + name + " with curgr.empty=" + curgr.isEmpty);
150  if(!curgr.isEmpty) PushGroup(name);
151  if(curgr.name == "default") curgr.name = name;
152  curgr.materialName = name;
153  }
154 
155  public void PushVertex(Vector3 v) {
156  vertices.Add(v);
157  vertextot++;
158  }
159 
160  public void PushUV(Vector2 v) {
161  uvs.Add(v);
162  }
163 
164  public void PushNormal(Vector3 v) {
165  normals.Add(v);
166  }
167 
168  public void PushFace(FaceIndices f) {
169  curgr.faces.Add(f);
170  current.allFaces.Add(f);
171 
172  }
173 
174  public void Trace() {
175  Debug.Log("OBJ has " + objects.Count + " object(s)");
176  Debug.Log("OBJ has " + vertices.Count + " vertice(s)");
177  Debug.Log("OBJ has " + uvs.Count + " uv(s)");
178  Debug.Log("OBJ has " + normals.Count + " normal(s)");
179  foreach(ObjectData od in objects) {
180  Debug.Log(od.name + " has " + od.groups.Count + " group(s)");
181  foreach(GroupData gd in od.groups) {
182  Debug.Log(od.name + "/" + gd.name + " has " + gd.faces.Count + " faces(s)");
183  }
184  }
185 
186  }
187 
188  public int numObjects { get { return objects.Count; } }
189  public bool isEmpty { get { return vertices.Count == 0; } }
190 // public bool hasUVs { get { return uvs.Count == 0; } }
191  public bool hasUVs = true;
192  public bool hasNormals { get { return normals.Count > 0; } }
193 
194  public void PopulateMeshes(GameObject[] gs, Dictionary<string, Material> mats) {
195  if(gs.Length != numObjects) return; // Should not happen unless obj file is corrupt...
196 
197  for(int i = 0; i < gs.Length; i++) {
198 
199  ObjectData od = objects[i];
200 
201  if(od.name != "default") gs[i].name = od.name;
202 
203 // Debug.Log("triangle tot:"+ triangletot);
204  Vector3[] tvertices = new Vector3[vertextot];
205  Vector2[] tuvs = new Vector2[vertextot];
206  Vector3[] tnormals = new Vector3[vertextot];
207 
208  int[] triangles = new int[od.allFaces.Count];
209 
210  //Calcul of baricenter of the surface
211  float barix=0;
212  float bariy=0;
213  float bariz=0;
214 //
215  for (int bari =0; bari< vertextot;bari++){
216  barix = barix + vertices[bari].x;
217 // Debug.Log("t " + vertices[bari]);
218  bariy = bariy + vertices[bari].y;
219  bariz = bariz + vertices[bari].z;
220  }
221 // Debug.Log(" baricentre: "+ barix +" "+ bariy+ " "+ bariz);
222 //
223  barix = barix/vertextot;
224  bariy = bariy/vertextot;
225  bariz = bariz/vertextot;
226  Debug.Log("Surface baricentre coords :: "+ barix +" "+ bariy+ " "+ bariz);
227 
228  int k=0;
229 // float pas = 1f/214f;
230 // float temp1=pas;
231 // float temp2=pas;
232 // int compteur=1;
233  for (int l=0; l<vertextot;l++){
234  tvertices[l] = vertices[l];
235  tvertices[l].x = vertices[l].x;//-barix;
236  tvertices[l].y = vertices[l].y;//-bariy;
237  tvertices[l].z = vertices[l].z;//-bariz;
238  tnormals[l]= normals[l];
239 
240  //Calcul de l'uv map non fonctionnel
241 
242 // tuvs[k]=new Vector2(temp1,temp2);
243 // Debug.Log("Vector : "+tuvs[k] +" pas : " +pas + "compteur :" +compteur);
244 // compteur++;
245 // temp1 += pas;
246 // if (temp1 >0.99){
247 // temp2 += pas;
248 // temp1= pas;
249 // }
250 
251  }
252  foreach(FaceIndices fi in od.allFaces) {
253  triangles[k] = fi.vi;
254 // tvertices[k] = vertices[fi.vi];
255 // if(hasUVs) tuvs[k] = uvs[fi.vu];
256 // if(hasNormals) tnormals[k] = normals[fi.vn];
257  k++;
258  }
259 
260  Vector3 baricenter = new Vector3(barix,bariy,bariz);
261  Debug.Log("Obj center = " + baricenter);
262  gs[i].transform.localPosition = gs[i].transform.localPosition + MoleculeModel.Offset;
263  gs[i].transform.localPosition = gs[i].transform.localPosition + MoleculeModel.Center;
264 // gs[i].transform.localPosition = gs[i].transform.localPosition + MoleculeModel.Offset;
265 // Debug.Log(" baricentre: "+ barix +" "+ bariy+ " "+ bariz);
266  Mesh m = (gs[i].GetComponent(typeof(MeshFilter)) as MeshFilter).mesh;
267  m.vertices = tvertices;
268  MoleculeModel.vertices = tvertices;
269  if(hasUVs) m.uv = tuvs;
270  if(hasNormals) m.normals = tnormals;
271 
272  //Unity has a left-handed coordinates system while Molecular obj are right-handed
273  //So we have to change the winding order
274  for(int tri=0; tri<triangles.Length; tri=tri+3)
275  {
276  int tmp = triangles[tri];
277  triangles[tri] = triangles[tri+2];
278  triangles[tri+2] = tmp;
279  }
280 
281  if(od.groups.Count == 1) {
282  GroupData gd = od.groups[0];
283  Debug.Log("Mesh material used :: "+gd.materialName);
284  gs[i].GetComponent<Renderer>().material = mats[gd.materialName];
285 
286 // int[] triangles = new int[gd.faces.Count];
287 // for(int j = 0; j < triangles.Length; j++) triangles[j] = j;
288 
289  m.triangles = triangles;
290 
291  } //else {
292 // int gl = od.groups.Count;
293 // Material[] sml = new Material[gl];
294 // m.subMeshCount = gl;
295 // int c = 0;
296 //
297 // for(int j = 0; j < gl; j++) {
298 // sml[j] = mats[od.groups[j].materialName];
299 // int[] triangles = new int[od.groups[j].faces.Count];
300 // int l = od.groups[j].faces.Count + c;
301 // int s = 0;
302 // for(; c < l; c++, s++) triangles[s] = c;
303 // m.SetTriangles(triangles, j);
304 // }
305 //
306 // gs[i].renderer.materials = sml;
307 // }
308  }
309  }
310 }
311 
312 
void PushObject(string name)
List< GroupData > groups
void PushGroup(string name)
void PushNormal(Vector3 v)
static Vector3 Offset
The offset for the molecule.
void PushMaterialName(string name)
void PushUV(Vector2 v)
GroupData curgr
List< ObjectData > objects
void PopulateMeshes(GameObject[] gs, Dictionary< string, Material > mats)
List< Vector3 > normals
List< FaceIndices > allFaces
List< FaceIndices > faces
void PushFace(FaceIndices f)
List< Vector2 > uvs
ObjectData current
void PushVertex(Vector3 v)
List< Vector3 > vertices
static Vector3 Center
The barycenter of the molecule.