示例#1
0
        public bool Hit(Ray ray, float tMin, float tMax, ref HitRecord hitRecord)
        {
            var currentCenter = Center(ray.Time);

            Vector3 oc    = ray.Origin - currentCenter;
            var     a     = ray.Direction.LengthSquared();
            var     halfB = Vector3.Dot(oc, ray.Direction);
            var     c     = oc.LengthSquared() - Radius * Radius;

            float discriminant = halfB * halfB - a * c;

            if (discriminant < 0.0f)
            {
                return(false);
            }

            // Find the nearest root that lies in the acceptable range.
            float sqrtD = MathF.Sqrt(discriminant);
            float root  = (-halfB - sqrtD) / a;

            if (root < tMin || root > tMax)
            {
                root = (-halfB + sqrtD) / a;
                if (root < tMin || root > tMax)
                {
                    return(false);
                }
            }

            hitRecord.T     = root;
            hitRecord.Point = ray.At(hitRecord.T);
            var outwardNormal = (hitRecord.Point - currentCenter) / Radius;

            hitRecord.SetFaceNormal(ray, outwardNormal);
            hitRecord.Material = _material;

            return(true);
        }
示例#2
0
        public bool Hit(Ray r, double tMin, double tMax, HitRecord record)
        {
            Vector3 oc    = r.Origin - Center;
            double  a     = r.Direction.LengthSquared();
            double  halfB = oc.Dot(r.Direction);
            double  c     = oc.LengthSquared() - Radius * Radius;

            double discriminant = halfB * halfB - a * c;

            if (discriminant < 0)
            {
                return(false);
            }
            double sqrtDiscriminant = Math.Sqrt(discriminant);

            // Find the nearest root that lies in the acceptable range
            double root = (-halfB - sqrtDiscriminant) / a;

            if (root < tMin || tMax < root)
            {
                root = (-halfB + sqrtDiscriminant) / a;
                if (root < tMin || tMax < root)
                {
                    return(false);
                }
            }

            record.T      = root;
            record.Point  = r.At(record.T);
            record.Normal = (record.Point - Center) / Radius;
            Vector3 outwardNormal = (record.Point - Center) / Radius;

            record.SetFaceNormal(r, outwardNormal);
            record.Material = Material;

            return(true);
        }