public static float[] VertexArrayToFloatArrayPlybin8(Vertex[] vertices) { var output = new float[vertices.Length*8]; for (int i = 0; i < vertices.Length; i++) { output [i * 8+0] = vertices [i].X; output [i * 8+1] = vertices [i].Y; output [i * 8+2] = vertices [i].Z; output [i * 8+3] = vertices [i].Normal.X; output [i * 8+4] = vertices [i].Normal.Y; output [i * 8+5] = vertices [i].Normal.Z; output [i * 8+6] = vertices [i].S; output [i * 8+7] = vertices [i].T; } return output; }
public static float[] VertexArrayToFloatArrayPlybin9(Vertex[] vertices) { var output = new float[vertices.Length*9]; for (int i = 0; i < vertices.Length; i++) { output [i * 9+0] = vertices [i].X; output [i * 9+1] = vertices [i].Y; output [i * 9+2] = vertices [i].Z; output [i * 9+3] = vertices [i].Normal.X; output [i * 9+4] = vertices [i].Normal.Y; output [i * 9+5] = vertices [i].Normal.Z; output [i * 9+6] = vertices [i].Red; output [i * 9+7] = vertices [i].Green; output [i * 9+8] = vertices [i].Blue; } return output; }
public static float[] VertexArrayToFloatArrayPlybin11(Vertex[] vertices) { var output = new float[vertices.Length*11]; for (int i = 0; i < vertices.Length; i++) { output [i * 11+0] = vertices [i].X; output [i * 11+1] = vertices [i].Y; output [i * 11+2] = vertices [i].Z; output [i * 11+3] = vertices [i].Normal.X; output [i * 11+4] = vertices [i].Normal.Y; output [i * 11+5] = vertices [i].Normal.Z; output [i * 11+6] = vertices [i].S; output [i * 11+7] = vertices [i].T; output [i * 11+8] = vertices [i].Red; output [i * 11+9] = vertices [i].Green; output [i * 11+10] = vertices [i].Blue; } return output; }
public void MeshDataLoaded(Vertex[] vertices, uint[] indices) { Vertices = vertices; Indices = indices; }
public MeshLoader(string filename) { try { string[] lines = File.ReadAllLines(filename); uint vertexAmount = 0; uint faceAmount = 0; bool normals = false; bool texCoords = false; bool colors = false; uint i = 0; for (; i < lines.Length; i++) { if (lines [i].StartsWith ("element vertex ", StringComparison.Ordinal)) vertexAmount = UInt32.Parse (lines [i].Substring (15)); if (lines [i].StartsWith ("element face ", StringComparison.Ordinal)) faceAmount = UInt32.Parse (lines [i].Substring (13)); normals |= lines [i].StartsWith ("property float nx", StringComparison.Ordinal); texCoords |= lines [i].StartsWith ("property float s", StringComparison.Ordinal); colors |= lines [i].StartsWith ("property uchar red", StringComparison.Ordinal); if (lines [i].StartsWith ("end_header", StringComparison.Ordinal)) break; } i++; Vertex[] vertices = new Vertex[vertexAmount]; String[] numbers; int colorStride = texCoords ? 8 : 6; for(uint j = 0; j < vertexAmount; j++) { numbers = lines[i].Split(' '); vertices [j] = new Vertex(Convert.ToSingle(numbers[0], CultureInfo.InvariantCulture), Convert.ToSingle(numbers[1], CultureInfo.InvariantCulture), Convert.ToSingle(numbers[2], CultureInfo.InvariantCulture)); if(normals) { vertices[j].Normal.X = Convert.ToSingle(numbers[3], CultureInfo.InvariantCulture); vertices[j].Normal.Y = Convert.ToSingle(numbers[4], CultureInfo.InvariantCulture); vertices[j].Normal.Z = Convert.ToSingle(numbers[5], CultureInfo.InvariantCulture); } if(texCoords) { vertices[j].S = Convert.ToSingle(numbers[6], CultureInfo.InvariantCulture); vertices[j].T = Convert.ToSingle(numbers[7], CultureInfo.InvariantCulture); } if(colors) { vertices[j].Red = Byte.Parse(numbers[colorStride])/255f; vertices[j].Green = Byte.Parse(numbers[colorStride+1])/255f; vertices[j].Blue = Byte.Parse(numbers[colorStride+2])/255f; } i++; } var indices = new List<uint>(); for(uint j = 0; j < faceAmount; j++) { numbers = lines[i].Split(' '); if(numbers[0].Equals("3")) { indices.Add(UInt32.Parse(numbers[1])); indices.Add(UInt32.Parse(numbers[2])); indices.Add(UInt32.Parse(numbers[3])); } else if(numbers[0].Equals("4")) { indices.Add(UInt32.Parse(numbers[1])); indices.Add(UInt32.Parse(numbers[2])); indices.Add(UInt32.Parse(numbers[3])); indices.Add(UInt32.Parse(numbers[1])); indices.Add(UInt32.Parse(numbers[3])); indices.Add(UInt32.Parse(numbers[4])); } i++; } uint[] indicesA = indices.ToArray(); if(!normals) CalcNormals(vertices, indicesA); MeshDataLoaded(vertices, indicesA); } catch(Exception e) { Console.Error.WriteLine ("Failed to load mesh! File: " + filename); Console.Error.WriteLine(e.StackTrace); throw e; //I'm not handeling this shit } }
public static void CalcNormals(Vertex[] vertices, uint[] indices) { for(int i = 0; i < indices.Length; i += 3) { uint i0 = indices[i]; uint i1 = indices[i + 1]; uint i2 = indices[i + 2]; var v1 = new Vector3(vertices[i1].X - vertices[i0].X, vertices[i1].Y - vertices[i0].Y, vertices[i1].Z - vertices[i0].Z); var v2 = new Vector3(vertices[i2].X - vertices[i0].X, vertices[i2].Y - vertices[i0].Y, vertices[i2].Z - vertices[i0].Z); Vector3 normal = Vector3.Cross(v1, v2).Normalized(); vertices[i0].Normal = vertices[i0].Normal + normal; vertices[i1].Normal = vertices[i1].Normal + normal; vertices[i2].Normal = vertices[i2].Normal + normal; } for (int i = 0; i < vertices.Length; i++) vertices [i].Normal.Normalize (); }
public MeshLoader(Stream file, int plybinVersion) { bool texCoords = plybinVersion==8 | plybinVersion==11; bool colors = plybinVersion==9 | plybinVersion==11; if(!texCoords & !colors) { Console.Out.WriteLine("Mesh Loader can't make sense of plybin version {0}", plybinVersion); } try { using(var reader = new BinaryReader(file)){ UInt32 vertexAmount = reader.ReadUInt32(); UInt32 faceAmount = reader.ReadUInt32(); var vertices = new Vertex[vertexAmount]; for(uint j = 0; j < vertexAmount; j++) { vertices [j] = new Vertex(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); vertices [j].Normal.X = reader.ReadSingle(); vertices [j].Normal.Y = reader.ReadSingle(); vertices [j].Normal.Z = reader.ReadSingle(); if(texCoords) { vertices[j].S = reader.ReadSingle(); vertices[j].T = reader.ReadSingle(); } if(colors) { vertices[j].Red = reader.ReadSingle(); vertices[j].Green = reader.ReadSingle(); vertices[j].Blue = reader.ReadSingle(); } } var indices = new uint[faceAmount*3]; for(uint j = 0; j < faceAmount; j++) { indices[j*3] = reader.ReadUInt32(); indices[j*3+1] = reader.ReadUInt32(); indices[j*3+2] = reader.ReadUInt32(); } MeshDataLoaded(vertices, indices); } } catch(Exception e) { Console.Error.WriteLine ("Failed to load mesh! Stream: " + file); Console.Error.WriteLine(e.StackTrace); throw e; //I'm not handeling this shit } }