Example #1
0
        public override ObjAssetPolygonFaceFormat Read(string header, string body)
        {
            Regex pattern = new Regex(@"\s+");

            string[] elements = pattern.Split(body);
            ObjAssetPolygonFaceFormat format = new ObjAssetPolygonFaceFormat();

            format.header = header;
            ObjAssetPolygonFaceFormat.Index[] indicies = new ObjAssetPolygonFaceFormat.Index[elements.Length];
            for (int i = 0; i < elements.Length; i++)
            {
                string   element = elements[i];
                string[] faces   = element.Split('/');
                ObjAssetPolygonFaceFormat.Index index = new ObjAssetPolygonFaceFormat.Index();
                index.vertexIndex = Convert.ToInt32(faces[0]) - 1;
                if (1 < faces.Length)
                {
                    index.uvIndex = Convert.ToInt32(faces[1]) - 1;
                }
                if (2 < faces.Length)
                {
                    index.normalVectorIndex = Convert.ToInt32(faces[2]) - 1;
                }
                indicies[i] = index;
            }
            if (3 == indicies.Length)
            {
                format.indexList.Add(indicies[0]);
                format.indexList.Add(indicies[1]);
                format.indexList.Add(indicies[2]);
            }
            else if (4 == indicies.Length)
            {
                format.indexList.Add(indicies[0]);
                format.indexList.Add(indicies[1]);
                format.indexList.Add(indicies[3]);
                format.indexList.Add(indicies[3]);
                format.indexList.Add(indicies[1]);
                format.indexList.Add(indicies[2]);
            }
            return(format);
        }
        public List <Mesh> Build()
        {
            List <Mesh> meshList         = new List <Mesh>();
            int         polygonFaceCount = format.GetPolygonFaceCount(this.groupPath);

            if (0 == polygonFaceCount)
            {
                return(meshList);
            }
            int        quotient = polygonFaceCount / ObjAssetMeshBuilder.MAX_UNITY_TRIANGLES_COUNT;
            int        surplus  = polygonFaceCount % ObjAssetMeshBuilder.MAX_UNITY_TRIANGLES_COUNT;
            MeshEntity entity   = new MeshEntity();

            if (0 == quotient)
            {
                Vector3[] verticies     = new Vector3[polygonFaceCount];
                Vector2[] uvs           = new Vector2[polygonFaceCount];
                Vector3[] normalVectors = new Vector3[polygonFaceCount];
                int[]     triangles     = new int[polygonFaceCount];
                entity.verticiesList.Add(verticies);
                entity.uvsList.Add(uvs);
                entity.normalVectorsList.Add(normalVectors);
                entity.trianglesList.Add(triangles);
                entity.totalMeshCount++;
            }
            else
            {
                int lastMeshInfoIdx = quotient + 1;
                for (int meshInfoIdx = 0; meshInfoIdx < lastMeshInfoIdx; meshInfoIdx++)
                {
                    Vector3[] verticies     = null;
                    Vector2[] uvs           = null;
                    Vector3[] normalVectors = null;
                    int[]     triangles     = null;
                    if (meshInfoIdx == quotient)
                    {
                        verticies     = new Vector3[surplus];
                        uvs           = new Vector2[surplus];
                        normalVectors = new Vector3[surplus];
                        triangles     = new int[surplus];
                    }
                    else
                    {
                        verticies     = new Vector3[ObjAssetMeshBuilder.MAX_UNITY_TRIANGLES_COUNT];
                        uvs           = new Vector2[ObjAssetMeshBuilder.MAX_UNITY_TRIANGLES_COUNT];
                        normalVectors = new Vector3[ObjAssetMeshBuilder.MAX_UNITY_TRIANGLES_COUNT];
                        triangles     = new int[ObjAssetMeshBuilder.MAX_UNITY_TRIANGLES_COUNT];
                    }
                    entity.verticiesList.Add(verticies);
                    entity.uvsList.Add(uvs);
                    entity.normalVectorsList.Add(normalVectors);
                    entity.trianglesList.Add(triangles);
                    entity.totalMeshCount++;
                }
            }
            int polygonFaceIndex = 0;
            int triangleIndex    = 0;
            int entityIndex      = 0;
            List <ObjAssetPolygonFaceFormat> flist = format.fDictionary[groupPath];

            for (int fidx = 0; fidx < flist.Count; fidx++)
            {
                ObjAssetPolygonFaceFormat fformat = flist[fidx];
                //ポリゴン面情報の頂点インデックス、UVインデックス、法線インデックスと頂点、UV、法線情報を用いて、Objのポリゴン面情報を生成する。
                for (int iidx = 0; iidx < fformat.indexList.Count; iidx++)
                {
                    if (polygonFaceIndex == ObjAssetMeshBuilder.MAX_UNITY_TRIANGLES_COUNT)
                    {
                        polygonFaceIndex = 0;
                        triangleIndex    = 0;
                        entityIndex++;
                    }
                    ObjAssetPolygonFaceFormat.Index fformatIndex = fformat.indexList[iidx];
                    if (0 < format.vList.Count)
                    {
                        ObjAssetVertexFormat vformat = format.vList[fformatIndex.vertexIndex];
                        entity.verticiesList[entityIndex][polygonFaceIndex] = vformat.vertex;
                    }
                    if (0 < format.vtList.Count)
                    {
                        ObjAssetTextureFormat vtformat = format.vtList[fformatIndex.uvIndex];
                        entity.uvsList[entityIndex][polygonFaceIndex] = vtformat.uv;
                    }
                    if (0 < format.vnList.Count)
                    {
                        ObjAssetNormalVectorFormat vnformat = format.vnList[fformatIndex.normalVectorIndex];
                        entity.normalVectorsList[entityIndex][polygonFaceIndex] = vnformat.normal;
                    }
                    entity.trianglesList[entityIndex][polygonFaceIndex] = triangleIndex;
                    triangleIndex++;
                    polygonFaceIndex++;
                }
            }
            for (int meshCount = 0; meshCount < entity.totalMeshCount; meshCount++)
            {
                Vector3[] verticies     = entity.verticiesList[meshCount];
                Vector2[] uvs           = entity.uvsList[meshCount];
                Vector3[] normalVectors = entity.normalVectorsList[meshCount];
                int[]     triangles     = entity.trianglesList[meshCount];
                //メッシュ作成
                Mesh mesh = new Mesh();
                mesh.vertices     = verticies;
                mesh.uv           = uvs;
                mesh.normals      = normalVectors;
                mesh.subMeshCount = 1;
                mesh.triangles    = triangles;
                mesh.RecalculateBounds();
                meshList.Add(mesh);
            }
            return(meshList);
        }