//Cube Data // Makes a cube /// <summary> /// Cube /// Makes a cube object /// </summary> /// <param name="num"> Number of iterations </param> /// <returns> A Mesh / Cube object</returns> private Mesh MakeCube(int num) { List <Color> colors = new List <Color>(); Mesh mesh = MeshTools.MakeCube(); Vector3[] verts = mesh.vertices; //Set hue min and Max float hue = Mathf.Lerp(hueMin, hueMax, (num / (float)iterations)); /* * for each vertices located in the array color them in a hue */ for (int i = 0; i < mesh.vertexCount; i++) { float tempHue = hue + (1 / (float)iterations) * verts[i].y; Color color = Color.HSVToRGB(tempHue, 1, 1); colors.Add(color); } mesh.SetColors(colors); return(mesh); }
/// <summary> /// Pieces together a mesh based on a set position, rotation, and iterations. /// </summary> /// <param name="num">The number of iterations that have passed already.</param> /// <param name="meshes">Stores all the meshes this function constructs.</param> /// <param name="pos">The position of the endPoint of the previous stem segment/spawner location.</param> /// <param name="rot">The rotation of the previous stem segment/spawner location</param> private void Grow(int num, List <CombineInstance> meshes, Vector3 pos, Quaternion rot) { if (num <= 0) { return; //stop recursive function } //Adjust randomness for scale values: Vector3 tempStemScaling = (stemScaling * Random.Range(minRandomScaling, maxRandomScaling)) * objectScaling; Vector3 tempLeafScaling = (leafScaling * Random.Range(minRandomScaling, maxRandomScaling)) * objectScaling; //Adjust randomness for leaves: numberOfLeaves = Random.Range(minLeaves, maxLeaves); // Add Stem Mesh: CombineInstance stem = new CombineInstance(); stem.mesh = MeshTools.MakeCylinder(5); AddColorToVertices(stem.mesh, num); stem.transform = Matrix4x4.TRS(pos, rot, tempStemScaling); meshes.Add(stem); // Add Leaves Mesh: for (int i = 1; i <= numberOfLeaves; i++) { CombineInstance leaves = new CombineInstance(); leaves.mesh = MeshTools.MakeCube(); AddColorToVertices(leaves.mesh, num); float rotAmount = 360 / (numberOfLeaves * i); Quaternion leafRot = rot * Quaternion.Euler(leafOffsetAngleX, leafOffsetAngleY * rotAmount, leafOffsetAngleZ); leaves.transform = Matrix4x4.TRS(pos, leafRot, tempLeafScaling); meshes.Add(leaves); } // Count down number of passes: num--; // Get the position of the end of this section: pos = stem.transform.MultiplyPoint(new Vector3(0, 1, 0)); //Generate random angles based on set angle values: float randX = stemAngleX + Random.Range(minRandomAngle, maxRandomAngle); float randY = stemAngleY + Random.Range(minRandomAngle, maxRandomAngle); float randZ = stemAngleZ + Random.Range(minRandomAngle, maxRandomAngle); //Create a Quaternion to change the local transform to the world Vector3.up: Quaternion rot1 = Quaternion.FromToRotation(transform.up, Vector3.up); //Create a Quaternion using the random angles from earlier: Quaternion rot2 = Quaternion.Euler(randX, randY, randZ); //Use a slerp to create a Quaternion between rot1 and rot2, leaning more towards randomness in later segments: Quaternion finalRot = Quaternion.Slerp(rot1, rot2, (num / (float)iterations)); //Grow another segment: Grow(num, meshes, pos, finalRot); }
/// <summary> /// creates the mesh of the cube that is generated /// </summary> private Mesh MakeCube() { List <Color> colors = new List <Color>(); Mesh mesh = MeshTools.MakeCube(); for (int i = 0; i < mesh.vertexCount; i++) { Color color = Color.HSVToRGB(1, 1, 1); colors.Add(color); } mesh.SetColors(colors); return(mesh); }
/// <summary> /// creates the mesh and colors it by vertex /// </summary> /// <param name="num">the amount of iterations</param> /// <returns></returns> private Mesh MakeCube(int num) { List <Color> colors = new List <Color>(); float hueMin = .1f; float hueMax = .001f; float hue = Mathf.Lerp(hueMin, hueMax, (num / (float)iterations)); Mesh mesh = MeshTools.MakeCube(); for (int i = 0; i < mesh.vertexCount; i++) { float tempHue = hue;// + (1/(float)iterations) * pos.y; Color color = Color.HSVToRGB(tempHue, 1, 1); colors.Add(color); } mesh.SetColors(colors); return(mesh); }
/// <summary> /// This creates the actual mesh /// </summary> /// <param name="num">the number of iterations</param> /// <returns></returns> private Mesh MakeCube(int num) { List <Color> colors = new List <Color> (); float hue = 1; Mesh mesh = MeshTools.MakeCube(); Vector3[] verts = mesh.vertices; for (int i = 0; i < mesh.vertexCount; i++) { float tempHue = hue + (1 * verts[i].y); Color color = Color.HSVToRGB(tempHue, .5f, 1); colors.Add(color); } mesh.SetColors(colors); return(mesh); }
}//end Grow /// <summary> /// Creates the mesh /// </summary> /// <returns></returns> private Mesh MakeCube() { List <Color> colors = new List <Color>(); //random vertex colors float hueMin = Random.Range(.4f, .7f); float hueMax = Random.Range(.7f, 1f); //blends between the min and max by way of the number of iterations float hue = Mathf.Lerp(hueMin, hueMax, (float)iterations); Mesh mesh = MeshTools.MakeCube(); //applies the colors to each vertex for (int i = 0; i < mesh.vertexCount; i++) { Color color = Color.HSVToRGB(hue, 1, 1); colors.Add(color); } mesh.SetColors(colors); return(mesh); } //end MakeCube