Color color(zRay r, Hitable world, int depth)
    {
        hit_record rec = new hit_record();

        if (world.hit(r, 0.0f, float.MaxValue, ref rec))
        {
            zRay    scattered   = new zRay();
            Vector3 attenuation = Vector3.zero;
            Color   emitted     = rec.mat.emitted();
            if (depth < maxDepth && rec.mat.scatter(r, rec, ref attenuation, ref scattered))
            {
                return(emitted + new Color(attenuation.x, attenuation.y, attenuation.z) * color(scattered, world, depth + 1));
            }
            else
            {
                return(emitted);
            }
        }
        else
        {
            //float t = 0.5f * (r.direction.normalized.y + 1f);
            //return (1f - t) * Color.white + t * new Color(0.5f, 0.7f, 1.0f);
            return(env_Color);
        }
    }
    public override bool hit(zRay r, float t_min, float t_max, ref hit_record rec)
    {
        hit_record temp_rec       = new hit_record();
        bool       hit_anything   = false;
        float      closest_so_far = t_max;

        if (!usBvh)
        {
            float minDis = float.MaxValue;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].hit(r, t_min, closest_so_far, ref temp_rec))
                {
                    float dis = (temp_rec.p - r.origin).magnitude;
                    if (dis < minDis)
                    {
                        hit_anything   = true;
                        closest_so_far = temp_rec.t;
                        rec            = temp_rec;
                        rec.mat        = list[i].material;
                        minDis         = dis;
                    }
                }
            }
        }
        else
        {
            hit_anything = bvh.hit(r, t_min, t_max, ref rec);
        }
        return(hit_anything);
    }
Exemple #3
0
        public static Color ray_color(Ray r, ref Hittable_list world, int depth)
        {
            hit_record rec = new hit_record();

            if (depth <= 0)
            {
                return(new Color(0, 0, 0));
            }
            if (world.hit(r, 0.001, Utilities.infinity, ref rec))
            {
                Ray   scattered   = new Ray();
                Color attenuation = new Color();
                if (rec.mat_prt.scatter(r, rec, ref attenuation, ref scattered))
                {
                    return(attenuation * ray_color(scattered, ref world, depth - 1));
                }
                //Random Ray
                Point3 target = rec.p + rec.normal + Vec3.random_unit_vector();
                return(0.5 * ray_color(new Ray(rec.p, target - rec.p), ref world, depth - 1));
            }
            Vec3 unit_direction = Vec3.unit_vector(r.direction);
            var  t = 0.5 * (unit_direction.y() + 1.0);

            return((1.0 - t) * new Color(1.0, 1.0, 1.0) + t * new Color(0.5, 0.7, 1.0));
        }
Exemple #4
0
    public override bool scatter(Ray ray_in, hit_record rec, out Color attenuation, out Ray scattered)
    {
        var target = rec.p + rec.normal + Util.random_in_unit_sphere();

        scattered   = new Ray(rec.p, target - rec.p);
        attenuation = albedo;
        return(true);
    }
Exemple #5
0
    public override bool scatter(zRay r_in, hit_record rec, ref Vector3 attenuation, ref zRay scattered)
    {
        Vector3 target = rec.p + rec.normal + zRandom.random_in_unit_sphere();

        scattered   = new zRay(rec.p, target - rec.p, r_in.time);
        attenuation = albedo.ToVector3();
        return(true);
    }
Exemple #6
0
    public override bool scatter(Ray ray_in, hit_record rec, out Color attenuation, out Ray scattered)
    {
        var reflected = Util.reflect(ray_in.direction().normalized, rec.normal);

        scattered   = new Ray(rec.p, reflected + fuzz * Util.random_in_unit_sphere());
        attenuation = albedo;
        return(Vector3.Dot(scattered.direction(), rec.normal) > 0);
    }
    public override bool scatter(zRay r_in, hit_record rec, ref Vector3 attenuation, ref zRay scattered)
    {
        Vector3 reflected = Vector3.Reflect(r_in.direction.normalized, rec.normal);

        scattered   = new zRay(rec.p, reflected + (1f - smoothness) * zRandom.random_in_unit_sphere(), r_in.time);
        attenuation = albedo.ToVector3();
        return(Vector3.Dot(scattered.direction, rec.normal) > 0);
    }
