private static Vertex GetVertex(PAKOBJ obj, int v0Offset, int n0Offset) { var v0 = obj.Verticies[v0Offset / VertexData.RecordSize]; var n0 = obj.Normals[n0Offset / NormalData.RecordSize]; Vertex v = new Vertex(); v.Position = new Point3D(v0.RawX, v0.RawY, v0.RawZ); v.Normal = new Vector3D(n0.RawX, n0.RawY, n0.RawZ); v.Texture = new Point2D(v0.RawU, v0.RawV) * (1 / 65536.0f); v.Normal.Normalize(); return v; }
public static Model LoadModel(PAKFile file, PAKOBJ obj) { Model mdl = new Model(); Func<FaceData, int> offConv = f => { int idx = f.ImageOffset/ImageData.RecordSize; return 2*idx + (obj.Images[idx].MaybeTransparent ? 1 : 0); }; var faceGroups = obj.Faces.GroupBy(offConv).ToList(); foreach (var faceGroup in faceGroups) { var imageIdx = faceGroup.First().ImageOffset/ImageData.RecordSize; var image = obj.Images[imageIdx]; var texture = file.Textures.Single(t => t.Name.EqualsCI(image.ImageName)); Mesh m = new Mesh(); m.Material = new Material { Transparent = image.MaybeTransparent, TextureName = texture.Name, }; mdl.Textures[m.Material.TextureName] = texture.Image; foreach (var face in faceGroup) { Face f = new Face(); f.Vertices[0] = GetVertex(obj, face.Vertex1Offset, face.Normal1Offset); f.Vertices[1] = GetVertex(obj, face.Vertex2Offset, face.Normal2Offset); f.Vertices[2] = GetVertex(obj, face.Vertex3Offset, face.Normal3Offset); m.Faces.Add(f); } mdl.Meshes.Add(m); mdl.Instances.Add(new MeshInstance { MeshIndex = mdl.Meshes.Count - 1, Transform = Matrix3D.Identity }); } return mdl; }
private void Deserialize() { var reader = OpenReader(0); string version = new string(reader.ReadChars(4)); int u1 = reader.ReadInt32(); string name = reader.ReadNullTermString(8); int u2 = reader.ReadInt32(); int[] extents = new int[6]; int[] counts = new int[6]; int[] indexes = new int[6]; for (int i = 0; i < extents.Length; i++) { extents[i] = reader.ReadInt32(); } for (int i = 0; i < counts.Length; i++) { counts[i] = reader.ReadInt32(); } for (int i = 0; i < indexes.Length; i++) { indexes[i] = reader.ReadInt32(); } var imageRaw = ReadBytes(indexes[0], counts[0] * ImageData.RecordSize); var vertexRaw = ReadBytes(indexes[1], counts[1] * VertexData.RecordSize); var faceRaw = ReadBytes(indexes[2], counts[2] * FaceData.RecordSize); var normalRaw = ReadBytes(indexes[3], counts[3] * NormalData.RecordSize); var data4 = ReadBytes(indexes[4], counts[4] * UnknownData4.RecordSize); var data5 = ReadBytes(indexes[5], counts[5] * AttachmentPoint.RecordSize); OBJ = new PAKOBJ { Version = version, Unknown1 = u1, Name = name, Unknown2 = u2, MinX = extents[0], MaxX = extents[1], MinY = extents[2], MaxY = extents[3], MinZ = extents[4], MaxZ = extents[5], ImageCount = counts[0], VertexCount = counts[1], FaceCount = counts[2], NormalCount = counts[3], Data4Count = counts[4], Data5Count = counts[5], ImageIndex = indexes[0], VertexIndex = indexes[1], FaceIndex = indexes[2], NormalIndex = indexes[3], Data4Index = indexes[4], Data5Index = indexes[5], ImageRaw = imageRaw, VertexRaw = vertexRaw, FaceRaw = faceRaw, NormalRaw = normalRaw, Data4 = data4, Data5 = data5, Images = ParseImages(imageRaw), Verticies = ParseRecord <VertexData>(vertexRaw), Faces = ParseRecord <FaceData>(faceRaw), Normals = ParseRecord <NormalData>(normalRaw), UnknownDatum4 = ParseRecord <UnknownData4>(data4), AttachmentPoints = ParseRecord <AttachmentPoint>(data5) }; }