-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvv.py
85 lines (70 loc) · 2.4 KB
/
vv.py
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
import taichi as ti
ti.init(arch=ti.vulkan)
N = 2
NV = (N + 1)**2
NT = 2 * N**2
NE = 2 * N * (N + 1) + N**2
pos = ti.Vector.field(3, ti.f32, shape=NV)
tri = ti.field(ti.i32, shape=3 * NT)
edge = ti.Vector.field(2, ti.i32, shape=NE)
@ti.kernel
def init_pos():
for i, j in ti.ndrange(N + 1, N + 1):
idx = i * (N + 1) + j
pos[idx] = ti.Vector([i / N, 0.5, 1.0 - j / N])
@ti.kernel
def init_tri():
for i, j in ti.ndrange(N, N):
tri_idx = 6 * (i * N + j)
pos_idx = i * (N + 1) + j
if (i + j) % 2 == 0:
tri[tri_idx + 0] = pos_idx
tri[tri_idx + 1] = pos_idx + N + 2
tri[tri_idx + 2] = pos_idx + 1
tri[tri_idx + 3] = pos_idx
tri[tri_idx + 4] = pos_idx + N + 1
tri[tri_idx + 5] = pos_idx + N + 2
else:
tri[tri_idx + 0] = pos_idx
tri[tri_idx + 1] = pos_idx + N + 1
tri[tri_idx + 2] = pos_idx + 1
tri[tri_idx + 3] = pos_idx + 1
tri[tri_idx + 4] = pos_idx + N + 1
tri[tri_idx + 5] = pos_idx + N + 2
@ti.kernel
def init_edge():
for i, j in ti.ndrange(N + 1, N):
edge_idx = i * N + j
pos_idx = i * (N + 1) + j
edge[edge_idx] = ti.Vector([pos_idx, pos_idx + 1])
start = N * (N + 1)
for i, j in ti.ndrange(N, N + 1):
edge_idx = start + j * N + i
pos_idx = i * (N + 1) + j
edge[edge_idx] = ti.Vector([pos_idx, pos_idx + N + 1])
start = 2 * N * (N + 1)
for i, j in ti.ndrange(N, N):
edge_idx = start + i * N + j
pos_idx = i * (N + 1) + j
if (i + j) % 2 == 0:
edge[edge_idx] = ti.Vector([pos_idx, pos_idx + N + 2])
else:
edge[edge_idx] = ti.Vector([pos_idx + 1, pos_idx + N + 1])
init_pos()
init_tri()
init_edge()
window = ti.ui.Window("Display Mesh", (1024, 1024), vsync=True)
canvas = window.get_canvas()
scene = ti.ui.Scene()
camera = ti.ui.make_camera()
camera.position(-1.82731234, 2.26492691, 2.27800684)
camera.lookat(-1.14022911, 1.79402169, 1.72468514)
camera.fov(45)
while window.running:
camera.track_user_inputs(window, movement_speed=0.003, hold_key=ti.ui.RMB)
scene.set_camera(camera)
scene.point_light(pos=(0.5, 1, 2), color=(1, 1, 1))
scene.particles(pos, radius=0.01, color=(1.0, 1.0, 0.0))
scene.mesh(pos, tri, color=(50/255, 144/255, 168/255))
canvas.scene(scene)
window.show()