Exemplo n.º 1
0
 public Mesh PresentMesh(Mesh mesh)
 {
     var rotation = mesh.Rotation;
     var position = mesh.Position;
     List<Vertex> presentPoints = mesh.vertices.Select(x =>
         camera.ProjectVertex(x, rotation, position)
     ).ToList();
     return new Mesh(mesh.Name, presentPoints, mesh.Faces);
 }
Exemplo n.º 2
0
        // Loading the JSON file in an asynchronous manner
        public static Mesh[] LoadJSONFile(string fileName)
        {
            var meshes = new List<Mesh>();
            var file = File.ReadAllText(fileName);
            dynamic jsonObject = Newtonsoft.Json.JsonConvert.DeserializeObject(file);

            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 = Vector<double>.Build.DenseOfArray(new double[3] {x, y, z})
                        , Normal = Vector<double>.Build.DenseOfArray(new double[3] {nx, ny, nz}) };
                }

                // 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 = Vector<double>.Build.DenseOfArray(new double[3] {position[0].Value, position[1].Value, position[2].Value});
                meshes.Add(mesh);
            }
            return meshes.ToArray();
        }