Beispiel #1
0
        public bool Scatter(Ray p_rayIn, HitRecord p_hitRecord, ref Color p_attenuation, ref Ray p_scatteredRay)
        {
            if (p_attenuation == null)
            {
                throw new ArgumentNullException(nameof(p_attenuation));
            }
            var reflected = Vec3.GetReflection(Vec3.GetUnitVector(p_rayIn.Direction), p_hitRecord.Normal);

            p_scatteredRay = new Ray(p_hitRecord.Point, reflected + Roughness * Math.GetRandomPositionInUnitSphere(), p_rayIn.Time);
            p_attenuation  = Albedo;
            return(Vec3.GetDotProduct(p_scatteredRay.Direction, p_hitRecord.Normal) > 0);
        }
        public bool Scatter(Ray p_rayIn, HitRecord p_hitRecord, ref Color p_attenuation, ref Ray p_scatteredRay)
        {
            if (p_attenuation == null)
            {
                throw new ArgumentNullException(nameof(p_attenuation));
            }
            var target = p_hitRecord.Point + p_hitRecord.Normal + Math.GetRandomPositionInUnitSphere();

            p_scatteredRay = new Ray(p_hitRecord.Point, target - p_hitRecord.Point, p_rayIn.Time);
            p_attenuation  = Albedo.GetValue(p_hitRecord.U, p_hitRecord.V, p_hitRecord.Point);
            return(true);
        }
Beispiel #3
0
        public bool Scatter(Ray p_rayIn, HitRecord p_hitRecord, ref Color p_attenuation, ref Ray p_scatteredRay)
        {
            if (p_attenuation == null)
            {
                throw new ArgumentNullException(nameof(p_attenuation));
            }
            var    rng = new Random();
            Vec3   outwardNormal;
            var    reflected = Vec3.GetReflection(p_rayIn.Direction, p_hitRecord.Normal);
            var    refracted = new Vec3(0);
            double niOverNt;

            p_attenuation = Color;

            double cosine;

            if (Vec3.GetDotProduct(p_rayIn.Direction, p_hitRecord.Normal) > 0)
            {
                outwardNormal = -p_hitRecord.Normal;
                niOverNt      = IndexOfRefraction;
                cosine        = IndexOfRefraction * Vec3.GetDotProduct(p_rayIn.Direction, p_hitRecord.Normal) /
                                p_rayIn.Direction.GetLength();
            }
            else
            {
                outwardNormal = p_hitRecord.Normal;
                niOverNt      = 1.0 / IndexOfRefraction;
                cosine        = -Vec3.GetDotProduct(p_rayIn.Direction, p_hitRecord.Normal) / p_rayIn.Direction.GetLength();
            }

            var reflectionProbability = Vec3.ShouldRefract(p_rayIn.Direction, outwardNormal, niOverNt, ref refracted) ? Math.SchlickApproximation(cosine, IndexOfRefraction) : 1.0d;

            p_scatteredRay = rng.NextDouble() < reflectionProbability ? new Ray(p_hitRecord.Point, reflected, p_rayIn.Time) : new Ray(p_hitRecord.Point, refracted, p_rayIn.Time);

            return(true);
        }