public void ReadFromFile(BinaryReader reader) { unk = reader.ReadUInt32(); pos = new Short3(reader); size = new Short3(reader); }
public void ReadFromM2T(BinaryReader reader) { if (new string(reader.ReadChars(3)) != fileHeader) { return; } if (reader.ReadByte() != fileVersion) { return; } //mesh name name = reader.ReadString(); //Number of Lods Lods = new Lod[reader.ReadByte()]; for (int i = 0; i != Lods.Length; i++) { Lods[i] = new Lod { VertexDeclaration = 0 }; if (reader.ReadBoolean()) { Lods[i].VertexDeclaration += (int)VertexFlags.Position; } if (reader.ReadBoolean()) { Lods[i].VertexDeclaration += (int)VertexFlags.Normals; } if (reader.ReadBoolean()) { Lods[i].VertexDeclaration += (int)VertexFlags.Tangent; } if (reader.ReadBoolean()) { Lods[i].VertexDeclaration += (int)VertexFlags.BlendData; } if (reader.ReadBoolean()) { Lods[i].VertexDeclaration += (int)VertexFlags.flag_0x80; } if (reader.ReadBoolean()) { Lods[i].VertexDeclaration += (int)VertexFlags.TexCoords0; } if (reader.ReadBoolean()) { Lods[i].VertexDeclaration += (int)VertexFlags.TexCoords1; } if (reader.ReadBoolean()) { Lods[i].VertexDeclaration += (int)VertexFlags.TexCoords2; } if (reader.ReadBoolean()) { Lods[i].VertexDeclaration += (int)VertexFlags.TexCoords7; } if (reader.ReadBoolean()) { Lods[i].VertexDeclaration += (int)VertexFlags.flag_0x20000; } if (reader.ReadBoolean()) { Lods[i].VertexDeclaration += (int)VertexFlags.flag_0x40000; } if (reader.ReadBoolean()) { Lods[i].VertexDeclaration += (int)VertexFlags.DamageGroup; } //write length and then all vertices. lods[i].Vertices = new Vertex[reader.ReadInt32()]; lods[i].NumUVChannels = 4; for (int x = 0; x != lods[i].Vertices.Length; x++) { Vertex vert = new Vertex(); vert.UVs = new UVVector2[lods[i].NumUVChannels]; if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.Position)) { vert.Position.ReadfromFile(reader); } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.Normals)) { vert.Normal.ReadfromFile(reader); } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.Tangent)) { vert.Tangent.ReadfromFile(reader); } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords0)) { vert.UVs[0] = new UVVector2(); vert.UVs[0].ReadFromFile(reader); } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords1)) { vert.UVs[1] = new UVVector2(); vert.UVs[1].ReadFromFile(reader); } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords2)) { vert.UVs[2] = new UVVector2(); vert.UVs[2].ReadFromFile(reader); } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords7)) { vert.UVs[3] = new UVVector2(); vert.UVs[3].ReadFromFile(reader); } lods[i].Vertices[x] = vert; } //write mesh count and texture names. Lods[i].Parts = new ModelPart[reader.ReadInt32()]; for (int x = 0; x != Lods[i].Parts.Length; x++) { Lods[i].Parts[x] = new ModelPart(); Lods[i].Parts[x].Material = reader.ReadString(); } List <List <Short3> > partTriangles = new List <List <Short3> >(Lods[i].Parts.Length); for (int x = 0; x != partTriangles.Capacity; x++) { partTriangles.Add(new List <Short3>()); } int totalFaces = reader.ReadInt32(); for (int x = 0; x != totalFaces; x++) { Short3 tri = new Short3(reader); short matId = reader.ReadInt16(); partTriangles[matId].Add(tri); } for (int x = 0; x != Lods[i].Parts.Length; x++) { Lods[i].Parts[x].Indices = partTriangles[x].ToArray(); Lods[i].Parts[x].CalculatePartBounds(lods[i].Vertices); } } }
/// <summary> /// Build Lods from retrieved data. /// </summary> public void BuildLods(FrameGeometry frameGeometry, FrameMaterial frameMaterial, VertexBuffer[] vertexBuffers, IndexBuffer[] indexBuffers) { lods = new Lod[frameGeometry.NumLods]; for (int i = 0; i != lods.Length; i++) { FrameLOD frameLod = frameGeometry.LOD[i]; lods[i] = new Lod { VertexDeclaration = frameGeometry.LOD[i].VertexDeclaration }; IndexBuffer indexBuffer = indexBuffers[i]; VertexBuffer vertexBuffer = vertexBuffers[i]; int vertexSize; Dictionary <VertexFlags, FrameLOD.VertexOffset> vertexOffsets = frameLod.GetVertexOffsets(out vertexSize); lods[i].NumUVChannels = 4; lods[i].Vertices = new Vertex[frameLod.NumVertsPr]; for (int v = 0; v != lods[i].Vertices.Length; v++) { Vertex vertex = new Vertex(); vertex.UVs = new UVVector2[lods[i].NumUVChannels]; if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Position)) { int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Position].Offset; vertex.ReadPositionData(vertexBuffer.Data, startIndex, frameGeometry.DecompressionFactor, frameGeometry.DecompressionOffset); } if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Tangent)) { int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Position].Offset; vertex.ReadTangentData(vertexBuffer.Data, startIndex); } if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Normals)) { int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Normals].Offset; vertex.ReadNormalData(vertexBuffer.Data, startIndex); } if (lods[i].VertexDeclaration.HasFlag(VertexFlags.BlendData)) { int startIndex = v * vertexSize + vertexOffsets[VertexFlags.BlendData].Offset; vertex.ReadBlendData(vertexBuffer.Data, startIndex); } if (lods[i].VertexDeclaration.HasFlag(VertexFlags.flag_0x80)) { Console.WriteLine("Skip vertex with flag_0x80"); } if (lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords0)) { int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords0].Offset; vertex.ReadUvData(vertexBuffer.Data, startIndex, 0); } if (lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords1)) { int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords1].Offset; vertex.ReadUvData(vertexBuffer.Data, startIndex, 1); } if (lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords2)) { int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords2].Offset; vertex.ReadUvData(vertexBuffer.Data, startIndex, 2); } if (lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords7)) { int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords7].Offset; vertex.ReadUvData(vertexBuffer.Data, startIndex, 3); } if (lods[i].VertexDeclaration.HasFlag(VertexFlags.flag_0x20000)) { Console.WriteLine("Skip vertex with flag_0x20000"); } if (lods[i].VertexDeclaration.HasFlag(VertexFlags.flag_0x40000)) { Console.WriteLine("Skip vertex with flag_0x40000"); } if (lods[i].VertexDeclaration.HasFlag(VertexFlags.DamageGroup)) { int startIndex = v * vertexSize + vertexOffsets[VertexFlags.DamageGroup].Offset; vertex.ReadDamageGroup(vertexBuffer.Data, startIndex); } if (lods[i].NormalMapInfoPresent) { vertex.BuildBinormals(); } lods[i].Vertices[v] = vertex; MaterialStruct[] materials = frameMaterial.Materials[i]; lods[i].Parts = new ModelPart[materials.Length]; for (int x = 0; x != materials.Length; x++) { ModelPart modelPart = new ModelPart(); Material mat = MaterialsManager.LookupMaterialByHash(materials[x].MaterialHash); if (mat == null || mat.SPS.Length == 0) { modelPart.Material = "_test_gray"; } else { modelPart.Material = (mat == null) ? "null" : mat.SPS[0].File; } int num = materials[x].StartIndex + materials[x].NumFaces * 3; List <Short3> intList = new List <Short3>(materials[x].NumFaces); int startIndex = materials[x].StartIndex; while (startIndex < num) { Short3 indice = new Short3(); indice.S1 = indexBuffer.Data[startIndex + 0]; indice.S2 = indexBuffer.Data[startIndex + 1]; indice.S3 = indexBuffer.Data[startIndex + 2]; intList.Add(indice); startIndex += 3; } modelPart.Indices = intList.ToArray(); lods[i].Parts[x] = modelPart; } } } }
/// <summary> /// Build Int3 from Short3 /// </summary> /// <param name="ints"></param> public Int3(Short3 s3) { I1 = (int)s3.S1; I2 = (int)s3.S2; I3 = (int)s3.S3; }