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; }
byte[] BuildVertexPT1Data(Vector3[] positions, Vector2[] texVtx, Index3i[] texIdx, MeshFace[] faces) { Dictionary<string, int> table = new Dictionary<string, int>(faces.Length * 3); FastList<VertexPT1> vertices = new FastList<VertexPT1>(faces.Length * 3); for (int i = 0; i < faces.Length; i++) { int index; VertexPT1 vtx; int vtxIdx = faces[i].IndexA; vtx.pos = positions[vtxIdx]; vtx.u1 = texVtx[texIdx[i].A].X; vtx.v1 = texVtx[texIdx[i].A].Y; 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.u1 = texVtx[texIdx[i].B].X; vtx.v1 = texVtx[texIdx[i].B].Y; 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.u1 = texVtx[texIdx[i].C].X; vtx.v1 = texVtx[texIdx[i].C].Y; 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(VertexPT1)]; fixed (byte* dst = &buffer[0]) { fixed (VertexPT1* src = &vertices.Elements[0]) { Memory.Copy(src, dst, buffer.Length); } } return buffer; }
Index3i[] ParseTexIndex(XmlReader xml, int count) { Index3i[] texIdx = new Index3i[count]; int subDepth = xml.Depth; while (xml.Read() && xml.Depth > subDepth) { if (xml.IsStartElement() && !xml.IsEmptyElement) { if (xml.Name == "Value") { int index = int.Parse(xml.GetAttribute("Index")); texIdx[index] = ParseIndex3(xml.ReadString()); } } } return texIdx; }