Created
February 19, 2021 12:18
-
-
Save TheCuttlefish/13809367db5f55240a6e78a66b7b7025 to your computer and use it in GitHub Desktop.
L-system with colour /size/ rotation variation
This file contains hidden or 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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.SceneManagement; | |
public class LSystem : MonoBehaviour | |
{ | |
[System.Serializable] | |
public class Customisation | |
{ | |
public Gradient colours; | |
public string axiom = "A"; | |
public string rule = "A[AA]"; | |
public int iterations = 3; | |
[TextArea] | |
public string output; | |
} | |
int index = 0; | |
string currentLetter; | |
public GameObject branch; | |
public GameObject berry; | |
GameObject currentBranch; | |
string instructions = "A"; | |
List<Vector3> positions = new List<Vector3>(); | |
public Customisation custumisation = new Customisation(); | |
private void Start() | |
{ | |
instructions = custumisation.axiom; | |
currentBranch = Instantiate(branch, transform.position, Quaternion.identity); | |
positions.Add(currentBranch.transform.Find("top").transform.position); | |
currentBranch.GetComponent<SpriteRenderer>().color = custumisation.colours.Evaluate(Random.Range(0f, 1f)); | |
currentBranch.transform.parent = transform; | |
while (custumisation.iterations > 0) | |
{ | |
Iterate(); | |
custumisation.iterations--; | |
} | |
} | |
void Iterate() | |
{ | |
int i = 0; | |
string newInstructions = ""; | |
while (i < instructions.Length) | |
{ | |
if (instructions[i].ToString() == "A") | |
{ | |
newInstructions += custumisation.rule;//A --> A[AA] | |
} | |
else if (instructions[i].ToString() == "B") | |
{ | |
newInstructions += "B[A]";//add berry and a branch | |
} | |
else // pass what is in there already | |
{ | |
newInstructions += instructions[i].ToString(); | |
} | |
i++; | |
} | |
instructions = newInstructions; | |
custumisation.output = instructions; | |
} | |
void Update() | |
{ | |
if (Input.GetKeyDown(KeyCode.Space)) { | |
SceneManager.LoadScene(0); | |
} | |
if (index < instructions.Length) | |
{ | |
currentLetter = instructions[index].ToString(); | |
index++; | |
//rules | |
if (currentLetter == "A") { | |
currentBranch =Instantiate(branch, positions[positions.Count-1], Quaternion.identity); | |
currentBranch.transform.eulerAngles = new Vector3(Random.Range(-60, 60), Random.Range(-60, 60), Random.Range(-60, 60)); | |
currentBranch.transform.localScale = new Vector3(3, Random.Range(3f,10f), 3); | |
currentBranch.GetComponent<SpriteRenderer>().color = custumisation.colours.Evaluate(Random.Range(0f, 1f)); | |
currentBranch.transform.parent = transform; | |
} | |
else if (currentLetter == "B") | |
{ | |
GameObject berryRef; | |
berryRef = Instantiate(berry, positions[positions.Count - 1], Quaternion.identity); | |
berryRef.transform.parent = transform; | |
//currentBranch.transform.eulerAngles = new Vector3(Random.Range(-60, 60), Random.Range(-60, 60), Random.Range(-60, 60)); | |
//currentBranch.transform.localScale = new Vector3(1, Random.Range(1f, 3f), 1); | |
//currentBranch.GetComponent<SpriteRenderer>().color = custumisation.colours.Evaluate(Random.Range(0f, 1f)); | |
} | |
else if (currentLetter == "[") { // go up a branch (push) | |
positions.Add(currentBranch.transform.Find("top").transform.position);// add to | |
} | |
else if (currentLetter == "]") { // go down a breanch (pop) | |
positions.RemoveAt(positions.Count-1); | |
} | |
} | |
//else index = 0; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment