public Vector3 CreateFloor(Vector3 position) { GenerateFloor(); floor = this.transform.Find("Floor").gameObject; floorAddOns = this.transform.Find("FloorAddOns").gameObject; parentInfo = this.transform.parent.GetComponent <Building>(); floor.GetComponent <MeshRenderer>().material = this.transform.parent.GetComponent <Building>().chosenMat; Vector3 point = position; MeshOps.CreateParall(point, parentInfo.width, this.floorHeight, parentInfo.depth, vertexList, triangleList, uvList); CreateWindows(point, position.y, this.floorNumber, parentInfo.width, parentInfo.depth); point = new Vector3(point.x, position.y + this.floorHeight, point.z); if (parentInfo.separatorList[this.floorNumber]) { //create separator MeshOps.CreateParall(point, parentInfo.width + this.separatorOffset * 2, this.separatorHeight, parentInfo.depth + this.separatorOffset * 2, vertexList2, triangleList2, uvList2); point = new Vector3(position.x, point.y + this.separatorHeight, position.z); } if (this.floorNumber == this.transform.parent.GetComponent <Building>().floors - 1) { //CreateRoof if needed if (parentInfo.hasRoof) { CreateRoof(point); } } SetAndClearFloorMesh(); SetAndClearFloorAddOnsMesh(); if (floor.GetComponent <BoxCollider>() != null) { DestroyImmediate(floor.gameObject.GetComponent <BoxCollider>()); } if (floorAddOns.GetComponent <BoxCollider>() != null) { DestroyImmediate(floorAddOns.gameObject.GetComponent <BoxCollider>()); } floor.AddComponent <BoxCollider>(); floorAddOns.AddComponent <BoxCollider>(); return(point); }
public void CreateSideWalks(List <GameObject> streetWeb, List <List <Vector3> > streetsPoly) { List <GameObject> sideWalks = new List <GameObject> (); for (int i = 0; i < streetWeb.Count; i++) { GameObject sideWalk = new GameObject("sideWalk", typeof(MeshRenderer), typeof(MeshFilter)); sideWalk.transform.parent = this.transform; sideWalk.GetComponent <MeshRenderer> ().material = (Material)Resources.Load("SideWalkMat"); List <List <IntPoint> > solution = new List <List <IntPoint> > (); //transform vertices of each mesh in points for clipper //List<IntPoint> intPoint = FromVecToIntPoint(streetWeb[i].GetComponent<MeshFilter>().sharedMesh.vertices); List <IntPoint> intPoint = FromVecToIntPoint(streetsPoly [i].ToArray()); //offset each mesh ClipperOffset co = new ClipperOffset(); co.AddPath(intPoint, JoinType.jtRound, EndType.etOpenRound); co.Execute(ref solution, 1000.0); List <Vector2> vertices2D = new List <Vector2> (); for (int j = 0; j < solution.Count; j++) { vertices2D = vertices2D.Concat(FromIntPointToVec(solution [j])).ToList(); } // Use the triangulator to get indices for creating triangles Triangulator tr = new Triangulator(vertices2D.ToArray()); int[] indices = tr.Triangulate(); // Create the Vector3 vertices Vector3[] vertices = new Vector3[vertices2D.Count]; for (int k = 0; k < vertices.Length; k++) { vertices [k] = new Vector3(vertices2D [k].x, 0f, vertices2D [k].y); } // Create the mesh Mesh msh = new Mesh(); msh.vertices = vertices; msh.triangles = indices; msh.RecalculateNormals(); msh.RecalculateBounds(); // Set up game object with mesh; sideWalk.GetComponent <MeshFilter> ().mesh = msh; sideWalks.Add(sideWalk); } //foreach intersectionCluster unify streets and sidewalks and subtract int swNumber = 1; foreach (HashSet <int> cluster in intersectionClusters) { Mesh newMesh = Subtract(UnifyPolygons(sideWalks, "sidewalks", cluster), UnifyPolygons(streetWeb, "streets", cluster)); //build 3D sidewalk List <GameObject> objs = new List <GameObject> (); GameObject sub = new GameObject("unifiedSideWalks", typeof(MeshFilter), typeof(MeshRenderer)); sub.transform.parent = this.transform; sub.GetComponent <MeshRenderer> ().material = (Material)Resources.Load("SideWalkMat"); sub.GetComponent <MeshFilter> ().mesh = newMesh; objs.Add(sub); GameObject sub1 = new GameObject("unifiedSideWalks", typeof(MeshFilter), typeof(MeshRenderer)); sub1.transform.parent = this.transform; sub1.GetComponent <MeshRenderer> ().material = (Material)Resources.Load("SideWalkMat"); sub1.GetComponent <MeshFilter> ().mesh = newMesh; sub1.transform.position = new Vector3(0f, 0.2f, 0f); objs.Add(sub1); GameObject sidewalk = new GameObject("sidewalk", typeof(MeshFilter), typeof(MeshRenderer)); sidewalk.transform.parent = this.transform; sidewalk.GetComponent <MeshRenderer> ().material = (Material)Resources.Load("SideWalkMat"); sidewalk.GetComponent <MeshFilter> ().sharedMesh = MeshOps.CreateSolid(newMesh, 0.15f); objs.Add(sidewalk); UnifyAndClearMesh(objs, "sideWalk" + swNumber, (Material)Resources.Load("SideWalkMat")); swNumber += 1; } foreach (GameObject sw in sideWalks) { DestroyImmediate(sw.gameObject); } }
public void CreateWindows(Vector3 position, float textCoord, int floorNumber, float width, float depth) { Vector3 point; if (this.floorNumber == parentInfo.floors) { return; } if (this.floorNumber == 0) //create door { point = new Vector3(position.x, position.y, position.z - depth / 2 - 0.01f); MeshOps.CreateParall(point, parentInfo.doorWidth, parentInfo.doorHeight, 0.3f, vertexList2, triangleList2, uvList2); return; } //create windows //front and back windows float winBox = Mathf.Min(this.windowBoxWidth, width); int winNumber = (int)Mathf.Floor(width / winBox); float winWidth = Mathf.Min(winBox - (0.1f * 2), this.windowWidth); float winHeight = Mathf.Min(this.windowHeight, this.floorHeight - 0.2f); float winDepth = 0.03f; float offset = width / winNumber; for (int i = 0; i < winNumber; i++) { //CreateBalcony if (this.frontWindows[i]) { var balconyWidth = winWidth + 0.3f; point = new Vector3(position.x - width / 2 + offset * i + (offset / 2), position.y + 0.05f, position.z - depth / 2 - 0.4f); MeshOps.CreateParall(point, balconyWidth, 0.4f, 0.1f, vertexList2, triangleList2, uvList2); //front var newPoint = new Vector3(point.x, point.y, point.z + 0.2f); MeshOps.CreateParall(newPoint, balconyWidth, 0.1f, 0.5f, vertexList2, triangleList2, uvList2); //bottom newPoint = new Vector3(point.x - balconyWidth / 2, point.y, point.z + 0.2f); MeshOps.CreateParall(newPoint, 0.1f, 0.4f, 0.5f, vertexList2, triangleList2, uvList2); //left newPoint = new Vector3(point.x + balconyWidth / 2, point.y, point.z + 0.2f); MeshOps.CreateParall(newPoint, 0.1f, 0.4f, 0.5f, vertexList2, triangleList2, uvList2); //right point = new Vector3(point.x, position.y + 0.05f, position.z - depth / 2 - 0.01f); MeshOps.CreateParall(point, winWidth, this.floorHeight - (this.floorHeight - winHeight) / 2 - 0.05f, winDepth, vertexList2, triangleList2, uvList2); //window } else { point = new Vector3(position.x - width / 2 + offset * i + (offset / 2), position.y + (this.floorHeight - winHeight) / 2, position.z - depth / 2 - 0.01f); MeshOps.CreateParall(point, winWidth, winHeight, winDepth, vertexList2, triangleList2, uvList2); } if (this.backWindows[i]) { var balconyWidth = winWidth + 0.3f; point = new Vector3(position.x - width / 2 + offset * i + (offset / 2), position.y + 0.05f, position.z + depth / 2); var newPoint = new Vector3(point.x, point.y, point.z + 0.4f); MeshOps.CreateParall(newPoint, balconyWidth, 0.4f, 0.1f, vertexList2, triangleList2, uvList2); //front newPoint = new Vector3(point.x, point.y, point.z + 0.2f); MeshOps.CreateParall(newPoint, balconyWidth, 0.1f, 0.5f, vertexList2, triangleList2, uvList2); //bottom newPoint = new Vector3(point.x - balconyWidth / 2, point.y, point.z + 0.2f); MeshOps.CreateParall(newPoint, 0.1f, 0.4f, 0.5f, vertexList2, triangleList2, uvList2); //left side newPoint = new Vector3(point.x + balconyWidth / 2, point.y, point.z + 0.2f); MeshOps.CreateParall(newPoint, 0.1f, 0.4f, 0.5f, vertexList2, triangleList2, uvList2); //right side point = new Vector3(point.x, position.y + 0.05f, position.z + depth / 2 + 0.01f); MeshOps.CreateParall(point, winWidth, this.floorHeight - (this.floorHeight - winHeight) / 2 - 0.05f, winDepth, vertexList2, triangleList2, uvList2); //window } else { point = new Vector3(position.x - width / 2 + offset * i + (offset / 2), position.y + (this.floorHeight - winHeight) / 2, position.z + depth / 2 + 0.01f); MeshOps.CreateParall(point, winWidth, winHeight, winDepth, vertexList2, triangleList2, uvList2); } } //side windows float sideWinBox = Mathf.Min(this.sideWinBoxWidth, depth); int sideWinNumber = (int)Mathf.Floor(depth / sideWinBox); winWidth = Mathf.Min(sideWinBox - (0.1f * 2), this.sideWindowWidth); winHeight = Mathf.Min(this.sideWindowHeight, this.floorHeight - 0.2f); winDepth = 0.03f; offset = depth / sideWinNumber; for (int i = 0; i < sideWinNumber; i++) { //CreateBalcony if (this.leftWindows[i]) { var balconyWidth = winWidth + 0.3f; point = new Vector3(position.x - width / 2, position.y + 0.05f, position.z - depth / 2 + offset * i + (offset / 2)); var newPoint = new Vector3(point.x - 0.4f, point.y, point.z); MeshOps.CreateParall(newPoint, 0.1f, 0.4f, balconyWidth, vertexList2, triangleList2, uvList2); //front newPoint = new Vector3(point.x - 0.2f, point.y, point.z); MeshOps.CreateParall(newPoint, 0.5f, 0.1f, balconyWidth, vertexList2, triangleList2, uvList2); //bottom newPoint = new Vector3(point.x - 0.2f, point.y, point.z + balconyWidth / 2); MeshOps.CreateParall(newPoint, 0.5f, 0.4f, 0.1f, vertexList2, triangleList2, uvList2); //left newPoint = new Vector3(point.x - 0.2f, point.y, point.z - balconyWidth / 2); MeshOps.CreateParall(newPoint, 0.5f, 0.4f, 0.1f, vertexList2, triangleList2, uvList2); //right point = new Vector3(point.x - 0.01f, position.y + 0.05f, point.z); MeshOps.CreateParall(point, winDepth, this.floorHeight - (this.floorHeight - winHeight) / 2 - 0.05f, winWidth, vertexList2, triangleList2, uvList2); //window } else { point = new Vector3(position.x - width / 2 - 0.01f, position.y + (this.floorHeight - winHeight) / 2, position.z - depth / 2 + offset * i + (offset / 2)); MeshOps.CreateParall(point, winDepth, winHeight, winWidth, vertexList2, triangleList2, uvList2); } if (this.rightWindows[i]) { var balconyWidth = winWidth + 0.3f; point = new Vector3(position.x + width / 2, position.y + 0.05f, position.z - depth / 2 + offset * i + (offset / 2)); var newPoint = new Vector3(point.x + 0.4f, point.y, point.z); MeshOps.CreateParall(newPoint, 0.1f, 0.4f, balconyWidth, vertexList2, triangleList2, uvList2); //front newPoint = new Vector3(point.x + 0.2f, point.y, point.z); MeshOps.CreateParall(newPoint, 0.5f, 0.1f, balconyWidth, vertexList2, triangleList2, uvList2); //bottom newPoint = new Vector3(point.x + 0.2f, point.y, point.z - balconyWidth / 2); MeshOps.CreateParall(newPoint, 0.5f, 0.4f, 0.1f, vertexList2, triangleList2, uvList2); //left newPoint = new Vector3(point.x + 0.2f, point.y, point.z + balconyWidth / 2); MeshOps.CreateParall(newPoint, 0.5f, 0.4f, 0.1f, vertexList2, triangleList2, uvList2); //right point = new Vector3(point.x + 0.01f, position.y + 0.05f, point.z); MeshOps.CreateParall(point, winDepth, this.floorHeight - (this.floorHeight - winHeight) / 2 - 0.05f, winWidth, vertexList2, triangleList2, uvList2); //window } else { point = new Vector3(position.x + width / 2 + 0.01f, position.y + (this.floorHeight - winHeight) / 2, position.z - depth / 2 + offset * i + (offset / 2)); MeshOps.CreateParall(point, winDepth, winHeight, winWidth, vertexList2, triangleList2, uvList2); } } }
public void CreateRoof(Vector3 position) { float rLowWidth; if ((parentInfo.roofLowWidth - parentInfo.width) < 0) { rLowWidth = parentInfo.width; } else { rLowWidth = parentInfo.roofLowWidth; } float rUpWidth = Mathf.Min(rLowWidth, parentInfo.roofUpWidth); float rDepth; if ((parentInfo.roofDepth - parentInfo.depth) < 0) { rDepth = parentInfo.depth; } else { rDepth = parentInfo.roofDepth; } var offsetLowX = (rLowWidth - parentInfo.width) / 2; var offsetUpX = (parentInfo.width - rUpWidth) / 2; var offsetZ = (rDepth - parentInfo.depth) / 2; //build classic roof if (floorNumber <= 3) { //front vertexList.Add(new Vector3(position.x - parentInfo.width / 2 + offsetUpX, position.y + parentInfo.roofHeight, position.z)); //top left vertexList.Add(new Vector3(position.x + parentInfo.width / 2 - offsetUpX, position.y + parentInfo.roofHeight, position.z)); //top right vertexList.Add(new Vector3(position.x - parentInfo.width / 2 - offsetLowX, position.y, position.z - parentInfo.depth / 2 - offsetZ)); //bot left vertexList.Add(new Vector3(position.x + parentInfo.width / 2 + offsetLowX, position.y, position.z - parentInfo.depth / 2 - offsetZ)); //bot right MeshOps.AddQuad(vertexList, triangleList); uvList.Add(new Vector2(position.x - parentInfo.width / 2 + offsetUpX, position.y + parentInfo.roofHeight)); //top left uvList.Add(new Vector2(position.x + parentInfo.width / 2 - offsetUpX, position.y + parentInfo.roofHeight)); //top right uvList.Add(new Vector2(position.x - parentInfo.width / 2 - offsetLowX, position.y)); //bot left uvList.Add(new Vector2(position.x + parentInfo.width / 2 + offsetLowX, position.y)); //bot right //back vertexList.Add(new Vector3(position.x + parentInfo.width / 2 - offsetUpX, position.y + parentInfo.roofHeight, position.z)); //top right vertexList.Add(new Vector3(position.x - parentInfo.width / 2 + offsetUpX, position.y + parentInfo.roofHeight, position.z)); //top left vertexList.Add(new Vector3(position.x + parentInfo.width / 2 + offsetLowX, position.y, position.z + parentInfo.depth / 2 + offsetZ)); //bot right vertexList.Add(new Vector3(position.x - parentInfo.width / 2 - offsetLowX, position.y, position.z + parentInfo.depth / 2 + offsetZ)); //bot left MeshOps.AddQuad(vertexList, triangleList); uvList.Add(new Vector2(position.x + parentInfo.width / 2 - offsetUpX, position.y + parentInfo.roofHeight)); //top right uvList.Add(new Vector2(position.x - parentInfo.width / 2 + offsetUpX, position.y + parentInfo.roofHeight)); //top left uvList.Add(new Vector2(position.x + parentInfo.width / 2 + offsetLowX, position.y)); //bot right uvList.Add(new Vector2(position.x - parentInfo.width / 2 - offsetLowX, position.y)); //bot left //left side vertexList.Add(new Vector3(position.x - parentInfo.width / 2 + offsetUpX, position.y + parentInfo.roofHeight, position.z)); //top vertexList.Add(new Vector3(position.x - parentInfo.width / 2 - offsetLowX, position.y, position.z - parentInfo.depth / 2 - offsetZ)); //bot right vertexList.Add(new Vector3(position.x - parentInfo.width / 2 - offsetLowX, position.y, position.z + parentInfo.depth / 2 + offsetZ)); //bot left MeshOps.AddTriangle(vertexList, triangleList); uvList.Add(new Vector2(position.x - parentInfo.width / 2 + offsetUpX, position.y + parentInfo.roofHeight)); //top right uvList.Add(new Vector2(position.x - parentInfo.width / 2 - offsetLowX, position.y)); //top left uvList.Add(new Vector2(position.x - parentInfo.width / 2 - offsetLowX, position.y)); //bot left //right side vertexList.Add(new Vector3(position.x + parentInfo.width / 2 - offsetUpX, position.y + parentInfo.roofHeight, position.z)); //top right vertexList.Add(new Vector3(position.x + parentInfo.width / 2 + offsetLowX, position.y, position.z + parentInfo.depth / 2 + offsetZ)); //bot right vertexList.Add(new Vector3(position.x + parentInfo.width / 2 + offsetLowX, position.y, position.z - parentInfo.depth / 2 - offsetZ)); //bot left MeshOps.AddTriangle(vertexList, triangleList); uvList.Add(new Vector2(position.x + parentInfo.width / 2 - offsetUpX, position.y + parentInfo.roofHeight)); //top right uvList.Add(new Vector2(position.x + parentInfo.width / 2 + offsetLowX, position.y)); //top left uvList.Add(new Vector2(position.x + parentInfo.width / 2 - offsetLowX, position.y)); //bot left //bottom vertexList.Add(new Vector3(position.x - parentInfo.width / 2 - offsetLowX, position.y, position.z - parentInfo.depth / 2 - offsetZ)); //top left vertexList.Add(new Vector3(position.x + parentInfo.width / 2 + offsetLowX, position.y, position.z - parentInfo.depth / 2 - offsetZ)); //top right vertexList.Add(new Vector3(position.x - parentInfo.width / 2 - offsetLowX, position.y, position.z + parentInfo.depth / 2 + offsetZ)); //bot left vertexList.Add(new Vector3(position.x + parentInfo.width / 2 + offsetLowX, position.y, position.z + parentInfo.depth / 2 + offsetZ)); //bot right MeshOps.AddQuad(vertexList, triangleList); uvList.Add(new Vector2(position.x - parentInfo.width / 2 - offsetLowX, position.y)); //top left uvList.Add(new Vector2(position.x + parentInfo.width / 2 + offsetLowX, position.y)); //top right uvList.Add(new Vector2(position.x - parentInfo.width / 2 - offsetLowX, position.y)); //bot left uvList.Add(new Vector2(position.x + parentInfo.width / 2 + offsetLowX, position.y)); //bot right } //build skyscraper roof else { Vector3 point; //square empty in the middle if (parentInfo.roofType == 1) { if (offsetZ <= 0.1f) { rDepth = rDepth - offsetZ * 2 + 0.4f; offsetZ = 0.2f; } if (offsetLowX <= 0.1f) { rLowWidth = rLowWidth - offsetLowX * 2 + 0.4f; offsetLowX = 0.2f; } point = new Vector3(position.x, position.y, position.z - parentInfo.depth / 2); MeshOps.CreateParall(point, Mathf.Min(rLowWidth, parentInfo.width * 11 / 10), 0.4f, offsetZ * 2, vertexList, triangleList, uvList); //front point = new Vector3(position.x, position.y, position.z + parentInfo.depth / 2); MeshOps.CreateParall(point, Mathf.Min(rLowWidth, parentInfo.width * 11 / 10), 0.4f, offsetZ * 2, vertexList, triangleList, uvList); //back point = new Vector3(position.x - parentInfo.width / 2, position.y, position.z); MeshOps.CreateParall(point, 2 * offsetLowX, 0.4f, Mathf.Min(rDepth, parentInfo.depth * 11 / 10), vertexList, triangleList, uvList); //left point = new Vector3(position.x + parentInfo.width / 2, position.y, position.z); MeshOps.CreateParall(point, 2 * offsetLowX, 0.4f, Mathf.Min(rDepth, parentInfo.depth * 11 / 10), vertexList, triangleList, uvList); } //single big square else { MeshOps.CreateParall(position, rLowWidth, 0.4f, rDepth, vertexList2, triangleList2, uvList2); } } }