Exemplo n.º 1
0
    public override bool Hit(RayTrace.Ray ray, float t_min, float t_max, ref HitRecord rec)
    {
        var   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 temp = (-b - Mathf.Sqrt(discriminant)) / a * 0.5f;
            if (temp < t_max && temp > t_min)
            {
                rec.t        = temp;
                rec.p        = ray.GetPoint(rec.t);
                rec.normal   = (rec.p - center).normalized;
                rec.material = material;
                return(true);
            }

            temp = (-b + Mathf.Sqrt(discriminant)) / a * 0.5f;
            if (temp < t_max && temp > t_min)
            {
                rec.t        = temp;
                rec.p        = ray.GetPoint(rec.t);
                rec.normal   = (rec.p - center).normalized;
                rec.material = material;
                return(true);
            }
        }

        return(false);
    }
Exemplo n.º 2
0
    static Color GetColorForTestNormal(Ray ray)
    {
        float t = HitSphereForTestNormal(new Vector3(0, 0, -1), 0.5f, ray);

        if (t > 0)
        {
            Vector3 normal = Vector3.Normalize(ray.GetPoint(t) - new Vector3(0, 0, -1));
            return(0.5f * new Color(normal.x + 1, normal.y + 1, normal.z + 1, 2f));
        }
        t = 0.5f * ray.normalDirection.y + 1f;
        return((1 - t) * new Color(1, 1, 1) + t * new Color(0.5f, 0.7f, 1));
    }