public static void SetUV(MaterialData mat, Mesh mesh, UVMapFn mapFn) { List <Vector2> uvs = new List <Vector2>(); for (int i = 0; i < mesh.vertices.Length; ++i) { uvs.Add(Vector2.zero); } for (int i = 0; i < mesh.triangles.Length; i += 3) { int t1 = mesh.triangles[i]; int t2 = mesh.triangles[i + 1]; int t3 = mesh.triangles[i + 2]; TriangleData td = new TriangleData(t1, t2, t3); Vector3 va = mesh.vertices[t1]; Vector3 vb = mesh.vertices[t2]; Vector3 vc = mesh.vertices[t3]; TriangleData.SetX(td, va.x, vb.x, vc.x); TriangleData.SetY(td, va.y, vb.y, vc.y); TriangleData.SetZ(td, va.z, vb.z, vc.z); mapFn(mat, td, uvs); } mesh.SetUVs(0, uvs); }
private void UpdateMesh(List <Vector3> input, Mesh mesh, MaterialID matid, UVMapper uvMapper) { Vertex3[] inputVtx3 = new Vertex3[input.Count]; CastToVertex3(input, inputVtx3); ConvexHull <Vertex3, Face3> hull = ConvexHull.Create <Vertex3, Face3>(inputVtx3, 0.0001); // List<Vertex3> hullVtx3 = new List<Vertex3>(hull.Points); List <Vertex3> hullVtx3 = new List <Vertex3>(); List <Face3> faces = new List <Face3>(hull.Faces); int[] indices = new int[faces.Count * 3]; int n = 0; for (int i = 0; i < faces.Count; ++i) { // Sometime in the future, I'd like each side of the log // to share vertices, and only separate them along the // cardinal edges. // This is how we do it when we want to separate each // triangle. We create a vertex for each point of each // triangle. hullVtx3.Add(faces[i].Vertices[0]); indices[n++] = hullVtx3.Count - 1; hullVtx3.Add(faces[i].Vertices[1]); indices[n++] = hullVtx3.Count - 1; hullVtx3.Add(faces[i].Vertices[2]); indices[n++] = hullVtx3.Count - 1; // This is the way to do it when you want to share // vertices between triangles. That's not going to // work with texture atlassing. // indices[n++] = hullVtx3.IndexOf(faces[i].Vertices[0]); // indices[n++] = hullVtx3.IndexOf(faces[i].Vertices[1]); // indices[n++] = hullVtx3.IndexOf(faces[i].Vertices[2]); } Vector3[] vertices = new Vector3[hullVtx3.Count]; CastToVector3(hullVtx3, vertices); mesh.Clear(); mesh.vertices = vertices; mesh.SetIndices(indices, MeshTopology.Triangles, 0); MaterialData md = MaterialIndex.GetMaterialData(matIndex, matid); UVMapFn mapFn = UVMapper.GetMapFunction(uvMapper, md.mapFnID); UVMapper.SetUV(md, mesh, mapFn); mesh.RecalculateNormals(); mesh.RecalculateBounds(); }