def scatter(self, r_in, rec): outward_normal = Vec3() reflected = Vec3.reflect(r_in.direction, rec["normal"]) ni_over_nt = 0.0 attenuation = Vec3(1.0, 1.0, 1.0) cosine = 0.0 if Vec3.dot(r_in.direction, rec["normal"]) > 0: outward_normal = -rec["normal"] ni_over_nt = self.ref_idx cosine = self.ref_idx * Vec3.dot( r_in.direction, rec["normal"]) / r_in.direction.length() else: outward_normal = rec["normal"] ni_over_nt = 1.0 / self.ref_idx cosine = -Vec3.dot(r_in.direction, rec["normal"]) / r_in.direction.length() scattered = Ray() reflect_prob = 0.0 refracted = Vec3.refract(r_in.direction, outward_normal, ni_over_nt) if refracted is not None: reflect_prob = schlick(cosine, self.ref_idx) else: reflect_prob = 1.0 if random() < reflect_prob: scattered = Ray(rec["p"], reflected) else: scattered = Ray(rec["p"], refracted) return attenuation, scattered
def scatter(self, r_in, rec): outward_normal = Vec3() reflected = Vec3.reflect(r_in.direction,rec["normal"]) ni_over_nt = 0.0 attenuation = Vec3(1.0,1.0,1.0) cosine = 0.0 if Vec3.dot(r_in.direction,rec["normal"]) > 0: outward_normal = -rec["normal"] ni_over_nt = self.ref_idx cosine = self.ref_idx * Vec3.dot(r_in.direction,rec["normal"])/ r_in.direction.length() else: outward_normal = rec["normal"] ni_over_nt = 1.0 / self.ref_idx cosine = -Vec3.dot(r_in.direction,rec["normal"])/ r_in.direction.length() scattered = Ray() reflect_prob = 0.0 refracted = Vec3.refract(r_in.direction,outward_normal,ni_over_nt) if refracted is not None: reflect_prob = schlick(cosine,self.ref_idx) else: reflect_prob = 1.0 if random() < reflect_prob: scattered = Ray(rec["p"],reflected) else: scattered = Ray(rec["p"],refracted) return attenuation, scattered
def scatter(self, r_in,rec): reflected = Vec3.reflect(Vec3.unit_vector(r_in.direction),rec["normal"]) scattered = Ray(rec["p"],reflected + self.fuzz * Material.random_in_unit_sphere()) attenuation = self.albedo if Vec3.dot(scattered.direction,rec["normal"]) > 0: return attenuation,scattered else: return None,None
def scatter(self, r_in, rec): reflected = Vec3.reflect(Vec3.unit_vector(r_in.direction), rec["normal"]) scattered = Ray( rec["p"], reflected + self.fuzz * Material.random_in_unit_sphere()) attenuation = self.albedo if Vec3.dot(scattered.direction, rec["normal"]) > 0: return attenuation, scattered else: return None, None