コード例 #1
0
        public static Mesh3[] Load(string fileName, Vector3 multiplier)
        {
            List <ObjMesh> meshes = new List <ObjMesh>();

            objVertices = new List <Vector3>();
            objUVs      = new List <Vector2>();
            objNormals  = new List <Vector3>();

            ObjMesh currentMesh = null;

            string[] lines = File.ReadAllLines(fileName);
            foreach (string line in lines)
            {
                string[] items = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (items.Length == 0)
                {
                    continue;
                }
                if (items[0] == "o" || items[0] == "g")
                {
                    currentMesh = new ObjMesh(multiplier);
                    meshes.Add(currentMesh);
                }
                else if (items[0] == "v")
                {
                    if (currentMesh == null)
                    {
                        currentMesh = new ObjMesh(multiplier);
                        meshes.Add(currentMesh);
                    }
                    Vector3 v = new Vector3(ParseFloat(items[1]), ParseFloat(items[2]), ParseFloat(items[3]));
                    objVertices.Add(v);
                }
                else if (items[0] == "vt")
                {
                    Vector2 vt = new Vector2(ParseFloat(items[1]), ParseFloat(items[2]));
                    objUVs.Add(vt);
                }
                else if (items[0] == "vn")
                {
                    Vector3 v = new Vector3(ParseFloat(items[1]), ParseFloat(items[2]), ParseFloat(items[3]));
                    objNormals.Add(v);
                }
                else if (items[0] == "f")
                {
                    currentMesh.AddFace(items);
                }
            }

            Mesh3[] finalMeshes = new Mesh3[meshes.Count];
            for (int i = 0; i < finalMeshes.Length; i++)
            {
                finalMeshes[i]    = new Mesh3();
                finalMeshes[i].v  = meshes[i].vList.ToArray();
                finalMeshes[i].uv = meshes[i].vtList.ToArray();
                finalMeshes[i].vn = meshes[i].vnList.ToArray();
                finalMeshes[i].Update();
                // check if normals are available
                if (meshes[i].vnList.Count == 0)
                {
                    finalMeshes[i].RegenerateNormals();
                }
                // check for invalid UVs
                if (meshes[i].vtList.Count == 0)
                {
                    // this will trigger uv regeneration
                    finalMeshes[i].uv = null;
                }
                finalMeshes[i].UpdateNormals();
            }
            return(finalMeshes);
        }
コード例 #2
0
ファイル: FbxLoader.cs プロジェクト: luca1337/aiv-fast3d
        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);
        }