public VertexRigged3D(Vector3 postion, Vector3 normal, Vector2 uv, IVector4 boneIdices, Vector4 boneWeigth) { Position = postion; Normal = normal; UV = uv; BoneIndices = boneIdices; BoneWeigths = boneWeigth; }
MeshDrawer ReadMeshChunk(int streamOffset) { //mesh name reader.BaseStream.Position = streamOffset + 0x7; int chunkSize = reader.ReadByte(); int meshNamePos = (int)reader.BaseStream.Position + reader.ReadInt32(); reader.BaseStream.Position = meshNamePos; string meshName = reader.readString(); //material reference reader.BaseStream.Position = streamOffset + 0x14; int matNamePos = (int)reader.BaseStream.Position + reader.ReadInt32(); reader.BaseStream.Position = matNamePos + 0x8; string matName = reader.readString(); //bones reference reader.BaseStream.Position = streamOffset + 0x58; int weightBoneNameTableStart = (int)reader.BaseStream.Position + reader.ReadInt32(); reader.BaseStream.Position = streamOffset + 0x5c; int WeightBoneTableStart = (int)reader.BaseStream.Position + reader.ReadInt32(); reader.BaseStream.Position = streamOffset + 0x78; int facesCount = reader.ReadInt32(); reader.BaseStream.Position = streamOffset + 0x84; int verticesCount = reader.ReadInt32(); byte size = 4; int SizeTest = verticesCount * chunkSize; if (SizeTest < 0x100) { size = 1; } else if (SizeTest < 0x10000) { size = 2; } reader.BaseStream.Position += 8; int vertSize = reader.readVal(size); int VertOffset = (int)reader.BaseStream.Position; List <VertexRigged3D> vertices = new List <VertexRigged3D>(); for (int i = 0; i < verticesCount; i++) { VertexRigged3D vertice = new VertexRigged3D(); vertice.Position = reader.readVector3(); //vertex color unsupported reader.BaseStream.Position += 4; //if (chunkSize == 0x24 || chunkSize == 0x28) if (chunkSize == 0x24) { reader.BaseStream.Position += 4; } else if (chunkSize == 0x30) { reader.BaseStream.Position += 4; reader.BaseStream.Position += 0xc; } vertice.UV = new Vector2(); vertice.UV.X = Half.FromBytes(reader.ReadBytes(2), 0); vertice.UV.Y = Half.FromBytes(reader.ReadBytes(2), 0); /* * if (chunkSize == 0x28) * file.BaseStream.Position += 4; */ vertice.BoneIndices = new IVector4(new int[] { reader.ReadByte(), reader.ReadByte(), reader.ReadByte(), reader.ReadByte() }); if (chunkSize == 0x28) { vertice.BoneWeigths = reader.readVector4(); } else { vertice.BoneWeigths = new Vector4(reader.ReadUInt16(), reader.ReadUInt16(), reader.ReadUInt16(), reader.ReadUInt16()); vertice.BoneWeigths /= 65535f; } vertices.Add(vertice); } int unknownSize = 4; if (size == 1) { unknownSize = 2; } reader.BaseStream.Position = VertOffset + vertSize + size + unknownSize; int unknownCount = reader.ReadInt32(); reader.BaseStream.Position += 0x10 * unknownCount; reader.ReadInt32(); //Read Faces size = 4; if (facesCount < 0x100) { size = 1; } else if (facesCount < 0x10000) { size = 2; } int faceSize = reader.readVal(size); byte indexValueSize = 4; if (verticesCount < 0x100) { indexValueSize = 1; } else if (verticesCount < 0x10000) { indexValueSize = 2; } int faceOffset = (int)reader.BaseStream.Position; int[] indexes = new int[facesCount]; for (int i = 0; i < facesCount; i++) { indexes[i] = reader.readVal(indexValueSize); } //Read Weigth Bones dictionary reader.BaseStream.Position = weightBoneNameTableStart; int weightBoneCount = reader.ReadInt32(); int[] boneIdDict = new int[weightBoneCount]; for (int i = 0; i < weightBoneCount; i++) { reader.BaseStream.Position = weightBoneNameTableStart + i * 4 + 4; reader.BaseStream.Position += reader.ReadInt32(); string weightBoneName = reader.readString(); boneIdDict[i] = Array.FindIndex(bones, (b) => b.Name == weightBoneName); } var vv = vertices.ToArray(); //set bone id to global for (int i = 0; i < vv.Length; i++) { IVector4 boneIndexes = vv[i].BoneIndices; boneIndexes.bone1 = boneIdDict[boneIndexes.bone1]; boneIndexes.bone2 = boneIdDict[boneIndexes.bone2]; boneIndexes.bone3 = boneIdDict[boneIndexes.bone3]; boneIndexes.bone4 = boneIdDict[boneIndexes.bone4]; vv[i].BoneIndices = boneIndexes; } CalculateVertNormals.CalculateNormals(vv, indexes); var mesh = new Mesh(vv, indexes); Material mat; if (materialTable.TryGetValue(matName, out mat)) { return(new MeshDrawerRigged(mesh, new Material[] { mat }, boneControll)); } else { return(new MeshDrawerRigged(mesh, boneControll)); } }