/* * static private float ch5_hit_sphere(Vector3 center, float radius, Ray ray) * { * Vector3 oc = ray.origin() - center; * float a = Vector3.dot(ray.direction(), ray.direction()); * float b = 2 * Vector3.dot(oc, ray.direction()); * float c = Vector3.dot(oc, oc) - radius * radius; * float discriminant = b * b - 4 * a * c; * if (discriminant < 0) * return -1.0f; * else * return -b - (float)Math.Sqrt(discriminant) / (2 * a); * } */ static private Vector3 ch5_color(Ray r, HitableList hitableList) { HitRecord rec = new HitRecord(); if (hitableList.hit(r, 0.0f, float.MaxValue, ref rec)) { return(0.5f * new Vector3(rec.normal.x() + 1, rec.normal.y() + 1, rec.normal.z() + 1)); } else { float t = 0.5f * (r.direction().normalized.y() + 1); return(new Vector3(1.0f, 1.0f, 1.0f) * (1 - t) + new Vector3(0.5f, 0.7f, 1.0f) * t); } }
static private Vector3 ch7_color(Ray r, HitableList hitableList) { HitRecord rec = new HitRecord(); if (hitableList.hit(r, 0.0001f, float.MaxValue, ref rec)) { Vector3 target = rec.p + rec.normal + Sphere.random_in_unit_sphere(); return(0.5f * ch7_color(new Ray(rec.p, target - rec.p), hitableList)); } else { float t = 0.5f * (r.direction().normalized.y() + 1); return(new Vector3(1.0f, 1.0f, 1.0f) * (1 - t) + new Vector3(0.5f, 0.7f, 1.0f) * t); } }
static private Vector3 ch8_color(Ray r, HitableList hitableList, int depth) { HitRecord rec = new HitRecord(); if (hitableList.hit(r, 0.0001f, float.MaxValue, ref rec)) { Ray scattered = null; Vector3 attenuation = null; if (depth > 0 && rec.mat.scatter(r, rec, ref attenuation, ref scattered)) { return(attenuation * ch8_color(scattered, hitableList, depth - 1)); } else { return(new Vector3(0, 0, 0)); } } else { float t = 0.5f * (r.direction().normalized.y() + 1); return(new Vector3(1.0f, 1.0f, 1.0f) * (1 - t) + new Vector3(0.5f, 0.7f, 1.0f) * t); } }