Homework 0: 玻璃上的水珠


静态图效果不是很好,最好是跑一下代码看看 :joy:

# shuizhu.py
import taichi as ti
ti.init(arch=ti.gpu)

pixels = ti.var(dt=ti.f32, shape=(800, 600))

@ti.func
def frac(uv):
  return ti.Vector([uv[0]%1,uv[1]%1])

@ti.func
def frac1(uv):
  return uv%1

@ti.func
def N21(p):
  tempVec=ti.Vector([123.34, 345.45])
  p = frac(p * tempVec);
  dotRe=p.dot(p + ti.Vector([34.345,34.345]))
  p += ti.Vector([dotRe,dotRe])
  return frac1(p[0]*p[1])

@ti.func
def smoothstep(a,b,x):
  result=0.0;
  y=(x-a)/(b-a)
  result = y*y*(3.0-2.0*y)
  if x < a:
    result = 0.0
  if x >= b:
    result = 1.0

  return result

@ti.func
def length(vec2):
  return ti.sqrt(vec2[0]*vec2[0]+vec2[1]*vec2[1])


@ti.kernel
def paint(t: ti.f32):
  for i, j in pixels: # 对于所有像素,并行执行

    UV=ti.Vector([i/800.0,j/600.0])
    time=t
    _Size = 5.0
    col=0.0
    aspect = ti.Vector([2.0,1.0])
    uv=UV*_Size*aspect
    uv[1]+=time*0.25
    gv=frac(uv)
    id=ti.floor(uv)
    n=N21(id)
    time+=n*6.28631
    w = UV[1] * 10.0
    x = (n - 0.5) * 0.8
    x += (0.4 - abs(x)) * ti.sin(3.0 * w) * pow(ti.sin(w), 6.0) * 0.45
    y = -ti.sin(time + ti.sin(time + ti.sin(time) * 0.5)) * 0.45
    y -= (gv[0] - x) * (gv[0] - x)
    dropPos = (gv - ti.Vector([x, y])) / aspect
    drop = 1.0-smoothstep(0.05, 0.03, length(dropPos))
    
    dropTrailPos = (gv - ti.Vector([x, time * 0.25])) / aspect
    dropTrailPos[1] = (frac1(dropTrailPos[1] * 8) / 8) - 0.03
    dropTrail = 1.0-smoothstep(0.03, 0.02, length(dropTrailPos));
    fogTrail = 1.0-smoothstep(-0.05, 0.05, dropPos[1])
    fogTrail *= 1.0-smoothstep(0.5, y, gv[1])
    dropTrail *= fogTrail
    fogTrail *= 1.0-smoothstep(0.05, 0.04, abs(dropPos[0]))
    col += fogTrail * 0.5
    col += dropTrail
    col += drop
    
    _Distortion = 0.7
    offs = drop * dropPos + dropTrail * dropTrailPos
    uuvv = UV + offs * _Distortion
    ufmod = uuvv[0]%0.1 - 0.05
    vfmod = uuvv[1]%0.1 - 0.05;
    back=0.0
    if ufmod * vfmod < 0.0:
        back=0.0
    else:
        back=0.8
    
    pixels[i, j] = back
    
    
    
    
gui = ti.GUI("Horrible Cvalry's Taichi program", (800, 600))

for i in range(1000000):
  paint(i * 0.03)
  gui.set_image(pixels)
  gui.show()

不怎么会贴代码,就先这样了(苦瓜脸)
扔上一个B站链接
效果

2 个赞

用三个反引号包起来

```
code
```

不是中文顿号,是1左侧的反引号。

你可以直接复制上面的代码块,然后在 code 处粘贴你的代码。

2 个赞

B站视频不见了,是否链接放错了哈