def on_render(self): for I in ti.grouped(self.img): uv = I / self.iResolution self.img[I] = ti.cos(uv.xyx + self.iTime + ts.vec(0, 2, 4)) * 0.5 + 0.5
import taichi as ti import taichi_glsl as tl import numpy as np import tina ti.init(arch=ti.gpu) ### Parameters N = 128 NN = N, N W = 1 L = W / N gravity = 0.5 stiffness = 1600 ball_pos = tl.vec(+0.0, +0.2, -0.0) ball_radius = 0.4 damping = 2 steps = 30 dt = 5e-4 ### Physics x = ti.Vector.field(3, float, NN) v = ti.Vector.field(3, float, NN) @ti.kernel def init(): for i in ti.grouped(x): x[i] = tl.vec((i + 0.5) * L - 0.5, 0.8).xzy
def init(): for i in ti.grouped(x): x[i] = tl.vec((i + 0.5) * L - 0.5, 0.8).xzy
def update_display(): for i in ti.grouped(x): j = i.dot(tl.vec(N, 1)) model.vi[j] = x[i]
__import__('sys').path.insert(0, '/home/bate/Develop/glsl_taichi') __import__('sys').path.insert(0, 'external') import numpy as np import taichi as ti import taichi_glsl as tl ts = tl V = lambda *_: tl.vec(*_).cast(float) if ti.impl.inside_kernel() else tl.vec(*_ ) V3 = lambda *_: tl.vec3(*_).cast(float) if ti.impl.inside_kernel( ) else tl.vec3(*_) V2 = lambda *_: tl.vec2(*_).cast(float) if ti.impl.inside_kernel( ) else tl.vec2(*_) EPS = 1e-6 INF = 1e6 @ti.pyfunc def tangent(Z): Y = V(0, 1, 0) if any(abs(Z.xz) >= 1e-2) else V(1, 0, 0) if any( abs(Z.yz) >= 1e-2) else V(0, 0, 1) X = Y.cross(Z).normalized() Y = Z.cross(X) return ti.Matrix([X.entries, Y.entries, Z.entries]).transpose() @ti.pyfunc def spherical(h, phi):
dt = 5e-4 ### Physics x = ti.Vector.field(3, float, NN) v = ti.Vector.field(3, float, NN) @ti.kernel def init(): for i in ti.grouped(x): x[i] = tl.vec((i + 0.5) * L - 0.5, 0.8).xzy links = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (1, -1), (-1, 1), (1, 1)] links = [tl.vec(*_) for _ in links] @ti.kernel def substep(): for i in ti.grouped(x): acc = x[i] * 0 for d in ti.static(links): disp = x[tl.clamp(i + d, 0, tl.vec(*NN) - 1)] - x[i] length = L * float(d).norm() acc += disp * (disp.norm() - length) / length**2 v[i] += stiffness * acc * dt for i in ti.grouped(x): v[i].y -= gravity * dt v[i] = tl.ballBoundReflect(x[i], v[i], tl.vec(+0.0, +0.2, -0.0), 0.4, 6) for i in ti.grouped(x):
def collide(x: ti.template(), v: ti.template()): for i in ti.grouped(x): v[i].y -= gravity * dt for i in ti.grouped(x): v[i] = ballBoundReflect(x[i], v[i], tl.vec(+0.0, +0.2, -0.0), 0.4)
def render_arrows(mx: ti.f32, my: ti.f32): pos = tl.vec(mx, my) acc = get_raw_gravity_at(pos) * 0.001 tl.paintArrow(display_image, pos, acc, tl.D.yyx) acc_tree = get_tree_gravity_at(pos) * 0.001 tl.paintArrow(display_image, pos, acc_tree, tl.D.yxy)
def update_display(): for i in ti.grouped(x): vertices[i.dot(tl.vec(N, 1))].pos = x[i]
### Generic helpers x = ti.Vector(3, ti.f32, NN) v = ti.Vector(3, ti.f32, NN) b = ti.Vector(3, ti.f32, NN) F = ti.Vector(3, ti.f32, NN) @ti.kernel def init(): for i in ti.grouped(x): x[i] = tl.vec((i + 0.5) * L - 0.5, 0.8).xzy links = [tl.vec(*_) for _ in [(-1, 0), (1, 0), (0, -1), (0, 1)]] @ti.func def accel(v: ti.template(), x: ti.template(), dt): for i in ti.grouped(x): acc = x[i] * 0 for d in ti.static(links): disp = x[tl.clamp(i + d, 0, tl.vec(*NN) - 1)] - x[i] dis = disp.norm() acc += disp * (dis - L) / L**2 v[i] += stiff * acc * dt v[i] *= ti.exp(-damp * dt) @ti.kernel
def on_start(self): for I in ti.grouped(self.color): self.color.old[I] = ts.imageChess(I / self.iResolution) for P in ti.grouped(self.color): p = P * self.dx self.vel.old[P] = (2 * p - 1).yx * ts.vec(-1, 1)
def calc_grad(self, p): return ts.vec( self.calc_sdf(p + ts.vec(EPS, 0, 0)), self.calc_sdf(p + ts.vec(0, EPS, 0)), self.calc_sdf(p + ts.vec(0, 0, EPS)))
import taichi_glsl as tl u = tl.vec(2, 3, 4, 5) print(u.y_xW)