コード例 #1
0
ファイル: DataUtil.cs プロジェクト: christinayan01/demo
        public SyncMesh BuildMesh(Obj curObj)
        {
            SyncMesh syncmesh = new SyncMesh(new SyncId("MeshId_" + curObj.Name), "Mesh_" + curObj.Name);

            // Faceの定義に従う。無駄は多くなる
            syncmesh.SubMeshes = new List <SyncSubMesh>();
            SyncSubMesh submesh = new SyncSubMesh();

            for (int j = 0; j < curObj.FaceList.Count; j++)
            {
                ObjParser.Types.Face face = curObj.FaceList[j];

                for (int i = 0; i < face.VertexIndexList.Length; i++)
                {
                    //idx
                    int vertexIdx = face.VertexIndexList[i] - 1;
                    int uvIdx     = face.TextureVertexIndexList[i] - 1;
                    int normalIdx = face.NormalIndexList[i] - 1;
                    //Console.WriteLine(string.Format("index[{0}] vert[{1}] uv[{2}] normal[{3}]",i, vertexIdx+1, uvIdx+1, normalIdx+1));

                    //ObjParser.Types.Vertex vertex = curObj.VertexList[vertexIdx];
                    //ObjParser.Types.TextureVertex uv = curObj.TextureList[uvIdx];
                    //ObjParser.Types.VertexNormal normal = curObj.VertexNormalList[normalIdx];
                    ObjParser.Types.Vertex        vertex = obj.VertexList[vertexIdx];
                    ObjParser.Types.TextureVertex uv     = obj.TextureList[uvIdx];
                    ObjParser.Types.VertexNormal  normal = obj.VertexNormalList[normalIdx];

                    //頂点
                    // OpenGL->DirectX変換のため、Xは反転する
                    syncmesh.Vertices.Add(new Vector3(vertex.X * -1.0f, vertex.Y, vertex.Z)); // 2021.5.26 X軸を反転
                    syncmesh.Uvs.Add(new Vector2(uv.X, uv.Y));
                    syncmesh.Normals.Add(new Vector3(normal.X * -1.0f, normal.Y, normal.Z));  // 2021.5.26 X軸を反転
                    //syncmesh.Normals.Add(new Vector3(0,0,1));
                    //Console.WriteLine(string.Format("vert={0}\t{1}\t{2}", (float)vertex.X, (float)vertex.Y, (float)vertex.Z));

                    //index
                    //submesh.Triangles.Add(i + j * face.VertexIndexList.Length);
                    // OpenGL->DirectX変換のため、逆順でセットする
                    submesh.Triangles.Add((face.VertexIndexList.Length - 1 - i) + j * face.VertexIndexList.Length);
                }
            }
            syncmesh.SubMeshes.Add(submesh);

            return(syncmesh);
        }
コード例 #2
0
ファイル: DataUtil.cs プロジェクト: christinayan01/demo
        public SyncMesh BuildMesh_Legacy(Obj curObj)
        {
            SyncMesh syncmesh = new SyncMesh(new SyncId("MeshId_" + curObj.Name), "Mesh_" + curObj.Name);

            foreach (ObjParser.Types.Vertex vertex in curObj.VertexList)
            {
                syncmesh.Vertices.Add(new Vector3((float)vertex.X, (float)vertex.Y, (float)vertex.Z));
            }

            foreach (ObjParser.Types.VertexNormal normal in curObj.VertexNormalList)
            {
                syncmesh.Normals.Add(new Vector3((float)normal.X, (float)normal.Y, (float)normal.Z));
            }

            foreach (ObjParser.Types.TextureVertex uv in curObj.TextureList)
            {
                syncmesh.Uvs.Add(new Vector2((float)uv.X, (float)uv.Y));
            }

            syncmesh.SubMeshes = new List <SyncSubMesh>();
            SyncSubMesh submesh = new SyncSubMesh();

            foreach (ObjParser.Types.Face face in curObj.FaceList)
            {
                //string[] line = face.ToString().Split(' ');
                //foreach (string data in line)
                //{
                //    string[] facegeom = data.Split('/');
                //    int result = 0;
                //    if (int.TryParse(data, out result))
                //    {
                //        submesh.Triangles.Add(result);
                //    }
                //}
                foreach (int idx in face.VertexIndexList)
                {
                    submesh.Triangles.Add(idx);
                }
            }
            syncmesh.SubMeshes.Add(submesh);

            return(syncmesh);
        }