# Homework 0: Yet another Shader toy reproduce

line by line port of:

``````import taichi as ti
import os
import time
import numpy as np

ti.init(arch=ti.gpu)
res=(512, 512)
color_buffer = ti.Vector(3, dt=ti.f32, shape=res)
res = ti.Vector(res)

@ti.func
def mix(a, b, t):
return a + (b - a) * t

@ti.func
def snoise(uv, res):
s = ti.Vector([1e0, 1e2, 1e3])
uv *= res

uv0 = ti.floor(uv % res) * s
uv1 = ti.floor((uv + ti.Vector([1, 1, 1])) % res) * s
f = uv % 1
f = f * f * (3 - 2 * f)
v = ti.Vector([uv0[0] + uv0[1] + uv0[2], uv1[0] + uv0[1] + uv0[2], uv0[0] + uv1[1] + uv0[2], uv1[0] + uv1[1] + uv0[2]])
r = ti.sin(v * 1e-1) * 1e3 % 1
r0 = mix(mix(r[0], r[1], f[0]), mix(r[2], r[3], f[0]), f[1])
r = (ti.sin((v + uv1[2] - uv0[2])*1e-1)*1e3) % 1
r1 = mix(mix(r[0], r[1], f[0]), mix(r[2], r[3], f[0]), f[1])
return mix(r0, r1, f[2])*2.-1.

@ti.kernel
def render(t: ti.f32):
power = 4
for u, v in color_buffer:
scaleduv = ti.Vector([u, v], dt=float) / res
p = scaleduv - 0.5
color = 3.0 - (3.0 * 2 * p.norm())
coord = ti.Vector([ti.atan2(p[0], p[1]) / 6.2832 + .5, p.norm() * 0.4, 0.5])
for i in range(8):
power = 2.0 ** i
color += 1.5 / power * snoise(coord + ti.Vector([0.0, -t * 0.05, t * 0.01]), power * 16)
color = ti.max(color, 0)
color_buffer[u, v] = ti.Vector([color, color ** 2 * 0.4, color ** 3 * 0.15])

gui = ti.GUI('Fire', (512, 512))

start_t = time.time()
while True:
t = time.time() - start_t
render(t)
img = color_buffer.to_numpy()
gui.set_image(img)
gui.show()
``````

2 Likes