public bool Scatter(ref Ray r, ref Hit_record rec, ref Vector3 attenuation, ref Ray scattered) { Vector3 target = rec.normal.normalized * 0.5f + new Vector3(Chapter11.RandomFloat11(), Chapter11.RandomFloat11(), Chapter11.RandomFloat11()).normalized; scattered.origin = rec.hitpoint; scattered.direction = target; attenuation = albedo; return(true); }
public bool Scatter(ref Ray r, ref Hit_record rec, ref Vector3 attenuation, ref Ray scattered) { Vector3 outward_normal = Vector3.zero; Vector3 reflected = Vector3.Reflect(r.direction.normalized, rec.normal.normalized); float ni_over_nt = 0f; attenuation.x = 1.0f; attenuation.y = 1.0f; attenuation.z = 1.0f; Vector3 refracted; float reflect_prob; float cosine; if (Vector3.Dot(r.direction, rec.normal) > 0) { outward_normal = -rec.normal; ni_over_nt = ref_idx; cosine = ref_idx * Vector3.Dot(r.direction, rec.normal) / r.direction.magnitude; } else { outward_normal = rec.normal; ni_over_nt = 1.0f / ref_idx; cosine = -Vector3.Dot(r.direction.normalized, rec.normal) / r.direction.magnitude; } var bRefracted = Refract(r.direction, outward_normal, ni_over_nt, out refracted); if (bRefracted) { reflect_prob = Schlick(cosine, ref_idx); } else { scattered.origin = rec.hitpoint; scattered.direction = reflected; reflect_prob = 1.0f; } if (Chapter11.RandomFloat01() < reflect_prob) { scattered.origin = rec.hitpoint; scattered.direction = reflected; } else { scattered.origin = rec.hitpoint; scattered.direction = refracted; } return(true); }
private Vector3 Random_in_unit_sphere() { return(new Vector3(Chapter11.RandomFloat11(), Chapter11.RandomFloat11(), Chapter11.RandomFloat11()).normalized); }