public static GameObject CreateSurface(string name, Polygon poly, Material material, Rect rect, Vector2 textureScale, Vector2 textureOffset, float textureRotation, DisposalManager disposalManager) { GameObject hexa = new GameObject(name, typeof(MeshRenderer), typeof(MeshFilter)); int triCount = poly.Triangles.Count; if (newPoints == null) { newPoints = new List <Vector3> (triCount * 3); } else { newPoints.Clear(); } int[] triNew = new int[triCount * 3]; int newPointsCount = -1; if (hit == null) { hit = new Dictionary <Vector3, int> (20000); } else { hit.Clear(); } Vector3 p; p.z = 0; for (int k = 0; k < triCount; k++) { DelaunayTriangle dt = poly.Triangles [k]; int ptmp; p.x = dt.Points [0].Xf; p.y = dt.Points [0].Yf; if (hit.TryGetValue(p, out ptmp)) { triNew [k * 3] = ptmp; } else { newPoints.Add(p); hit [p] = ++newPointsCount; triNew [k * 3] = newPointsCount; } p.x = dt.Points [2].Xf; p.y = dt.Points [2].Yf; if (hit.TryGetValue(p, out ptmp)) { triNew [k * 3 + 1] = ptmp; } else { newPoints.Add(p); hit [p] = ++newPointsCount; triNew [k * 3 + 1] = newPointsCount; } p.x = dt.Points [1].Xf; p.y = dt.Points [1].Yf; if (hit.TryGetValue(p, out ptmp)) { triNew [k * 3 + 2] = ptmp; } else { newPoints.Add(p); hit [p] = ++newPointsCount; triNew [k * 3 + 2] = newPointsCount; } } Mesh mesh = new Mesh(); if (disposalManager != null) { if (disposalManager != null) { disposalManager.MarkForDisposal(hexa); } if (disposalManager != null) { disposalManager.MarkForDisposal(mesh); } } hexa.hideFlags |= HideFlags.HideInHierarchy; mesh.SetVertices(newPoints); // uv mapping if (material.mainTexture != null) { Vector2[] uv = new Vector2[newPoints.Count]; for (int k = 0; k < uv.Length; k++) { Vector2 coor = newPoints [k]; coor.x /= textureScale.x; coor.y /= textureScale.y; if (textureRotation != 0) { coor = RotatePoint(coor, Misc.Vector2zero, textureRotation); } coor += textureOffset; uv [k].x = (coor.x - rect.xMin) / rect.width; uv [k].y = (coor.y - rect.yMax) / rect.height; } mesh.uv = uv; } mesh.SetTriangles(triNew, 0); mesh.RecalculateNormals(); MeshFilter meshFilter = hexa.GetComponent <MeshFilter> (); meshFilter.mesh = mesh; hexa.GetComponent <MeshRenderer> ().sharedMaterial = material; return(hexa); }
public static Renderer CreateSurface(string name, Vector3[] surfPoints, int[] indices, Material material, DisposalManager disposalManager) { Rect dummyRect = new Rect(); return(CreateSurface(name, surfPoints, indices, material, dummyRect, Misc.Vector2one, Misc.Vector2zero, 0, disposalManager)); }
public static Renderer CreateSurface(string name, Vector3[] points, int[] indices, Material material, Rect rect, Vector2 textureScale, Vector2 textureOffset, float textureRotation, DisposalManager disposalManager) { GameObject hexa = new GameObject(name, typeof(MeshFilter), typeof(MeshRenderer)); if (disposalManager != null) { disposalManager.MarkForDisposal(hexa); // hexa.hideFlags = HideFlags.DontSave; } hexa.hideFlags |= HideFlags.HideInHierarchy; Mesh mesh = new Mesh(); if (disposalManager != null) { disposalManager.MarkForDisposal(mesh); //mesh.hideFlags = HideFlags.DontSave; } mesh.vertices = points; mesh.triangles = indices; // uv mapping if (material.mainTexture != null) { Vector2[] uv = new Vector2[points.Length]; for (int k = 0; k < uv.Length; k++) { Vector2 coor = points [k]; coor.x /= textureScale.x; coor.y /= textureScale.y; if (textureRotation != 0) { coor = RotatePoint(coor, Misc.Vector2zero, textureRotation); } coor += textureOffset; Vector2 normCoor = new Vector2((coor.x - rect.xMin) / rect.width, (coor.y - rect.yMax) / rect.height); uv [k] = normCoor; } mesh.uv = uv; } mesh.RecalculateNormals(); MeshFilter meshFilter = hexa.GetComponent <MeshFilter> (); meshFilter.mesh = mesh; Renderer renderer = hexa.GetComponent <MeshRenderer> (); renderer.sharedMaterial = material; return(renderer); }
public static Renderer CreateSurface(string name, Vector3[] points, int[] indices, Material material, Rect rect, Vector2 textureScale, Vector2 textureOffset, float textureRotation, DisposalManager disposalManager, bool addVerticesPositionsAsUV2 = false) { GameObject hexa = new GameObject(name, typeof(MeshFilter), typeof(MeshRenderer)); if (disposalManager != null) { disposalManager.MarkForDisposal(hexa); } hexa.hideFlags |= HideFlags.HideInHierarchy; Mesh mesh = new Mesh(); if (disposalManager != null) { disposalManager.MarkForDisposal(mesh); } mesh.vertices = points; mesh.triangles = indices; // uv mapping if (material.mainTexture != null) { if (uv == null || uv.Length != points.Length) { uv = new Vector2[points.Length]; } for (int k = 0; k < uv.Length; k++) { Vector2 coor = points[k]; coor.x /= textureScale.x; coor.y /= textureScale.y; if (textureRotation != 0) { coor = RotatePoint(coor, Misc.Vector2zero, textureRotation); } coor += textureOffset; Vector2 normCoor = new Vector2((coor.x - rect.xMin) / rect.width, (coor.y - rect.yMax) / rect.height); uv[k] = normCoor; } mesh.uv = uv; } // pass vertices positions as option in uv2 (need them due to dynamic batching converting to world space positions) if (addVerticesPositionsAsUV2) { if (uv2 == null || uv2.Length != points.Length) { uv2 = new Vector2[points.Length]; } for (int k = 0; k < uv2.Length; k++) { uv2[k].x = points[k].x + 0.5f; uv2[k].y = points[k].y + 0.5f; } mesh.uv2 = uv2; } mesh.RecalculateNormals(); MeshFilter meshFilter = hexa.GetComponent <MeshFilter>(); meshFilter.mesh = mesh; Renderer renderer = hexa.GetComponent <MeshRenderer>(); renderer.sharedMaterial = material; return(renderer); }