Exemple #1
0
        public bool Scatter(Ray rayIn, HitRecord record, ref Color attenuation, ref Ray scatteredRay)
        {
            Vector3 outwardNormal;

            attenuation = Color.white;
            var eta         = 0f;
            var cosi        = 0f;
            var reflectProb = 0f;

            if (Vector3.Dot(rayIn.Direction, record.Normal) > 0)
            {
                outwardNormal = -record.Normal;
                eta           = this.eta;
                cosi          = eta * Vector3.Dot(rayIn.NormalizedDirection, record.Normal);
            }
            else
            {
                outwardNormal = record.Normal;
                eta           = 1f / this.eta;
                cosi          = -Vector3.Dot(rayIn.NormalizedDirection, record.Normal);
            }

            var refracted = Vector3.zero;

            if (RayTraceUtility.Refract(rayIn.Direction, outwardNormal, eta, ref refracted))
            {
                reflectProb = RayTraceUtility.Schlick(cosi, eta);
            }
            else
            {
                reflectProb = 1f;
            }

            if (Random.Range(0f, 1f) <= reflectProb)
            {
                var reflected = RayTraceUtility.Reflect(rayIn.Direction, record.Normal);
                scatteredRay = new Ray(record.Point, reflected);
            }
            else
            {
                scatteredRay = new Ray(record.Point, refracted);
            }

            return(true);
        }
Exemple #2
0
        public bool Hit(Ray ray, float min, float max, ref HitRecord rec)
        {
            var tempRec = new HitRecord();
            var hit     = false;
            var cloest  = max;

            foreach (var h in List)
            {
                if (h.Hit(ray, min, cloest, ref tempRec))
                {
                    hit    = true;
                    cloest = tempRec.Root;
                    rec    = tempRec;
                }
            }

            return(hit);
        }
        static Color GetColorForTestMetal(Ray ray, HitableList hitableList, int depth)
        {
            HitRecord record = new HitRecord();

            if (hitableList.Hit(ray, 0.0001f, float.MaxValue, ref record))
            {
                Ray   r           = new Ray(Vector3.zero, Vector3.zero);
                Color attenuation = Color.black;
                if (depth < MAX_SCATTER_TIME && record.material.Scatter(ray, record, ref attenuation, ref r))
                {
                    Color c = GetColorForTestMetal(r, hitableList, depth + 1);
                    return(new Color(c.r * attenuation.r, c.g * attenuation.g, c.b * attenuation.b));
                }

                return(Color.black);
            }
            float t = 0.5f * ray.normalDirection.y + 1f;

            return((1 - t) * new Color(1, 1, 1) + t * new Color(0.5f, 0.7f, 1));
        }
Exemple #4
0
        static Color GetColorFromHitRecord(Ray ray, HitableList list, int depth)
        {
            var record = new HitRecord();

            if (list.Hit(ray, 0.001f, float.MaxValue, ref record))
            {
                var scatterdRay = new Ray(Vector3.zero, Vector3.zero);
                var attenuation = Color.black;
                if (depth < MaxDepth && record.Material.Scatter(ray, record, ref attenuation, ref scatterdRay))
                {
                    var c = GetColorFromHitRecord(scatterdRay, list, depth + 1);
                    return(new Color(c.r * attenuation.r, c.g * attenuation.g, c.b * attenuation.b));
                }
                else
                {
                    return(Color.black);
                }
            }

            var t = (ray.NormalizedDirection.y + 1f) * 0.5f;

            return((1 - t) * Color.white + t * new Color(0.5f, 0.7f, 1f));
        }
Exemple #5
0
 public abstract bool Scatter(Ray rayIn, HitRecord hitRecord, ref Color albedo, ref Ray scatterRay);