Ejemplo n.º 1
0
        public bool Scatter(ref Ray r, ref Hit_record rec, ref Vector3 attenuation, ref Ray scattered, System.Random seed)
        {
            Vector3 target = rec.normal.normalized * 0.5f +
                             new Vector3(Chapter12.RandomFloat11(seed), Chapter12.RandomFloat11(seed), Chapter12.RandomFloat11(seed)).normalized;

            scattered.origin    = rec.hitpoint;
            scattered.direction = target;
            attenuation         = albedo;
            return(true);
        }
Ejemplo n.º 2
0
        public bool Scatter(ref Ray r, ref Hit_record rec, ref Vector3 attenuation, ref Ray scattered, System.Random seed)
        {
            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 (Chapter12.RandomFloat01(seed) < reflect_prob)
            {
                scattered.origin    = rec.hitpoint;
                scattered.direction = reflected;
            }
            else
            {
                scattered.origin    = rec.hitpoint;
                scattered.direction = refracted;
            }
            return(true);
        }
Ejemplo n.º 3
0
 private Vector3 Random_in_unit_sphere(System.Random seed)
 {
     return(new Vector3(Chapter12.RandomFloat11(seed), Chapter12.RandomFloat11(seed), Chapter12.RandomFloat11(seed)).normalized);
 }