-
Notifications
You must be signed in to change notification settings - Fork 3
/
cloth.py
304 lines (238 loc) · 8.3 KB
/
cloth.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import taichi as ti
ti.init(arch=ti.gpu)
#gui system using taichi-ggui:
#https://docs.taichi.graphics/zh-Hans/docs/lang/articles/misc/ggui
imgSize = 512
clothWid = 4.0
clothHgt = 4.0
clothResX = 31
num_triangles = clothResX * clothResX * 2
indices = ti.field(int, num_triangles * 3)
vertices = ti.Vector.field(3, float, (clothResX+1)*(clothResX+1))
pos_pre = ti.Vector.field(3, dtype=ti.f32, shape=(clothResX+1, clothResX+1))
pos = ti.Vector.field(3, dtype=ti.f32, shape=(clothResX+1, clothResX+1))
vel = ti.Vector.field(3, dtype=ti.f32, shape=(clothResX+1, clothResX+1))
F = ti.Vector.field(3, dtype=ti.f32, shape=(clothResX+1, clothResX+1))
J = ti.Matrix.field(3, 3, dtype=ti.f32, shape=(clothResX+1, clothResX+1))
KsStruct = ti.field(dtype=ti.f32, shape=())
KdStruct = ti.field(dtype=ti.f32, shape=())
KsShear = ti.field(dtype=ti.f32, shape=())
KdShear = ti.field(dtype=ti.f32, shape=())
KsBend = ti.field(dtype=ti.f32, shape=())
KdBend = ti.field(dtype=ti.f32, shape=())
@ti.func
def getNextNeighborKs(n):
ks = 0.0
if(n<4):
ks = KsStruct
else:
if(n<8):
ks = KsShear
if(n<12) :
ks = KsBend
return ks
@ti.func
def getNextNeighborKd(n):
kd = 0.0
if(n<4):
kd = KdStruct
else:
if(n<8):
kd = KdShear
else :
kd = KdBend
return kd
@ti.func
def getNextNeighborX(n):
cood = 0
if (n == 0) or (n == 4) or (n == 7):
cood = 1
if (n == 2) or (n == 5) or (n == 6):
cood = -1
if (n == 8):
cood = 2
if (n ==10):
cood = -2
return cood
@ti.func
def getNextNeighborY(n):
cood = 0
if (n == 1) or (n == 4) or (n == 5):
cood = -1
if (n == 3) or (n == 6) or (n == 7):
cood = 1
if (n == 9):
cood = -2
if (n ==11):
cood = 2
return cood
@ti.func
def get_length3(v):
return ti.sqrt(v.x*v.x+v.y*v.y+v.z*v.z)
@ti.func
def get_length2(v):
return ti.sqrt(v.x*v.x+ v.y*v.y)
@ti.func
def SolveConjugateGradient(A, x, b):
r = b-A@x
d = r
q = ti.Vector([0.0, 0.0, 0.0])
alpha_new = 0.0
alpha = 0.0
beta = 0.0
delta_old = 0.0
delta_new = r.dot(r)
delta0 = delta_new
for i in range(0,16):
q = A@d
alpha = delta_new/d.dot(q)
x = x + alpha*d
r = r - alpha*q
delta_old = delta_new
delta_new = r.dot(r)
beta = delta_new/delta_old
d = r + beta*d
i += 1
if delta_new< 0.0000001:
break
return x
@ti.kernel
def reset_cloth():
for i, j in pos:
pos[i, j] = ti.Vector([clothWid * (i / clothResX) - clothWid / 2.0, 5.0, clothHgt * (j / clothResX)- clothHgt/2.0])
pos_pre [i, j] = pos[i, j]
vel[i, j] = ti.Vector([0.0, 0.0, 0.0])
F[i, j] = ti.Vector([0.0, 0.0, 0.0])
if i < clothResX - 1 and j < clothResX - 1:
tri_id = ((i * (clothResX - 1)) + j) * 2
indices[tri_id * 3+2] = i * clothResX + j
indices[tri_id * 3+1] = (i + 1) * clothResX + j
indices[tri_id * 3+0] = i * clothResX + (j + 1)
tri_id += 1
indices[tri_id * 3+2] = (i + 1) * clothResX + j + 1
indices[tri_id * 3+1] = i * clothResX + (j + 1)
indices[tri_id * 3+0] = (i + 1) * clothResX + j
ball_centers[0] = ti.Vector([0.0, 3.0, 0.0])
ball_radius[0] = 1.0
deltaT[0] = 0.05
@ti.func
def compute_force(coord, jacobian):
p1 = pos[coord]
v1 = vel[coord]
F[coord] = gravity*mass + vel[coord]*damping
E = ti.Matrix.identity(ti.f32, 3)
J[coord] = ti.Matrix([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] )
for k in range(0,12):
ks = getNextNeighborKs(k)
kd = getNextNeighborKd(k)
coord_offcet = ti.Vector([getNextNeighborX(k), getNextNeighborY(k)])
coord_neigh = coord + coord_offcet
if (coord_neigh.x >= 0) and (coord_neigh.x <= clothResX) and (coord_neigh.y >= 0) and (coord_neigh.y <= clothResX):
rest_length = get_length2(coord_offcet * ti.Vector([clothWid / clothResX, clothHgt / clothResX]))
p2 = pos[coord_neigh]
v2 = (p2 - pos_pre[coord_neigh]) / deltaT[0]
deltaP = p1 - p2
deltaV = v1 - v2
dist = get_length3(deltaP)
if jacobian > 0:
dist2 = dist*dist
lo_l = rest_length/dist
J[coord] += ks*(lo_l * (E - deltaP.outer_product(deltaP) / dist2) - E)
leftTerm = -ks * (dist-rest_length)
rightTerm = kd * (deltaV.dot(deltaP)/dist)
F[coord] += deltaP.normalized() * (leftTerm + rightTerm)
@ti.func
def collision(coord):
#collosoin
if(pos[coord].y<0):
pos[coord].y=0
offcet = pos[coord] - ball_centers[0]
dist = get_length3(offcet)
if(dist < ball_radius[0]):
delta0 = (ball_radius[0] - dist)
pos[coord] += offcet.normalized() *delta0
pos_pre[coord] = pos[coord]
vel[coord] = ti.Vector([0.0, 0.0, 0.0])
@ti.kernel
def integrator_verlet():
for i, j in pos:
coord = ti.Vector([i, j])
compute_force(coord, 0)
index = j * (clothResX+1) + i
if (index != 0) and (index != clothResX):
collision(coord)
acc = F[coord] / mass
tmp = pos[coord]
pos[coord] = pos[coord] * 2.0 - pos_pre[coord] + acc * deltaT[0] * deltaT[0]
vel[coord] = (pos[coord] - pos_pre[coord]) /deltaT[0]
pos_pre[coord] = tmp
@ti.kernel
def integrator_explicit():
for i, j in pos:
coord = ti.Vector([i, j])
compute_force(coord, 0)
index = j * (clothResX+1) + i
if (index != 0) and (index != clothResX):
collision(coord)
tmp = pos[coord]
acc = F[coord] / mass
pos[coord] += vel[coord] * deltaT[0]
vel[coord] += acc * deltaT[0]
pos_pre[coord] = tmp
@ti.kernel
def integrator_implicit():
for i, j in pos:
coord = ti.Vector([i, j])
compute_force(coord, 1)
index = j * (clothResX+1) + i
if (index != 0) and (index != clothResX):
collision(coord)
tmp = pos[coord]
M = ti.Matrix.identity(ti.f32, 3)*mass
A = M - deltaT[0] * deltaT[0]* J[coord]
b = M@vel[coord] + deltaT[0]*F[coord]
vel[coord] = SolveConjugateGradient(A, ti.Vector([0.0, 0.0, 0.0]), b)
pos[coord] += vel[coord] * deltaT[0]
pos_pre[coord] = tmp
@ti.kernel
def update_verts():
for i, j in ti.ndrange(clothResX, clothResX):
vertices[i * clothResX + j] = pos[i, j]
gravity = ti.Vector([0.0, -0.05, 0.0])
ball_centers = ti.Vector.field(3, float, 1)
ball_radius = ti.field(float, shape=(1))
deltaT = ti.field(float, shape=(1))
mass = 1.0
damping = -0.0125
KsStruct = 50.0
KdStruct = -0.25
KsShear = 50.0
KdShear = -0.25
KsBend = 50.0
KdBend = -0.25
gui = ti.ui.Window('Cloth', (imgSize, imgSize), vsync=True)
canvas = gui.get_canvas()
scene = ti.ui.Scene()
camera = ti.ui.make_camera()
camera.position(5.0, 3.0, 5.0)
camera.lookat(0.0, 3.0, 0.0)
camera.up(0.0, 1.0, 0.0)
mode = 2
reset_cloth()
frame = 0
while gui.running:
for i in range(0, 3):
if mode == 0:
integrator_explicit()
if mode == 1:
integrator_implicit()
if mode == 2:
integrator_verlet()
update_verts()
scene.mesh(vertices, indices=indices, color=(0.5, 0.5, 0.5))
scene.particles(ball_centers, radius=0.95, color=(1.0, 0, 0))
scene.point_light(pos=(10.0, 10.0, 0.0), color=(1.0,1.0,1.0))
camera.track_user_inputs(gui, movement_speed=0.03, hold_key=ti.ui.LMB)
scene.set_camera(camera)
canvas.scene(scene)
gui.show()