public static int GetLength(MDLHeader header, bool useNbt) { int length = 5; if (header.NormalCount > 0) { length += 2; } if (useNbt) { //length += 4; } if (header.ColorCount > 0) { length += 2; } if (header.TextureCoordinateCount > 0) { length += 2; } return(length); }
public MDLVertex(DhBinaryReader br, MDLHeader header, bool useNbt) { MatrixIndex = br.ReadS8(); Tex0MatrixIndex = br.ReadS8(); Tex1MatrixIndex = br.ReadS8(); PositionIndex = br.ReadU16(); if (header.NormalCount > 0) { NormalIndex = br.ReadU16(); } if (useNbt) { //TangentIndex = br.ReadU16(); //BiTangentIndex = br.ReadU16(); } if (header.ColorCount > 0) { ColorIndex = br.ReadU16(); } if (header.TextureCoordinateCount > 0) { TexCoordIndex = br.ReadU16(); } }
public MDL(byte[] data) { // Define a binary reader to read with. DhBinaryReader br = new DhBinaryReader(data, DhEndian.Big); Header = new MDLHeader(br); br.Goto(Header.MatricesOffset); Matrices = new Mat4[Header.JointCount + Header.WeightCount]; for (int i = 0; i < Matrices.Length; i++) { Matrices[i] = Mat4.Identity; } for (int i = 0; i < Header.JointCount; i++) { float[] matrix = br.ReadF32s(12); Matrices[i] = new Mat4() { Row1 = new Vec4(matrix[0], matrix[1], matrix[2], matrix[3]), Row2 = new Vec4(matrix[4], matrix[5], matrix[6], matrix[7]), Row3 = new Vec4(matrix[8], matrix[9], matrix[10], matrix[11]), Row4 = new Vec4(0, 0, 0, 1), }; } br.Goto(Header.PositionOffset); Positions = br.ReadVec3s(Header.PositionCount).ToList(); br.Goto(Header.NormalOffset); Normals = br.ReadVec3s(Header.NormalCount).ToList(); br.Goto(Header.ColorOffset); Colors = br.ReadClr4s(Header.ColorCount).ToList(); br.Goto(Header.TextureCoordinateOffset); TextureCoordinates = br.ReadVec2s(Header.TextureCoordinateCount).ToList(); br.Goto(Header.PacketOffset); Packets = new List <MDLShapePacket>(); for (int i = 0; i < Header.PacketCount; i++) { Packets.Add(new MDLShapePacket(br)); } br.Goto(Header.ShapeOffset); Shapes = new List <MDLShape>(); for (int i = 0; i < Header.ShapeCount; i++) { Shapes.Add(new MDLShape(br, Packets)); } foreach (MDLShape shape in Shapes) { ushort[] matrixIndices = new ushort[10]; foreach (MDLShapePacket packet in shape.Packets) { for (int m = 0; m < packet.MatrixCount; m++) { if (packet.MatrixIndices[m] == ushort.MaxValue) { continue; } matrixIndices[m] = packet.MatrixIndices[m]; } DhBinaryReader reader = new DhBinaryReader(packet.Data, DhEndian.Big); while (reader.Position() < packet.DataSize) { ShapePacketData packetData = new ShapePacketData(); packetData.PrimitiveType = reader.ReadU8(); if (packetData.PrimitiveType == 0x00) { continue; } packetData.VertexCount = reader.ReadU16(); for (int i = 0; i < packetData.VertexCount; i++) { MDLVertex vertex = new MDLVertex(reader, Header, shape.UseNbt); if (vertex.MatrixIndex != -1) { vertex.MatrixDataIndex = matrixIndices[(vertex.MatrixIndex / 3)]; } packetData.Vertices.Add(vertex); } packet.PacketData.Add(packetData); } } } br.Goto(Header.TextureLocationOffset); Textures = new List <BTI>(); for (int i = 0; i < Header.TextureCount; i++) { uint offset = br.ReadU32(); long currentPosition = br.Position(); br.Goto(offset); BTI bti = new BTI(br, offset, Header.TextureLocationOffset); Textures.Add(bti); br.Goto(currentPosition); } br.Goto(Header.SamplerOffset); Samplers = new List <MDLSampler>(); for (int i = 0; i < Header.SamplerCount; i++) { Samplers.Add(new MDLSampler(br)); } br.Goto(Header.MaterialOffset); Materials = new List <MDLMaterial>(); for (int i = 0; i < Header.MaterialCount; i++) { Materials.Add(new MDLMaterial(br, Samplers)); } br.Goto(Header.DrawElementOffset); DrawElements = new List <MDLDrawElement>(); for (int i = 0; i < Header.DrawElementCount; i++) { DrawElements.Add(new MDLDrawElement(br, Materials, Shapes)); } br.Goto(Header.NodeOffset); Nodes = new List <MDLNode>(); for (int i = 0; i < Header.NodeCount; i++) { Nodes.Add(new MDLNode(br, DrawElements)); } }