コード例 #1
0
        public static void Main3()
        {
            int       nx       = 1280;
            int       ny       = 640;
            int       ns       = 16;
            Vector3   lookfrom = new Vector3(-2, 2, 1);
            Vector3   lookat   = new Vector3(0, 0, -1);
            Vector3   up       = new Vector3(0, 1, 0);
            RayCamera camera   = new RayCamera(lookfrom, lookat, up, 35, (float)(nx) / (float)(ny));

            HitList list = new HitList();

            list.Add(new Sphere(new Vector3(0, 0, -1), 0.5f, new Lambertian(new Vector3(0.1f, 0.2f, 0.5f))));
            list.Add(new Sphere(new Vector3(0, -100.5f, -1), 100, new Lambertian(new Vector3(0.8f, 0.8f, 0.0f))));
            list.Add(new Sphere(new Vector3(1, 0, -1), 0.5f, new Metal(new Vector3(0.8f, 0.6f, 0.2f), 0.3f)));
            list.Add(new Sphere(new Vector3(-1, 0, -1), 0.5f, new DielectricShlick(1.5f)));
            list.Add(new Sphere(new Vector3(-1, 0, -1), -0.45f, new DielectricShlick(1.5f)));
            Texture2D tex = ImageHelper.CreateImg(nx, ny);

            for (int j = ny - 1; j >= 0; --j)
            {
                for (int i = 0; i < nx; ++i)
                {
                    Vector3 color = Vector3.zero;
                    for (int k = 0; k < ns; ++k)
                    {
                        float u = (float)(i + Random.Range(0f, 1f)) / (float)(nx);
                        float v = (float)(j + Random.Range(0f, 1f)) / (float)(ny);

                        Ray r = camera.GetRay(u, v);
                        color += RayCast(r, list, 0);
                    }
                    color   = color / (float)(ns);
                    color.x = Mathf.Sqrt(color.x);
                    color.y = Mathf.Sqrt(color.y);
                    color.z = Mathf.Sqrt(color.z);
                    ImageHelper.SetPixel(tex, i, j, color);
                }
            }

            ImageHelper.SaveImg(tex, "Img/chapter10_3.png");
            Debug.Log("Chapter 10_3 done");
        }
コード例 #2
0
        public static void Main()
        {
            int nx = 1280;
            int ny = 640;
            int ns = 8;

            RayCamera camera = new RayCamera();

            HitList list = new HitList();

            list.Add(new Sphere(new Vector3(0, 0, -1), 0.5f));
            list.Add(new Sphere(new Vector3(0, -100.5f, -1), 100));

            Texture2D tex  = ImageHelper.CreateImg(nx, ny);
            Texture2D tex2 = ImageHelper.CreateImg(nx, ny);

            for (int j = ny - 1; j >= 0; --j)
            {
                for (int i = 0; i < nx; ++i)
                {
                    Vector3 color = Vector3.zero;
                    for (int k = 0; k < ns; ++k)
                    {
                        float u = (float)(i + Random.Range(-1f, 1f)) / (float)(nx);
                        float v = (float)(j + Random.Range(-1f, 1f)) / (float)(ny);

                        Ray r = camera.GetRay(u, v);
                        color += RayCast(r, list);
                    }
                    color = color / (float)(ns);
                    ImageHelper.SetPixel(tex, i, j, color);
                    color.x = Mathf.Pow(color.x, 1 / 2.2f);
                    color.y = Mathf.Pow(color.y, 1 / 2.2f);
                    color.z = Mathf.Pow(color.z, 1 / 2.2f);
                    ImageHelper.SetPixel(tex2, i, j, color);
                }
            }

            ImageHelper.SaveImg(tex, "Img/chapter7_1.png");
            ImageHelper.SaveImg(tex2, "Img/chapter7_2.png");
            Debug.Log("Done");
        }
