-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.compute
71 lines (61 loc) · 1.57 KB
/
Cell.compute
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
// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel Init
#pragma kernel Process
#pragma kernel Update
struct Cell
{
int posx;
int posy;
int pre;
int val;
};
RWStructuredBuffer<Cell> cells;
int width;
int height;
float rand(float2 st, float n) {
st = floor(st * n);
return frac(sin(dot(st.xy, float2(12.9898,78.233)))*43758.5453123);
}
[numthreads(20,1,1)]
void Init (uint3 id : SV_DispatchThreadID)
{
cells[id.x].posx = id.x%width;
cells[id.x].posy = id.x/width;
float r = rand(float2(id.x, 11), 10);
cells[id.x].pre = r < 0.2 ? 1 : 0;
cells[id.x].val = cells[id.x].pre;
};
[numthreads(20,1,1)]
void Process (uint3 id : SV_DispatchThreadID)
{
int val = 0;
int x = id.x % width;
int y = id.y / width + 1;
val += x - 1 >= 0 ? cells[id.x - 1].pre : 0;
val += x + 1 < width ? cells[id.x + 1].pre : 0;
if(y - 1 >= 0)
{
val += cells[id.x - width].pre;
val += x - 1 >= 0 ? cells[id.x - width - 1].pre : 0;
val += x + 1 < width ? cells[id.x - width + 1].pre : 0;
}
if(y + 1 < width)
{
val += cells[id.x + width].pre;
val += x - 1 >= 0 ? cells[id.x + width - 1].pre : 0;
val += x + 1 < width ? cells[id.x + width + 1].pre : 0;
}
if(val >= 3 && val <= 5)
{
cells[id.x].val = 1;
}
else
{
cells[id.x].val = 0;
}
};
[numthreads(20,1,1)]
void Update (uint3 id : SV_DispatchThreadID)
{
cells[id.x].pre = cells[id.x].val;
}