IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
BlenderMesh.cs
1 using System;
2 using System.Collections.Generic;
3 using UnityEngine;
4 
5 namespace BlenderMeshReader
6 {
7  //This class repesents a mesh in unity
8  //Coordinate system is right-handed
10  {
11  public string Name { get; set; }
12  public ulong UniqueIdentifier { get; set; }
13  public Vector3[] VertexList { get; set; }
14  public Vector3[] NormalList { get; set; }
15  public List<PolygonListEntry> PolygonList { get; set; }
16  public int[] LoopList { get; set; }
17  public int[] TriangleList { get; set; }
18 
19  public BlenderMesh()
20  {
21  this.Name = "defaultMesh";
22  this.UniqueIdentifier = 0;
23  VertexList = new Vector3[0];
24  NormalList = new Vector3[0];
25  PolygonList = new List<PolygonListEntry>();
26  LoopList = new int[0];
27  TriangleList = new int[0];
28  }
29 
30 
31  public BlenderMesh(string name, ulong uniqueIdentifier)
32  {
33  this.Name = name;
34  this.UniqueIdentifier = uniqueIdentifier;
35  VertexList = new Vector3[0];
36  NormalList = new Vector3[0];
37  PolygonList = new List<PolygonListEntry>();
38  LoopList = new int[0];
39  TriangleList = new int[0];
40  }
41 
42  //This method creates the TriangleList from PolygonList and LoopList.
43  //This method must be called after the VertexList, PolygonList and LoopList are complete
44  public void createTriangleList()
45  {
46  List<int> triangle = new List<int>(); //TODO check what is faster: List (create List, add elements, ToArray()) or Array (calc length, fill array)
47  foreach (PolygonListEntry polygon in PolygonList)
48  {
49  for (int i = 0; i < polygon.Lenght - 2; i++)
50  {
51  for (int j = 0; j < 3; j++)
52  {
53  if (i == 0)
54  {
55  triangle.Add(LoopList[polygon.StartIndex + j]);
56  }
57  else
58  {
59  if (j != 2)
60  {
61  triangle.Add(LoopList[polygon.StartIndex + j + i + 1]);
62  }
63  else
64  {
65  triangle.Add(LoopList[polygon.StartIndex]);
66  }
67  }
68 
69  }
70 
71  }
72  }
73  TriangleList = triangle.ToArray();
74 
75 
76  return;
77  }
78 
79  //Return a unity mesh in a left-handed coordinate system
80  public UnityMesh ToUnityMesh()
81  {
82  UnityMesh result = new UnityMesh(this.Name, this.UniqueIdentifier);
83 
84  Vector3[] vertices = new Vector3[VertexList.Length];
85  //Flip z component of all Vector3 in vertex list
86  for (int i = 0; i < VertexList.Length; i++)
87  {
88  vertices[i] = new Vector3(VertexList[i].x, VertexList[i].y,-VertexList[i].z);
89  }
90  result.VertexList = vertices;
91 
92  Vector3[] normals = new Vector3[NormalList.Length];
93  //Flip z component of all Vector3 in normal list
94  for (int i = 0; i < NormalList.Length; i++)
95  {
96  normals[i] = new Vector3(NormalList[i].x, NormalList[i].y, -NormalList[i].z);
97  }
98  result.NormalList = normals;
99 
100  int[] triangles = new int[TriangleList.Length];
101  //Flip the order of triangle vertices. v0,v1,v2 -> v0,v2,v1.
102  for (int i = 0; i < TriangleList.Length; i += 3)
103  {
104  triangles[i] = TriangleList[i];
105  triangles[i + 1] = TriangleList[i + 2];
106  triangles[i + 2] = TriangleList[i + 1];
107  }
108  result.TriangleList = triangles;
109 
110  return result;
111 
112  }
113 
114 
115  }
116 }