Exemple #1
0
    protected virtual void Start()
    {
        ppmTexture.Init(200, 100);

        RTRay   ray              = new RTRay();
        Vector3 origin           = Vector3.zero;
        Vector3 leftBottomCorner = new Vector3(-2, -1, -1);
        Vector3 horizontal       = new Vector3(4, 0, 0);
        Vector3 vertical         = new Vector3(0, 2, 0);

        for (int j = 0; j < ppmTexture.Height; ++j)
        {
            for (int i = 0; i < ppmTexture.Width; ++i)
            {
                float u = (float)i / ppmTexture.Width;
                float v = (float)j / ppmTexture.Height;
                ray.Set(origin, leftBottomCorner + horizontal * u + vertical * v);
                Color color = GetColor(ray, 0);
                ppmTexture.WriteAPixel(color);
            }
        }

        ppmTexture.Complete();
        RenderingComplete();
    }
Exemple #2
0
    public static Color GetEnvironmentColor(RTRay ray)
    {
        Vector3 unit_dir = ray.direction.normalized;
        float   blend    = (unit_dir.y + 1) * 0.5f;

        return(Color.white * (1 - blend) + new Color(0.5f, 0.7f, 1) * blend);
    }
Exemple #3
0
    protected Color GetBackgroundColor(RTRay ray)
    {
        Vector3 unit_dir = ray.direction.normalized;
        float   blend    = (unit_dir.y + 1) * 0.5f;

        return(Color.white * (1 - blend) + new Color(0.5f, 0.7f, 1) * blend);
    }
Exemple #4
0
    public RTRay GetRay(float u, float v)
    {
        RTRay ray = new RTRay();

        ray.Set(origin, leftBottomCorner + horizontal * u + vertical * v - origin);
        return(ray);
    }
Exemple #5
0
    private void Working()
    {
        RTMath.ThreadInitRnd();

        for (int i = 0; i < pIndex; ++i)
        {
            if (IsDestroied)
            {
                return;
            }

            Item  item  = items[i];
            Color color = Color.black;
            for (int s = 0; s < numSamples; ++s)
            {
                float u   = (item.i + RTMath.Rnd01()) / canvasWidth;
                float v   = (item.j + RTMath.Rnd01()) / canvasHeight;
                RTRay ray = cam.GetRay(u, v);
                color += renderer.GetColor(ray, 0);
            }
            color          /= numSamples;
            item.finalColor = color;
            items[i]        = item;
        }

        status = STATUS_COMPLETE;
    }
Exemple #6
0
    public override bool Scatter(RTRay ray, HitRecord hit, out Color attenuation, out RTRay scattered)
    {
        Vector3 reflected = RTMath.Reflect(ray.direction.normalized, hit.n);

        scattered   = new RTRay().Set(hit.p, reflected + fuzz * RTMath.RndInUnitSphere());
        attenuation = albedo;
        return(Vector3.Dot(scattered.direction, hit.n) > 0);
    }
Exemple #7
0
    public override bool Scatter(RTRay ray, HitRecord hit, out Color attenuation, out RTRay scattered)
    {
        Vector3 target = hit.p + hit.n + RTMath.RndInUnitSphere();

        scattered   = new RTRay().Set(hit.p, target - hit.p);
        attenuation = albedo;
        return(true);
    }
    private bool HitSphere(Vector3 center, float radius, RTRay ray)
    {
        Vector3 oc           = ray.origin - center;
        float   a            = Vector3.Dot(ray.direction, ray.direction);
        float   b            = 2.0f * Vector3.Dot(oc, ray.direction);
        float   c            = Vector3.Dot(oc, oc) - radius * radius;
        float   discriminant = b * b - 4 * a * c;

        return(discriminant >= 0);
    }
Exemple #9
0
    public RTRay GetRay(float s, float t)
    {
        Vector3 rd     = RTMath.RndInUnitSphere() * len_radius;
        Vector3 offset = u * rd.x + v * rd.y;

        RTRay ray = new RTRay();

        ray.Set(origin + offset, leftBottomCorner + horizontal * s + vertical * t - origin - offset);
        return(ray);
    }
