def color(ray, world): hit_record = Hit_record(t=0, p=Vec3(0,0,0), normal=Vec3(0,0,0)) if world.hit(ray, 0.0, float("inf"), hit_record): return Vec3(hit_record.normal.x() + 1, hit_record.normal.y() + 1, hit_record.normal.z() + 1) * 0.5 else: unit_direction = ray.direction.unit_vector() t = 0.5 * (unit_direction.y() + 1.0) return Vec3(1.0, 1.0, 1.0) * (1.0 - t) + Vec3(0.5, 0.7, 1.0) * t
def color(ray, world): hit_record = Hit_record(t=0, p=Vec3(0, 0, 0), normal=Vec3(0, 0, 0)) if world.hit(ray, 0.0, float("inf"), hit_record): target = hit_record.p + hit_record.normal + random_in_unit_sphere() return color(Ray(hit_record.p, target - hit_record.p), world) * 0.5 else: unit_direction = ray.direction.unit_vector() t = 0.5 * (unit_direction.y() + 1.0) return Vec3(1.0, 1.0, 1.0) * (1.0 - t) + Vec3(0.5, 0.7, 1.0) * t
def hit(self, ray, tmin, tmax, hit_record): hit_anything = False closest_so_far = tmax temp_rec = Hit_record(t=0, p=Vec3(0, 0, 0), normal=Vec3(0, 0, 0)) for hitable in self.hitables: if hitable.hit(ray, tmin, closest_so_far, temp_rec): hit_anything = True closest_so_far = temp_rec.t hit_record.t = temp_rec.t hit_record.p = temp_rec.p hit_record.normal = temp_rec.normal return hit_anything
def color(ray, world, depth): rec = Hit_record(t=0, p=Vec3(0, 0, 0), normal=Vec3(0, 0, 0)) if world.hit(ray, 0.0, float("inf"), rec): scattered = Ray(origin=Vec3(0, 0, 0), direction=Vec3(0, 0, 0)) attenuation = Vec3(1.0, 1.0, 1.0) t = rec.material.scatter(ray, rec, attenuation, scattered) # print '----', t, t[1] if depth < 50 and t[0]: return color(scattered, world, depth + 1).mul(t[1]) else: return Vec3(0, 0, 0) else: unit_direction = ray.direction.unit_vector() t = 0.5 * (unit_direction.e1 + 1.0) return Vec3(1.0, 1.0, 1.0) * (1 - t) + Vec3(0.5, 0.7, 1.0) * t