internal static XAnimationKey Read(XFileTokenReader tokenReader) { var key = new XAnimationKey(); if (tokenReader.FileReader.GetNextToken() == XToken.Name) { key.Name = tokenReader.ReadName(); } tokenReader.ReadAssert(XToken.OpenedBrace); key.KeyType = (XAnimationKeyType)tokenReader.ReadIntegerFromList(); int keysCount = tokenReader.ReadIntegerFromList(); key.Keys.Capacity = keysCount; for (int i = 0; i < keysCount; i++) { int time = tokenReader.ReadIntegerFromList(); int valuesCount = tokenReader.ReadIntegerFromList(); float[] values = tokenReader.ReadFloatArrayFromList(valuesCount); tokenReader.ReadSeparator(); key.Keys.Add(Tuple.Create(time, values)); } tokenReader.ReadAssert(XToken.ClosedBrace); return(key); }
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 ReadMeshMaterialList(XFileTokenReader tokenReader) { if (tokenReader.FileReader.GetNextToken() == XToken.Name) { tokenReader.ReadName(); } tokenReader.ReadAssert(XToken.OpenedBrace); int nMaterials = tokenReader.ReadIntegerFromList(); int nFaceIndexes = tokenReader.ReadIntegerFromList(); this.MaterialsFacesIndices.Capacity = nFaceIndexes; for (int i = 0; i < nFaceIndexes; i++) { this.MaterialsFacesIndices.Add(tokenReader.ReadIntegerFromList()); } for (int i = 0; i < nMaterials; i++) { XToken token = tokenReader.FileReader.GetNextToken(); if (token == XToken.OpenedBrace) { tokenReader.ReadAssert(XToken.OpenedBrace); string reference = tokenReader.ReadName(); tokenReader.ReadAssert(XToken.ClosedBrace); XMaterial material = new XMaterial { Name = reference, IsReference = true }; this.Materials.Add(material); } else { string identifier = tokenReader.ReadName(); switch (identifier) { case "Material": { XMaterial material = XMaterial.Read(tokenReader); this.Materials.Add(material); break; } default: throw new InvalidDataException(); } } } tokenReader.ReadAssert(XToken.ClosedBrace); }
private void ReadXSkinMeshHeader(XFileTokenReader tokenReader) { if (tokenReader.FileReader.GetNextToken() == XToken.Name) { tokenReader.ReadName(); } tokenReader.ReadAssert(XToken.OpenedBrace); this.MaxSkinWeightsPerVertex = tokenReader.ReadIntegerFromList(); this.MaxSkinWeightsPerFace = tokenReader.ReadIntegerFromList(); this.BonesCount = tokenReader.ReadIntegerFromList(); 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); }
private void ReadVertexDuplicationIndices(XFileTokenReader tokenReader) { if (tokenReader.FileReader.GetNextToken() == XToken.Name) { tokenReader.ReadName(); } tokenReader.ReadAssert(XToken.OpenedBrace); int indicesCount = tokenReader.ReadIntegerFromList(); this.OriginalVerticesCount = tokenReader.ReadIntegerFromList(); this.VertexDuplicationIndices = tokenReader.ReadIntegerArrayFromList(indicesCount); 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); }
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); }
private void ReadVertexElements(XFileTokenReader tokenReader) { if (tokenReader.FileReader.GetNextToken() == XToken.Name) { tokenReader.ReadName(); } tokenReader.ReadAssert(XToken.OpenedBrace); int nElements = tokenReader.ReadIntegerFromList(); this.VertexElements.Capacity = nElements; for (int i = 0; i < nElements; i++) { var element = new XVertexElement { DataType = (XVertexElementDataType)tokenReader.ReadIntegerFromList(), Method = (XVertexElementMethod)tokenReader.ReadIntegerFromList(), Usage = (XVertexElementUsage)tokenReader.ReadIntegerFromList(), UsageIndex = tokenReader.ReadIntegerFromList(), }; this.VertexElements.Add(element); } int nDWords = tokenReader.ReadIntegerFromList(); this.VertexElementsData = tokenReader.ReadUnsignedIntegerArrayFromList(nDWords); tokenReader.ReadSeparator(); tokenReader.ReadAssert(XToken.ClosedBrace); }
private void ReadEffectParamDWord(XFileTokenReader tokenReader) { tokenReader.ReadAssert(XToken.OpenedBrace); string paramName = tokenReader.ReadString(); int value = tokenReader.ReadIntegerFromList(); tokenReader.ReadSeparator(); this.IntegerParameters.Add(Tuple.Create(paramName, value)); tokenReader.ReadAssert(XToken.ClosedBrace); }
private void ReadEffectParamFloats(XFileTokenReader tokenReader) { tokenReader.ReadAssert(XToken.OpenedBrace); string paramName = tokenReader.ReadString(); int valuesCount = tokenReader.ReadIntegerFromList(); float[] values = tokenReader.ReadFloatArrayFromList(valuesCount); tokenReader.ReadSeparator(); this.FloatParameters.Add(Tuple.Create(paramName, values)); tokenReader.ReadAssert(XToken.ClosedBrace); }
private void ReadFVFData(XFileTokenReader tokenReader) { if (tokenReader.FileReader.GetNextToken() == XToken.Name) { tokenReader.ReadName(); } tokenReader.ReadAssert(XToken.OpenedBrace); this.FVF = tokenReader.ReadUnsignedIntegerFromList(); int count = tokenReader.ReadIntegerFromList(); for (int i = 0; i < count; i++) { this.FVFData.Add(tokenReader.ReadUnsignedIntegerFromList()); } tokenReader.ReadSeparator(); tokenReader.ReadAssert(XToken.ClosedBrace); }
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); }
internal static XAnimation Read(XFileTokenReader tokenReader) { var animation = new XAnimation(); if (tokenReader.FileReader.GetNextToken() == XToken.Name) { animation.Name = tokenReader.ReadName(); } tokenReader.ReadAssert(XToken.OpenedBrace); XToken token; while ((token = tokenReader.FileReader.GetNextToken()) != XToken.ClosedBrace) { if (token == XToken.OpenedBrace) { if (animation.FrameReference != null) { throw new InvalidDataException(); } tokenReader.ReadAssert(XToken.OpenedBrace); animation.FrameReference = tokenReader.ReadName(); tokenReader.ReadAssert(XToken.ClosedBrace); continue; } string identifier = tokenReader.ReadName(); switch (identifier) { case "AnimationKey": { XAnimationKey key = XAnimationKey.Read(tokenReader); animation.Keys.Add(key); break; } case "AnimationOptions": { if (tokenReader.FileReader.GetNextToken() == XToken.Name) { tokenReader.ReadName(); } tokenReader.ReadAssert(XToken.OpenedBrace); animation.OpenClosedOption = tokenReader.ReadIntegerFromList(); animation.PositionQualityOption = tokenReader.ReadIntegerFromList(); tokenReader.ReadAssert(XToken.ClosedBrace); break; } default: throw new NotImplementedException(); } } tokenReader.ReadAssert(XToken.ClosedBrace); return(animation); }
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); }
private void ReadFile(IXFileReader fileReader) { var tokenReader = new XFileTokenReader(fileReader); var templateReader = new XFileTemplateReader(tokenReader); while (fileReader.GetNextToken() == XToken.Template) { Tuple <string, Guid> template = templateReader.ReadTemplate(); this.Templates.Add(template); } bool headerRead = false; bool animTicksPerSecondRead = false; while (fileReader.GetNextToken() == XToken.Name) { string identifier = tokenReader.ReadName(); switch (identifier) { case "Header": { if (headerRead) { throw new InvalidDataException(); } tokenReader.ReadAssert(XToken.OpenedBrace); int majorVersion = tokenReader.ReadIntegerFromList(); int minorVersion = tokenReader.ReadIntegerFromList(); this.FileVersion = new Version(majorVersion, minorVersion); this.FileFlags = tokenReader.ReadIntegerFromList(); tokenReader.ReadAssert(XToken.ClosedBrace); headerRead = true; break; } case "Material": { XMaterial material = XMaterial.Read(tokenReader); this.Materials.Add(material); break; } case "Mesh": { XMesh mesh = XMesh.Read(tokenReader); this.Meshes.Add(mesh); break; } case "Frame": { XFrame frame = XFrame.Read(tokenReader); this.Frames.Add(frame); break; } case "AnimationSet": { XAnimationSet animationSet = XAnimationSet.Read(tokenReader); this.AnimationSets.Add(animationSet); break; } case "AnimTicksPerSecond": { if (animTicksPerSecondRead) { throw new InvalidDataException(); } tokenReader.ReadAssert(XToken.OpenedBrace); this.AnimTicksPerSecond = tokenReader.ReadIntegerFromList(); tokenReader.ReadAssert(XToken.ClosedBrace); animTicksPerSecondRead = true; break; } default: throw new NotImplementedException(); } } }