public static Mesh CreateLeafMesh2(int seed, int leaflets, float length) { Random.seed = seed; float outerAngle = 110.0f; Vector2 outerP0 = Vector2.zero; Vector2 outerP1 = new Vector2(0.0f, length); BenderRodriguez outerBend = new BenderRodriguez(outerP0, outerP1, outerAngle); float innerAngle = 60.0f; BenderRodriguez innerBend = new BenderRodriguez(outerP0, outerP1, innerAngle); int leafletDetail = 1; // 1 or above int vertsPrLeaflet = leafletDetail + 2; int vertexCountPrSide = leaflets * vertsPrLeaflet; Vector3[] verts = new Vector3[vertexCountPrSide * 2]; Debug.Log("||vertices|| " + verts.Length); // Build the first side of the leaf verts[0] = Vector3.zero; for (int l = 0; l < leaflets; ++l) { // Vertex on 'spine' float spineDelta = (l + 0.3f) / (float)leaflets; verts[1 + l * vertsPrLeaflet] = new Vector3(0.0f, 0.0f, spineDelta * length); // Outer vertex float outerDelta = (l + 1.0f) / (float)leaflets; Vector2 outerPos = outerBend.GetPosition(outerAngle * outerDelta); verts[1 + l * vertsPrLeaflet + 1] = new Vector3(outerPos.x, -0.4f * outerPos.x, outerPos.y); // Inner vertex if not last leaflet if (l < leaflets-1) { float innerDelta = (l + 0.9f) / (float)leaflets; Vector2 innerPos = innerBend.GetPosition(innerAngle * innerDelta); verts[1 + l * vertsPrLeaflet + 2] = new Vector3(innerPos.x, -0.4f * innerPos.x, innerPos.y); } } // Hard set last vertex to length away from origin to avoit precision errors verts[vertexCountPrSide-1] = new Vector3(0.0f, 0.0f, length); // Build the second side of the leaf for (int v = 0; v < vertexCountPrSide; ++v) { Vector3 vert = verts[v]; vert.x *= -1.0f; verts[v + vertexCountPrSide] = vert; } string vertsStr = ""; foreach (Vector3 v in verts) vertsStr += v.ToString("0.000"); Debug.Log(vertsStr); // Index the first side of the leaf int trisPrSide = leaflets * leafletDetail + (leaflets - 1) * 2; int indicesPrSide = trisPrSide * 3; int[] indices = new int[indicesPrSide * 2]; Debug.Log("||indices|| " + indices.Length); int prevSpineIndex = 1 - vertsPrLeaflet; int i = 0; while (i < indicesPrSide - 1) { int spineIndex = prevSpineIndex + vertsPrLeaflet; indices[i] = spineIndex-1; ++i; indices[i] = spineIndex+1; ++i; indices[i] = spineIndex; ++i; for (int l = 0; l < leafletDetail && i < indices.Length-1; ++l) { indices[i] = spineIndex; ++i; indices[i] = spineIndex + l + 1; ++i; indices[i] = spineIndex + l + 2; ++i; } if (i < indices.Length-1) { int nextSpineIndex = spineIndex + vertsPrLeaflet; indices[i] = spineIndex; ++i; indices[i] = nextSpineIndex - 1; ++i; indices[i] = nextSpineIndex; ++i; } prevSpineIndex = spineIndex; } // Index the second side of the leaf for (i = 0; i < indicesPrSide; ++i) indices[indicesPrSide + i] = indices[i] + vertexCountPrSide; string trisStr = ""; for (i = 0; i < indices.Length / 3; ++i) trisStr += "[" + indices[i*3] + ", " + indices[i*3+1] + ", " + indices[i*3+2] + "], "; Debug.Log(trisStr); Mesh mesh = new Mesh(); mesh.name = "PalmLeaf_" + seed; mesh.vertices = verts; mesh.triangles = indices; mesh.RecalculateNormals(); mesh.RecalculateBounds(); mesh.Optimize(); return mesh; }
public static Mesh CreateLeafMesh(int seed, float length) { Random.seed = seed; float outerAngle = 90.0f; Vector2 outerP0 = Vector2.zero; Vector2 outerP1 = new Vector2(0.0f, length); BenderRodriguez outerBend = new BenderRodriguez(outerP0, outerP1, outerAngle); int quads = 7; // TODO Add a 'spine' Vector3[] verts = new Vector3[(quads+1) * 2]; for (int q = 0; q <= quads; ++q) { float delta = q / (float)quads; Vector2 outerPos = outerBend.GetPosition(outerAngle * delta * delta); verts[2*q ] = outerPos; outerPos.x *= -1.0f; verts[2*q+1] = outerPos; } int[] indices = new int[quads * 6]; for (int q = 0; q < quads; ++q) { indices[6*q ] = 2*q; indices[6*q+1] = 2*q+2; indices[6*q+2] = 2*q+1; indices[6*q+3] = 2*q+1; indices[6*q+4] = 2*q+2; indices[6*q+5] = 2*q+3; } Mesh mesh = new Mesh(); mesh.name = "PalmLeaf_" + seed; mesh.vertices = verts; mesh.triangles = indices; mesh.RecalculateNormals(); mesh.RecalculateBounds(); mesh.Optimize(); return mesh; }