コード例 #3
0
        public static void Main()
        {
            int nx = 1280;
            int ny = 640;
            int ns = 64;

            RayCamera camera = new RayCamera();

            HitList list = new HitList();

            list.Add(new Sphere(new Vector3(0, 0, -1), 0.5f, new Lambertian(new Vector3(0.8f, 0.3f, 0.3f), 0.382f)));
            list.Add(new Sphere(new Vector3(0, -100.5f, -1), 100, new Lambertian(new Vector3(0.8f, 0.8f, 0.0f), 0.618f)));
            list.Add(new Sphere(new Vector3(1, 0, -1), 0.5f, new Metal(new Vector3(0.8f, 0.6f, 0.2f), 0.3f)));
            list.Add(new Sphere(new Vector3(-1, 0, -1), 0.5f, new Metal(new Vector3(0.8f, 0.8f, 0.8f), 1.0f)));
            Texture2D tex = ImageHelper.CreateImg(nx, ny);

            for (int j = ny - 1; j >= 0; --j)
            {
                for (int i = 0; i < nx; ++i)
                {
                    Vector3 color = Vector3.zero;
                    for (int k = 0; k < ns; ++k)
                    {
                        float u = (float)(i + Random.Range(-1f, 1f)) / (float)(nx);
                        float v = (float)(j + Random.Range(-1f, 1f)) / (float)(ny);

                        Ray r = camera.GetRay(u, v);
                        color += RayCast(r, list, 0);
                    }
                    color   = color / (float)(ns);
                    color.x = Mathf.Sqrt(color.x);
                    color.y = Mathf.Sqrt(color.y);
                    color.z = Mathf.Sqrt(color.z);
                    ImageHelper.SetPixel(tex, i, j, color);
                }
            }

            ImageHelper.SaveImg(tex, "Img/chapter8fuzz2.png");
            Debug.Log("Chapter 8 fuzz is done");
        }
コード例 #4
0
        public static void Main()
        {
            int nx = 1280;
            int ny = 640;
            int ns = 16;

            RayCamera camera = new RayCamera();
            float     R      = Mathf.Cos(Mathf.PI / 4);
            HitList   list   = new HitList();

            list.Add(new Sphere(new Vector3(-R, 0, -1), R, new Lambertian(new Vector3(0f, 0f, 1f))));
            list.Add(new Sphere(new Vector3(R, 0, -1), R, new Lambertian(new Vector3(1f, 0f, 0f))));

            Texture2D tex = ImageHelper.CreateImg(nx, ny);

            for (int j = ny - 1; j >= 0; --j)
            {
                for (int i = 0; i < nx; ++i)
                {
                    Vector3 color = Vector3.zero;
                    for (int k = 0; k < ns; ++k)
                    {
                        float u = (float)(i + Random.Range(-1f, 1f)) / (float)(nx);
                        float v = (float)(j + Random.Range(-1f, 1f)) / (float)(ny);

                        Ray r = camera.GetRay(u, v);
                        color += RayCast(r, list, 0);
                    }
                    color   = color / (float)(ns);
                    color.x = Mathf.Sqrt(color.x);
                    color.y = Mathf.Sqrt(color.y);
                    color.z = Mathf.Sqrt(color.z);
                    ImageHelper.SetPixel(tex, i, j, color);
                }
            }

            ImageHelper.SaveImg(tex, "Img/chapter10_1.png");
            Debug.Log("Chapter 10_1 done");
        }
コード例 #5
0
    public static void Main()
    {
        int nx = 1280;
        int ny = 640;
        int ns = 64; // 一个像素需要采样64次

        RayCamera camera = new RayCamera();

        HitList list = new HitList();

        list.Add(new Sphere(new Vector3(0, 0, -1), 0.5f));
        list.Add(new Sphere(new Vector3(0, -100.5f, -1), 100));

        Texture2D tex = ImageUtils.CreateImg(nx, ny);

        // pixels are written from left to right, from top to bottom
        for (int j = ny - 1; j >= 0; j--)
        {
            for (int i = 0; i < nx; i++)
            {
                Vector3 color = Vector3.zero;
                // 对于每个射线,随机采样周围的8格,用平均颜色来作为该射线对应像素的颜色
                // 一次采样需要解2个一元二次方程组。也就是说,一共需要解1280X640X64X2=104857600个一元二次方程组。
                for (int k = 0; k < ns; k++)
                {
                    float u   = (i + Random.Range(-1f, 1f)) / nx;
                    float v   = (j + Random.Range(-1f, 1f)) / ny;
                    Ray   ray = camera.GetRay(u, v);
                    color += GetColor(ray, list);
                }

                color = color / ns;
                ImageUtils.SetPixel(tex, i, j, color);
            }
        }

        ImageUtils.SaveImg(tex, "OutputImg/chapter08.png");
    }