VertexWeight ParseVertexWeight(XmlReader xml) { VertexWeight w = new VertexWeight(); List <int> bones = new List <int>(); List <float> weights = new List <float>(); int subDepth = xml.Depth; while (xml.Read() && xml.Depth > subDepth) { if (xml.IsStartElement() && !xml.IsEmptyElement) { if (xml.Name == "Value") { int id = int.Parse(xml.GetAttribute("BoneID")); float weight = float.Parse(xml.ReadString()); bones.Add(id); weights.Add(weight); } } } w.BoneID = bones.ToArray(); w.Weight = weights.ToArray(); return(w); }
VertexWeight[] ParseVertexWeightArray(XmlReader xml, int count) { VertexWeight[] vtxWeights = new VertexWeight[count]; int subDepth = xml.Depth; while (xml.Read() && xml.Depth > subDepth) { if (xml.IsStartElement() && !xml.IsEmptyElement) { if (xml.Name == "Vertex") { int index = int.Parse(xml.GetAttribute("Index")); vtxWeights[index] = ParseVertexWeight(xml); } } } return(vtxWeights); }
byte[] BuildVertexPBNT1Data(Vector3[] positions, Vector3[] normals, Vector2[] texVtx, Index3i[] texIdx, VertexWeight[] vtxWeights, MeshFace[] faces) { Dictionary<string, int> table = new Dictionary<string, int>(faces.Length * 3); FastList<VertexPBNT1> vertices = new FastList<VertexPBNT1>(faces.Length * 3); float[] blendBuf = new float[4]; byte[] blendBuf2 = new byte[4]; for (int i = 0; i < faces.Length; i++) { int index; VertexPBNT1 vtx; int vtxIdx = faces[i].IndexA; vtx.pos = positions[vtxIdx]; vtx.n = normals[vtxIdx]; vtx.u = texVtx[texIdx[i].A].X; vtx.v = texVtx[texIdx[i].A].Y; for (int j = 0; j < 4; j++) { if (j < vtxWeights[vtxIdx].BoneID.Length) { blendBuf[j] = vtxWeights[vtxIdx].Weight[j]; blendBuf2[j] = (byte)vtxWeights[vtxIdx].BoneID[j]; } else { blendBuf2[j] = 0; blendBuf[j] = 0; } } vtx.blend = new Vector4(blendBuf[0], blendBuf[1], blendBuf[2], blendBuf[3]); vtx.boneId1 = blendBuf2[0]; vtx.boneId2 = blendBuf2[1]; vtx.boneId3 = blendBuf2[2]; vtx.boneId4 = blendBuf2[3]; string desc = vtx.ToString(); if (!table.TryGetValue(desc, out index)) { table.Add(desc, vertices.Count); faces[i].IndexA = vertices.Count; vertices.Add(ref vtx); } else { faces[i].IndexA = index; } // ========================================= vtxIdx = faces[i].IndexB; vtx.pos = positions[vtxIdx]; vtx.n = normals[vtxIdx]; vtx.u = texVtx[texIdx[i].B].X; vtx.v = texVtx[texIdx[i].B].Y; for (int j = 0; j < 4; j++) { if (j < vtxWeights[vtxIdx].BoneID.Length) { blendBuf[j] = vtxWeights[vtxIdx].Weight[j]; blendBuf2[j] = (byte)vtxWeights[vtxIdx].BoneID[j]; } else { blendBuf2[j] = 0; blendBuf[j] = 0; } } vtx.blend = new Vector4(blendBuf[0], blendBuf[1], blendBuf[2], blendBuf[3]); vtx.boneId1 = blendBuf2[0]; vtx.boneId2 = blendBuf2[1]; vtx.boneId3 = blendBuf2[2]; vtx.boneId4 = blendBuf2[3]; desc = vtx.ToString(); if (!table.TryGetValue(desc, out index)) { table.Add(desc, vertices.Count); faces[i].IndexB = vertices.Count; vertices.Add(ref vtx); } else { faces[i].IndexB = index; } // ========================================= vtx.pos = positions[faces[i].IndexC]; vtx.n = normals[faces[i].IndexC]; vtx.u = texVtx[texIdx[i].C].X; vtx.v = texVtx[texIdx[i].C].Y; for (int j = 0; j < 4; j++) { if (j < vtxWeights[vtxIdx].BoneID.Length) { blendBuf[j] = vtxWeights[vtxIdx].Weight[j]; blendBuf2[j] = (byte)vtxWeights[vtxIdx].BoneID[j]; } else { blendBuf2[j] = 0; blendBuf[j] = 0; } } vtx.blend = new Vector4(blendBuf[0], blendBuf[1], blendBuf[2], blendBuf[3]); vtx.boneId1 = blendBuf2[0]; vtx.boneId2 = blendBuf2[1]; vtx.boneId3 = blendBuf2[2]; vtx.boneId4 = blendBuf2[3]; desc = vtx.ToString(); if (!table.TryGetValue(desc, out index)) { table.Add(desc, vertices.Count); faces[i].IndexC = vertices.Count; vertices.Add(ref vtx); } else { faces[i].IndexC = index; } } byte[] buffer = new byte[vertices.Count * sizeof(VertexPBNT1)]; fixed (byte* dst = &buffer[0]) { fixed (VertexPBNT1* src = &vertices.Elements[0]) { Memory.Copy(src, dst, buffer.Length); } } return buffer; }
VertexWeight[] ParseVertexWeightArray(XmlReader xml, int count) { VertexWeight[] vtxWeights = new VertexWeight[count]; int subDepth = xml.Depth; while (xml.Read() && xml.Depth > subDepth) { if (xml.IsStartElement() && !xml.IsEmptyElement) { if (xml.Name == "Vertex") { int index = int.Parse(xml.GetAttribute("Index")); vtxWeights[index] = ParseVertexWeight(xml); } } } return vtxWeights; }
VertexWeight ParseVertexWeight(XmlReader xml) { VertexWeight w = new VertexWeight(); List<int> bones = new List<int>(); List<float> weights = new List<float>(); int subDepth = xml.Depth; while (xml.Read() && xml.Depth > subDepth) { if (xml.IsStartElement() && !xml.IsEmptyElement) { if (xml.Name == "Value") { int id = int.Parse(xml.GetAttribute("BoneID")); float weight = float.Parse(xml.ReadString()); bones.Add(id); weights.Add(weight); } } } w.BoneID = bones.ToArray(); w.Weight = weights.ToArray(); return w; }