Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        private void Draw()
        {
            Vector3 lowerLeftCorner = new Vector3(-2f, -1f, -1f);
            Vector3 horizontal      = new Vector3(4f, 0f, 0f);
            Vector3 vertical        = new Vector3(0f, 2f, 0f);
            Vector3 origin          = Vector3.zero;

            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    float u   = (float)i / Width;
                    float v   = (float)j / Height;
                    MyRay ray = new MyRay(origin, lowerLeftCorner + u * horizontal + v * vertical);
                    Color color;
                    if (HitSphere(new Vector3(0, 0, -1), 0.5f, ray))
                    {
                        color = Color.red;
                    }
                    else
                    {
                        color = GetBackgroundColor(ray);
                    }

                    m_texture2D.SetPixel(i, j, color);
                }
            }

            m_texture2D.Apply();
        }
Ejemplo n.º 3
0
        private Color GetBackgroundColor(MyRay _ray)
        {
            Vector3 unitDirection = _ray.Direction;
            float   t             = 0.5f * unitDirection.y + 1f;
            Vector3 c             = (1f - t) * m_bottomColor + t * m_topColor;

            return(new Color(c.x, c.y, c.z));
        }
Ejemplo n.º 4
0
        public override bool Scatter(MyRay _ray, HitInfo _hitInfo, out Vector3 _attenuation, out MyRay _scattered)
        {
            Vector3 target = _hitInfo.HitPoint + _hitInfo.Normal + _8_Metal.RandomPointInUnitSphere();

            _scattered   = new MyRay(_hitInfo.HitPoint, target - _hitInfo.HitPoint);
            _attenuation = Albedo;
            return(true);
        }
Ejemplo n.º 5
0
        public override bool Scatter(MyRay _ray, HitInfo _hitInfo, out Vector3 _attenuation, out MyRay _scattered)
        {
            Vector3 reflected = Reflect(_ray.Direction.normalized, _hitInfo.Normal);

            _scattered   = new MyRay(_hitInfo.HitPoint, reflected + m_fuzziness * _8_Metal.RandomPointInUnitSphere());
            _attenuation = m_albedo;
            return(Vector3.Dot(_scattered.Direction, _hitInfo.Normal) > 0);
        }
Ejemplo n.º 6
0
        private float HitSphere(Vector3 _center, float _radius, MyRay _ray)
        {
            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;

            return(discriminant < 0 ? -1f : (-b - Mathf.Sqrt(discriminant)) / (2 * a));
        }
Ejemplo n.º 7
0
        private bool HitSphere(Vector3 _center, float _radius, MyRay _ray)
        {
            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;

            return(discriminant > 0);
        }
        private Color GetColor(MyRay _ray)
        {
            HitInfo hitInfo = new HitInfo();

            if (m_worldElements.Hit(_ray, 0, float.MaxValue, hitInfo))
            {
                Vector3 normalAsRGB = 0.5f * (hitInfo.Normal + Vector3.one);
                return(new Color(normalAsRGB.x, normalAsRGB.y, normalAsRGB.z));
            }

            return(GetBackgroundColor(_ray));
        }
Ejemplo n.º 9
0
        private Color GetColor(MyRay _ray)
        {
            HitInfo hitInfo = new HitInfo();

            if (m_worldElements.Hit(_ray, 0.001f, float.MaxValue, hitInfo))
            {
                Vector3 target = hitInfo.HitPoint + hitInfo.Normal + RandomPointInUnitSphere();
                return(0.5f * GetColor(new MyRay(hitInfo.HitPoint, target - hitInfo.HitPoint)));
            }

            return(GetBackgroundColor(_ray));
        }
Ejemplo n.º 10
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));
        }
