Created
July 18, 2015 10:21
-
-
Save valbeat/894264c436f62bd51bab to your computer and use it in GitHub Desktop.
Fractal.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class Fractal : MonoBehaviour { | |
public int maxDepth = 4; | |
private int depth; | |
Matrix4x4 mat; | |
Vector3[] vertex,afterVertex; | |
public Mesh mesh; | |
public Material material; | |
public float branchScale; | |
private void Start () { | |
gameObject.AddComponent<MeshFilter>().mesh = mesh; | |
gameObject.AddComponent<MeshRenderer>().material = material; | |
vertex = mesh.vertices; | |
afterVertex = new Vector3[vertex.Length]; | |
if (depth < maxDepth) { | |
StartCoroutine(CreateChildren()); | |
} | |
} | |
private IEnumerator CreateChildren() { | |
yield return StartCoroutine(Grow ()); | |
yield return new WaitForSeconds(Random.Range(0.1f, 0.5f)); | |
new GameObject("Fractal Child").AddComponent<Fractal>(). | |
Initialize(this, Vector3. up, Quaternion.Euler(0f, 0f, - 45f)); | |
yield return new WaitForSeconds(Random.Range(0.1f, 0.5f)); | |
new GameObject("Fractal Child").AddComponent<Fractal>(). | |
Initialize(this, Vector3.up, Quaternion.Euler(0f, 0f, 45f)); | |
} | |
private void Initialize (Fractal parent , Vector3 direction, Quaternion orientation) { | |
mesh = parent.mesh; | |
material = parent.material; | |
maxDepth = parent.maxDepth; | |
depth = parent.depth + 1; | |
branchScale = parent.branchScale; | |
transform.parent = parent.transform; | |
transform.localScale = Vector3.one * branchScale; | |
transform.localPosition = direction * (0.5f + 0.66f * branchScale); | |
transform.localRotation = orientation; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment