-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutomata.cs
257 lines (232 loc) · 5.32 KB
/
Automata.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Tilemaps;
public struct Cell
{
public int pre;
public int val;
}
public class Automata : MonoBehaviour {
public ComputeShader compute;
public float camZoom = 1;
public Vector2 camPos;
// Default 2K
public int WIDTH = 2560;
public int HEIGHT = 1440;
Vector2 originPos;
Cell[] data;
int size;
ComputeBuffer buffer;
RenderTexture texture;
bool pause = true;
bool random = true;
// Default rule: B3/S23
int rule = Convert.ToInt32("000001000000001100", 2);
private void Awake() {
data = new Cell[WIDTH * HEIGHT];
size = 2 * sizeof(int);
}
private void Start() {
camPos = new Vector2(WIDTH / 2, HEIGHT / 2);
originPos = new Vector2(0, 0);
texture = new RenderTexture(Screen.width, Screen.height, 24);
texture.enableRandomWrite = true;
texture.useMipMap = false;
texture.Create();
buffer = new ComputeBuffer(data.Length, size);
Parse();
compute.SetBuffer(0, "cells", buffer);
compute.SetBuffer(1, "cells", buffer);
compute.SetBuffer(2, "cells", buffer);
compute.SetBuffer(3, "cells", buffer);
compute.SetInt("Width", WIDTH);
compute.SetInt("rule", rule);
compute.SetTexture(3, "tex", texture);
if(random) compute.Dispatch(0, data.Length/320, 1, 1);
}
private void Update() {
Viewport();
if(Input.GetKeyUp(KeyCode.Space)) pause = !pause;
if(!pause) Compute();
Process();
}
private void Viewport()
{
camZoom += Input.GetAxis("Mouse ScrollWheel");
camZoom = camZoom < 1 ? 1 : camZoom;
compute.SetFloat("zoom", camZoom);
camPos += new Vector2(Input.GetAxis("Horizontal") * 10 / camZoom, Input.GetAxis("Vertical") * 10 / camZoom);
float xoffset = Screen.width / (2 * camZoom);
float yoffset = Screen.height / (2 * camZoom);
camPos.x = Mathf.Max(Mathf.Min(camPos.x, WIDTH - xoffset), xoffset);
camPos.y = Mathf.Max(Mathf.Min(camPos.y, HEIGHT - yoffset), yoffset);
originPos.x = camPos.x - xoffset;
originPos.y = camPos.y - yoffset;
compute.SetInt("posx", (int)originPos.x);
compute.SetInt("posy", (int)originPos.y);
}
private void Process()
{
if(Input.GetMouseButtonUp(0))
{
buffer.GetData(data);
Vector2 pos = Input.mousePosition / camZoom;
Vector2Int posi = new Vector2Int((int)pos.x + (int)originPos.x, (int)pos.y + (int)originPos.y);
if(posi.x < 0 || posi.x > WIDTH || posi.y < 0 || posi.y > HEIGHT) return;
int idx = posi.y*WIDTH+posi.x;
data[idx].val = data[idx].val == 0 ? 1 : 0;
buffer.SetData(data);
compute.Dispatch(2, data.Length/320, 1, 1);
}
}
private void Compute()
{
compute.Dispatch(2, data.Length/320, 1, 1);
compute.Dispatch(1, data.Length/320, 1, 1);
}
private void OnRenderImage(RenderTexture src, RenderTexture dest) {
compute.Dispatch(3, texture.width/4, texture.height/4, 1);
Graphics.Blit(texture, dest);
}
private void OnDestroy() {
buffer.Dispose();
}
private void Parse()
{
int x = 0;
int y = 0;
try
{
random = false;
string[] rle = File.ReadAllLines(Environment.CurrentDirectory + "\\test.rle");
rle = rle.ToList().Where(s => s[0]!='#').ToArray();
string[] configs = rle[0].Replace(" ", "").Split(',');
StringBuilder sb = new StringBuilder();
for(int i = 1; i < rle.Length; i++)
{
sb.Append(rle[i]);
}
string[] contents = sb.ToString().Split('$');
foreach (string c in configs)
{
switch (c[0])
{
case 'x':
{
x = int.Parse(c.Remove(0, 2));
break;
}
case 'y':
{
y = int.Parse(c.Remove(0, 2));
break;
}
// Rule
default:
{
string[] types = c.Remove(0, 5).Split('/');
char[] b = "000000000".ToCharArray();
char[] s = "000000000".ToCharArray();
foreach (string t in types)
{
string tmp;
if(t[0] == 'B')
{
tmp = t.Remove(0, 1);
foreach(char r in tmp)
{
b[8-int.Parse(r.ToString())] = '1';
}
}
else if(t[0] == 'S')
{
tmp = t.Remove(0, 1);
foreach(char r in tmp)
{
s[8-int.Parse(r.ToString())] = '1';
}
}
}
rule = Convert.ToInt32(new string(b) + new string(s), 2);
break;
}
}
}
Vector2Int startPoint = new Vector2Int((WIDTH - x) / 2, (HEIGHT - y) / 2);
int row = startPoint.y + y;
foreach (string s in contents)
{
int ptr = 0;
int num = 0;
foreach (char c in s)
{
switch (c)
{
case 'b':
if(num != 0)
{
ptr += num;
num = 0;
}
else
{
ptr++;
}
break;
case 'o':
if(num != 0)
{
int max = ptr + num;
for(;ptr<max;ptr++)
{
data[row*WIDTH+ptr+startPoint.x].val = 1;
}
num = 0;
}
else
{
data[row*WIDTH+ptr+startPoint.x].val = 1;
ptr++;
}
break;
case '!':
{
break;
}
default:
{
int tmp = int.Parse(c.ToString());
if(num != 0)
{
num = 10 * num + tmp;
}
else
{
num = tmp;
}
break;
}
}
}
if(num != 0)
{
row -= num;
}
else
{
row--;
}
}
buffer.SetData(data);
}
catch (System.Exception)
{
random = true;
return;
}
}
}