Here is my current result.
I took and modified the code I was using to complete this tutorial.
I'm pleased with the result, though I'm surprised by one thing: why is it that my initially cubic shape turns into a trapezoid shape as it is being turned around? Is it because of the 3D nature of the rotation (Euler transformations?)?
I wanted rectangular lines the whole way.
Code
using UnityEngine;
using System.Collections;
public class BranchGen : MonoBehaviour {
public Mesh mesh;
public Material material;
public int maxDepth;
public float childScale;
private int depth;
private Material[,] materials;
private static Vector3[] childDirections = {Vector3.up, Vector3.up};
private static Quaternion[] childOrientations = {Quaternion.Euler(0f, 0f, 35f), Quaternion.Euler(0f, 0f, -35f)};
private void Start () {
if (materials == null){
InitializeMaterials();
}
gameObject.AddComponent<MeshFilter>().mesh = mesh;
gameObject.AddComponent<MeshRenderer>().material = materials[depth, Random.Range(0, 2)];
if (depth < maxDepth) {
StartCoroutine(CreateChildren());
}
}
private void InitializeMaterials (){
materials = new Material[maxDepth +1, 2];
for (int i = 0; i <= maxDepth; i++){
float t = i / (maxDepth - 1f);
t *= t;
materials[i, 0] = new Material(material);
materials[i, 0].color = Color.Lerp(Color.white, Color.yellow, t);
materials[i, 1] = new Material(material);
materials[i, 1].color = Color.Lerp(Color.white, Color.cyan, t);
}
materials[maxDepth,0].color = Color.magenta;
materials[maxDepth,1].color = Color.red;
}
private void Initialize (BranchGen parent, int childIndex) {
mesh = parent.mesh;
materials = parent.materials;
maxDepth = parent.maxDepth;
depth = parent.depth + 1;
childScale = parent.childScale;
transform.parent = parent.transform;
transform.localScale = new Vector3(1, 1.5f, 1) * childScale;
transform.localPosition = childDirections[childIndex] * (0.5f + 0.5f * childScale);
transform.localRotation = childOrientations[childIndex];
}
private IEnumerator CreateChildren () {
for (int i = 0; i < childDirections.Length; i++){
yield return new WaitForSeconds(.2f);
new GameObject("BranchGen Child").AddComponent<BranchGen>().Initialize(this, i);
}
}
}
using System.Collections;
public class BranchGen : MonoBehaviour {
public Mesh mesh;
public Material material;
public int maxDepth;
public float childScale;
private int depth;
private Material[,] materials;
private static Vector3[] childDirections = {Vector3.up, Vector3.up};
private static Quaternion[] childOrientations = {Quaternion.Euler(0f, 0f, 35f), Quaternion.Euler(0f, 0f, -35f)};
private void Start () {
if (materials == null){
InitializeMaterials();
}
gameObject.AddComponent<MeshFilter>().mesh = mesh;
gameObject.AddComponent<MeshRenderer>().material = materials[depth, Random.Range(0, 2)];
if (depth < maxDepth) {
StartCoroutine(CreateChildren());
}
}
private void InitializeMaterials (){
materials = new Material[maxDepth +1, 2];
for (int i = 0; i <= maxDepth; i++){
float t = i / (maxDepth - 1f);
t *= t;
materials[i, 0] = new Material(material);
materials[i, 0].color = Color.Lerp(Color.white, Color.yellow, t);
materials[i, 1] = new Material(material);
materials[i, 1].color = Color.Lerp(Color.white, Color.cyan, t);
}
materials[maxDepth,0].color = Color.magenta;
materials[maxDepth,1].color = Color.red;
}
private void Initialize (BranchGen parent, int childIndex) {
mesh = parent.mesh;
materials = parent.materials;
maxDepth = parent.maxDepth;
depth = parent.depth + 1;
childScale = parent.childScale;
transform.parent = parent.transform;
transform.localScale = new Vector3(1, 1.5f, 1) * childScale;
transform.localPosition = childDirections[childIndex] * (0.5f + 0.5f * childScale);
transform.localRotation = childOrientations[childIndex];
}
private IEnumerator CreateChildren () {
for (int i = 0; i < childDirections.Length; i++){
yield return new WaitForSeconds(.2f);
new GameObject("BranchGen Child").AddComponent<BranchGen>().Initialize(this, i);
}
}
}
I also wanted to make a (1,VARIABLE FLOAT VALUE,1) Mesh to use so that I wouldn't have to change the code itself to quickly look at different results, but couldn't figure out how to do it. I tried using an IF-THEN statement to set the scales (I wanted the first-created Cube Mesh to see its scale modified to that VARIABLE FLOAT VALUE, and then every subsequent one to have yet another VARIABLE (already in the code) modify it as it goes through the iterations).
I couldn't figure out how to get the IF statement to work properly, nor how to plug a FLOAT VAR into this line:
Code
transform.localScale = new Vector3(1, 1.5f, 1) * childScale;
So that it would look like:
Code
transform.localScale = new Vector3(1, VARIABLE, 1) * childScale;
I was basically trying to achieve this result:
My next goal after solving this is to create a Snowflake fractal.
I've no idea how to make a starting Mesh that would be a Triangle, though.