Ejemplo n.º 11
0
        public override bool Scatter(MyRay _ray, HitInfo _hitInfo, out Vector3 _attenuation, out MyRay _scattered)
        {
            Vector3 outwardNormal;
            Vector3 reflected = Reflect(_ray.Direction.normalized, _hitInfo.Normal);
            float   ni_over_nt;

            _attenuation = new Vector3(1, 1, 1);
            Vector3 refracted = Vector3.zero;
            float   reflectProb;
            float   cosine;

            if (Vector3.Dot(_ray.Direction, _hitInfo.Normal) > 0)
            {
                outwardNormal = -_hitInfo.Normal;
                ni_over_nt    = m_refractiveIndex;
                cosine        = m_refractiveIndex * Vector3.Dot(_ray.Direction, _hitInfo.Normal) / _ray.Direction.magnitude;
            }
            else
            {
                outwardNormal = _hitInfo.Normal;
                ni_over_nt    = 1 / m_refractiveIndex;
                cosine        = -Vector3.Dot(_ray.Direction, _hitInfo.Normal) / _ray.Direction.magnitude;
            }

            if (Refract(_ray.Direction, outwardNormal, ni_over_nt, ref refracted))
            {
                reflectProb = Schlick(cosine, m_refractiveIndex);
            }
            else
            {
                _scattered = new MyRay(_hitInfo.HitPoint, reflected);
                return(true);
            }

            if (Random.Range(0f, 1f) < reflectProb)
            {
                _scattered = new MyRay(_hitInfo.HitPoint, reflected);
            }
            else
            {
                _scattered = new MyRay(_hitInfo.HitPoint, refracted);
            }

            return(true);
        }
Ejemplo n.º 12
0
        private Color GetColor(MyRay _ray, int _depth)
        {
            HitInfo hitInfo = new HitInfo();

            if (m_worldElements.Hit(_ray, 0.001f, float.MaxValue, hitInfo))
            {
                MyRay   scattered;
                Vector3 attenuation;
                if (_depth < 50 && hitInfo.Material.Scatter(_ray, hitInfo, out attenuation, out scattered))
                {
                    Color attAsRGB = new Color(attenuation.x, attenuation.y, attenuation.z, 1);
                    return(attAsRGB * GetColor(scattered, _depth + 1));
                }

                return(Color.black);
            }

            return(GetBackgroundColor(_ray));
        }
        private void Draw()
        {
            Vector3 lowerLeftCorner = new Vector3(-2f, -1f, -1f);
            Vector3 horizontal      = new Vector3(4f, 0f, 0f);
            Vector3 vertical        = new Vector3(0f, 2f, 0f);
            Vector3 origin          = Vector3.zero;

            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    float u   = (float)i / Width;
                    float v   = (float)j / Height;
                    MyRay ray = new MyRay(origin, lowerLeftCorner + u * horizontal + v * vertical);
                    m_texture2D.SetPixel(i, j, GetBackgroundColor(ray));
                }
            }

            m_texture2D.Apply();
        }
Ejemplo n.º 14
0
        public static bool Hit <THitable>(this List <THitable> _list, MyRay _ray, float _tmin, float _tmax, HitInfo _hitInfo) where THitable : IHitable
        {
            bool    hitAnything = false;
            float   closestHit  = _tmax;
            HitInfo tempHitInfo = new HitInfo();

            for (int i = 0; i < _list.Count; i++)
            {
                if (_list[i].Hit(_ray, _tmin, closestHit, tempHitInfo))
                {
                    hitAnything       = true;
                    closestHit        = tempHitInfo.T;
                    _hitInfo.T        = closestHit;
                    _hitInfo.HitPoint = tempHitInfo.HitPoint;
                    _hitInfo.Normal   = tempHitInfo.Normal;
                    _hitInfo.Material = tempHitInfo.Material;
                }
            }

            return(hitAnything);
        }
Ejemplo n.º 15
0
        private void Draw()
        {
            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    Vector3 sumColor = Vector3.zero;
                    for (int sample = 0; sample < SamplesPerPixel; sample++)
                    {
                        float u     = (i + Random.Range(0f, 1f)) / Width;
                        float v     = (j + Random.Range(0f, 1f)) / Height;
                        MyRay ray   = m_myCamera.GetRay(u, v);
                        Color color = GetColor(ray);
                        sumColor += new Vector3(color.r, color.g, color.b);
                    }

                    Vector3 averageColor = sumColor / SamplesPerPixel;
                    m_texture2D.SetPixel(i, j, new Color(averageColor.x, averageColor.y, averageColor.z));
                }
            }

            m_texture2D.Apply();
        }
Ejemplo n.º 16
0
 public abstract bool Scatter(MyRay _ray, HitInfo _hitInfo, out Vector3 _attenuation, out MyRay _scattered);