Ejemplo n.º 1
0
        public override GraphNode3D LoadNode(string path)
        {
            GraphEntity3D root = new GraphEntity3D();
            string        file = path;

            var e  = new Assimp.AssimpContext();
            var c1 = new Assimp.Configs.NormalSmoothingAngleConfig(45);

            e.SetConfig(c1);
            Console.WriteLine("Impporting:" + file);
            Assimp.Scene s = null;
            try
            {
                s = e.ImportFile(file, PostProcessSteps.CalculateTangentSpace | PostProcessSteps.GenerateSmoothNormals | PostProcessSteps.Triangulate | PostProcessSteps.GenerateNormals);
            }
            catch (AssimpException ae)
            {
                Console.WriteLine(ae);
                Console.WriteLine("Failed to import");
            }
            Console.WriteLine("Imported.");
            Dictionary <string, VMesh> ml = new Dictionary <string, VMesh>();
            List <VMesh> ml2 = new List <VMesh>();

            foreach (var m in s.Meshes)
            {
                var vm = new Material.Material3D();

                var m2 = new VMesh(m.VertexCount, m.GetIndices().Length);
                ml2.Add(m2);
                // ml.Add(m.Name, m2);

                m2.Mat = vm;
                // root.AddMesh(m2);
                m2.Name = m.Name;
                var         mat = s.Materials[m.MaterialIndex];
                TextureSlot t1;

                if (mat.GetMaterialTextureCount(TextureType.Diffuse) > 0)
                {
                    t1 = mat.GetMaterialTextures(TextureType.Diffuse)[0];


                    if (t1.FilePath != null)
                    {
                        vm.TCol = new Tex.Tex2D(IPath + t1.FilePath, false);
                        Console.WriteLine("TexLoaded");
                    }
                    if (true)
                    {
                        if (new FileInfo(t1.FilePath).Exists == true)
                        {
                            //  var tex = App.AppSal.CreateTex2D();
                            //  tex.Path = t1.FilePath;
                            // tex.Load();
                            //m2.DiffuseMap = tex;
                        }
                    }
                }
                for (int i = 0; i < m2.NumVertices; i++)
                {
                    var      v = m.Vertices[i];
                    var      n = m.Normals[i];
                    var      t = m.TextureCoordinateChannels[0];
                    Vector3D tan, bi;
                    if (m.Tangents != null && m.Tangents.Count > 0)
                    {
                        tan = m.Tangents[i];
                        bi  = m.BiTangents[i];
                    }
                    else
                    {
                        tan = new Vector3D(0, 0, 0);
                        bi  = new Vector3D(0, 0, 0);
                    }
                    if (t.Count() == 0)
                    {
                        m2.SetVertex(i, Cv(v), Cv(tan), Cv(bi), Cv(n), Cv2(t[i]));
                    }
                    else
                    {
                        m2.SetVertex(i, Cv(v), Cv(tan), Cv(bi), Cv(n), Cv2(t[i]));
                    }
                }
                int[]  id = m.GetIndices();
                int    fi = 0;
                uint[] nd = new uint[id.Length];
                for (int i = 0; i < id.Length; i++)
                {
                    nd[i] = (uint)id[i];
                }

                m2.Indices = nd;

                m2.Final();
            }

            ProcessNode(root, s.RootNode, ml2);


            return(root as GraphNode3D);
        }
Ejemplo n.º 2
0
		static public AssimpVolume LoadFromFile(string filename)
		{
			string path = Path.Combine("Assets", "Models", filename);

			AssimpContext importer = new AssimpContext();

			NormalSmoothingAngleConfig normalSmoothing = new NormalSmoothingAngleConfig(66.0f);
			importer.SetConfig(normalSmoothing);

			LogStream logStream = new LogStream
			(
				delegate(string message, string userData)
				{
					Console.Write(message);
				}
			);
			logStream.Attach();

			Scene model = importer.ImportFile(path, PostProcessPreset.TargetRealTimeMaximumQuality);
			Mesh mesh = model.Meshes[0];

			AssimpVolume v = new AssimpVolume();

			List<Vector3> newVertices = new List<Vector3>();
			foreach (Assimp.Vector3D vert in mesh.Vertices)
			{
				newVertices.Add(new Vector3(vert.X, vert.Y, vert.Z));
			}
			v.vertices = newVertices.ToArray();

			v.indices = mesh.GetIndices();

			if (mesh.HasNormals)
			{
				v.generateNormals = false;
				List<Vector3> newNormals = new List<Vector3>();
				foreach (Assimp.Vector3D n in mesh.Normals)
				{
					newNormals.Add(new Vector3(n.X, n.Y, n.Z));
				}
				v.normals = newNormals.ToArray();
			}

			if (mesh.HasTextureCoords(0))
			{
				List<Vector2> newTextureCoords = new List<Vector2>();
				foreach (Assimp.Vector3D tc in mesh.TextureCoordinateChannels[0])
				{
					newTextureCoords.Add(new Vector2(tc.X, tc.Y));
				}
				v.textureCoords = newTextureCoords.ToArray();
			}

			if (mesh.HasVertexColors(0))
			{
				List<Vector3> newColors = new List<Vector3>();
				foreach (Assimp.Color4D c in mesh.VertexColorChannels[0])
				{
					newColors.Add(new Vector3(c.R, c.G, c.B));
				}
				v.colors = newColors.ToArray();
			}

			importer.Dispose();
			return v;
		}