public static MMesh GetMMesh() { mesh = new MMesh(); MPoint center = mesh.CreatePoint(Vector3.zero); mesh.CreateSphereFace(center, 0.5f); return(mesh); }
// 针对导入模型的初始化 public MObject(GameObject template, string filename) { this.template = template; gameObject = GameObject.Instantiate(template); mesh = new MMesh(); using(StreamReader sr = new StreamReader(MDefinitions.SAVE_PATH + "/" + filename)) { string line = sr.ReadLine(); string[] lineComponent; bool parseLine; while (line!=null) { lineComponent = line.Split(' '); string type = lineComponent[0]; parseLine = true; switch (type) { case "p": float x = (float)Convert.ToDouble(lineComponent[1]); float y = (float)Convert.ToDouble(lineComponent[2]); float z = (float)Convert.ToDouble(lineComponent[3]); Vector3 position = new Vector3(x, y, z); mesh.CreatePoint(position); break; case "le": int start = Convert.ToInt32(lineComponent[1]); int end = Convert.ToInt32(lineComponent[2]); List<MPoint> pointList = mesh.pointList; mesh.CreateLinearEdge(pointList[start], pointList[end]); break; case "ce": int center = Convert.ToInt32(lineComponent[1]); float radius = (float)Convert.ToDouble(lineComponent[2]); Vector3 normal = new Vector3( (float)Convert.ToDouble(lineComponent[3]), (float)Convert.ToDouble(lineComponent[4]), (float)Convert.ToDouble(lineComponent[5])); mesh.CreateCurveEdge(mesh.pointList[center], radius, normal); break; case "ge": List<Vector3> points = new List<Vector3>(); line = sr.ReadLine(); while (line != null) { lineComponent = line.Split(' '); if(lineComponent[0] != "v") { parseLine = false; break; } else { points.Add(new Vector3( (float)Convert.ToDouble(lineComponent[1]), (float)Convert.ToDouble(lineComponent[2]), (float)Convert.ToDouble(lineComponent[3]))); } line = sr.ReadLine(); } mesh.CreateGeneralEdge(points); break; case "polyf": List<MLinearEdge> edgeList = new List<MLinearEdge>(); line = sr.ReadLine(); while (line != null) { lineComponent = line.Split(' '); if (lineComponent[0] != "e") { parseLine = false; break; } else { int index = Convert.ToInt32(lineComponent[1]); edgeList.Add((MLinearEdge)mesh.edgeList[index]); } line = sr.ReadLine(); } mesh.CreatePolygonFace(edgeList); break; case "circf": int circle = Convert.ToInt32(lineComponent[1]); mesh.CreateCircleFace((MCurveEdge)mesh.edgeList[circle]); break; case "conf": int top = Convert.ToInt32(lineComponent[1]); int bottom = Convert.ToInt32(lineComponent[2]); mesh.CreateConeFace(mesh.pointList[top], (MCurveEdge)mesh.edgeList[bottom]); break; case "cylf": top = Convert.ToInt32(lineComponent[1]); bottom = Convert.ToInt32(lineComponent[2]); mesh.CreateCylinderFace((MCurveEdge)mesh.edgeList[top], (MCurveEdge)mesh.edgeList[bottom]); break; case "sf": center = Convert.ToInt32(lineComponent[1]); radius = (float)Convert.ToDouble(lineComponent[2]); mesh.CreateSphereFace(mesh.pointList[center], radius); break; case "gff": points = new List<Vector3>(); line = sr.ReadLine(); while (line != null) { lineComponent = line.Split(' '); if (lineComponent[0] != "v") { parseLine = false; break; } else { points.Add(new Vector3( (float)Convert.ToDouble(lineComponent[1]), (float)Convert.ToDouble(lineComponent[2]), (float)Convert.ToDouble(lineComponent[3]))); } line = sr.ReadLine(); } mesh.CreateGeneralFlatFace(points); break; case "gcf": List<Vector3> vertices = new List<Vector3>(); line = sr.ReadLine(); while (line != null) { lineComponent = line.Split(' '); if (lineComponent[0] != "v") { parseLine = false; break; } else { vertices.Add(new Vector3( (float)Convert.ToDouble(lineComponent[1]), (float)Convert.ToDouble(lineComponent[2]), (float)Convert.ToDouble(lineComponent[3]))); } line = sr.ReadLine(); } List<int> triangles = new List<int>(); if (!parseLine) line = sr.ReadLine(); while (line != null) { lineComponent = line.Split(' '); if (lineComponent[0] != "f") { parseLine = false; break; } else { triangles.Add(Convert.ToInt32(lineComponent[1])); triangles.Add(Convert.ToInt32(lineComponent[2])); triangles.Add(Convert.ToInt32(lineComponent[3])); } line = sr.ReadLine(); } Mesh m = new Mesh(); m.vertices = vertices.ToArray(); m.triangles = triangles.ToArray(); m.RecalculateNormals(); mesh.CreateGeneralCurveFace(m); break; } if(parseLine)line = sr.ReadLine(); } sr.Close(); } InitObject(); }