public Mesh (string name, Material mat, VertexFormat format, float[] vertices, int[] indices, Bone[] bones) { _name = name; _material = mat; _vbuffer = new VertexBuffer(format, vertices); _ibuffer = new IndexBuffer(indices, BeginMode.Triangles); _bones = bones; if(_bones != null) _boneTransforms = new Matrix4[_bones.Length]; }
unsafe Mesh[] ReadMeshes (BinaryReader reader, Material[] materials) { var meshCount = reader.ReadInt32(); var meshes = new Mesh[meshCount]; for (var i = 0; i < meshCount; i++) { var name = reader.ReadString(); var format = GetVertexFormat((VertexFlags)reader.ReadInt32(), reader.ReadInt32()); var matIdx = reader.ReadInt32(); if (matIdx < 0 || matIdx >= materials.Length) throw new ContentException("Invalid material index " + matIdx); var count = reader.ReadInt32(); var vertices = new float[count]; byte[] buf = new byte[count * sizeof(float)]; reader.Read(buf, 0, buf.Length); fixed(byte *bp = &buf[0]) { fixed(float *fp = &vertices[0]) { float* src = (float*)bp; float* dst = fp; for (var j = 0; j < count; j++) { *(dst++) = *(src++); } } } count = reader.ReadInt32(); var indices = new int[count]; buf = new byte[count * sizeof(int)]; reader.Read(buf, 0, buf.Length); fixed(byte *bp = &buf[0]) { fixed(int *ip = &indices[0]) { int* src = (int*)bp; int* dst = ip; for (var j = 0; j < count; j++) { *(dst++) = *(src++); } } } count = reader.ReadInt32(); Bone[] bones = null; if (count > 0) { bones = new Bone[count]; for (var j = 0; j < count; j++) { var boneName = reader.ReadString(); Matrix4 offset; reader.ReadMatrix4(out offset); var weightCount = reader.ReadInt32(); var weights = new VertexWeight[weightCount]; var sz = weightCount * sizeof(VertexWeight); if (buf.Length < sz) buf = new byte[sz]; reader.Read(buf, 0, sz); fixed(byte *src = &buf[0]) { var p = (VertexWeight*)src; fixed(VertexWeight *dst = &weights[0]) { for (var k = 0; k < weightCount; k++) { *(dst + k) = *(p + k); } } } bones[j] = new Bone(j, boneName, ref offset, weights); } } meshes[i] = new Mesh(name, materials[matIdx], format, vertices, indices, bones); } return meshes; }