private void ReadMeshVertexColors(XFileTokenReader tokenReader)
        {
            if (tokenReader.FileReader.GetNextToken() == XToken.Name)
            {
                tokenReader.ReadName();
            }

            tokenReader.ReadAssert(XToken.OpenedBrace);

            int vertexCount = tokenReader.ReadIntegerFromList();

            for (int i = 0; i < vertexCount; i++)
            {
                int        index = tokenReader.ReadIntegerFromList();
                XColorRgba color = new XColorRgba
                {
                    Red   = tokenReader.ReadFloatFromList(),
                    Green = tokenReader.ReadFloatFromList(),
                    Blue  = tokenReader.ReadFloatFromList(),
                    Alpha = tokenReader.ReadFloatFromList(),
                };

                tokenReader.ReadSeparator();

                this.VertexColors.Add(Tuple.Create(index, color));
            }

            tokenReader.ReadSeparator();

            tokenReader.ReadAssert(XToken.ClosedBrace);
        }
        private void ReadMeshTextureCoords(XFileTokenReader tokenReader)
        {
            if (tokenReader.FileReader.GetNextToken() == XToken.Name)
            {
                tokenReader.ReadName();
            }

            tokenReader.ReadAssert(XToken.OpenedBrace);

            int nTextureCoords = tokenReader.ReadIntegerFromList();

            this.TextureCoords.Capacity = nTextureCoords;

            for (int i = 0; i < nTextureCoords; i++)
            {
                var coord = new XCoords2d
                {
                    U = tokenReader.ReadFloatFromList(),
                    V = tokenReader.ReadFloatFromList()
                };

                this.TextureCoords.Add(coord);
            }

            tokenReader.ReadAssert(XToken.ClosedBrace);
        }
        private void ReadMeshNormals(XFileTokenReader tokenReader)
        {
            if (tokenReader.FileReader.GetNextToken() == XToken.Name)
            {
                tokenReader.ReadName();
            }

            tokenReader.ReadAssert(XToken.OpenedBrace);

            int nNormals = tokenReader.ReadIntegerFromList();

            this.Normals.Capacity = nNormals;

            for (int i = 0; i < nNormals; i++)
            {
                var vector = new XVector
                {
                    X = tokenReader.ReadFloatFromList(),
                    Y = tokenReader.ReadFloatFromList(),
                    Z = tokenReader.ReadFloatFromList()
                };

                this.Normals.Add(vector);
            }

            tokenReader.ReadSeparator();

            int nFacesNormals = tokenReader.ReadIntegerFromList();

            this.FacesNormalsIndices.Capacity = nFacesNormals;

            for (int faceIndex = 0; faceIndex < nFacesNormals; faceIndex++)
            {
                int indicesCount = tokenReader.ReadIntegerFromList();
                var vertices     = new List <int>(indicesCount);

                for (int i = 0; i < indicesCount; i++)
                {
                    int v = tokenReader.ReadIntegerFromList();
                    vertices.Add(v);
                }

                this.FacesNormalsIndices.Add(vertices);
            }

            tokenReader.ReadSeparator();
            tokenReader.ReadAssert(XToken.ClosedBrace);
        }
        internal static XSkinWeights Read(XFileTokenReader tokenReader)
        {
            var skin = new XSkinWeights();

            if (tokenReader.FileReader.GetNextToken() == XToken.Name)
            {
                skin.Name = tokenReader.ReadName();
            }

            tokenReader.ReadAssert(XToken.OpenedBrace);

            skin.TransformNodeName = tokenReader.ReadString();

            int nWeights = tokenReader.ReadIntegerFromList();

            skin.VertexIndices.Capacity = nWeights;
            skin.Weights.Capacity       = nWeights;

            for (int i = 0; i < nWeights; i++)
            {
                skin.VertexIndices.Add(tokenReader.ReadIntegerFromList());
            }

            tokenReader.ReadSeparator();

            for (int i = 0; i < nWeights; i++)
            {
                skin.Weights.Add(tokenReader.ReadFloatFromList());
            }

            tokenReader.ReadSeparator();

            skin.MatrixOffset = new XMatrix4x4
            {
                Matrix = tokenReader.ReadFloatArrayFromList(16)
            };

            tokenReader.ReadAssert(XToken.ClosedBrace);

            return(skin);
        }
        internal static XMesh Read(XFileTokenReader tokenReader)
        {
            var mesh = new XMesh();

            if (tokenReader.FileReader.GetNextToken() == XToken.Name)
            {
                mesh.Name = tokenReader.ReadName();
            }

            tokenReader.ReadAssert(XToken.OpenedBrace);

            int nVertices = tokenReader.ReadIntegerFromList();

            mesh.Vertices.Capacity = nVertices;

            for (int i = 0; i < nVertices; i++)
            {
                var vector = new XVector
                {
                    X = tokenReader.ReadFloatFromList(),
                    Y = tokenReader.ReadFloatFromList(),
                    Z = tokenReader.ReadFloatFromList()
                };

                mesh.Vertices.Add(vector);
            }

            int nFaces = tokenReader.ReadIntegerFromList();

            mesh.FacesIndices.Capacity = nFaces;

            for (int faceIndex = 0; faceIndex < nFaces; faceIndex++)
            {
                int indicesCount = tokenReader.ReadIntegerFromList();
                var vertices     = new List <int>(indicesCount);

                for (int i = 0; i < indicesCount; i++)
                {
                    vertices.Add(tokenReader.ReadIntegerFromList());
                }

                mesh.FacesIndices.Add(vertices);
            }

            bool meshMaterialListRead         = false;
            bool meshNormalsRead              = false;
            bool meshTextureCoordsRead        = false;
            bool vertexDuplicationIndicesRead = false;
            bool meshVertexColorsRead         = false;
            bool xSkinMeshHeaderRead          = false;
            bool fvfDataRead        = false;
            bool vertexElementsRead = false;

            while (tokenReader.FileReader.GetNextToken() != XToken.ClosedBrace)
            {
                string identifier = tokenReader.ReadName();

                if (string.IsNullOrEmpty(identifier))
                {
                    if (tokenReader.FileReader is XFileBinaryReader binaryReader)
                    {
                        identifier = binaryReader.ReadNullTerminatedString();
                    }
                }

                switch (identifier)
                {
                case "MeshMaterialList":
                    if (meshMaterialListRead)
                    {
                        throw new InvalidDataException();
                    }

                    mesh.ReadMeshMaterialList(tokenReader);
                    meshMaterialListRead = true;
                    break;

                case "MeshNormals":
                    if (meshNormalsRead)
                    {
                        //throw new InvalidDataException();
                        mesh.Normals.Clear();
                        mesh.FacesNormalsIndices.Clear();
                    }

                    mesh.ReadMeshNormals(tokenReader);
                    meshNormalsRead = true;
                    break;

                case "MeshTextureCoords":
                    if (meshTextureCoordsRead)
                    {
                        //throw new InvalidDataException();
                        mesh.TextureCoords.Clear();
                    }

                    mesh.ReadMeshTextureCoords(tokenReader);
                    meshTextureCoordsRead = true;
                    break;

                case "VertexDuplicationIndices":
                    if (vertexDuplicationIndicesRead)
                    {
                        throw new InvalidDataException();
                    }

                    mesh.ReadVertexDuplicationIndices(tokenReader);
                    vertexDuplicationIndicesRead = true;
                    break;

                case "MeshVertexColors":
                    if (meshVertexColorsRead)
                    {
                        throw new InvalidDataException();
                    }

                    mesh.ReadMeshVertexColors(tokenReader);
                    meshVertexColorsRead = true;
                    break;

                case "XSkinMeshHeader":
                    if (xSkinMeshHeaderRead)
                    {
                        throw new InvalidDataException();
                    }

                    mesh.ReadXSkinMeshHeader(tokenReader);
                    xSkinMeshHeaderRead = true;
                    break;

                case "SkinWeights":
                {
                    XSkinWeights skin = XSkinWeights.Read(tokenReader);
                    mesh.SkinWeights.Add(skin);
                    break;
                }

                case "FVFData":
                {
                    if (fvfDataRead)
                    {
                        throw new InvalidDataException();
                    }

                    mesh.ReadFVFData(tokenReader);
                    fvfDataRead = true;
                    break;
                }

                case "DeclData":
                {
                    if (vertexElementsRead)
                    {
                        throw new InvalidDataException();
                    }

                    mesh.ReadVertexElements(tokenReader);
                    vertexElementsRead = true;
                    break;
                }

                case "":
                    throw new InvalidDataException();

                default:
                    throw new NotImplementedException();
                }
            }

            tokenReader.ReadAssert(XToken.ClosedBrace);

            return(mesh);
        }
