forked from ltt1598/--Shadertoys
-
Notifications
You must be signed in to change notification settings - Fork 3
/
02_circle.py
71 lines (56 loc) · 1.66 KB
/
02_circle.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
# reference ==>
import taichi as ti
import handy_shader_functions as hsf
ti.init(arch = ti.cpu)
res_x = 512
res_y = 512
scatter = 1
pixels = ti.Vector.field(3, ti.f32, shape=(res_x, res_y))
@ ti.func
def circle(pos, center, radius, blur):
r = (pos - center).norm()
t = 0.0
if blur > 1.0: blur = 1.0
if blur <= 0.0:
t = 1.0-hsf.step(1.0, r/radius)
else:
t = hsf.smoothstep(1.0, 1.0-blur, r/radius)
return t
@ti.func
def square(pos, center, radius, blur):
diff = ti.abs(pos-center)
r = ti.max(diff[0], diff[1])
t = 0.0
if blur > 1.0: blur = 1.0
if blur <= 0.0:
t = 1.0-hsf.step(1.0, r/radius)
else:
t = hsf.smoothstep(1.0, 1.0-blur, r/radius)
return t
@ti.kernel
def render(t:ti.f32):
center = ti.Vector([res_x//scatter//2, res_y//scatter//2])
r1 = 100.0 / scatter
for i,j in pixels:
color = ti.Vector([0.0, 0.0, 0.0]) # init your canvas to white
pos = ti.Vector([i//scatter, j//scatter])
r = (pos - center).norm()
# # discrete circle
# if r <= r1:
# color = ti.Vector([1.0, 1.0, 1.0])
# # smooth circle
# color = ti.Vector([1.0, 1.0, 1.0]) * (1.0-r/r1)
# # smooth circle 2
# color = ti.Vector([1.0, 1.0, 1.0]) * hsf.smoothstep(1.0, 0.8, r/r1)
c = circle(pos, center, r1, 0.1)
mask = c
c2 = square(pos, center, 30.0/scatter, 0.1)
mask -= c2
color = ti.Vector([1.0, 1.0, 0.0]) * mask
pixels[i, j] = color
gui = ti.GUI("Solid Circle", res=(res_x, res_y))
for i in range(100000):
t = i * 0.03
render(t)
gui.set_image(pixels)
gui.show()