public GameObject(GameObject gameobject) { this.Name = gameobject.Name; this.type = gameobject.type; this.tag = gameobject.tag; this.position = gameobject.position; this.material = gameobject.material; this.mesh = gameobject.mesh; }
public void TestMeshGenerationSimple(Vector3[] Points, float Width, float Smoothness, OSMType type, string Id, string other, string tag, string MaterialName, float height = 0f) { GameObject gameObject = new GameObject(type + "|" + ((int)type) + "|" + Id + (!string.IsNullOrEmpty(other) ? "|" + other : "")); gameObject.tag = tag; Polygon p = gameObject.AddComponent <Polygon>(); if (gameObject.GetComponent <MeshFilter>() == null) { MeshFilter meshFilter = gameObject.AddComponent <MeshFilter>(); meshFilter.mesh = new Mesh(); } else { gameObject.GetComponent <MeshFilter>().mesh = new Mesh(); } List <Vector3> vertices = new List <Vector3>(); List <Vector2> UVs = new List <Vector2>(); Vector2 uvTopLeft = new Vector2(1, 0); // new Vector2(0,0); Vector2 uvTopRight = new Vector2(0, 1); //new Vector2(1,0); Vector2 uvBottomLeft = new Vector2(1, 1); //new Vector2(0,1); Vector2 uvBottomRight = new Vector2(0, 0); //new Vector2(1,1); int offset = 1; if (Points.Length > 2) { offset = 2; } Vector3 AverageScale = new Vector3(0.5f, 1, 0.5f); Vector3[][] AllQuads = new Vector3[Points.Length - 1][]; for (int i = 0; i < Points.Length - 1; i++) { AllQuads[i] = Interpolations.MakeQuad(Points[i], Points[i + 1], Width, gameObject.transform); } for (int i = 0; i < Points.Length - offset; i++) { // Start 0 ,Start-L 1 ,End 2 , End-L 3 if (Points.Length == 2) { vertices.AddRange( new Vector3[] { AllQuads[i][0], AllQuads[i][2], AllQuads[i][1], AllQuads[i][1], AllQuads[i][2], AllQuads[i][3], // Reverse AllQuads[i][0], AllQuads[i][1], AllQuads[i][2], AllQuads[i][1], AllQuads[i][3], AllQuads[i][2] }); } else { var AvgESIdx = Vector3.Scale(AllQuads[i][2] + AllQuads[i + 1][0], AverageScale); var AvgELSLIdx = Vector3.Scale(AllQuads[i][3] + AllQuads[i + 1][1], AverageScale); if (i == 0) { vertices.AddRange( new Vector3[] { // First Quad AllQuads[i][0], AvgESIdx, AllQuads[i][1], AllQuads[i][1], AvgESIdx, AvgELSLIdx, // Reverse AllQuads[i][0], AllQuads[i][1], AvgESIdx, AllQuads[i][1], AvgELSLIdx, AvgESIdx, }); } else { var AvgESIdxP = Vector3.Scale(AllQuads[i - 1][2] + AllQuads[i][0], AverageScale); var AvgELSLIdxP = Vector3.Scale(AllQuads[i - 1][3] + AllQuads[i][1], AverageScale); vertices.AddRange( new Vector3[] { // First Quad AvgESIdxP, AvgESIdx, AvgELSLIdxP, AvgELSLIdxP, AvgESIdx, AvgELSLIdx, // Reverse AvgESIdxP, AvgELSLIdxP, AvgESIdx, AvgELSLIdxP, AvgELSLIdx, AvgESIdx, }); } } UVs.AddRange( new Vector2[] { // First Quad uvTopLeft, uvTopRight, uvBottomLeft, uvBottomLeft, uvTopRight, uvBottomRight, //Reverse uvTopLeft, uvBottomLeft, uvTopRight, uvBottomLeft, uvBottomRight, uvTopRight, }); } // Last quad if (Points.Length > 2) { var AvgESIdxlq = Vector3.Scale(AllQuads[AllQuads.Length - 2][2] + AllQuads[AllQuads.Length - 1][0], AverageScale); var AvgELSLIdxlq = Vector3.Scale(AllQuads[AllQuads.Length - 2][3] + AllQuads[AllQuads.Length - 1][1], AverageScale); vertices.AddRange( new Vector3[] { AvgESIdxlq, AllQuads[AllQuads.Length - 1][2], AvgELSLIdxlq, AvgELSLIdxlq, AllQuads[AllQuads.Length - 1][2], AllQuads[AllQuads.Length - 1][3], // Reverse AvgESIdxlq, AvgELSLIdxlq, AllQuads[AllQuads.Length - 1][2], AvgELSLIdxlq, AllQuads[AllQuads.Length - 1][3], AllQuads[AllQuads.Length - 1][2] }); UVs.AddRange( new Vector2[] { uvTopLeft, uvTopRight, uvBottomLeft, uvBottomLeft, uvTopRight, uvBottomRight, //Reverse uvTopLeft, uvBottomLeft, uvTopRight, uvBottomLeft, uvBottomRight, uvTopRight, }); } int[] tris = new int[vertices.Count]; for (int i = 0; i < tris.Length; i++) { tris[i] = i; } p.Rebuild(vertices.ToArray(), tris, UVs.ToArray(), MaterialName, true, false); }
public void GenerateRoad(Vector3[] Points, float Width, float Smoothness, OSMType type, string Id, string other, string tag, string MaterialName, float height = 0f) { // GameObject pathMesh = new GameObject(); // pathMesh.name = type + "|" + ((int)type) + "|" + Id + (!string.IsNullOrEmpty(other) ? "|" + other : ""); // pathMesh.tag = tag; // pathMesh.AddComponent(typeof(MeshFilter)); // pathMesh.AddComponent(typeof(MeshRenderer)); // pathMesh.renderer.sharedMaterial = Resources.Load(MaterialName, typeof(Material)) as Material; // pathMesh.AddComponent("AttachedPathScript"); // AttachedPathScript pathScript = pathMesh.GetComponent<AttachedPathScript>(); // pathScript.pathMesh = pathMesh; // pathScript.parentTerrain = GameObject.Find("Terrain"); //gameObject; // pathScript.NewPath(); // // pathScript.isRoad = true; // pathScript.pathSmooth = (int)Smoothness; // pathScript.pathWidth = Width; // // Debug.Log(pathScript.pathWidth); // // List<TerrainPathCell> nodes = new List<TerrainPathCell>(); // foreach (var p in Points) // { // TerrainPathCell temp = new TerrainPathCell(); // temp.position = new Vector2(p.x, p.z); // temp.heightAtCell = height; // pathScript.CreatePathNode(temp, Width); // } // // // EditorUtility.SetDirty(pathScript)s; // //pathScript.CreatePath(pathScript.pathSmooth, true, true); // // if (pathScript.nodeObjects.Length > 1) // { // // define terrain cells // pathScript.terrainCells = new TerrainPathCell[pathScript.terData.heightmapResolution * pathScript.terData.heightmapResolution]; // // for (int x = 0; x < pathScript.terData.heightmapResolution; x++) // { // for (int y = 0; y < pathScript.terData.heightmapResolution; y++) // { // pathScript.terrainCells[(y) + (x * pathScript.terData.heightmapResolution)].position.y = y; // pathScript.terrainCells[(y) + (x * pathScript.terData.heightmapResolution)].position.x = x; // pathScript.terrainCells[(y) + (x * pathScript.terData.heightmapResolution)].heightAtCell = pathScript.terrainHeights[y, x]; // pathScript.terrainCells[(y) + (x * pathScript.terData.heightmapResolution)].isAdded = false; // } // } // // // finalize path // // Undo.RegisterUndo(pathScript.terData, "Undo finalize path"); // bool success = pathScript.FinalizePath(); // // if (success) // { // if (pathScript.isRoad) // { // pathScript.pathMesh.renderer.enabled = true; // } // // else // { // MeshFilter meshFilter = (MeshFilter)pathScript.pathMesh.GetComponent(typeof(MeshFilter)); // Mesh destroyMesh = meshFilter.sharedMesh; // // DestroyImmediate(destroyMesh); // DestroyImmediate(meshFilter); // } // } // } // // else // Debug.Log("Not enough nodes to finalize"); //Object.DestroyImmediate(pathScript); }
public void Draw(Vector2[] Points, Color StartingColor, Color EndingColor, float WidthStart, float WidthEnd, OSMType type, string Id, string other, string tag) { GameObject gameObject = new GameObject(type + "|" + ((int)type) + "|" + Id + (!string.IsNullOrEmpty(other) ? "|" + other : "")); gameObject.tag = tag; LineRenderer lineRenderer = gameObject.AddComponent <LineRenderer>(); lineRenderer.material = new Material(Shader.Find("Particles/Zwrite")); //Particles/Additive lineRenderer.SetColors(StartingColor, EndingColor); lineRenderer.SetWidth(WidthStart, WidthEnd); lineRenderer.SetVertexCount(Points.Length); lineRenderer.useWorldSpace = false; for (int i = 0; i < Points.Length; i++) { lineRenderer.SetPosition(i, new Vector3(Points[i].x, 0, Points[i].y)); } }
public static void MeshGenerationFilledCornersORIGINAL(Vector3[] Points, float Width, OSMType type, string Id, string other, string tag, string MaterialName, float height) { GameObject gameObject = new GameObject(type + "|" + ((int)type) + "|" + Id + (!string.IsNullOrEmpty(other) ? "|" + other : "")); if (!string.IsNullOrEmpty(tag)) { gameObject.tag = tag; } Polygon p = gameObject.AddComponent <Polygon>(); if (gameObject.GetComponent <MeshFilter>() == null) { MeshFilter meshFilter = gameObject.AddComponent <MeshFilter>(); meshFilter.mesh = new Mesh(); } else { gameObject.GetComponent <MeshFilter>().mesh = new Mesh(); } if (DebugMode) { if (Points.Length > 2) { for (int i = 0; i < Points.Length - 2; i++) { GameObject g = new GameObject(); g.transform.position = Points[i + 1]; g.transform.parent = gameObject.transform; if (IsClockwise(Points[i], Points[i + 1], Points[i + 2])) { g.name = i + "Clockwise"; } else { g.name = i + "Counterclockwise"; } g.name += "-L1:" + (GetAngleOfLineBetweenTwoPoints(Points[i], Points[i + 1]) - GetAngleOfLineBetweenTwoPoints(Points[i + 1], Points[i + 2])); } } } List <Vector3> vertices = new List <Vector3>(); List <Vector2> UVs = new List <Vector2>(); Vector2 uvTopLeft = new Vector2(0, 0); // new Vector2(0,0); Vector2 uvTopRight = new Vector2(1, 0); //new Vector2(1,0); Vector2 uvBottomLeft = new Vector2(0, 1); //new Vector2(0,1); Vector2 uvBottomRight = new Vector2(1, 1); //new Vector2(1,1); Vector2 uvMiddleLeft = new Vector2(0, 0.5f); Vector2 uvMiddleRight = new Vector2(1, 0.5f); int offset = 1; //if (Points.Length > 2) // offset = 2; Vector3 AverageScale = new Vector3(0.5f, 1, 0.5f); Vector3[][] AllQuads = new Vector3[Points.Length - 1][]; for (int i = 0; i < Points.Length - 1; i++) { AllQuads[i] = Interpolations.MakeQuad(Points[i], Points[i + 1], Width, gameObject.transform); AllQuads[i][0].y = height; AllQuads[i][1].y = height; AllQuads[i][2].y = height; AllQuads[i][3].y = height; if (DebugMode) { GameObject s = GameObject.CreatePrimitive(PrimitiveType.Cube); s.name = "S " + i; s.transform.localScale = s.transform.localScale * 0.25f; s.transform.position = AllQuads[i][0]; s.GetComponent <Renderer>().sharedMaterial = Resources.Load("S", typeof(Material)) as Material; GameObject sl = GameObject.CreatePrimitive(PrimitiveType.Cube); sl.name = "SL " + i; sl.transform.localScale = sl.transform.localScale * 0.25f; sl.transform.position = AllQuads[i][1]; sl.GetComponent <Renderer>().sharedMaterial = Resources.Load("SL", typeof(Material)) as Material; GameObject e = GameObject.CreatePrimitive(PrimitiveType.Cube); e.name = "E " + i; e.transform.localScale = e.transform.localScale * 0.25f; e.transform.position = AllQuads[i][2]; e.GetComponent <Renderer>().sharedMaterial = Resources.Load("E", typeof(Material)) as Material; GameObject el = GameObject.CreatePrimitive(PrimitiveType.Cube); el.name = "EL " + i; el.transform.localScale = el.transform.localScale * 0.25f; el.transform.position = AllQuads[i][3]; el.GetComponent <Renderer>().sharedMaterial = Resources.Load("EL", typeof(Material)) as Material; s.transform.parent = gameObject.transform; sl.transform.parent = gameObject.transform; e.transform.parent = gameObject.transform; el.transform.parent = gameObject.transform; } } bool Clockwise = true; for (int i = 0; i < Points.Length - offset; i++) { Clockwise = true; // Start 0 ,Start-L 1 ,End 2 , End-L 3 if (Points.Length == 2 || i == 0) { #region First segment vertices.AddRange( new Vector3[] { AllQuads[i][0], AllQuads[i][2], AllQuads[i][1], AllQuads[i][1], AllQuads[i][2], AllQuads[i][3], // Reverse AllQuads[i][0], AllQuads[i][1], AllQuads[i][2], AllQuads[i][1], AllQuads[i][3], AllQuads[i][2] }); #endregion } else if (i == Points.Length - 1) { #region Last segment Vector3 MatchClockwise; Vector3 OffsetClockwise = Vector3.zero; // CLOCKWISE if (IsClockwise(Points[i - 2], Points[i - 1], Points[i])) /*Sl Match to El*/ { Clockwise = true; MatchClockwise = AllQuads[i - 2][2]; // El OffsetClockwise = AllQuads[i - 2][2] - AllQuads[i - 1][0]; OffsetClockwise.y = 0; } else { MatchClockwise = AllQuads[i - 1][0]; // S } Vector3 MatchCounterClockwise; Vector3 OffsetCounterClockwise = Vector3.zero; // COUNTERCLOCKWISE if (!IsClockwise(Points[i - 2], Points[i - 1], Points[i])) /*Sl Match to El*/ { Clockwise = false; MatchCounterClockwise = AllQuads[i - 2][3]; // El OffsetCounterClockwise = AllQuads[i - 2][3] - AllQuads[i - 1][1]; OffsetCounterClockwise.y = 0; } else { MatchCounterClockwise = AllQuads[i - 1][1]; // S } // Generate filled triangles or filled quads // Add UV before adding the vertices. #region Triangles if (Clockwise) { Vector3 t1 = AllQuads[i - 2][3]; Vector3 t2 = AllQuads[i - 1][1] + OffsetClockwise; Vector3 t3 = AllQuads[i - 2][2]; vertices.AddRange( new Vector3[] { t1, t2, t3, t1, t3, t2 }); UVs.AddRange( new Vector2[] { uvTopLeft, uvBottomLeft, uvMiddleRight, uvTopLeft, uvMiddleRight, uvBottomLeft }); } else if (!Clockwise) { Vector3 t1 = AllQuads[i - 2][2]; Vector3 t2 = AllQuads[i - 2][3]; Vector3 t3 = AllQuads[i - 1][0] + OffsetCounterClockwise; vertices.AddRange( new Vector3[] { t1, t2, t3, t1, t3, t2 }); UVs.AddRange( new Vector2[] { uvTopRight, uvMiddleLeft, uvBottomRight, uvTopRight, uvBottomRight, uvMiddleLeft }); } #endregion vertices.AddRange( new Vector3[] { OffsetCounterClockwise + MatchClockwise /*+AllQuads[i][0]*/, AllQuads[i][2], OffsetClockwise + MatchCounterClockwise /*AllQuads[i][1]*/, OffsetClockwise + MatchCounterClockwise /*AllQuads[i][1]*/, AllQuads[i][2], AllQuads[i][3], // Reverse OffsetCounterClockwise + MatchClockwise /*+AllQuads[i][0]*/, OffsetClockwise + MatchCounterClockwise /*AllQuads[i][1]*/, AllQuads[i][2], OffsetClockwise + MatchCounterClockwise /*AllQuads[i][1]*/, AllQuads[i][3], AllQuads[i][2] }); #endregion } else { #region Middle segments Vector3 MatchClockwise; Vector3 OffsetClockwise = Vector3.zero; // CLOCKWISE if (IsClockwise(Points[i - 1], Points[i], Points[i + 1])) /*Sl Match to El*/ { Clockwise = true; MatchClockwise = AllQuads[i - 1][2]; // El OffsetClockwise = AllQuads[i - 1][2] - AllQuads[i][0]; OffsetClockwise.y = 0; } else { MatchClockwise = AllQuads[i][0]; // S } Vector3 MatchCounterClockwise; Vector3 OffsetCounterClockwise = Vector3.zero; // COUNTERCLOCKWISE if (!IsClockwise(Points[i - 1], Points[i], Points[i + 1])) /*Sl Match to El*/ { Clockwise = false; MatchCounterClockwise = AllQuads[i - 1][3]; // El OffsetCounterClockwise = AllQuads[i - 1][3] - AllQuads[i][1]; OffsetCounterClockwise.y = 0; } else { MatchCounterClockwise = AllQuads[i][1]; // S } // Generate filled triangles or filled quads // Add UV before adding the vertices. #region Triangles if (Clockwise) { Vector3 t1 = AllQuads[i - 1][3]; Vector3 t2 = AllQuads[i][1] + OffsetClockwise; Vector3 t3 = AllQuads[i - 1][2]; vertices.AddRange( new Vector3[] { t1, t2, t3, t1, t3, t2 }); UVs.AddRange( new Vector2[] { uvTopLeft, uvBottomLeft, uvMiddleRight, uvTopLeft, uvMiddleRight, uvBottomLeft }); } else if (!Clockwise) { Vector3 t1 = AllQuads[i - 1][2]; Vector3 t2 = AllQuads[i - 1][3]; Vector3 t3 = AllQuads[i][0] + OffsetCounterClockwise; vertices.AddRange( new Vector3[] { t1, t2, t3, t1, t3, t2 }); UVs.AddRange( new Vector2[] { uvTopRight, uvMiddleLeft, uvBottomRight, uvTopRight, uvBottomRight, uvMiddleLeft }); } #endregion vertices.AddRange( new Vector3[] { OffsetCounterClockwise + MatchClockwise /*+AllQuads[i][0]*/, AllQuads[i][2], OffsetClockwise + MatchCounterClockwise /*AllQuads[i][1]*/, OffsetClockwise + MatchCounterClockwise /*AllQuads[i][1]*/, AllQuads[i][2], AllQuads[i][3], // Reverse OffsetCounterClockwise + MatchClockwise /*+AllQuads[i][0]*/, OffsetClockwise + MatchCounterClockwise /*AllQuads[i][1]*/, AllQuads[i][2], OffsetClockwise + MatchCounterClockwise /*AllQuads[i][1]*/, AllQuads[i][3], AllQuads[i][2] }); #endregion } UVs.AddRange( new Vector2[] { // First Quad uvTopLeft, uvBottomRight, uvTopLeft, uvTopLeft, uvBottomRight, uvBottomLeft, //Reverse uvTopRight, uvTopLeft, uvBottomRight, uvTopLeft, uvBottomLeft, uvBottomRight }); } int[] tris = new int[vertices.Count]; for (int i = 0; i < tris.Length; i++) { tris[i] = i; } p.Rebuild(vertices.ToArray(), tris, UVs.ToArray(), MaterialName, true, true); GameObject.DestroyImmediate(p); }
public void DrawUnscaled(Vector3[] Points, Color StartingColor, Color EndingColor, float WidthStart, float WidthEnd, OSMType type, string Id, string other, string tag, string MaterialName) { GameObject gameObject = new GameObject(type + "|" + ((int)type) + "|" + Id + (!string.IsNullOrEmpty(other) ? "|" + other : "")); gameObject.tag = tag; LineRenderer lineRenderer = gameObject.AddComponent <LineRenderer>(); lineRenderer.material = Resources.Load(MaterialName, typeof(Material)) as Material; lineRenderer.sharedMaterial.mainTextureScale = new Vector2(Points.Length, 1); lineRenderer.SetColors(StartingColor, EndingColor); lineRenderer.SetWidth(WidthStart, WidthEnd); lineRenderer.SetVertexCount(Points.Length); lineRenderer.useWorldSpace = false; for (int i = 0; i < Points.Length; i++) { lineRenderer.SetPosition(i, Points[i]); } }
public void TestMeshGenerationSimpleBackup(Vector3[] Points, float Width, float Smoothness, OSMType type, string Id, string other, string tag, string MaterialName, float height = 0f) { GameObject gameObject = new GameObject(type + "|" + ((int)type) + "|" + Id + (!string.IsNullOrEmpty(other) ? "|" + other : "")); gameObject.tag = tag; Polygon p = gameObject.AddComponent <Polygon>(); if (gameObject.GetComponent <MeshFilter>() == null) { MeshFilter meshFilter = gameObject.AddComponent <MeshFilter>(); meshFilter.mesh = new Mesh(); } else { gameObject.GetComponent <MeshFilter>().mesh = new Mesh(); } List <Vector3> vertices = new List <Vector3>(); List <Vector2> UVs = new List <Vector2>(); Vector2 uvTopLeft = new Vector2(1, 0); // new Vector2(0,0); Vector2 uvTopRight = new Vector2(0, 1); //new Vector2(1,0); Vector2 uvBottomLeft = new Vector2(1, 1); //new Vector2(0,1); Vector2 uvBottomRight = new Vector2(0, 0); //new Vector2(1,1); for (int i = 0; i < Points.Length - 1; i++) { // Start 0 ,Start-L 1 ,End 2 , End-L 3 var quads = Interpolations.MakeQuad(Points[i], Points[i + 1], Width, gameObject.transform); // S,E,SL // vertices.AddRange(new Vector3[] { S, E, SL }); // vertices.AddRange(new Vector3[] { SL, E, EL }); // Reverse // vertices.AddRange(new Vector3[] { S, SL, E }); // vertices.AddRange(new Vector3[] { SL, EL, E }); vertices.AddRange( new Vector3[] { quads[0], quads[2], quads[1], quads[1], quads[2], quads[3], // Reverse quads[0], quads[1], quads[2], quads[1], quads[3], quads[2] }); UVs.AddRange( new Vector2[] { uvTopLeft, uvTopRight, uvBottomLeft, uvBottomLeft, uvTopRight, uvBottomRight, //Reverse uvTopLeft, uvBottomLeft, uvTopRight, uvBottomLeft, uvBottomRight, uvTopRight }); } int[] tris = new int[vertices.Count]; for (int i = 0; i < tris.Length; i++) { tris[i] = i; } p.Rebuild(vertices.ToArray(), tris, UVs.ToArray(), MaterialName, true, false); // if (GenerateColliders) // gameObject.GetComponent<MeshCollider>().sharedMesh = gameObject.GetComponent<MeshFilter>().sharedMesh; }
public static GameObject MeshGenerationFilledCorners(Vector3[] Points, float Width, OSMType type, string Id, string other, string tag, string MaterialName, float height) { GameObject gameObject = new GameObject(type + "|" + ((int)type) + "|" + Id + (!string.IsNullOrEmpty(other) ? "|" + other : "")); gameObject.material = new Material(MaterialName); if (!string.IsNullOrEmpty(tag)) { gameObject.tag = tag; } List <Vector3> vertices = new List <Vector3>(); List <Vector2> UVs = new List <Vector2>(); Vector2 uvTopLeft = new Vector2(0, 0); // new Vector2(0,0); Vector2 uvTopRight = new Vector2(1, 0); //new Vector2(1,0); Vector2 uvBottomLeft = new Vector2(0, 1); //new Vector2(0,1); Vector2 uvBottomRight = new Vector2(1, 1); //new Vector2(1,1); Vector2 uvMiddleLeft = new Vector2(0, 0.5f); Vector2 uvMiddleRight = new Vector2(1, 0.5f); int offset = 1; //if (Points.Length > 2) // offset = 2; Vector3 AverageScale = new Vector3(0.5f, 1, 0.5f); Vector3[][] AllQuads = new Vector3[Points.Length - 1][]; for (int i = 0; i < Points.Length - 1; i++) { AllQuads[i] = MakeQuad(Points[i], Points[i + 1], Width); AllQuads[i][0].y = height; AllQuads[i][1].y = height; AllQuads[i][2].y = height; AllQuads[i][3].y = height; } bool Clockwise = true; for (int i = 0; i < Points.Length - offset; i++) { Clockwise = true; // Start 0 ,Start-L 1 ,End 2 , End-L 3 if (Points.Length == 2 || i == 0) { #region First segment vertices.AddRange( new Vector3[] { AllQuads[i][0], AllQuads[i][2], AllQuads[i][1], AllQuads[i][1], AllQuads[i][2], AllQuads[i][3], // Reverse AllQuads[i][0], AllQuads[i][1], AllQuads[i][2], AllQuads[i][1], AllQuads[i][3], AllQuads[i][2] }); #endregion } else if (i == Points.Length - 1) { #region Last segment Vector3 MatchClockwise; Vector3 OffsetClockwise = Vector3.zero; // CLOCKWISE if (IsClockwise(Points[i - 2], Points[i - 1], Points[i])) /*Sl Match to El*/ { Clockwise = true; MatchClockwise = AllQuads[i - 2][2]; // El OffsetClockwise = AllQuads[i - 2][2] - AllQuads[i - 1][0]; OffsetClockwise.y = 0; } else { MatchClockwise = AllQuads[i - 1][0]; // S } Vector3 MatchCounterClockwise; Vector3 OffsetCounterClockwise = Vector3.zero; // COUNTERCLOCKWISE if (!IsClockwise(Points[i - 2], Points[i - 1], Points[i])) /*Sl Match to El*/ { Clockwise = false; MatchCounterClockwise = AllQuads[i - 2][3]; // El OffsetCounterClockwise = AllQuads[i - 2][3] - AllQuads[i - 1][1]; OffsetCounterClockwise.y = 0; } else { MatchCounterClockwise = AllQuads[i - 1][1]; // S } // Generate filled triangles or filled quads // Add UV before adding the vertices. #region Triangles if (Clockwise) { Vector3 t1 = AllQuads[i - 2][3]; Vector3 t2 = AllQuads[i - 1][1] + OffsetClockwise; Vector3 t3 = AllQuads[i - 2][2]; vertices.AddRange( new Vector3[] { t1, t2, t3, t1, t3, t2 }); UVs.AddRange( new Vector2[] { uvTopLeft, uvBottomLeft, uvMiddleRight, uvTopLeft, uvMiddleRight, uvBottomLeft }); } else if (!Clockwise) { Vector3 t1 = AllQuads[i - 2][2]; Vector3 t2 = AllQuads[i - 2][3]; Vector3 t3 = AllQuads[i - 1][0] + OffsetCounterClockwise; vertices.AddRange( new Vector3[] { t1, t2, t3, t1, t3, t2 }); UVs.AddRange( new Vector2[] { uvTopRight, uvMiddleLeft, uvBottomRight, uvTopRight, uvBottomRight, uvMiddleLeft }); } #endregion vertices.AddRange( new Vector3[] { OffsetCounterClockwise + MatchClockwise /*+AllQuads[i][0]*/, AllQuads[i][2], OffsetClockwise + MatchCounterClockwise /*AllQuads[i][1]*/, OffsetClockwise + MatchCounterClockwise /*AllQuads[i][1]*/, AllQuads[i][2], AllQuads[i][3], // Reverse OffsetCounterClockwise + MatchClockwise /*+AllQuads[i][0]*/, OffsetClockwise + MatchCounterClockwise /*AllQuads[i][1]*/, AllQuads[i][2], OffsetClockwise + MatchCounterClockwise /*AllQuads[i][1]*/, AllQuads[i][3], AllQuads[i][2] }); #endregion } else { #region Middle segments Vector3 MatchClockwise; Vector3 OffsetClockwise = Vector3.zero; // CLOCKWISE if (IsClockwise(Points[i - 1], Points[i], Points[i + 1])) /*Sl Match to El*/ { Clockwise = true; MatchClockwise = AllQuads[i - 1][2]; // El OffsetClockwise = AllQuads[i - 1][2] - AllQuads[i][0]; OffsetClockwise.y = 0; } else { MatchClockwise = AllQuads[i][0]; // S } Vector3 MatchCounterClockwise; Vector3 OffsetCounterClockwise = Vector3.zero; // COUNTERCLOCKWISE if (!IsClockwise(Points[i - 1], Points[i], Points[i + 1])) /*Sl Match to El*/ { Clockwise = false; MatchCounterClockwise = AllQuads[i - 1][3]; // El OffsetCounterClockwise = AllQuads[i - 1][3] - AllQuads[i][1]; OffsetCounterClockwise.y = 0; } else { MatchCounterClockwise = AllQuads[i][1]; // S } // Generate filled triangles or filled quads // Add UV before adding the vertices. #region Triangles if (Clockwise) { Vector3 t1 = AllQuads[i - 1][3]; Vector3 t2 = AllQuads[i][1] + OffsetClockwise; Vector3 t3 = AllQuads[i - 1][2]; vertices.AddRange( new Vector3[] { t1, t2, t3, t1, t3, t2 }); UVs.AddRange( new Vector2[] { uvTopLeft, uvBottomLeft, uvMiddleRight, uvTopLeft, uvMiddleRight, uvBottomLeft }); } else if (!Clockwise) { Vector3 t1 = AllQuads[i - 1][2]; Vector3 t2 = AllQuads[i - 1][3]; Vector3 t3 = AllQuads[i][0] + OffsetCounterClockwise; vertices.AddRange( new Vector3[] { t1, t2, t3, t1, t3, t2 }); UVs.AddRange( new Vector2[] { uvTopRight, uvMiddleLeft, uvBottomRight, uvTopRight, uvBottomRight, uvMiddleLeft }); } #endregion vertices.AddRange( new Vector3[] { OffsetCounterClockwise + MatchClockwise /*+AllQuads[i][0]*/, AllQuads[i][2], OffsetClockwise + MatchCounterClockwise /*AllQuads[i][1]*/, OffsetClockwise + MatchCounterClockwise /*AllQuads[i][1]*/, AllQuads[i][2], AllQuads[i][3], // Reverse OffsetCounterClockwise + MatchClockwise /*+AllQuads[i][0]*/, OffsetClockwise + MatchCounterClockwise /*AllQuads[i][1]*/, AllQuads[i][2], OffsetClockwise + MatchCounterClockwise /*AllQuads[i][1]*/, AllQuads[i][3], AllQuads[i][2] }); #endregion } UVs.AddRange( new Vector2[] { // First Quad (BOTTOM) uvTopLeft, uvTopRight, uvBottomRight, uvTopLeft, uvBottomRight, uvBottomLeft, //Reverse (TOP) uvTopRight, uvTopLeft, uvBottomRight, uvTopLeft, uvBottomLeft, uvBottomRight }); } int[] tris = new int[vertices.Count]; for (int i = 0; i < tris.Length; i++) { tris[i] = i; } // Pivot on center of bounding box. gameObject.position = ChangePivot(ref vertices, PivotLocation.Center); //// gameObject.mesh = Rebuild(vertices.ToArray(), tris, UVs.ToArray()); return(gameObject); }
public static GameObject GenerateShapeUVedWithWalls_Balanced(this Poly2Tri.Polygon shape, OSMType type, string Id, string other, string tag, string MaterialName = "Building", float WallHeight = 5, float MaximumHeight = 12, bool GenerateColliders = true) { GameObject gameObject = new GameObject(type + "|" + ((int)type) + "|" + Id + (!string.IsNullOrEmpty(other) ? "|" + other : "")); gameObject.tag = tag; if (shape.Triangles == null) { return(gameObject); } if (shape.Triangles.Count == 0) { return(gameObject); } List <Vector3> vertices = new List <Vector3>(); for (int i = 0; i < shape.Triangles.Count; i++) { vertices.AddRange(shape.Triangles[i].Points.Select(s => new Vector3((float)s.X, WallHeight, (float)s.Y)).Reverse()); } // This is the starting index in the vertex list that belongs to the walls. // We use this to distinguish between roof UVs and wall UVs. int WallStartIndex = vertices.Count; List <Vector2> UVs = new List <Vector2>(); var minXRoof = vertices.GetRange(0, WallStartIndex).Select(i => i.x).Min(); var minZRoof = vertices.GetRange(0, WallStartIndex).Select(i => i.z).Min(); var maxXRoof = vertices.GetRange(0, WallStartIndex).Select(i => i.x).Max(); var maxZRoof = vertices.GetRange(0, WallStartIndex).Select(i => i.z).Max(); // for (int i = 0; i < UVs.Length; i++) for (int i = 0; i < WallStartIndex; i++) { UVs.Add(new Vector2( linear(vertices[i].x, minXRoof, maxXRoof, 0, 1), linear(vertices[i].z, minZRoof, maxZRoof, 0, 0.5f) )); } Vector2[] UVRef = new Vector2[] { new Vector2(0, 0.5f), // top left new Vector2(1, 0.5f), // top right new Vector2(0, 0.5f + 0.5f * WallHeight / MaximumHeight), // bottom left new Vector2(1, 0.5f + 0.5f * WallHeight / MaximumHeight) // bottom right // new Vector2(0,1), // bottom left // new Vector2(1,1) // bottom right }; #region Outer wall Vector3 HeightVector = new Vector3(0, WallHeight, 0); Vector3[] OriginalVertices = shape.Points.Select(s => new Vector3((float)s.X, WallHeight, (float)s.Y)).ToArray(); // Generating Walls - Total: (OriginalVertices.Length)*4*3 for (int i = 0; i < OriginalVertices.Length - 1; i++) { Vector3 v1 = OriginalVertices[i]; Vector3 v2 = OriginalVertices[i + 1]; var dist = Vector3.Distance(v1, v2); //Debug.Log("Distance: "+dist); dist = dist.Clamp(0, 5); var UVratioTopRight = new Vector2(dist / 5, UVRef[1].y); var UVratioBottomRight = new Vector2(dist / 5, UVRef[3].y); Vector3 v3 = OriginalVertices[i] - HeightVector; Vector3 v4 = OriginalVertices[i + 1] - HeightVector; vertices.AddRange(new Vector3[] { v1, v2, v3 }); UVs.Add(UVRef[0]); UVs.Add(UVratioTopRight); //UVs.Add(UVRef[1]); UVs.Add(UVRef[2]); vertices.AddRange(new Vector3[] { v3, v2, v4 }); UVs.Add(UVRef[2]); UVs.Add(UVratioTopRight); //UVs.Add(UVRef[1]); UVs.Add(UVratioBottomRight); //UVs.Add(UVRef[3]); // Reverse vertices.AddRange(new Vector3[] { v1, v3, v2 }); UVs.Add(UVRef[0]); UVs.Add(UVRef[2]); UVs.Add(UVratioTopRight); //UVs.Add(UVRef[1]); vertices.AddRange(new Vector3[] { v3, v4, v2 }); UVs.Add(UVRef[2]); UVs.Add(UVratioBottomRight); //UVs.Add(UVRef[3]); UVs.Add(UVratioTopRight); //UVs.Add(UVRef[1]); } // Last Wall - Total: 4*3 Vector3 lv1 = OriginalVertices[OriginalVertices.Length - 1]; Vector3 lv2 = OriginalVertices[0]; var distlw = Vector3.Distance(lv1, lv2); //Debug.Log("Distance: "+distlw); distlw = distlw.Clamp(0, 5); var UVratioTopRightlw = new Vector2(distlw / 5, UVRef[1].y); var UVratioBottomRightlw = new Vector2(distlw / 5, UVRef[3].y); Vector3 lv3 = OriginalVertices[OriginalVertices.Length - 1] - HeightVector; Vector3 lv4 = OriginalVertices[0] - HeightVector; vertices.AddRange(new Vector3[] { lv1, lv2, lv3 }); UVs.Add(UVRef[0]); UVs.Add(UVratioTopRightlw); //UVs.Add(UVRef[1]); UVs.Add(UVRef[2]); vertices.AddRange(new Vector3[] { lv3, lv2, lv4 }); UVs.Add(UVRef[2]); UVs.Add(UVratioTopRightlw); //UVs.Add(UVRef[1]); UVs.Add(UVratioBottomRightlw); //UVs.Add(UVRef[3]); // Reverse vertices.AddRange(new Vector3[] { lv1, lv3, lv2 }); UVs.Add(UVRef[0]); UVs.Add(UVRef[2]); UVs.Add(UVratioTopRightlw); //UVs.Add(UVRef[1]); vertices.AddRange(new Vector3[] { lv3, lv4, lv2 }); UVs.Add(UVRef[2]); UVs.Add(UVratioBottomRightlw); //UVs.Add(UVRef[3]); UVs.Add(UVratioTopRightlw); //UVs.Add(UVRef[1]); #endregion #region Inner walls List <Vector3[]> OriginalInnerVertices = new List <Vector3[]>(); if (shape.Holes != null) { for (int i = 0; i < shape.Holes.Count; i++) { OriginalInnerVertices.Add(shape.Holes[i].Points.Select(s => new Vector3((float)s.X, WallHeight, (float)s.Y)).ToArray()); } for (int holeidx = 0; holeidx < shape.Holes.Count; holeidx++) { // Generating Walls - Total: (OriginalInnerVertices.Length)*4*3 for (int i = 0; i < OriginalInnerVertices[holeidx].Length - 1; i++) { Vector3 v1 = OriginalInnerVertices[holeidx][i]; Vector3 v2 = OriginalInnerVertices[holeidx][i + 1]; var dist = Vector3.Distance(v1, v2); //Debug.Log("Distance: "+dist); dist = dist.Clamp(0, 5); var UVratioTopRight = new Vector2(dist / 5, UVRef[1].y); var UVratioBottomRight = new Vector2(dist / 5, UVRef[3].y); Vector3 v3 = OriginalInnerVertices[holeidx][i] - HeightVector; Vector3 v4 = OriginalInnerVertices[holeidx][i + 1] - HeightVector; vertices.AddRange(new Vector3[] { v1, v2, v3 }); UVs.Add(UVRef[0]); UVs.Add(UVratioTopRight); //UVs.Add(UVRef[1]); UVs.Add(UVRef[2]); vertices.AddRange(new Vector3[] { v3, v2, v4 }); UVs.Add(UVRef[2]); UVs.Add(UVratioTopRight); //UVs.Add(UVRef[1]); UVs.Add(UVratioBottomRight); //UVs.Add(UVRef[3]); // Reverse vertices.AddRange(new Vector3[] { v1, v3, v2 }); UVs.Add(UVRef[0]); UVs.Add(UVRef[2]); UVs.Add(UVratioTopRight); //UVs.Add(UVRef[1]); vertices.AddRange(new Vector3[] { v3, v4, v2 }); UVs.Add(UVRef[2]); UVs.Add(UVratioBottomRight); //UVs.Add(UVRef[3]); UVs.Add(UVratioTopRight); //UVs.Add(UVRef[1]); } // Last Wall - Total: 4*3 Vector3 lv1_hole = OriginalInnerVertices[holeidx][OriginalInnerVertices[holeidx].Length - 1]; Vector3 lv2_hole = OriginalInnerVertices[holeidx][0]; var distlw_hole = Vector3.Distance(lv1_hole, lv2_hole); //Debug.Log("Distance: "+distlw_hole); distlw_hole = distlw_hole.Clamp(0, 5); var UVratioTopRightlw_hole = new Vector2(distlw_hole / 5, UVRef[1].y); var UVratioBottomRightlw_hole = new Vector2(distlw_hole / 5, UVRef[3].y); Vector3 lv3_hole = OriginalInnerVertices[holeidx][OriginalInnerVertices[holeidx].Length - 1] - HeightVector; Vector3 lv4_hole = OriginalInnerVertices[holeidx][0] - HeightVector; vertices.AddRange(new Vector3[] { lv1_hole, lv2_hole, lv3_hole }); UVs.Add(UVRef[0]); UVs.Add(UVratioTopRightlw_hole); //UVs.Add(UVRef[1]); UVs.Add(UVRef[2]); vertices.AddRange(new Vector3[] { lv3_hole, lv2_hole, lv4_hole }); UVs.Add(UVRef[2]); UVs.Add(UVratioTopRightlw_hole); //UVs.Add(UVRef[1]); UVs.Add(UVratioBottomRightlw_hole); //UVs.Add(UVRef[3]); // Reverse vertices.AddRange(new Vector3[] { lv1_hole, lv3_hole, lv2_hole }); UVs.Add(UVRef[0]); UVs.Add(UVRef[2]); UVs.Add(UVratioTopRightlw_hole); //UVs.Add(UVRef[1]); vertices.AddRange(new Vector3[] { lv3_hole, lv4_hole, lv2_hole }); UVs.Add(UVRef[2]); UVs.Add(UVratioBottomRightlw_hole); //UVs.Add(UVRef[3]); UVs.Add(UVratioTopRightlw_hole); //UVs.Add(UVRef[1]); } } #endregion // int[] tris = new int[shape.Triangles.Count * 3 + (OriginalVertices.Length - 1) * 4 * 3 + 4 * 3]; int innerTriCount = 0; for (int i = 0; i < OriginalInnerVertices.Count; i++) { innerTriCount += (OriginalInnerVertices[i].Length - 1) * 4 * 3 + 4 * 3; } int[] tris = new int[shape.Triangles.Count * 3 + (OriginalVertices.Length - 1) * 4 * 3 + 4 * 3 + innerTriCount]; for (int i = 0; i < tris.Length; i++) { tris[i] = i; } gameObject.position = CoordinateConvertor.ChangePivot(ref vertices, CoordinateConvertor.PivotLocation.ZeroBottomCenter); gameObject.mesh = Rebuild(vertices.ToArray(), tris, UVs.ToArray()); gameObject.material = new Material(MaterialName); return(gameObject); }
public static GameObject GenerateShapeUVedPlanar_Balanced(this PolygonCuttingEar.CPolygonShape shape, OSMType type, string Id, string other, string tag, string MaterialName = "Area", float Height = 0, bool GenerateColliders = true) { GameObject gameObject = new GameObject(type + "|" + ((int)type) + "|" + Id + (!string.IsNullOrEmpty(other) ? "|" + other : "")); gameObject.tag = tag; List <Vector3> vertices = new List <Vector3>(); for (int i = 0; i < shape.NumberOfPolygons; i++) { vertices.AddRange(shape.Polygons(i).ToVector3(Height)); } // This is the starting index in the vertex list that belongs to the walls. // We use this to distinguish between roof UVs and wall UVs. int WallStartIndex = vertices.Count; List <Vector2> UVs = new List <Vector2>(); var minXRoof = vertices.GetRange(0, WallStartIndex).Select(i => i.x).Min(); var minZRoof = vertices.GetRange(0, WallStartIndex).Select(i => i.z).Min(); var maxXRoof = vertices.GetRange(0, WallStartIndex).Select(i => i.x).Max(); var maxZRoof = vertices.GetRange(0, WallStartIndex).Select(i => i.z).Max(); var average = new Vector3( vertices.GetRange(0, WallStartIndex).Select(s => s.x).Average(), vertices.GetRange(0, WallStartIndex).Select(s => s.y).Average(), vertices.GetRange(0, WallStartIndex).Select(s => s.z).Average()); //var avgx = linear(average.x, minXRoof, maxXRoof, 0, 1); //var avgy = linear(average.z, minZRoof, maxZRoof, 0, 1); //Vector3[] OriginalVertices = shape.InputVertices.ToVector3(Height); for (int i = 0; i < WallStartIndex; i++) { // bool found = false; var x = linear(vertices[i].x, minXRoof, maxXRoof, 0, 1); var y = linear(vertices[i].z, minZRoof, maxZRoof, 0, 1); //for (int j = 0; j < OriginalVertices.Length; j++) //{ // if (Math.Abs(OriginalVertices[j].x - vertices[i].x) < 0.001f && Math.Abs(OriginalVertices[j].z - vertices[i].z) < 0.001f) // { // //if (j % 2 == 0) // // UVs.Add(new Vector2(0, 0)); // //else // // UVs.Add(new Vector2(1, 0)); // if (x > avgx && Math.Abs(x - avgx) < Math.Abs(y - avgy)) // x = 1; // else if (x <= avgx && Math.Abs(avgx - x) < Math.Abs(y - avgy)) // x = 0; // if (y > avgy && Math.Abs(y - avgy) < Math.Abs(x - avgx)) // y = 1; // else if (y <= avgy && Math.Abs(avgy - y) < Math.Abs(x - avgx)) // y = 0; // UVs.Add(new Vector2(x, y)); // found = true; // break; // } //} //if (!found) //{ // var x = linear(vertices[i].x, minXRoof, maxXRoof, 0, 1); // var y = linear(vertices[i].z, minZRoof, maxZRoof, 0, 1); UVs.Add(new Vector2(x, y)); //} } Vector2[] UVRef = new Vector2[] { new Vector2(0, 0), // top left new Vector2(1, 0), // top right new Vector2(0, 1), // bottom left new Vector2(1, 1) // bottom right // new Vector2(0,1), // bottom left // new Vector2(1,1) // bottom right }; Vector3 HeightVector = new Vector3(0, 0, 0); int[] tris = new int[shape.NumberOfPolygons * 3]; for (int i = 0; i < tris.Length; i++) { tris[i] = i; } gameObject.position = CoordinateConvertor.ChangePivot(ref vertices, CoordinateConvertor.PivotLocation.ZeroBottomCenter); gameObject.mesh = Rebuild(vertices.ToArray(), tris, UVs.ToArray()); gameObject.material = new Material(MaterialName); return(gameObject); }