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); }
public override ObjAssetFormat Read(string assetPath) { string objAllData = System.IO.File.ReadAllText(assetPath); string[] objDatas = objAllData.Split('\n'); ObjAssetFormat objAssetFormat = new ObjAssetFormat(); objAssetFormat.Create(); foreach (string objData in objDatas) { if (0 == objData.Length) { continue; } string trimedObjData = objData.Trim(); int firstWhiteSpaceIndex = trimedObjData.IndexOf(" ", 0); if (-1 == firstWhiteSpaceIndex) { continue; } string header = trimedObjData.Substring(0, firstWhiteSpaceIndex).Trim(); string body = trimedObjData.Substring(firstWhiteSpaceIndex, trimedObjData.Length - firstWhiteSpaceIndex).Trim(); switch (header) { case ObjAssetBaseFormat.COMMENT: break; case ObjAssetBaseFormat.F: ObjAssetBaseParser <ObjAssetPolygonFaceFormat> fStream = new ObjAssetPolygonFaceParser(); ObjAssetPolygonFaceFormat fformat = fStream.Read(header, body); objAssetFormat.AddPolygonFace(fformat); break; case ObjAssetBaseFormat.G: ObjAssetBaseParser <ObjAssetGroupFormat> gstream = new ObjAssetGroupParser(); ObjAssetGroupFormat gformat = gstream.Read(header, body); objAssetFormat.AddGroup(gformat); break; case ObjAssetBaseFormat.MTLLIB: ObjAssetBaseParser <ObjAssetMaterialFormat> mtlibstream = new ObjAssetMaterialParser(); ObjAssetMaterialFormat mtlibFormat = mtlibstream.Read(header, body); objAssetFormat.mtllibList.Add(mtlibFormat); break; case ObjAssetBaseFormat.O: ObjAssetBaseParser <ObjAssetObjectFormat> ostream = new ObjAssetObjectParser(); ObjAssetObjectFormat oformat = ostream.Read(header, body); objAssetFormat.AddObject(oformat); break; case ObjAssetBaseFormat.USEMTL: ObjAssetBaseParser <ObjAssetUseMaterialFormat> usemtlstream = new ObjAssetUseMaterialParser(); ObjAssetUseMaterialFormat usemtlformat = usemtlstream.Read(header, body); objAssetFormat.AddUseMaterial(usemtlformat); break; case ObjAssetBaseFormat.V: ObjAssetBaseParser <ObjAssetVertexFormat> vstream = new ObjAssetVertexParser(); ObjAssetVertexFormat vformat = vstream.Read(header, body); objAssetFormat.vList.Add(vformat); break; case ObjAssetBaseFormat.VT: ObjAssetBaseParser <ObjAssetTextureFormat> vtstream = new ObjAssetTextureParser(); ObjAssetTextureFormat vtformat = vtstream.Read(header, body); objAssetFormat.vtList.Add(vtformat); break; case ObjAssetBaseFormat.VN: ObjAssetBaseParser <ObjAssetNormalVectorFormat> vnstream = new ObjAssetNormalVectorParser(); ObjAssetNormalVectorFormat vnformat = vnstream.Read(header, body); objAssetFormat.vnList.Add(vnformat); break; default: string log = string.Format("unknown format.header::{0},body{1}", header, body); Debug.LogWarning(log); break; } } return(objAssetFormat); }