Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using Ephere.Geometry;
- /// Source: http://demonstrations.wolfram.com/VirtualFlowersWithCrispatePetals/
- namespace Flowers
- {
- public static class FlowerUtils
- {
- public static PolygonMesh CreateFlower(double details, int innerSegments, double r0, double r1,
- double c, double d, double a1, double p, double growth, double aa, double b, double a)
- {
- const double tMax = 6.255;
- int silhouetteVertCount = (int)(tMax / details);
- int numVerts = innerSegments * silhouetteVertCount + 1;
- Vector3[] vertices = new Vector3[numVerts];
- vertices[vertices.Length - 1] = new Vector3(0, 0, 0);
- PolygonMesh.Polygon[] faces = new PolygonMesh.Polygon[numVerts - 1];
- double angleStep = 2 * Math.PI * details / tMax;
- for (int i = 0; i < silhouetteVertCount; i++)
- {
- double angle = i * angleStep;
- double radius = r0 + (r1 * Math.Pow(Math.Abs(Math.Sin(c * angle * 0.5)), d));
- double x = radius * Math.Cos(angle);
- double y = radius * Math.Sin(angle);
- double rad = Math.Pow(x, 2) + Math.Pow(y, 2);
- double z = (1 + rad) * a1 * Math.Sin(p * i);
- vertices[i] = new Vector3((float)x, (float)y, (float)z) * (float)growth;
- }
- if (innerSegments == 1)
- for (int i = 0; i < faces.Length; i++)
- faces[i] = new PolygonMesh.Polygon(new int[] { vertices.Length - 1, i, (i + 1) % faces.Length });
- else
- for (int i = 0; i < innerSegments ; i++)
- {
- float discount = (i + 1.25f) / (innerSegments + 1);
- for (int j = 0; j < silhouetteVertCount; j++)
- {
- if (i < innerSegments - 1)
- vertices[j + (i + 1) * silhouetteVertCount] = vertices[j] * discount;
- int k = (i + 1) * silhouetteVertCount;
- int l = i * silhouetteVertCount;
- if (i == 0)
- faces[j] = new PolygonMesh.Polygon(new int[] { vertices.Length - 1, k + j, ((j + 1) % silhouetteVertCount) + silhouetteVertCount});
- else if (i < innerSegments - 1)
- faces[i * silhouetteVertCount + j] = new PolygonMesh.Polygon(
- new int[] { l + j, k + j, ((k + 1 + j) % silhouetteVertCount) + k, ((l + 1 + j) % silhouetteVertCount) + l });
- else
- faces[i * silhouetteVertCount + j] = new PolygonMesh.Polygon(
- new int[] { l + j, j, (j + 1) % silhouetteVertCount, ((l + 1 + j) % silhouetteVertCount) + l });
- }
- }
- for (int i = 0; i < vertices.Length; i++)
- {
- double dist = vertices[i].Length;
- vertices[i].z += (float)(aa * Math.Exp(-b * Math.Pow(dist, 1.5)) * Math.Pow(dist, a));
- }
- foreach (var face in faces)
- face.SmoothingGroup = 1;
- return new PolygonMesh(vertices, faces);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement