// Loading the JSON file in an asynchronous manner public async Task <Mesh[]> LoadJSONFileAsync(string fileName) { var meshes = new List <Mesh>(); var materials = new Dictionary <String, Material>(); var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(fileName); var data = await Windows.Storage.FileIO.ReadTextAsync(file); dynamic jsonObject = Newtonsoft.Json.JsonConvert.DeserializeObject(data); for (var materialIndex = 0; materialIndex < jsonObject.materials.Count; materialIndex++) { var material = new Material(); material.Name = jsonObject.materials[materialIndex].name.Value; material.ID = jsonObject.materials[materialIndex].id.Value; if (jsonObject.materials[materialIndex].diffuseTexture != null) { material.DiffuseTextureName = jsonObject.materials[materialIndex].diffuseTexture.name.Value; } materials.Add(material.ID, material); } for (var meshIndex = 0; meshIndex < jsonObject.meshes.Count; meshIndex++) { var verticesArray = jsonObject.meshes[meshIndex].vertices; // Faces var indicesArray = jsonObject.meshes[meshIndex].indices; var uvCount = jsonObject.meshes[meshIndex].uvCount.Value; var verticesStep = 1; // Depending of the number of texture's coordinates per vertex // we're jumping in the vertices array by 6, 8 & 10 windows frame switch ((int)uvCount) { case 0: verticesStep = 6; break; case 1: verticesStep = 8; break; case 2: verticesStep = 10; break; } // the number of interesting vertices information for us var verticesCount = verticesArray.Count / verticesStep; // number of faces is logically the size of the array divided by 3 (A, B, C) var facesCount = indicesArray.Count / 3; var mesh = new Mesh(jsonObject.meshes[meshIndex].name.Value, verticesCount, facesCount); // Filling the Vertices array of our mesh first for (var index = 0; index < verticesCount; index++) { var x = (float)verticesArray[index * verticesStep].Value; var y = (float)verticesArray[index * verticesStep + 1].Value; var z = (float)verticesArray[index * verticesStep + 2].Value; // Loading the vertex normal exported by Blender var nx = (float)verticesArray[index * verticesStep + 3].Value; var ny = (float)verticesArray[index * verticesStep + 4].Value; var nz = (float)verticesArray[index * verticesStep + 5].Value; mesh.Vertices[index] = new Vertex { Coordinates = new Vector3(x, y, z), Normal = new Vector3(nx, ny, nz) }; if (uvCount > 0) { // Loading the texture coordinates float u = (float)verticesArray[index * verticesStep + 6].Value; float v = (float)verticesArray[index * verticesStep + 7].Value; mesh.Vertices[index].TextureCoordinates = new Vector2(u, v); } } // Then filling the Faces array for (var index = 0; index < facesCount; index++) { var a = (int)indicesArray[index * 3].Value; var b = (int)indicesArray[index * 3 + 1].Value; var c = (int)indicesArray[index * 3 + 2].Value; mesh.Faces[index] = new Face { A = a, B = b, C = c }; } // Getting the position you've set in Blender var position = jsonObject.meshes[meshIndex].position; mesh.Position = new Vector3((float)position[0].Value, (float)position[1].Value, (float)position[2].Value); if (uvCount > 0) { // Texture var meshTextureID = jsonObject.meshes[meshIndex].materialId.Value; var meshTextureName = materials[meshTextureID].DiffuseTextureName; mesh.Texture = new Texture(meshTextureName, 512, 512); } mesh.ComputeFacesNormals(); meshes.Add(mesh); } return(meshes.ToArray()); }
public static Mesh LoadOBJ(string objFile) { StreamReader reader = new StreamReader(File.OpenRead(objFile)); List <Vector3> vertices = new List <Vector3>(); List <Vector3> normals = new List <Vector3>(); List <Vector2> uvs = new List <Vector2>(); List <Tuple <int, int, int>[]> faces = new List <Tuple <int, int, int>[]>(); while (!reader.EndOfStream) { string line = reader.ReadLine(); string[] tokens = line.Split(' ').Skip(1).SkipWhile(t => t == string.Empty).ToArray(); if (line.StartsWith("#")) { continue; } else if (line.StartsWith("vn")) { float x = float.Parse(tokens[0]); float y = float.Parse(tokens[1]); float z = float.Parse(tokens[2]); normals.Add(new Vector3(x, y, z)); } else if (line.StartsWith("vt")) { float x = float.Parse(tokens[0]); float y = float.Parse(tokens[1]); uvs.Add(new Vector2(x, y)); } else if (line.StartsWith("v")) { float x = float.Parse(tokens[0]); float y = float.Parse(tokens[1]); float z = float.Parse(tokens[2]); vertices.Add(new Vector3(x, y, z)); } else if (line.StartsWith("f")) { Tuple <int, int, int> v1 = ParseIndicies(tokens[0]); Tuple <int, int, int> v2 = ParseIndicies(tokens[1]); Tuple <int, int, int> v3 = ParseIndicies(tokens[2]); faces.Add(new Tuple <int, int, int>[3] { v1, v2, v3 }); if (tokens.Length > 3) { Tuple <int, int, int> v4 = ParseIndicies(tokens[3]); faces.Add(new Tuple <int, int, int>[3] { v1, v3, v4 }); } } } List <Vertex> modelVerts = new List <Vertex>(); List <Face> modelFaces = new List <Face>(); for (int i = 0; i < faces.Count; i++) { Vertex[] faceVerts = new Vertex[3]; for (int j = 0; j < 3; j++) { int vertIdx = faces[i][j].Item1; int uvIdx = faces[i][j].Item2; int normIdx = faces[i][j].Item3; faceVerts[j] = new Vertex { Coordinates = vertices[vertIdx], Normal = (normIdx == -1) ? Vector3.Zero : normals[normIdx], TextureCoordinates = (uvIdx == -1) ? Vector2.Zero : uvs[uvIdx] }; } modelVerts.AddRange(faceVerts); modelFaces.Add(new Face { A = i * 3 + 0, B = i * 3 + 1, C = i * 3 + 2 }); } Mesh mesh = new Mesh("objMesh", modelVerts.Count, modelFaces.Count); modelVerts.CopyTo(mesh.Vertices); modelFaces.CopyTo(mesh.Faces); mesh.ComputeFacesNormals(); return(mesh); }