示例#1
0
        public bool Hit(MyRay _ray, float _tmin, float _tmax, HitInfo _hitInfo)
        {
            Vector3 oc           = _ray.Origin - Center;
            float   a            = Vector3.Dot(_ray.Direction, _ray.Direction);
            float   b            = 2f * Vector3.Dot(oc, _ray.Direction);
            float   c            = Vector3.Dot(oc, oc) - Radius * Radius;
            float   discriminant = b * b - 4 * a * c;

            if (discriminant > 0)
            {
                float t = (-b - Mathf.Sqrt(discriminant)) / (2 * a);
                if (t > _tmin && t < _tmax)
                {
                    _hitInfo.T        = t;
                    _hitInfo.HitPoint = _ray.PointAtT(t);
                    _hitInfo.Normal   = (_hitInfo.HitPoint - Center) / Radius;
                    _hitInfo.Material = Material;
                    return(true);
                }

                t = (-b + Mathf.Sqrt(discriminant)) / (2 * a);
                if (t > _tmin && t < _tmax)
                {
                    _hitInfo.T        = t;
                    _hitInfo.HitPoint = _ray.PointAtT(t);
                    _hitInfo.Normal   = (_hitInfo.HitPoint - Center) / Radius;
                    _hitInfo.Material = Material;
                    return(true);
                }
            }

            return(false);
        }
示例#2
0
        private Color GetColor(MyRay _ray)
        {
            float t = HitSphere(new Vector3(0, 0, -1), 0.5f, _ray);

            if (t > 0)
            {
                Vector3 normal      = _ray.PointAtT(t) - new Vector3(0, 0, -1);
                Vector3 normalAsRGB = 0.5f * (normal + Vector3.one);
                return(new Color(normalAsRGB.x, normalAsRGB.y, normalAsRGB.z));
            }

            return(GetBackgroundColor(_ray));
        }