/// <summary> /// Helper method to read a single vertex /// </summary> /// <param name="sectionReader">BinaryReader instance</param> /// <returns>An instance of <see cref="PangLib.PET.Models.Vertex"/></returns> public static Vertex ReadVertex(BinaryReader sectionReader) { Vertex vertex = new Vertex(); vertex.X = sectionReader.ReadSingle(); vertex.Y = sectionReader.ReadSingle(); vertex.Z = sectionReader.ReadSingle(); byte fullWeight = 0; int readCount = 0; vertex.BoneInformation = new List <BoneInformation>(); while (fullWeight != 255 || readCount < 2) { BoneInformation boneInformation = new BoneInformation(); byte weight = sectionReader.ReadByte(); boneInformation.Weight = weight; fullWeight += weight; boneInformation.BoneID = sectionReader.ReadByte(); vertex.BoneInformation.Add(boneInformation); readCount += 1; } return(vertex); }
// array size is vertex count private static BoneWeight[] CreateBoneWeights(PETFile pet) { int uniqueVertexCount = pet.Vertices.Count; // create unique vertices with the index used in the polygons BoneWeight[] uniqueBoneWeights = new BoneWeight[uniqueVertexCount]; for (int i = 0; i < uniqueVertexCount; i++) { List <BoneInformation> boneInformationList = pet.Vertices[i].BoneInformation; // TODO meh for (int index = 0; index < boneInformationList.Count; index++) { BoneInformation boneInformation = boneInformationList[index]; switch (index) { case 0: uniqueBoneWeights[i].boneIndex0 = boneInformation.BoneID; uniqueBoneWeights[i].weight0 = boneInformation.Weight; break; case 1: uniqueBoneWeights[i].boneIndex1 = boneInformation.BoneID; uniqueBoneWeights[i].weight1 = boneInformation.Weight; break; case 2: uniqueBoneWeights[i].boneIndex2 = boneInformation.BoneID; uniqueBoneWeights[i].weight2 = boneInformation.Weight; break; case 3: uniqueBoneWeights[i].boneIndex3 = boneInformation.BoneID; uniqueBoneWeights[i].weight3 = boneInformation.Weight; break; default: throw new NotSupportedException( "Unity only supports 4 different weights for a single bone"); } } } BoneWeight[] boneWeights = pet.Polygons .SelectMany(polygon => polygon.PolygonIndices) .Select(polygonIndex => uniqueBoneWeights[polygonIndex.Index]) .ToArray(); return(boneWeights); }
public static List <Vertex> ReadAllVertices(BinaryReader sectionReader) { List <Vertex> Vertices = new List <Vertex>(); uint vertexCount = sectionReader.ReadUInt32(); for (int i = 0; i < vertexCount; i++) { Vertex vertex = new Vertex(); vertex.X = sectionReader.ReadSingle(); vertex.Y = sectionReader.ReadSingle(); vertex.Z = sectionReader.ReadSingle(); sbyte fullWeight = 0; int readCount = 0; while (fullWeight != 255 && readCount < 2) { BoneInformation boneInformation = new BoneInformation(); sbyte weight = sectionReader.ReadSByte(); boneInformation.Weight = weight; fullWeight += weight; boneInformation.BoneID = sectionReader.ReadSByte(); vertex.BoneInformation.Add(boneInformation); readCount += 1; } Vertices.Add(vertex); } return(Vertices); }