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; }
internal Node (string name, Node parent, ref Matrix4 transform, Mesh[] meshes) { _name = name; _parent = parent; this.transform = transform; _meshes = meshes; }
Node ReadNode(BinaryReader reader, Node parent, Mesh[] meshes) { var name = reader.ReadString(); Matrix4 transform; reader.ReadMatrix4(out transform); var meshCount = reader.ReadInt32(); Mesh[] meshList = null; if(meshCount > 0) { meshList = new Mesh[meshCount]; for (var i = 0; i < meshCount; i++) { var idx = reader.ReadInt32(); if (idx < 0 || idx >= meshes.Length) throw new ContentException("Invalid mesh index " + idx); meshList[i] = meshes[idx]; } } var node = new Node(name, parent, ref transform, meshList); var childCount = reader.ReadInt32(); Node[] children = null; if(childCount > 0) { children = new Node[childCount]; for (var i = 0; i < childCount; i++) { children[i] = this.ReadNode(reader, node, meshes); } } node.Children = children; return node; }