public void ExportCollisionToM2T(string directory, string name) { this.name = name; using (BinaryWriter writer = new BinaryWriter(File.Create(directory + "\\" + name + ".m2t"))) { writer.Write(fileHeader.ToCharArray()); writer.Write(fileVersion); //mesh name writer.Write(name); writer.Write(isSkinned); //Number of Lods writer.Write((byte)1); for (int i = 0; i != 1; i++) { //Write section for VertexFlags. writer.Write(257); //write length and then all vertices. writer.Write(lods[i].Vertices.Length); for (int x = 0; x != lods[i].Vertices.Length; x++) { Vertex vert = lods[i].Vertices[x]; vert.Position.WriteToFile(writer); Half2Extenders.WriteToFile(new Half2(0.0f, 0.0f), writer); } writer.Write(lods[i].Parts.Length); for (int x = 0; x != lods[i].Parts.Length; x++) { writer.Write(lods[i].Parts[x].Material); writer.Write(lods[i].Parts[x].StartIndex); writer.Write(lods[i].Parts[x].NumFaces); } //write triangle data. writer.Write(lods[i].Indices.Length); for (int x = 0; x != lods[i].Indices.Length; x++) { writer.Write(lods[i].Indices[x]); } } } }
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.Skin; } if (reader.ReadBoolean()) { Lods[i].VertexDeclaration += (int)VertexFlags.Color; } 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.ShadowTexture; } if (reader.ReadBoolean()) { Lods[i].VertexDeclaration += (int)VertexFlags.Color1; } if (reader.ReadBoolean()) { Lods[i].VertexDeclaration += (int)VertexFlags.BBCoeffs; } 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 Half2[lods[i].NumUVChannels]; if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.Position)) { vert.Position = Vector3Extenders.ReadFromFile(reader); } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.Normals)) { vert.Normal = Vector3Extenders.ReadFromFile(reader); } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.Tangent)) { vert.Tangent = Vector3Extenders.ReadFromFile(reader); } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords0)) { vert.UVs[0] = Half2Extenders.ReadFromFile(reader); } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords1)) { vert.UVs[1] = Half2Extenders.ReadFromFile(reader); } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords2)) { vert.UVs[2] = Half2Extenders.ReadFromFile(reader); } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.ShadowTexture)) { vert.UVs[3] = Half2Extenders.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(); ulong hash = 0; ulong.TryParse(Lods[i].Parts[x].Material, out hash); Lods[i].Parts[x].Hash = hash; Lods[i].Parts[x].StartIndex = reader.ReadUInt32(); Lods[i].Parts[x].NumFaces = reader.ReadUInt32(); } int numIndices = reader.ReadInt32(); Lods[i].Indices = new ushort[numIndices]; for (int x = 0; x != Lods[i].Indices.Length; x++) { Lods[i].Indices[x] = reader.ReadUInt16(); } Lods[i].CalculatePartBounds(); } }
private void ReadM2TVersionTwo(BinaryReader reader) { //mesh name name = reader.ReadString(); isSkinned = reader.ReadBoolean(); if (isSkinned) { byte size = reader.ReadByte(); skeleton = new Skeleton(); skeleton.Joints = new Joint[size]; for (int i = 0; i < size; i++) { Joint joint = new Joint(); joint.Name = reader.ReadString8(); joint.ParentIndex = reader.ReadByte(); joint.Parent = (joint.ParentIndex != 0xFF) ? skeleton.Joints[joint.ParentIndex] : null; //may crash because root will not be in range Vector3 position = Vector3Extenders.ReadFromFile(reader); Quaternion rotation = QuaternionExtensions.ReadFromFile(reader); Vector3 scale = Vector3Extenders.ReadFromFile(reader); joint.WorldTransform = MatrixExtensions.SetMatrix(rotation, scale, position); skeleton.Joints[i] = joint; } } //Number of Lods Lods = new Lod[reader.ReadByte()]; for (int i = 0; i != Lods.Length; i++) { Lods[i] = new Lod { VertexDeclaration = 0 }; lods[i].VertexDeclaration = (VertexFlags)reader.ReadInt32(); //write length and then all vertices. lods[i].Vertices = new Vertex[reader.ReadInt32()]; for (int x = 0; x != lods[i].Vertices.Length; x++) { Vertex vert = new Vertex(); vert.UVs = new Half2[4]; if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.Position)) { vert.Position = Vector3Extenders.ReadFromFile(reader); } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.Normals)) { vert.Normal = Vector3Extenders.ReadFromFile(reader); } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.Tangent)) { vert.Tangent = Vector3Extenders.ReadFromFile(reader); } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.Skin)) { vert.BoneIDs = reader.ReadBytes(4); vert.BoneWeights = new float[4]; for (int z = 0; z < 4; z++) { vert.BoneWeights[z] = reader.ReadSingle(); } } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.Color)) { vert.Color0 = reader.ReadBytes(4); } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.Color1)) { vert.Color1 = reader.ReadBytes(4); } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords0)) { vert.UVs[0] = Half2Extenders.ReadFromFile(reader); } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords1)) { vert.UVs[1] = Half2Extenders.ReadFromFile(reader); } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords2)) { vert.UVs[2] = Half2Extenders.ReadFromFile(reader); } if (Lods[i].VertexDeclaration.HasFlag(VertexFlags.ShadowTexture)) { vert.UVs[3] = Half2Extenders.ReadFromFile(reader); } lods[i].Vertices[x] = vert; } //read 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(); Lods[i].Parts[x].StartIndex = reader.ReadUInt32(); Lods[i].Parts[x].NumFaces = reader.ReadUInt32(); var material = MaterialsManager.LookupMaterialByName(Lods[i].Parts[x].Material); if (material != null) { Lods[i].Parts[x].Hash = material.GetMaterialHash(); } } int numIndices = reader.ReadInt32(); Lods[i].Indices = new uint[numIndices]; for (int x = 0; x != Lods[i].Indices.Length; x++) { Lods[i].Indices[x] = reader.ReadUInt32(); } Lods[i].CalculatePartBounds(); } }