private Vector3D GetColor(Ray r, HittableList world, int depth) { HitRecord rec; if (world.Hit(r, 0.001, double.MaxValue, out rec)) { Ray scattered; Vector3D attenuation; if (depth < 50 && rec.matPtr.Scatter(r, rec, out attenuation, out scattered)) //只递归50次,避免无谓的性能浪费 { Vector3D color = GetColor(scattered, world, depth + 1); //每次光线衰减之后深度加一 return(new Vector3D(attenuation.X * color.X, attenuation.Y * color.Y, attenuation.Z * color.Z)); } else { return(new Vector3D(0, 0, 0)); } } else { Vector3D unitDirection = r.Direction.UnitVector(); double t = 0.5 * (unitDirection.Y + 1); return((1 - t) * new Vector3D(1, 1, 1) + t * new Vector3D(0.5, 0.7, 1)); } }
private Vector3D GetColor(Ray r, HittableList world) { HitRecord rec; if (world.Hit(r, 0, double.MaxValue, out rec)) { return(0.5 * new Vector3D(rec.normal.X + 1, rec.normal.Y + 1, rec.normal.Z + 1)); } else { Vector3D unitDirection = r.Direction.UnitVector(); double t = 0.5 * (unitDirection.Y + 1); return((1 - t) * new Vector3D(1, 1, 1) + t * new Vector3D(0.5, 0.7, 1)); } }
private Vector3D GetColor(Ray r, HittableList world) { HitRecord rec; if (world.Hit(r, 0.0000001, double.MaxValue, out rec)) { Vector3D target = rec.p + rec.normal + RandomInUnitShpere( ); //击中点加法线向量得到击中点单位球的球心,球心加上 //随机向量得到反射的方向。 return(0.5 * GetColor(new Ray(rec.p, target - rec.p), world)); //每次碰撞都使光线强度减半 } else { Vector3D unitDirection = r.Direction.UnitVector(); double t = 0.5 * (unitDirection.Y + 1); return((1 - t) * new Vector3D(1, 1, 1) + t * new Vector3D(0.5, 0.7, 1)); } }