Exemple #8
0
        public override zwischenSpeicher scatter(ray r_in, hit_record rec, Vektor attenuation, ray scattered)
        {
            zwischenSpeicher zw        = new zwischenSpeicher();
            Vektor           reflected = Vektor.reflect(Vektor.unit_Vektor(r_in.Direction), rec.normal);

            scattered   = new ray(rec.p, reflected + fuzz * Vektor.random_in_unit_sphere());
            attenuation = albedo;

            zw.scattered   = scattered;
            zw.attenuation = attenuation;
            zw.IsTrue      = (Vektor.dot(scattered.Direction, rec.normal) > 0.0);

            return(zw);
        }
    public override bool hit(zRay r, float t_min, float t_max, ref hit_record rec)
    {
        if (rec.lastHit == id)
        {
            return(false);
        }
        Vector3 oc           = r.origin - center;
        float   a            = Vector3.Dot(r.direction, r.direction);
        float   b            = Vector3.Dot(oc, r.direction);
        float   c            = Vector3.Dot(oc, oc) - radius * radius;
        float   discriminant = b * b - a * c;

        //Debug.Log("try hit " + discriminant + " " + r.origin + " " + r.direction);
        if (discriminant > 0)
        {
            rec.mat = material;
            float temp = (-b - Mathf.Sqrt(b * b - a * c)) / a;
            if (temp < t_max && temp > t_min)
            {
                rec.t       = temp;
                rec.p       = r.point_at_parameter(rec.t);
                rec.normal  = (rec.p - center) / radius;
                rec.lastHit = id;
                Debug.Log("hit");
                if (DebugDrawer.isDebug)
                {
                    DebugDrawer.points.Add(rec.p);
                    DebugDrawer.normals.Add(rec.normal);
                }
                return(true);
            }
            temp = (-b + Mathf.Sqrt(b * b - a * c)) / a;
            if (temp < t_max && temp > t_min)
            {
                rec.t       = temp;
                rec.p       = r.point_at_parameter(rec.t);
                rec.normal  = (rec.p - center) / radius;
                rec.lastHit = id;
                Debug.Log("hit");
                if (DebugDrawer.isDebug)
                {
                    DebugDrawer.points.Add(rec.p);
                    DebugDrawer.normals.Add(rec.normal);
                }
                return(true);
            }
        }
        return(false);
    }
    public override bool scatter(zRay r_in, hit_record rec, ref Vector3 attenuation, ref zRay scattered)
    {
        Vector3 outward_normal = Vector3.zero;
        Vector3 reflected = Vector3.Reflect(r_in.direction, rec.normal);
        float ni_over_nt = 0;
        attenuation = Vector3.one;
        Vector3 refracted = Vector3.zero;
        float reflect_prob = 0f;
        float cosine = 0f;

        if (Vector3.Dot(r_in.direction, rec.normal) > 0)
        {
            outward_normal = -rec.normal;
            ni_over_nt = ref_idx;
            cosine = ref_idx * Vector3.Dot(r_in.direction, rec.normal);
        }
        else
        {
            outward_normal = rec.normal;
            ni_over_nt = 1f / ref_idx;
            cosine = -Vector3.Dot(r_in.direction, rec.normal);
        }

        if (r_in.direction.refract(outward_normal, ni_over_nt, ref refracted))
        {
            reflect_prob = rtUtils.schlick(cosine, ref_idx);
        }
        else
        {
            scattered = new zRay(rec.p, reflected, r_in.time);
            reflect_prob = 1f;
        }

        if (zRandom.drand() < reflect_prob)
        {
            scattered = new zRay(rec.p, reflected, r_in.time);
        }
        else
        {
            scattered = new zRay(rec.p, refracted, r_in.time);
        }

        return true;
    }
Exemple #11
0
        public override zwischenSpeicher scatter(ray r_in, hit_record rec, Vektor attenuation, ray scattered)
        {
            zwischenSpeicher zw   = new zwischenSpeicher();
            var scatter_direction = rec.normal + Vektor.random_unit_vektor();

            //catch degenerate scatter direction
            if (scatter_direction.near_zero())
            {
                scatter_direction = rec.normal;
            }

            scattered   = new ray(rec.p, scatter_direction);
            attenuation = albedo;

            zw.scattered   = scattered;
            zw.attenuation = attenuation;
            zw.IsTrue      = true;

            return(zw);
        }