示例#6
0
        internal static XFrame Read(XFileTokenReader tokenReader)
        {
            var frame = new XFrame();

            if (tokenReader.FileReader.GetNextToken() == XToken.Name || tokenReader.FileReader.GetNextToken() == XToken.None)
            {
                frame.Name = tokenReader.ReadName();

                if (string.IsNullOrEmpty(frame.Name))
                {
                    if (tokenReader.FileReader is XFileBinaryReader binaryReader)
                    {
                        frame.Name = binaryReader.ReadNullTerminatedString();
                    }
                }
            }

            tokenReader.ReadAssert(XToken.OpenedBrace);

            bool frameTransformMatrixRead = false;

            while (tokenReader.FileReader.GetNextToken() != XToken.ClosedBrace)
            {
                string identifier = tokenReader.ReadName();

                if (string.IsNullOrEmpty(identifier))
                {
                    if (tokenReader.FileReader is XFileBinaryReader binaryReader)
                    {
                        identifier = binaryReader.ReadNullTerminatedString();
                    }
                }

                switch (identifier)
                {
                case "FrameMeshName":
                {
                    tokenReader.ReadAssert(XToken.OpenedBrace);
                    int    renderPass = tokenReader.ReadIntegerFromList();
                    string meshName   = tokenReader.ReadString();
                    tokenReader.ReadAssert(XToken.ClosedBrace);
                    frame.MeshesNames.Add(renderPass, meshName);
                    break;
                }

                case "FrameTransformMatrix":
                {
                    if (frameTransformMatrixRead)
                    {
                        throw new InvalidDataException();
                    }

                    tokenReader.ReadAssert(XToken.OpenedBrace);

                    frame.TransformMatrix = new XMatrix4x4
                    {
                        Matrix = tokenReader.ReadFloatArrayFromList(16)
                    };

                    tokenReader.ReadAssert(XToken.ClosedBrace);
                    frameTransformMatrixRead = true;
                    break;
                }

                case "FrameCamera":
                {
                    if (frame.FrameCamera != null)
                    {
                        throw new InvalidDataException();
                    }

                    var camera = new XFrameCamera();

                    tokenReader.ReadAssert(XToken.OpenedBrace);
                    camera.RotationScaler = tokenReader.ReadFloatFromList();
                    camera.MoveScaler     = tokenReader.ReadFloatFromList();
                    tokenReader.ReadAssert(XToken.ClosedBrace);

                    frame.FrameCamera = camera;
                    break;
                }

                case "Mesh":
                {
                    XMesh mesh = XMesh.Read(tokenReader);
                    frame.Meshes.Add(mesh);
                    break;
                }

                case "Frame":
                {
                    XFrame subFrame = XFrame.Read(tokenReader);
                    frame.Frames.Add(subFrame);
                    break;
                }

                case "":
                    throw new InvalidDataException();

                default:
                    throw new NotImplementedException();
                }
            }

            tokenReader.ReadAssert(XToken.ClosedBrace);

            return(frame);
        }
        internal static XMaterial Read(XFileTokenReader tokenReader)
        {
            var material = new XMaterial();

            if (tokenReader.FileReader.GetNextToken() == XToken.Name)
            {
                material.Name = tokenReader.ReadName();
            }

            tokenReader.ReadAssert(XToken.OpenedBrace);

            material.FaceColor = new XColorRgba
            {
                Red   = tokenReader.ReadFloatFromList(),
                Green = tokenReader.ReadFloatFromList(),
                Blue  = tokenReader.ReadFloatFromList(),
                Alpha = tokenReader.ReadFloatFromList()
            };

            material.Power = tokenReader.ReadFloatFromList();

            material.SpecularColor = new XColorRgb
            {
                Red   = tokenReader.ReadFloatFromList(),
                Green = tokenReader.ReadFloatFromList(),
                Blue  = tokenReader.ReadFloatFromList()
            };

            material.EmissiveColor = new XColorRgb
            {
                Red   = tokenReader.ReadFloatFromList(),
                Green = tokenReader.ReadFloatFromList(),
                Blue  = tokenReader.ReadFloatFromList()
            };

            bool textureFilenameRead = false;
            bool effectInstanceRead  = false;

            while (tokenReader.FileReader.GetNextToken() != XToken.ClosedBrace)
            {
                string identifier = tokenReader.ReadName();

                switch (identifier)
                {
                case "TextureFilename":
                    if (textureFilenameRead)
                    {
                        throw new InvalidDataException();
                    }

                    material.ReadTextureFilename(tokenReader);
                    textureFilenameRead = true;
                    break;

                case "EffectInstance":
                    if (effectInstanceRead)
                    {
                        throw new InvalidDataException();
                    }

                    material.EffectInstance = XEffectInstance.Read(tokenReader);
                    effectInstanceRead      = true;
                    break;

                default:
                    throw new InvalidDataException();
                }
            }

            tokenReader.ReadAssert(XToken.ClosedBrace);

            return(material);
        }