示例#1
0
        protected override void _Read(BinaryReader reader)
        {
            List <Vector3> vertices = new List <Vector3>();
            List <Vector3> normals  = new List <Vector3>();
            List <Vector2> uvs      = new List <Vector2>();

            Dictionary <int, Vertex> nodeVertices = new Dictionary <int, Vertex>();

            MeshFace currentFace = null;

            int currentTextureIndex = -1;

            MTL mtl = null;

            Textures.Clear();
            RootNode         = new ModelNode();
            RootNode.HasMesh = true;

            bool hasUV              = false;
            bool hasNormals         = false;
            bool faceTextureChanged = false;

            string[] lines = reader.ReadToEnd().Split('\n');
            foreach (string line in lines)
            {
                if (line.StartsWith("#") || String.IsNullOrEmpty(line))
                {
                    continue;
                }
                else if (line.StartsWith("mtllib "))
                {
                    string[] values = line.Split(' ');
                    if (values.Length < 2)
                    {
                        continue;
                    }

                    string mtlPath = "";
                    string dir     = Path.GetDirectoryName(FilePath);
                    mtlPath = String.Format("{0}\\{1}", Path.GetDirectoryName(FilePath), values[1]);

                    if (File.Exists(mtlPath))
                    {
                        mtl      = new MTL(mtlPath);
                        Textures = mtl.Textures;
                    }
                }
                else if (line.StartsWith("v "))
                {
                    string[] values = line.Split(' ');
                    if (values.Length < 4)
                    {
                        continue;
                    }
                    Vector3 vertex = new Vector3();
                    vertex.X = float.Parse(values[1], m_cultureInfo);
                    vertex.Y = float.Parse(values[2], m_cultureInfo);
                    vertex.Z = float.Parse(values[3], m_cultureInfo);
                    RootNode.VertexPositions.Add(vertex);
                }
                else if (line.StartsWith("vt "))
                {
                    string[] values = line.Split(' ');
                    if (values.Length < 3)
                    {
                        continue;
                    }
                    Vector2 uv = new Vector2();
                    uv.X = float.Parse(values[1], m_cultureInfo);
                    uv.Y = float.Parse(values[2], m_cultureInfo);
                    RootNode.VertexUVs.Add(uv);
                }
                else if (line.StartsWith("vn "))
                {
                    string[] values = line.Split(' ');
                    if (values.Length < 4)
                    {
                        continue;
                    }
                    Vector3 normal = new Vector3();
                    normal.X = float.Parse(values[1], m_cultureInfo);
                    normal.Y = float.Parse(values[2], m_cultureInfo);
                    normal.Z = float.Parse(values[3], m_cultureInfo);
                    RootNode.VertexNormals.Add(normal);
                }
                else if (line.StartsWith("f "))
                {
                    if (currentFace == null)
                    {
                        continue;
                    }

                    if (line.Contains("//"))
                    {
                        string[] stringSeparators = new string[] { "//" };

                        string[] values = line.Split(' ');
                        if (values.Length < 4)
                        {
                            continue;
                        }

                        string[] vert1Values = values[1].Split(stringSeparators, StringSplitOptions.None);
                        string[] vert2Values = values[2].Split(stringSeparators, StringSplitOptions.None);
                        string[] vert3Values = values[3].Split(stringSeparators, StringSplitOptions.None);

                        currentFace.PositionIndices.Add((ushort)(int.Parse(vert1Values[0]) - 1));
                        currentFace.NormalIndices.Add((ushort)(int.Parse(vert1Values[1]) - 1));
                        currentFace.PositionIndices.Add((ushort)(int.Parse(vert2Values[0]) - 1));
                        currentFace.NormalIndices.Add((ushort)(int.Parse(vert2Values[1]) - 1));
                        currentFace.PositionIndices.Add((ushort)(int.Parse(vert3Values[0]) - 1));
                        currentFace.NormalIndices.Add((ushort)(int.Parse(vert3Values[1]) - 1));
                    }
                    else if (line.Contains("/"))
                    {
                        string[] stringSeparators = new string[] { "/" };

                        string[] values = line.Split(' ');
                        if (values.Length < 4)
                        {
                            continue;
                        }

                        string[] vert1Values = values[1].Split(stringSeparators, StringSplitOptions.None);
                        string[] vert2Values = values[2].Split(stringSeparators, StringSplitOptions.None);
                        string[] vert3Values = values[3].Split(stringSeparators, StringSplitOptions.None);

                        currentFace.PositionIndices.Add((ushort)(int.Parse(vert1Values[0]) - 1));
                        currentFace.UVIndices.Add((ushort)(int.Parse(vert1Values[1]) - 1));
                        if (vert1Values.Length > 2)
                        {
                            currentFace.NormalIndices.Add((ushort)(int.Parse(vert1Values[2]) - 1));
                        }

                        currentFace.PositionIndices.Add((ushort)(int.Parse(vert2Values[0]) - 1));
                        currentFace.UVIndices.Add((ushort)(int.Parse(vert2Values[1]) - 1));
                        if (vert2Values.Length > 2)
                        {
                            currentFace.NormalIndices.Add((ushort)(int.Parse(vert2Values[2]) - 1));
                        }

                        currentFace.PositionIndices.Add((ushort)(int.Parse(vert3Values[0]) - 1));
                        currentFace.UVIndices.Add((ushort)(int.Parse(vert3Values[1]) - 1));
                        if (vert3Values.Length > 2)
                        {
                            currentFace.NormalIndices.Add((ushort)(int.Parse(vert3Values[2]) - 1));
                        }
                    }
                    else
                    {
                        string[] values = line.Split(' ');
                        if (values.Length < 4)
                        {
                            continue;
                        }

                        currentFace.PositionIndices.Add((ushort)(int.Parse(values[0]) - 1));
                        currentFace.PositionIndices.Add((ushort)(int.Parse(values[1]) - 1));
                        currentFace.PositionIndices.Add((ushort)(int.Parse(values[2]) - 1));
                    }
                }
                else if (line.StartsWith("usemtl "))
                {
                    if (mtl == null)
                    {
                        continue;
                    }
                    string[] values = line.Split(' ');
                    if (values.Length < 2)
                    {
                        continue;
                    }

                    if (faceTextureChanged)
                    {
                        RootNode.Faces.Add(currentFace);
                    }
                    currentFace              = new MeshFace();
                    currentTextureIndex      = mtl.GetMaterialTextureIndex(values[1]);
                    currentFace.TextureIndex = (uint)currentTextureIndex;
                    faceTextureChanged       = true;
                }
                else if (line.StartsWith("o ") || line.StartsWith("g "))
                {
                    if (currentFace != null)
                    {
                        RootNode.Faces.Add(currentFace);
                    }
                    currentFace        = new MeshFace();
                    faceTextureChanged = false;
                }
            }
            RootNode.Faces.Add(currentFace);
            RootNode.ResolveFaceTextures(Textures);
        }
示例#2
0
 private void WriteNode(BinaryWriter writer, ModelNode node)
 {
 }