void GenerateMesh() { Meshes = new Mesh[triangles.Count]; for (int i = 0; i < triangles.Count; ++i) { GameObject triObj = new GameObject(i.ToString(), typeof(MeshFilter), typeof(MeshRenderer), typeof(MeshCollider)); Utils.SetupMeshRenderer(triObj); triObj.tag = Tags.Debris; triObj.layer = Layers.Debris; triObj.transform.SetParent(mainObj.transform); var mesh = new Mesh(); mesh.name = "mesh_" + i; Vector2[] points = triangles[i]; Vector2 centroid = PolyGraph.GetCentroid(points); Vector2[] vertices = new Vector2[3]; for (int j = 0; j < 3; ++j) { vertices[j] = points[j] - centroid; } mesh.vertices = Array.ConvertAll(vertices, v => (Vector3)v); mesh.triangles = new int[] { 0, 1, 2 }; mesh.colors = new Color[] { colors[i], colors[i], colors[i] }; triObj.GetComponent <MeshFilter>().mesh = mesh; triObj.transform.localPosition = centroid; triObj.GetComponent <MeshCollider>().sharedMesh = mesh; Meshes[i] = mesh; } }
void GenerateMesh() { Texture2D texture = null; if (useVertColor) { string path = string.Format("{0}/{1}/{1}.png", Paths.AssetArtworks, graph.name); texture = AssetDatabase.LoadAssetAtPath <Texture2D>(path); if (null == texture) { throw new Exception("Failed to load texture " + path); } } Meshes = new Mesh[triangles.Count]; for (int i = 0; i < triangles.Count; ++i) { GameObject triObj = new GameObject(i.ToString(), typeof(MeshFilter), typeof(MeshRenderer), typeof(MeshCollider)); Utils.SetupMeshRenderer(triObj); triObj.tag = Tags.Debris; triObj.layer = Layers.Debris; triObj.transform.SetParent(mainObj.transform); var mesh = new Mesh(); mesh.name = "mesh_" + i; Vector2[] points = triangles[i]; Vector2 centroid = PolyGraph.GetCentroid(points); Vector2[] vertices = new Vector2[3]; for (int j = 0; j < 3; ++j) { vertices[j] = points[j] - centroid; } mesh.vertices = Array.ConvertAll(vertices, v => (Vector3)v); mesh.triangles = new int[] { 0, 1, 2 }; if (useVertColor) { Color fillColor = texture.GetPixelBilinear( centroid.x / graph.size.x, centroid.y / graph.size.y); mesh.colors = new Color[] { fillColor, fillColor, fillColor }; } else { Vector2[] uv = new Vector2[3]; for (int j = 0; j < mesh.vertices.Length; ++j) { uv[j].x = points[j].x / graph.size.x; uv[j].y = points[j].y / graph.size.y; } mesh.uv = uv; } triObj.GetComponent <MeshFilter>().mesh = mesh; triObj.transform.localPosition = centroid; triObj.GetComponent <MeshCollider>().sharedMesh = mesh; Meshes[i] = mesh; } }
static void NewRegion( List <int> region, List <Triangle> triangles, PolyGraph graph, Material mat, Vector3[] verts, Color[] colors, Vector2[] uv, int index) { Vector3[] newVerts = new Vector3[region.Count * 3]; Color[] newColors = new Color[region.Count * 3]; for (int i = 0; i < region.Count; ++i) { for (int j = 0; j < 3; ++j) { int k = triangles[region[i]].vertices[j]; newVerts[i * 3 + j] = verts[k]; newColors[i * 3 + j] = colors[k]; } } Vector3 centroid = PolyGraph.GetCentroid(newVerts); int[] newTris = new int[newVerts.Length]; for (int i = 0; i < newVerts.Length; ++i) { newVerts[i] -= centroid; newTris[i] = i; } var mesh = new Mesh(); mesh.name = "mesh_" + index; mesh.vertices = newVerts; mesh.triangles = newTris; mesh.colors = newColors; mesh.uv = uv; MeshUtility.Optimize(mesh); string savePath = string.Format("{0}/{1}/Meshes/{2}.prefab", Paths.AssetArtworks, graph.name, mesh.name); AssetDatabase.CreateAsset(mesh, savePath); AssetDatabase.SaveAssets(); GameObject triObj = new GameObject( index.ToString(), typeof(MeshFilter), typeof(MeshRenderer), typeof(MeshCollider)); Utils.SetupMeshRenderer(triObj); triObj.tag = Tags.Debris; triObj.layer = Layers.Debris; triObj.transform.SetParent(graph.transform); triObj.transform.localPosition = centroid; triObj.GetComponent <MeshFilter>().mesh = mesh; triObj.GetComponent <MeshRenderer>().sharedMaterial = mat; triObj.GetComponent <MeshCollider>().sharedMesh = mesh; }
public static Color AvarageColor(PolyGraph graph) { Vector4 c = Vector4.zero; int count = 0; for (int i = 0; i < graph.transform.childCount; ++i) { var child = graph.transform.GetChild(i); var mesh = child.GetComponent <MeshFilter>().sharedMesh; var material = child.GetComponent <MeshRenderer>().sharedMaterial; int[] tris = mesh.triangles; if (material.IsKeywordEnabled(ShaderFeatures._USE_VERT_COLOR)) { Color[] colors = mesh.colors; for (int j = 0; j < tris.Length; j += 3, ++count) { c += (Vector4)colors[tris[j]]; } } else { Vector3[] verts = mesh.vertices; var texture = (Texture2D)material.mainTexture; for (int j = 0; j < tris.Length; j += 3, ++count) { Vector2[] points = new Vector2[] { child.localPosition + verts[tris[j]], child.localPosition + verts[tris[j + 1]], child.localPosition + verts[tris[j + 2]] }; Vector2 centroid = PolyGraph.GetCentroid(points); c += (Vector4)texture.GetPixelBilinear( centroid.x / graph.size.x, centroid.y / graph.size.y); } } } c /= count; return(c); }