Exemple #10
0
    public override Color GetColor(RTRay ray, int depth)
    {
        HitRecord hit;

        if (scene.Hit(ray, 0.001f, 1000, out hit))
        {
            Vector3 n = hit.n;
            return(new Color((n.x + 1) * 0.5f, (n.y + 1) * 0.5f, (n.z + 1) * 0.5f));
        }

        return(RTCanvas.GetEnvironmentColor(ray));
    }
Exemple #11
0
    public override Color GetColor(RTRay ray, int depth)
    {
        HitRecord hit;

        if (sphere.Hit(ray, 0.001f, 1000, out hit))
        {
            Vector3 n = hit.n;
            return(new Color((n.x + 1) * 0.5f, (n.y + 1) * 0.5f, (n.z + 1) * 0.5f));
        }

        return(GetBackgroundColor(ray));
    }
    public override Color GetColor(RTRay ray, int depth)
    {
        Vector3 sphereCenter = new Vector3(0, 0, -1);
        float   sphereRadius = 0.5f;

        if (HitSphere(sphereCenter, sphereRadius, ray))
        {
            return(Color.red);
        }

        return(GetBackgroundColor(ray));
    }
Exemple #13
0
    public override Color GetColor(RTRay ray, int depth)
    {
        HitRecord hit;

        if (scene.Hit(ray, 0.001f, 1000, out hit))
        {
            Vector3 target       = hit.p + hit.n + RTMath.RndInUnitSphere();
            float   absorptivity = 0.5f;
            return(absorptivity * GetColor(new RTRay().Set(hit.p, target - hit.p), depth + 1));
        }

        return(RTCanvas.GetEnvironmentColor(ray));
    }
    public override Color GetColor(RTRay ray, int depth)
    {
        Vector3 sphereCenter = new Vector3(0, 0, -1);
        float   sphereRadius = 0.5f;
        float   hit          = HitSphere(sphereCenter, sphereRadius, ray);

        if (hit >= 0)
        {
            Vector3 n = (ray.PointAt(hit) - sphereCenter).normalized;
            return(new Color((n.x + 1) * 0.5f, (n.y + 1) * 0.5f, (n.z + 1) * 0.5f));
        }

        return(GetBackgroundColor(ray));
    }
    private float HitSphere(Vector3 center, float radius, RTRay ray)
    {
        Vector3 oc           = ray.origin - center;
        float   a            = Vector3.Dot(ray.direction, ray.direction);
        float   b            = 2.0f * Vector3.Dot(oc, ray.direction);
        float   c            = Vector3.Dot(oc, oc) - radius * radius;
        float   discriminant = b * b - 4 * a * c;

        if (discriminant < 0)
        {
            return(-1.0f);
        }
        else
        {
            return((-b - Mathf.Sqrt(discriminant)) / (2 * a));
        }
    }
Exemple #16
0
    public override bool Scatter(RTRay ray, HitRecord hit, out Color attenuation, out RTRay scattered)
    {
        attenuation = new Color(1, 1, 1);

        Vector3 outwardN;
        float   ni_over_nt;
        Vector3 refracted;
        float   reflect_prob;
        float   cosine;

        if (Vector3.Dot(ray.direction, hit.n) > 0)
        {
            outwardN   = -hit.n;
            ni_over_nt = refractiveIndex;
            cosine     = Vector3.Dot(ray.direction, hit.n) / ray.direction.magnitude;
        }
        else
        {
            outwardN   = hit.n;
            ni_over_nt = 1.0f / refractiveIndex;
            cosine     = -Vector3.Dot(ray.direction, hit.n) / ray.direction.magnitude;
        }

        if (RTMath.Refract(ray.direction, outwardN, ni_over_nt, out refracted))
        {
            reflect_prob = RTMath.schlick(cosine, refractiveIndex);
        }
        else
        {
            reflect_prob = 1.0f;
        }

        if (RTMath.Rnd01() < reflect_prob)
        {
            scattered = new RTRay().Set(hit.p, RTMath.Reflect(ray.direction, hit.n));
        }
        else
        {
            scattered = new RTRay().Set(hit.p, refracted);
        }

        return(true);
    }
    public override Color GetColor(RTRay ray, int depth)
    {
        HitRecord hit;

        if (scene.Hit(ray, 0.001f, 1000, out hit))
        {
            RTRay scattered;
            Color attenuation;
            if (depth < 50 && hit.material.Scatter(ray, hit, out attenuation, out scattered))
            {
                return(attenuation * GetColor(scattered, depth + 1));
            }
            else
            {
                return(Color.black);
            }
        }

        return(RTCanvas.GetEnvironmentColor(ray));
    }
