コード例 #1
0
    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);
        }
    }
コード例 #2
0
    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);
    }
コード例 #3
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);
    }
コード例 #4
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);
    }
コード例 #5
0
    IEnumerator Render()
    {
        int nx = Screen.width;
        int ny = Screen.height;

        progress   = 0f;
        startTime  = time();
        renderDone = false;

        Vector3 lookFrom      = new Vector3(1f, 2f, 2f);
        Vector3 lookAt        = new Vector3(0f, 0f, -1f);
        float   dist_to_focus = 2f;
        float   aperture      = 0f;
        zCamera zcam          = new zCamera(cam);
        uint    index         = 0;

        if (debugMod)
        {
            Color col = Color.black;
            for (int s = 0; s < 1; s++)
            {
                float u = (float)((int)debugPoint.x + zRandom.Halton5(index++)) / (float)(nx);
                float v = (float)((int)debugPoint.y + zRandom.Halton5(index++)) / (float)(ny);
                zRay  r = zcam.get_ray(u, v);
                DebugDrawer.Init(cam);
                col += color(r, world, 0);
            }
        }
        else
        {
            for (int j = ny - 1; j >= 0; j--)
            {
                Color col = Color.black;
                for (int i = 0; i < nx; i++)
                {
                    for (int s = 0; s < ns; s++)
                    {
                        float u = (float)(i + zRandom.Halton5(index++)) / (float)(nx);
                        float v = (float)(j + zRandom.Halton5(index++)) / (float)(ny);
                        zRay  r = zcam.get_ray(u, v);
                        col += color(r, world, 0);
                    }
                    col /= (float)ns;
                    col  = col.gamma;
                    rtResult.SetPixel(rtResult.width - i, j, col);
                }
                progress += nx;
                yield return(null);
            }
            rtResult.Apply();
            renderDone = true;
            timePassed = time();
        }
        yield return(null);
    }
コード例 #6
0
    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);
    }
コード例 #7
0
    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;
    }
コード例 #8
0
    public bool hit(zRay r, float tmin, float tmax)
    {
        for (int a = 0; a < 3; a++)
        {
            float invD = 1f / r.direction[a];
            float t0   = (_min[a] - r.origin[a]) * invD;
            float t1   = (_max[a] - r.origin[a]) * invD;
            if (invD < 0f)
            {
                swap(ref t0, ref t1);
            }

            tmin = t0 > tmin ? t0 : tmin;
            tmax = t1 < tmax ? t1 : tmax;
            if (tmax <= tmin)
            {
                return(false);//no overlap
            }
        }
        return(true);
    }
コード例 #9
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);
     }
 }
コード例 #10
0
    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);
    }
コード例 #11
0
    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);
    }
コード例 #12
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);
    }
コード例 #13
0
 public abstract bool scatter(zRay r_in, hit_record rec, ref Vector3 attenuation, ref zRay scattered);
コード例 #14
0
    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);
    }
コード例 #15
0
 public abstract bool hit(zRay r, float t_min, float t_max, ref hit_record rec);
コード例 #16
0
 public override bool scatter(zRay r_in, hit_record rec, ref Vector3 attenuation, ref zRay scattered)
 {
     return(false);
 }