Ejemplo n.º 1
0
        private static void FixBoneParenting(Node node, Mesh3 mesh, Mesh3.Bone parentBone)
        {
            string nodeName = SanitizeBoneName(node.Name);

            if (mesh.HasBone(nodeName))
            {
                if (parentBone == null || (parentBone != null && parentBone.Name != nodeName))
                {
                    Mesh3.Bone currentBone = mesh.GetBone(nodeName);
                    currentBone.SetParent(parentBone);
                    parentBone = currentBone;
                }
            }


            for (int i = 0; i < node.ChildCount; i++)
            {
                Node childNode = node.Children[i];
                FixBoneParenting(childNode, mesh, parentBone);
            }
        }
Ejemplo n.º 2
0
        private static Mesh3 FbxToMesh3(Scene scene, Assimp.Mesh mesh, Vector3 multiplier)
        {
            FbxMeshData fbxData = new FbxMeshData(multiplier);

            if (mesh.HasBones)
            {
                foreach (Bone bone in mesh.Bones)
                {
                    foreach (VertexWeight vertexWeight in bone.VertexWeights)
                    {
                        if (!fbxData.bonesInfluences.ContainsKey(vertexWeight.VertexID))
                        {
                            fbxData.bonesInfluences[vertexWeight.VertexID] = new List <FbxBoneInfluence>();
                        }
                        fbxData.bonesInfluences[vertexWeight.VertexID].Add(new FbxBoneInfluence()
                        {
                            bone = bone, weight = vertexWeight.Weight
                        });
                    }
                }
            }

            foreach (Face face in mesh.Faces)
            {
                if (face.IndexCount == 3 || face.IndexCount == 4)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        FbxIndexToFloatList(i, mesh, face, fbxData);
                    }

                    // manage quads
                    if (face.IndexCount == 4)
                    {
                        FbxIndexToFloatList(3, mesh, face, fbxData);
                        FbxIndexToFloatList(0, mesh, face, fbxData);
                        FbxIndexToFloatList(2, mesh, face, fbxData);
                    }
                }
                else
                {
                    throw new FbxLoader.InvalidPolygon();
                }
            }

            Aiv.Fast3D.Mesh3 mesh3 = new Mesh3();
            mesh3.v  = fbxData.vList.ToArray();
            mesh3.uv = fbxData.vtList.ToArray();
            mesh3.vn = fbxData.vnList.ToArray();
            if (fbxData.bonesMappingList.Count > 0)
            {
                mesh3.bonesMapping = fbxData.bonesMappingList.ToArray();
            }
            if (fbxData.bonesWeightList.Count > 0)
            {
                mesh3.bonesWeight = fbxData.bonesWeightList.ToArray();
            }
            mesh3.Update();
            mesh3.UpdateNormals();
            mesh3.UpdateBones();

            if (mesh.HasBones)
            {
                for (int i = 0; i < mesh.BoneCount; i++)
                {
                    Mesh3.Bone newBone = mesh3.AddBone(i, mesh.Bones[i].Name);

                    //Assimp.Matrix4x4 rootMatrix = scene.RootNode.Transform;
                    //rootMatrix.Inverse();

                    //Assimp.Matrix4x4 nodeMatrix = scene.RootNode.FindNode(mesh.Bones[i].Name).Transform;

                    //Assimp.Matrix4x4 m = rootMatrix * nodeMatrix * mesh.Bones[i].OffsetMatrix;
                    //Assimp.Matrix4x4 m = rootMatrix;
                    //m.Inverse();

                    //newBone.rootMatrix = new OpenTK.Matrix4(m.A1, m.A2, m.A3, m.A4, m.B1, m.B2, m.B3, m.B4, m.C1, m.C2, m.C3, m.C4, m.D1, m.D2, m.D3, m.D4);
                    //newBone.rootMatrix = new OpenTK.Matrix4(m.A1, m.B1, m.C1, m.D1, m.A2, m.B2, m.C2, m.D2, m.A3, m.B3, m.C3, m.D3, m.A4, m.B4, m.C4, m.D4);

                    //m = nodeMatrix;
                    //m = mesh.Bones[i].OffsetMatrix * nodeMatrix;
                    Assimp.Matrix4x4 m = mesh.Bones[i].OffsetMatrix;
                    //m.Inverse();
                    //newBone.BaseMatrix = new OpenTK.Matrix4(m.A1, m.A2, m.A3, m.A4, m.B1, m.B2, m.B3, m.B4, m.C1, m.C2, m.C3, m.C4, m.D1, m.D2, m.D3, m.D4);

                    newBone.BaseMatrix = new OpenTK.Matrix4(m.A1, m.B1, m.C1, m.D1, m.A2, m.B2, m.C2, m.D2, m.A3, m.B3, m.C3, m.D3, m.A4, m.B4, m.C4, m.D4);
                }

                Console.WriteLine("Bones count = " + mesh3.BonesCount);

                // fixup bones
                FixBoneParenting(scene.RootNode, mesh3, null);
            }

            return(mesh3);
        }