コード例 #1
0
        protected void GenerateVertices()
        {
            var Count = VertexIndices.Count;

            VertexIndices.Clear();

            List <ObjMeshSection> SortedMeshSectionList = new List <ObjMeshSection>();

            foreach (var sectionlist in MeshSectionList.GroupBy(x => x.SectionName))
            {
                var sectionName  = sectionlist.First().SectionName;
                var sectionCount = sectionlist.Count();

                UInt32 StartIndex = (UInt32)VertexIndices.Count;

                foreach (var section in sectionlist)
                {
                    for (int i = (int)section.StartIndex; i < section.EndIndex; ++i)
                    {
                        var V1 = new PNTT_VertexAttribute();

                        V1.VertexPosition = TempVertexList[(int)VertexIndexList[i]];

                        if (HasTexCoordinate)
                        {
                            V1.TexCoord = TempTexCoordList[(int)TexCoordIndexList[i]];
                            V1.Tangent  = TempTangentList[(int)VertexIndexList[i]];
                        }

                        if (HasNormal)
                        {
                            V1.VertexNormal = TempNormalList[(int)NormalIndexList[i]];
                        }

                        uint index = 0;
                        if (GetSimilarVertexIndex(ref V1, out index))
                        {
                            VertexIndices.Add(index);
                        }
                        else
                        {
                            Vertices.Add(V1);
                            uint newIndex = (uint)Vertices.Count - 1;
                            VertexCacheMap.Add(V1, newIndex);
                            VertexIndices.Add(newIndex);
                        }
                    }
                }

                UInt32 EndIndex = (UInt32)VertexIndices.Count;

                SortedMeshSectionList.Add(new ObjMeshSection(sectionName, StartIndex, EndIndex));
            }

            MeshSectionList = SortedMeshSectionList;

            Debug.Assert(Count == VertexIndices.Count);
        }
コード例 #2
0
        public void Import(string FilePath, string MtlPath)
        {
            if (File.Exists(MtlPath))
            {
                ParseMtlFile(MtlPath);
            }

            if (File.Exists(FilePath))
            {
                var Lines = File.ReadAllLines(FilePath);

                foreach (var line in Lines)
                {
                    var Trimmedline = line.TrimStart(new char[] { ' ', '\t' });

                    if (Trimmedline.StartsWith("vn"))
                    {
                        var tokens = Trimmedline.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);

                        HasNormal = true;

                        if (tokens.Count() >= 4)
                        {
                            Vector3 VN = new Vector3();
                            VN.X = Convert.ToSingle(tokens[1]);
                            VN.Y = Convert.ToSingle(tokens[2]);
                            VN.Z = Convert.ToSingle(tokens[3]);

                            TempNormalList.Add(VN);
                        }
                    }
                    else if (Trimmedline.StartsWith("v "))
                    {
                        var tokens = Trimmedline.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);

                        if (tokens.Count() == 4)
                        {
                            Vector3 V = new Vector3();
                            V.X = Convert.ToSingle(tokens[1]);
                            V.Y = Convert.ToSingle(tokens[2]);
                            V.Z = Convert.ToSingle(tokens[3]);

                            UpdateMinMaxVertex(ref V);

                            TempVertexList.Add(V);
                        }
                    }
                    else if (Trimmedline.StartsWith("vt"))
                    {
                        HasTexCoordinate = true;

                        var tokens = Trimmedline.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);

                        if (tokens.Count() >= 3)
                        {
                            Vector2 V = new Vector2();
                            V.X = Convert.ToSingle(tokens[1]);
                            V.Y = Convert.ToSingle(tokens[2]);

                            TempTexCoordList.Add(V);
                        }
                    }
                    else if (Trimmedline.StartsWith("f "))
                    {
                        var tokens = Trimmedline.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);

                        if (tokens.Count() == 4)
                        {
                            var Token1 = tokens[1].Split('/');
                            var Token2 = tokens[2].Split('/');
                            var Token3 = tokens[3].Split('/');

                            uint Index1 = Convert.ToUInt32(Token1[0]);
                            uint Index2 = Convert.ToUInt32(Token2[0]);
                            uint Index3 = Convert.ToUInt32(Token3[0]);

                            VertexIndexList.Add(Index1 - 1);
                            VertexIndexList.Add(Index2 - 1);
                            VertexIndexList.Add(Index3 - 1);

                            if (HasTexCoordinate)
                            {
                                uint TexIndex1 = Convert.ToUInt32(Token1[1]);
                                uint TexIndex2 = Convert.ToUInt32(Token2[1]);
                                uint TexIndex3 = Convert.ToUInt32(Token3[1]);

                                TexCoordIndexList.Add(TexIndex1 - 1);
                                TexCoordIndexList.Add(TexIndex2 - 1);
                                TexCoordIndexList.Add(TexIndex3 - 1);
                            }

                            if (HasNormal)
                            {
                                uint NormIndex1 = Convert.ToUInt32(Token1[2]);
                                uint NormIndex2 = Convert.ToUInt32(Token2[2]);
                                uint NormIndex3 = Convert.ToUInt32(Token3[2]);

                                NormalIndexList.Add(NormIndex1 - 1);
                                NormalIndexList.Add(NormIndex2 - 1);
                                NormalIndexList.Add(NormIndex3 - 1);
                            }

                            VertexIndices.Add((uint)VertexIndices.Count);
                            VertexIndices.Add((uint)VertexIndices.Count);
                            VertexIndices.Add((uint)VertexIndices.Count);
                        }
                    }
                    else if (Trimmedline.StartsWith("usemtl"))
                    {
                        var MtlLine = Trimmedline.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);

                        if (MtlLine.Count() == 2)
                        {
                            if (MeshSectionList.Count() == 0)
                            {
                                ObjMeshSection NewSection = new ObjMeshSection();
                                NewSection.StartIndex  = 0;
                                NewSection.SectionName = MtlLine[1];
                                MeshSectionList.Add(NewSection);
                            }
                            else
                            {
                                MeshSectionList.Last().EndIndex = (UInt32)VertexIndices.Count;

                                ObjMeshSection NewSection = new ObjMeshSection();
                                NewSection.SectionName = MtlLine[1];
                                NewSection.StartIndex  = (UInt32)VertexIndices.Count;
                                MeshSectionList.Add(NewSection);
                            }
                        }
                    }
                }

                if (MeshSectionList.Count > 0)
                {
                    MeshSectionList.Last().EndIndex = (UInt32)VertexIndices.Count;
                }
            }

            // update min,max,center
            MinVertex    = new Vector3(MinX, MinY, MinZ);
            MaxVertex    = new Vector3(MaxX, MaxY, MaxZ);
            CenterVertex = (MinVertex + MaxVertex) / 2;


            if (HasTexCoordinate)
            {
                GenerateTangents();
            }

            GenerateVertices();

            Clear();
        }