-
Notifications
You must be signed in to change notification settings - Fork 2
/
Lsystem.cs
191 lines (184 loc) · 4.82 KB
/
Lsystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using System;
using System.Linq;
using static Global;
using static Extension;
namespace Tree
{
using Rules = Dictionary<char, string>;
using States = Stack<State>;
public class State
{
public Vector3 position;
public Quaternion rotation;
public GameObject instance;
public State(Transform transform, GameObject instance_)
{
position = transform.position;
rotation = transform.rotation;
instance = instance_;
}
}
[Serializable] public struct Rule
{
public char target;
public string replacement;
public Rule(char target, string replacement)
{
this.target = target;
this.replacement = replacement;
}
}
public class Lsystem : MonoBehaviour
{
[SerializeField] private int iteration = 4;
[SerializeField] private float length = .05f;
[SerializeField] private float width = .2f;
[SerializeField] private float angle = 30;
[SerializeField] private GameObject branch;
[Range(0, 100)] public int speed = 100;
[Range(0f, 10f)] public float thicknessVariety = 3;
[Range(0f, 10f)] public int angleVariety = 10;
[Range(0f, 10f)] public float delay = 0;
[Range(0f, 1f)] public float age = 0;
private States states;
public Rule[] rawRules = new Rule[] {
new Rule('x',"[f-[[x]+x]+f[+fx]-x]"),
new Rule('f',"ff")
};
public string axiom = "x";
private Rules rules;
private string currentString = "";
private Vector3 root;
private int stringLength;
private float interval;
private bool isBranching = true;
private int i;
void Start()
{
i = time.Length;
time = time.Append(0).ToArray();
timeLimit = timeLimit.Append(0).ToArray();
root = transform.position;
states = new States();
rules = makeRules();
parent = gameObject;
interval = 25f / speed;
cursor = new GameObject().transform;
generate();
grow();
}
private Rules makeRules()
{
Rules rules = new Rules();
foreach (var rawRule in rawRules)
rules.Add(rawRule.target, rawRule.replacement);
return (rules);
}
private bool stop = false;
void Update()
{
if (delay > 0)
delay -= Time.deltaTime;
else if (!stop && time[i] < timeLimit[i])
{
time[i] += Time.deltaTime;
age = Mathf.Min(1, time[i]/timeLimit[i]);
}
else
{
time[i] = timeLimit[i] * age;
stop = true;
}
}
private GameObject parent;
private GameObject instance;
private LineRenderer line;
private Transform cursor;
private float birth = 0;
private Twig twig;
private void generate()
{
currentString = axiom;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < iteration; i++)
{
foreach (char c in currentString)
builder.Append(rules.ContainsKey(c) ? rules[c] : c.ToString());
currentString = builder.ToString();
builder = new StringBuilder();
}
stringLength = currentString.Length;
}
private void grow()
{
float resistance = angleVariety/10;
float minWidth = width / (thicknessVariety+1);
foreach (char c in currentString)
switch (c)
{
case '+':
cursor.Rotate(randomize(angle, resistance) * (randomBool() ? Vector3.forward : Vector3.back));
prepareBranch();
break;
case '-':
cursor.Rotate(randomize(angle, resistance) * (randomBool() ? Vector3.right : Vector3.left));
prepareBranch();
break;
case '[':
states.Push(new State(cursor, instance));
prepareBranch();
break;
case ']':
State state = states.Pop();
cursor.position = state.position;
cursor.rotation = state.rotation;
parent = state.instance;
birth = parent?.GetComponent<Twig>()?.endTime ?? birth;
prepareBranch();
break;
case 'f':
if (isBranching)
{
instance = Instantiate(branch);
twig = instance.GetComponent<Twig>();
line = instance.GetComponent<LineRenderer>();
line.SetPosition(0, cursor.position);
line.SetPosition(1, cursor.position);
line.SetWidth(minWidth, minWidth);
birth += interval;
}
else
{
twig = instance.GetComponent<Twig>();
line = instance.GetComponent<LineRenderer>();
}
instance.transform.SetParent(parent.transform, false);
cursor.Translate(Vector3.up * length);
twig.startTime = birth;
twig.endTime = birth + interval;
twig.minWidth = minWidth;
twig.maxWidth = width;
twig.destination = cursor.position;
twig.i = i;
isBranching = false;
timeLimit[i] = Mathf.Max(twig.endTime, timeLimit[i]);
parent = instance;
break;
default:
break;
};
}
private void prepareBranch()
{
isBranching = true;
}
public static float randomize(float value, float resistance)
{
return ((value * UnityEngine.Random.value + value * resistance) / (resistance + 1));
}
}
}