private static ModelNode LoadNode(JObject data) { if (data == null) { return(null); } ModelNode result; if (data.ContainsKey("parts")) { // Mesh var mesh = new MeshNode(); var partsData = (JArray)data["parts"]; foreach (JObject partData in partsData) { var newPart = new MeshPart { MeshPartId = partData["meshpartid"].ToString(), MaterialId = partData["materialid"].ToString() }; if (partData.ContainsKey("bones")) { var bonesData = (JArray)partData["bones"]; foreach (JObject boneData in bonesData) { var bone = LoadBone(boneData); newPart.Bones.Add(bone); } } mesh.Parts.Add(newPart); } result = mesh; } else { result = new ModelNode(); } result.Id = data.GetId(); result.DefaultTransform = result.Transform = LoadTransform(data); if (data.ContainsKey("children")) { var children = (JArray)data["children"]; foreach (JObject child in children) { var childNode = LoadNode(child); childNode.Parent = result; result.Children.Add(childNode); } } return(result); }
private static void LoadMeshData(JObject meshData, Dictionary <string, MeshPart> meshes) { // Determine vertex type int elementsPerRow; var declaration = LoadDeclaration((JArray)meshData["attributes"], out elementsPerRow); var vertices = (JArray)meshData["vertices"]; int bonesCount = 0; foreach (var element in declaration.GetVertexElements()) { if (element.VertexElementUsage != VertexElementUsage.BlendWeight) { continue; } if (element.UsageIndex + 1 > bonesCount) { bonesCount = element.UsageIndex + 1; } } var bonesPerMesh = BonesPerMesh.None; if (bonesCount >= 3) { bonesPerMesh = BonesPerMesh.Four; } else if (bonesCount == 2) { bonesPerMesh = BonesPerMesh.Two; } else if (bonesCount == 1) { bonesPerMesh = BonesPerMesh.One; } List <Vector3> positions; var vertexBuffer = LoadVertexBuffer(ref declaration, elementsPerRow, vertices, out positions); var partsData = (JArray)meshData["parts"]; foreach (JObject partData in partsData) { var id = partData.GetId(); // var type = (PrimitiveType)Enum.Parse(typeof(PrimitiveType), partData.EnsureString("type")); var partPositions = new List <Vector3>(); var indicesData = (JArray)partData["indices"]; var indices = new short[indicesData.Count]; for (var i = 0; i < indicesData.Count; ++i) { var idx = Convert.ToInt16(indicesData[i]); indices[i] = idx; partPositions.Add(positions[idx]); } var indexBuffer = new IndexBuffer(Nrs.GraphicsDevice, IndexElementSize.SixteenBits, indices.Length, BufferUsage.None); indexBuffer.SetData(indices); var boundingSphere = BoundingSphere.CreateFromPoints(partPositions); var mesh = new Mesh { IndexBuffer = indexBuffer, VertexBuffer = vertexBuffer }; meshes[id] = new MeshPart { Mesh = mesh, BonesPerMesh = bonesPerMesh, BoundingSphere = boundingSphere }; } }