-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
91 lines (79 loc) · 2.44 KB
/
index.js
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
import * as ti from '../../dist/taichi.dev.js';
let main = async () => {
await ti.init();
let N = 128;
let liveness = ti.field(ti.i32, [N, N]);
let numNeighbors = ti.field(ti.i32, [N, N]);
ti.addToKernelScope({ N, liveness, numNeighbors });
let init = ti.kernel(() => {
for (let I of ti.ndrange(N, N)) {
liveness[I] = 0;
let f = ti.random();
if (f < 0.2) {
liveness[I] = 1;
}
}
});
await init();
let countNeighbors = ti.kernel(() => {
for (let I of ti.ndrange(N, N)) {
let neighbors = 0;
for (let delta of ti.ndrange(3, 3)) {
let J = (I + delta - 1) % N;
if ((J.x != I.x || J.y != I.y) && liveness[J] == 1) {
neighbors = neighbors + 1;
}
}
numNeighbors[I] = neighbors;
}
});
let updateLiveness = ti.kernel(() => {
for (let I of ti.ndrange(N, N)) {
let neighbors = numNeighbors[I];
if (liveness[I] == 1) {
if (neighbors < 2 || neighbors > 3) {
liveness[I] = 0;
}
} else {
if (neighbors == 3) {
liveness[I] = 1;
}
}
}
});
let htmlCanvas = document.getElementById('result_canvas');
htmlCanvas.width = 512;
htmlCanvas.height = 512;
let renderTarget = ti.canvasTexture(htmlCanvas);
let vertices = ti.field(ti.types.vector(ti.f32, 2), [6]);
await vertices.fromArray([
[-1, -1],
[1, -1],
[-1, 1],
[1, -1],
[1, 1],
[-1, 1],
]);
ti.addToKernelScope({ vertices, renderTarget });
let render = ti.kernel(() => {
ti.clearColor(renderTarget, [0.0, 0.0, 0.0, 1.0]);
for (let v of ti.inputVertices(vertices)) {
ti.outputPosition([v.x, v.y, 0.0, 1.0]);
ti.outputVertex(v);
}
for (let f of ti.inputFragments()) {
let coord = (f + 1) / 2.0;
let cellIndex = ti.i32(coord * (liveness.dimensions - 1));
let live = ti.f32(liveness[cellIndex]);
ti.outputColor(renderTarget, [live, live, live, 1.0]);
}
});
async function frame() {
countNeighbors();
updateLiveness();
await render();
requestAnimationFrame(frame);
}
await frame();
};
main();