public void ReadVertex(ref vertex_field v) { v.position = ReadVector3(); v.normal = ReadVector3(); v.u = reader.ReadSingle(); v.v = reader.ReadSingle(); int bone_weight_entry_count = reader.ReadInt32(); v.skin_weights = new SkinWeight[bone_weight_entry_count]; for (int i = 0; i < bone_weight_entry_count; i++) { uint index = reader.ReadUInt32(); float weight = reader.ReadSingle(); v.skin_weights[i] = new SkinWeight(weight, index); } Array.Sort(v.skin_weights); Array.Resize(ref v.skin_weights, 4); for (int i = bone_weight_entry_count; i < 4; i++) { v.skin_weights[i] = new SkinWeight(0.0f, 0); } byte[] idx = new byte[4]; for (int i = 0; i < 4; i++) { idx[i] = (byte)v.skin_weights[i].index; } v.skin_weight_indices = BitConverter.ToUInt32(idx, 0); }
/* * ar.AttributeId = 0; * ar.FaceStart = 0; * ar.FaceCount = 0; * ar.VertexStart = 0; * ar.VertexCount = 0; */ public void LoadMesh() { foreach (TSOMesh tm in meshes) { foreach (TSOMesh tm_sub in tm.sub_meshes) { int numVertices = tm_sub.vertices.Length; int numFaces = numVertices - 2; tm_sub.dm = new Mesh(numFaces, numVertices, MeshFlags.Managed | MeshFlags.WriteOnly, ve, device); // // rewrite vertex buffer // { GraphicsStream gs = tm_sub.dm.LockVertexBuffer(LockFlags.None); { for (int i = 0; i < tm_sub.vertices.Length; i++) { vertex_field tv = tm_sub.vertices[i]; gs.Write(tv.position); for (int j = 0; j < 4; j++) { gs.Write(tv.skin_weights[j].weight); } gs.Write(tv.skin_weight_indices); gs.Write(tv.normal); gs.Write(tv.u); gs.Write(1 - tv.v); } } tm_sub.dm.UnlockVertexBuffer(); } // // rewrite index buffer // { GraphicsStream gs = tm_sub.dm.LockIndexBuffer(LockFlags.None); { for (int i = 2; i < tm_sub.vertices.Length; i++) { if (i % 2 != 0) { gs.Write((short)(i - 0)); gs.Write((short)(i - 1)); gs.Write((short)(i - 2)); } else { gs.Write((short)(i - 2)); gs.Write((short)(i - 1)); gs.Write((short)(i - 0)); } } } tm_sub.dm.UnlockIndexBuffer(); } // // rewrite attribute buffer // { tm_sub.dm.SetAttributeTable(new AttributeRange[] { ar }); } } } }