Exemple #12
0
    public override bool scatter(Ray r_in, hit_record rec, out Color attenuation, out Ray scattered)
    {
        Vector3 outward_normal;
        var     reflected = Util.reflect(r_in.direction(), rec.normal);
        float   ni_over_nt;

        attenuation = new Color(1.0f, 1.0f, 1.0f);
        Vector3 refracted;
        float   reflect_prob;
        float   cosine;

        if (Vector3.Dot(r_in.direction(), rec.normal) > 0.0f)
        {
            outward_normal = -rec.normal;
            ni_over_nt     = ref_idx;
            cosine         = ref_idx * Vector3.Dot(r_in.direction(), rec.normal) / r_in.direction().magnitude;
        }
        else
        {
            outward_normal = rec.normal;
            ni_over_nt     = 1.0f / ref_idx;
            cosine         = -Vector3.Dot(r_in.direction(), rec.normal) / r_in.direction().magnitude;
        }
        if (Util.refract(r_in.direction(), outward_normal, ni_over_nt, out refracted))
        {
            reflect_prob = Util.schlick(cosine, ref_idx);
        }
        else
        {
            reflect_prob = 1;
        }
        if (Random.value < reflect_prob)
        {
            scattered = new Ray(rec.p, reflected);
        }
        else
        {
            scattered = new Ray(rec.p, refracted);
        }
        return(true);
    }
Exemple #13
0
 public override bool hit(zRay r, float t_min, float t_max, ref hit_record rec)
 {
     if (box.hit(r, t_min, t_max))
     {
         hit_record left_rec = new hit_record(), right_rec = new hit_record();
         bool       hit_left  = left.hit(r, t_min, t_max, ref left_rec);
         bool       hit_right = right.hit(r, t_min, t_max, ref right_rec);
         if (hit_left && hit_right)
         {
             if (left_rec.t < right_rec.t)
             {
                 rec = left_rec;
             }
             else
             {
                 rec = right_rec;
             }
             return(true);
         }
         else if (hit_left)
         {
             rec = left_rec;
             return(true);
         }
         else if (hit_right)
         {
             rec = right_rec;
             return(true);
         }
         else
         {
             return(false);
         }
     }
     else
     {
         return(false);
     }
 }
    public override bool hit(zRay r, float t_min, float t_max, ref hit_record rec)
    {
        if (rec.lastHit == id)
        {
            return(false);
        }
        Vector3 oc           = r.origin - center(r.time);
        float   a            = Vector3.Dot(r.direction, r.direction);
        float   b            = Vector3.Dot(oc, r.direction);
        float   c            = Vector3.Dot(oc, oc) - radius * radius;
        float   discriminant = b * b - a * c;

        if (discriminant > 0)
        {
            rec.mat = material;
            float temp = (-b - Mathf.Sqrt(discriminant)) / a;
            if (temp < t_max && temp > t_min)
            {
                rec.t       = temp;
                rec.p       = r.point_at_parameter(rec.t);
                rec.normal  = (rec.p - center(r.time)) / radius;
                rec.mat     = material;
                rec.lastHit = id;
                return(true);
            }
            temp = (-b + Mathf.Sqrt(discriminant)) / a;
            if (temp < t_max && temp > t_min)
            {
                rec.t       = temp;
                rec.p       = r.point_at_parameter(rec.t);
                rec.normal  = (rec.p - center(r.time)) / radius;
                rec.mat     = material;
                rec.lastHit = id;
                return(true);
            }
        }
        return(false);
    }
Exemple #15
0
    public override bool hit(Ray r, float t_min, float t_max, out hit_record rec)
    {
        var oc         = r.origin() - center;
        var a          = Vector3.Dot(r.direction(), r.direction());
        var b          = Vector3.Dot(oc, r.direction());
        var c          = Vector3.Dot(oc, oc) - radius * radius;
        var discrement = b * b - a * c;

        if (discrement > 0)
        {
            var temp = (-b - Mathf.Sqrt(b * b - a * c)) / a;
            if (temp < t_max && temp > t_min)
            {
                rec.t      = temp;
                rec.p      = r.point_at_parameter(rec.t);
                rec.normal = (rec.p - center) / radius;
                rec.mat    = material;
                return(true);
            }
            temp = (-b + Mathf.Sqrt(b * b - a * c)) / a;
            if (temp < t_max && temp > t_min)
            {
                rec.t      = temp;
                rec.p      = r.point_at_parameter(rec.t);
                rec.normal = (rec.p - center) / radius;
                rec.mat    = material;
                return(true);
            }
        }

        rec.normal = new Vector3();
        rec.p      = new Vector3();
        rec.t      = 0;
        rec.mat    = null;

        return(false);
    }
    public override bool hit(zRay r, float t_min, float t_max, ref hit_record rec)
    {
        if (rec.lastHit == id)
        {
            return(false);
        }
        List <hit_record> recs = new List <hit_record>();

        foreach (var tri in list)
        {
            if (tri.hit(r, t_min, t_max, ref rec))
            {
                recs.Add(rec);
            }
        }

        if (recs.Count == 0)
        {
            return(false);
        }

        float tmin      = float.MaxValue;
        int   tminIndex = 0;

        for (int i = 0; i < recs.Count; i++)
        {
            if (recs[i].t < tmin)
            {
                tmin      = recs[i].t;
                tminIndex = i;
            }
        }

        rec = recs[tminIndex];
        return(true);
    }