Exemple #18
0
    public override bool Hit(RTRay ray, float t_min, float t_max, out HitRecord rec)
    {
        rec = new HitRecord();

        HitRecord hit;
        bool      hitAnything = false;
        float     t_closest   = t_max;
        int       numItems    = list == null ? 0 : list.Count;

        for (int i = 0; i < numItems; ++i)
        {
            Hitable hitable = list[i];
            if (hitable != null && hitable.Hit(ray, t_min, t_closest, out hit))
            {
                hitAnything = true;
                t_closest   = hit.t;
                rec         = hit;
            }
        }
        return(hitAnything);
    }
Exemple #19
0
    public override bool Hit(RTRay ray, float t_min, float t_max, out HitRecord rec)
    {
        rec = new HitRecord();

        Vector3 oc           = ray.origin - center;
        float   a            = Vector3.Dot(ray.direction, ray.direction);
        float   b            = 2.0f * Vector3.Dot(oc, ray.direction);
        float   c            = Vector3.Dot(oc, oc) - radius * radius;
        float   discriminant = b * b - 4 * a * c;

        if (discriminant < 0)
        {
            return(false);
        }
        else
        {
            float t = (-b - Mathf.Sqrt(discriminant)) / (2 * a);
            if (t < t_max && t > t_min)
            {
                rec.t        = t;
                rec.p        = ray.PointAt(t);
                rec.n        = (rec.p - center) / radius;
                rec.material = material;
                return(true);
            }
            t = (-b + Mathf.Sqrt(discriminant)) / (2 * a);
            if (t < t_max && t > t_min)
            {
                rec.t        = t;
                rec.p        = ray.PointAt(t);
                rec.n        = (rec.p - center) / radius;
                rec.material = material;
                return(true);
            }

            return(false);
        }
    }
Exemple #20
0
 public virtual Color GetColor(RTRay ray, int depth)
 {
     return(Color.white);
 }
Exemple #21
0
 public abstract bool Hit(RTRay ray, float t_min, float t_max, out HitRecord rec);
Exemple #22
0
    protected virtual void Start()
    {
        int canvasWidth  = isScreenSize ? Screen.width : 200;
        int canvasHeight = isScreenSize ? Screen.height : 100;

        cam = CreateCamera(canvasWidth, canvasHeight);
        ppmTexture.Init(canvasWidth, canvasHeight);

        renderingTasksManager = new RenderingTasksManager();

        int           pixelIndex = 0;
        RenderingTask task       = null;
        int           itemsCount = 0;

        for (int j = 0; j < ppmTexture.Height; ++j)
        {
            for (int i = 0; i < ppmTexture.Width; ++i)
            {
                if (multiThreadRendering)
                {
                    if (itemsCount == 0)
                    {
                        task = new RenderingTask();
                        task.Init(this, cam, ppmTexture.Width, ppmTexture.Height, numSamples);
                        renderingTasksManager.AddTask(task, RenderingTaskCompleteCB);
                    }
                    task.AddItem(i, j, pixelIndex);
                    ++itemsCount;
                    if (itemsCount == RenderingTask.SIZE)
                    {
                        itemsCount = 0;
                    }
                    ++pixelIndex;
                }
                else
                {
                    Color color = Color.black;
                    for (int s = 0; s < numSamples; ++s)
                    {
                        float u = (i + RTMath.Rnd01()) / ppmTexture.Width;
                        float v = (j + RTMath.Rnd01()) / ppmTexture.Height;

                        RTRay ray = cam.GetRay(u, v);
                        color += GetColor(ray, 0);
                    }
                    color /= numSamples;
                    ppmTexture.WriteAPixel(color);
                }
            }
        }

        if (!multiThreadRendering)
        {
            ppmTexture.Complete();
            RenderingComplete();
        }
        else
        {
            renderingTasksManager.Start();
        }
    }
 public override Color GetColor(RTRay ray, int depth)
 {
     return(GetBackgroundColor(ray));
 }
Exemple #24
0
 public abstract bool Scatter(RTRay ray, HitRecord hit, out Color attenuation, out RTRay scattered);