import taichi as ti
import numpy as np
ti.init(arch=ti.gpu)
@ti.func
def hit_sphere(ray, sphere, tmin, tmax):
do_hit = 0
hit_time = -1.0
#################################################
# 如果这样定义: hit_pos = ti.Vector([0, 0, 0], dt=ti.f32)
# 那么hit_pos结果都是不对的,猜测是把hit_pos当整型计算了,但是我已经明确定义了是float32
hit_pos = ti.Vector([0, 0, 0], dt=ti.f32)
normal = ti.Vector([0.0, 0.0, 0.0], dt=ti.f32)
material = -1
ray_origin = ti.Vector([ray[0], ray[1], ray[2]], dt=ti.f32)
ray_dir = ti.Vector([ray[3], ray[4], ray[5]], dt=ti.f32)
sphere_center = ti.Vector([sphere[0], sphere[1], sphere[2]], dt=ti.f32)
oc = ray_origin - sphere_center
a = ray_dir.norm_sqr()
b = 2 * ray_dir.dot(oc)
c = oc.norm_sqr() - sphere[3] * sphere[3]
delta = b * b - 4 * a * c
if (delta >= 0):
sqrt_delta = ti.sqrt(delta)
t1 = (-b - sqrt_delta) * 0.5 / a
t2 = (-b + sqrt_delta) * 0.5 / a
if (t1>tmin) and (t1<=tmax):
do_hit = 1
hit_time = t1
hit_pos = ray_origin + t1 * ray_dir
print(ray_origin + t1 * ray_dir)
print(hit_pos)
normal = (hit_pos - sphere_center) / sphere[3]
material = sphere[4]
elif (t2>tmin) and (t2<=tmax):
do_hit = 1
hit_time = t2
hit_pos = ray_origin + t2 * ray_dir
normal = (hit_pos - sphere_center) / sphere[3]
material = sphere[4]
return do_hit, hit_time, hit_pos, normal, material
看代码注释部分: 如果这样定义: hit_pos = ti.Vector([0, 0, 0], dt=ti.f32)
那么hit_pos结果都是不对的,猜测是把hit_pos当整型计算了,但是我已经明确定义了是float32
如果这样定义: hit_pos = ti.Vector([0.0, 0.0, 0.0], dt=ti.f32)结果就对了
另外编译过程报了一堆看不懂的warning,其中有这句话: Local store may lose precision (target = i32, value = f32) at