Exemple #17
0
    public override bool hit(zRay r, float t_min, float t_max, ref hit_record rec)
    {
        if (Vector3.Dot(r.direction, normal) > 0)
        {
            return(false);
        }
        r = new zRay(trans.worldToLocalMatrix.MultiplyPoint(r.origin), trans.worldToLocalMatrix.MultiplyVector(r.direction));
        float t = (k - r.origin.z) / r.direction.z;

        if (t < t_min || t > t_max)
        {
            return(false);
        }
        float x = r.origin.x + t * r.direction.x;
        float y = r.origin.y + t * r.direction.y;

        if (x < x0 || x > x1 || y < y0 || y > y1)
        {
            return(false);
        }
        rec.uv      = new Vector2((x - x0) / (x1 - x0), (y - y0) / (y1 - y0));
        rec.t       = t * trans.lossyScale.x;
        rec.mat     = material;
        rec.p       = trans.localToWorldMatrix.MultiplyPoint(r.point_at_parameter(t));
        rec.normal  = normal;
        rec.lastHit = id;
        if (DebugDrawer.isDebug)
        {
            DebugDrawer.points.Add(rec.p);
            DebugDrawer.normals.Add(rec.normal);
            //Debug.Log(r.origin + " " + r.direction);
            //Debug.Log("rec.p " + rec.p + " local " + r.point_at_parameter(t));
            //Debug.Log("id " + id);
        }
        return(true);
    }
Exemple #18
0
    public bool hit(Ray r, float t_min, float t_max, out hit_record rec)
    {
        hit_record temp_rec;

        rec.normal = new Vector3();
        rec.p      = new Vector3();
        rec.t      = 0;
        rec.mat    = null;

        var hit_anything   = false;
        var closest_so_far = t_max;

        foreach (var h in this)
        {
            if (h.hit(r, t_min, closest_so_far, out temp_rec))
            {
                hit_anything   = true;
                closest_so_far = temp_rec.t;
                rec            = temp_rec;
            }
        }

        return(hit_anything);
    }
    public override bool hit(zRay r, float t_min, float t_max, ref hit_record rec)
    {
        if (Vector3.Dot(normal, r.direction) > 0)
        {
            return(false);
        }

        Vector3 e1  = b - a;
        Vector3 e2  = c - a;
        Vector3 p   = Vector3.Cross(r.direction, e2);
        float   det = Vector3.Dot(e1, p);
        Vector3 T;

        if (det > 0)
        {
            T = r.origin - a;
        }
        else
        {
            T   = a - r.origin;
            det = -det;
        }

        if (det < 0.0001f)
        {
            return(false);
        }

        float u = Vector3.Dot(T, p);

        if (u < 0f || u > det)
        {
            return(false);
        }

        Vector3 Q = Vector3.Cross(T, e1);
        float   v = Vector3.Dot(r.direction, Q);

        if (v < 0 || u + v > det)
        {
            return(false);
        }

        float t      = Vector3.Dot(e2, Q);
        float invDet = 1 / det;

        t *= invDet;
        u *= invDet;
        v *= invDet;

        rec.mat     = material;
        rec.t       = t;
        rec.p       = r.point_at_parameter(t);
        rec.normal  = normal;
        rec.lastHit = id;
        if (DebugDrawer.isDebug)
        {
            DebugDrawer.points.Add(rec.p);
            DebugDrawer.normals.Add(rec.normal);
        }
        return(true);
    }
 public abstract bool scatter(zRay r_in, hit_record rec, ref Vector3 attenuation, ref zRay scattered);
Exemple #21
0
        public virtual zwischenSpeicher scatter(ray r_in, hit_record rec, Vektor attenuation, ray scattered)
        {
            zwischenSpeicher zw = new zwischenSpeicher();

            return(zw);
        }
Exemple #22
0
 public override bool scatter(zRay r_in, hit_record rec, ref Vector3 attenuation, ref zRay scattered)
 {
     return(false);
 }
Exemple #23
0
        public virtual zwischenSpeicher Hit(ray r, double t_min, double t_max, hit_record rec)
        {
            zwischenSpeicher zw = new zwischenSpeicher();

            return(zw);
        }
 public abstract bool hit(zRay r, float t_min, float t_max, ref hit_record rec);
Exemple #25
0
 public abstract bool hit(Ray r, float t_min, float t_max, out hit_record rec);
Exemple #26
0
 public abstract bool scatter(Ray ray_in, hit_record rec, out Color attenuation, out Ray scattered);